1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 
3 /*
4  *  Goo
5  *
6  *  Copyright (C) 2007 Free Software Foundation, Inc.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include <config.h>
23 #include <stdio.h>
24 #include <glib/gi18n.h>
25 #include <gst/gst.h>
26 #include "album-info.h"
27 #include "glib-utils.h"
28 #include "gth-user-dir.h"
29 #include "track-info.h"
30 
31 
32 #define MBI_VARIOUS_ARTIST_ID  "89ad4ac3-39f7-470e-963a-56509c546377"
33 
34 
35 AlbumInfo*
album_info_new(void)36 album_info_new (void)
37 {
38 	AlbumInfo *album;
39 
40 	album = g_new0 (AlbumInfo, 1);
41 
42 	album->ref = 1;
43 	album->release_date = g_date_new ();
44 	album_info_set_id (album, NULL);
45 	album_info_set_title (album, NULL);
46 	album_info_set_artist (album, NULL, NULL);
47 	album_info_set_tracks (album, NULL);
48 
49 	return album;
50 }
51 
52 
53 static void
album_info_free(AlbumInfo * album)54 album_info_free (AlbumInfo *album)
55 {
56 	g_free (album->id);
57 	g_free (album->title);
58 	g_free (album->artist);
59 	g_free (album->artist_id);
60 	g_free (album->genre);
61 	g_free (album->asin);
62 	g_date_free (album->release_date);
63 	track_list_free (album->tracks);
64 	g_free (album);
65 }
66 
67 
68 GType
album_info_get_type(void)69 album_info_get_type (void)
70 {
71 	static GType type = 0;
72 
73 	if (type == 0)
74 		type = g_boxed_type_register_static ("AlbumInfo",
75 						     (GBoxedCopyFunc) album_info_copy,
76 						     (GBoxedFreeFunc) album_info_free);
77 
78 	return type;
79 }
80 
81 
82 AlbumInfo *
album_info_ref(AlbumInfo * album)83 album_info_ref (AlbumInfo *album)
84 {
85 	album->ref++;
86 	return album;
87 }
88 
89 
90 void
album_info_unref(AlbumInfo * album)91 album_info_unref (AlbumInfo *album)
92 {
93 	if (album == NULL)
94 		return;
95 	album->ref--;
96 	if (album->ref == 0)
97 		album_info_free (album);
98 }
99 
100 
101 AlbumInfo *
album_info_copy(AlbumInfo * src)102 album_info_copy (AlbumInfo *src)
103 {
104 	AlbumInfo *dest;
105 
106 	dest = album_info_new ();
107 	album_info_set_id (dest, src->id);
108 	album_info_set_title (dest, src->title);
109 	album_info_set_artist (dest, src->artist, src->artist_id);
110 	album_info_set_genre (dest, src->genre);
111 	album_info_set_asin (dest, src->asin);
112 	album_info_set_release_date (dest, src->release_date);
113 	dest->various_artist = src->various_artist;
114 	album_info_set_tracks (dest, src->tracks);
115 
116 	return dest;
117 }
118 
119 
120 void
album_info_set_id(AlbumInfo * album,const char * id)121 album_info_set_id (AlbumInfo  *album,
122 		   const char *id)
123 {
124 	if (album->id == id)
125 		return;
126 
127 	g_free (album->id);
128 	if (id != NULL)
129 		album->id = g_strdup (id);
130 	else
131 		album->id = NULL;
132 }
133 
134 
135 void
album_info_set_title(AlbumInfo * album,const char * title)136 album_info_set_title (AlbumInfo  *album,
137 		      const char *title)
138 {
139 	if (title == NULL) {
140 		g_free (album->title);
141 		album->title = NULL /*g_strdup (_("Unknown Album"))*/;
142 		return;
143 	}
144 
145 	if (album->title == title)
146 		return;
147 
148 	g_free (album->title);
149 	album->title = g_strdup (title);
150 }
151 
152 
153 void
album_info_set_artist(AlbumInfo * album,const char * artist,const char * artist_id)154 album_info_set_artist (AlbumInfo  *album,
155 		       const char *artist,
156 		       const char *artist_id)
157 {
158 	if ((artist == NULL) || (artist[0] == 0)) {
159 		g_free (album->artist);
160 		album->artist = NULL /*g_strdup (_("Unknown Artist"))*/;
161 		g_free (album->artist_id);
162 		album->artist_id = NULL;
163 		return;
164 	}
165 
166 	if (album->artist == artist)
167 		return;
168 
169 	g_free (album->artist);
170 	g_free (album->artist_id);
171 
172 	album->various_artist = (artist_id != NULL) && (strcmp (artist_id, MBI_VARIOUS_ARTIST_ID) == 0);
173 	if (artist != NULL)
174 		album->artist = g_strdup (artist);
175 	else if (album->various_artist)
176 		album->artist = g_strdup (_("Various"));
177 	else
178 		album->artist = NULL;
179 
180 	if ((artist_id != NULL) && (strcmp (artist_id, KEEP_PREVIOUS_VALUE) != 0))
181 		album->artist_id = g_strdup (artist_id);
182 }
183 
184 
185 void
album_info_set_genre(AlbumInfo * album,const char * genre)186 album_info_set_genre (AlbumInfo  *album,
187 		      const char *genre)
188 {
189 	if (album->genre == genre)
190 		return;
191 
192 	g_free (album->genre);
193 	album->genre = NULL;
194 
195 	if (genre != NULL)
196 		album->genre = g_strdup (genre);
197 }
198 
199 
200 void
album_info_set_asin(AlbumInfo * album,const char * asin)201 album_info_set_asin (AlbumInfo  *album,
202 		     const char *asin)
203 {
204 	if (album->asin == asin)
205 		return;
206 
207 	g_free (album->asin);
208 	album->asin = NULL;
209 
210 	if (asin != NULL)
211 		album->asin = g_strdup (asin);
212 }
213 
214 
215 void
album_info_set_release_date(AlbumInfo * album,GDate * date)216 album_info_set_release_date (AlbumInfo *album,
217 			     GDate     *date)
218 {
219 	if ((date != NULL) && (g_date_valid (date)))
220 		g_date_set_julian (album->release_date, g_date_get_julian (date));
221 	else
222 		g_date_clear (album->release_date, 1);
223 }
224 
225 
226 void
album_info_set_tracks(AlbumInfo * album,GList * tracks)227 album_info_set_tracks (AlbumInfo  *album,
228 		       GList      *tracks)
229 {
230 	GList *scan;
231 
232 	if (album->tracks == tracks)
233 		return;
234 
235 	track_list_free (album->tracks);
236 	album->tracks = track_list_dup (tracks);
237 
238 	album->n_tracks = 0;
239 	album->total_length = 0;
240 	for (scan = album->tracks; scan; scan = scan->next) {
241 		TrackInfo *track = scan->data;
242 
243 		if ((album->artist != NULL) && (track->artist == NULL))
244 			track_info_set_artist (track, album->artist, album->artist_id);
245 
246 		album->n_tracks++;
247 		album->total_length += track->length;
248 	}
249 }
250 
251 
252 TrackInfo *
album_info_get_track(AlbumInfo * album,int track_number)253 album_info_get_track (AlbumInfo  *album,
254 		      int         track_number)
255 {
256 	GList *scan;
257 
258 	for (scan = album->tracks; scan; scan = scan->next) {
259 		TrackInfo *track = scan->data;
260 
261 		if (track->number == track_number)
262 			return track_info_copy (track);
263 	}
264 
265 	return NULL;
266 }
267 
268 
269 void
album_info_copy_metadata(AlbumInfo * to_album,AlbumInfo * from_album)270 album_info_copy_metadata (AlbumInfo *to_album,
271 			  AlbumInfo *from_album)
272 {
273 	GList *scan_to, *scan_from;
274 
275 	album_info_set_id (to_album, from_album->id);
276 	album_info_set_title (to_album, from_album->title);
277 	album_info_set_artist (to_album, from_album->artist, from_album->artist_id);
278 	to_album->various_artist = from_album->various_artist;
279 	album_info_set_genre (to_album, from_album->genre);
280 	album_info_set_asin (to_album, from_album->asin);
281 	album_info_set_release_date (to_album, from_album->release_date);
282 
283 	for (scan_to = to_album->tracks, scan_from = from_album->tracks;
284 	     scan_to && scan_from;
285 	     scan_to = scan_to->next, scan_from = scan_from->next)
286 	{
287 		TrackInfo *to_track = scan_to->data;
288 		TrackInfo *from_track = scan_from->data;
289 
290 		track_info_copy_metadata (to_track, from_track);
291 	}
292 }
293 
294 
295 static char *
get_cache_path(AlbumInfo * album,const char * disc_id)296 get_cache_path (AlbumInfo  *album,
297 		const char *disc_id)
298 {
299 	if (disc_id == NULL)
300 		return NULL;
301 	else
302 		return gth_user_dir_get_file (GTH_DIR_DATA, "goobox", "albums", disc_id, NULL);
303 }
304 
305 
306 gboolean
album_info_load_from_cache(AlbumInfo * album,const char * disc_id)307 album_info_load_from_cache (AlbumInfo  *album,
308 			    const char *disc_id)
309 {
310 	GKeyFile *f;
311 	char     *path;
312 	char     *s;
313 	int       i;
314 	GList    *scan;
315 
316 	path = get_cache_path (album, disc_id);
317 	if (path == NULL)
318 		return FALSE;
319 
320 	f = g_key_file_new ();
321 	if (! g_key_file_load_from_file (f, path, G_KEY_FILE_NONE, NULL)) {
322 		g_free (path);
323 		g_key_file_free (f);
324 		return FALSE;
325 	}
326 	g_free (path);
327 
328 	s = g_key_file_get_string (f, "Album", "ID", NULL);
329 	if (s != NULL) {
330 		album_info_set_id (album, s);
331 		g_free (s);
332 	}
333 
334 	s = g_key_file_get_string (f, "Album", "Title", NULL);
335 	if (s != NULL) {
336 		album_info_set_title (album, s);
337 		g_free (s);
338 	}
339 
340 	s = g_key_file_get_string (f, "Album", "Artist", NULL);
341 	if (s != NULL) {
342 		album_info_set_artist (album, s, "");
343 		g_free (s);
344 	}
345 	album->various_artist = g_key_file_get_boolean (f, "Album", "VariousArtists", NULL);
346 
347 	s = g_key_file_get_string (f, "Album", "Genre", NULL);
348 	if (s != NULL) {
349 		album_info_set_genre (album, s);
350 		g_free (s);
351 	}
352 
353 	s = g_key_file_get_string (f, "Album", "ReleaseDate", NULL);
354 	if (s != NULL) {
355 		int y = 0, m = 0, d = 0;
356 
357 		if (sscanf (s, "%d/%d/%d", &d, &m, &y) > 0) {
358 			GDate *date;
359 
360 			date = g_date_new_dmy ((d > 0) ? d : 1, (m > 0) ? m : 1, (y > 0) ? y : 1);
361 			album_info_set_release_date (album, date);
362 			g_date_free (date);
363 		}
364 		g_free (s);
365 	}
366 
367 	i = 1;
368 	for (scan = album->tracks; scan; scan = scan->next) {
369 		TrackInfo *track = scan->data;
370 		char      *group;
371 
372 		group = g_strdup_printf ("Track %d", i);
373 		s = g_key_file_get_string (f, group, "Title", NULL);
374 		if (s != NULL) {
375 			track_info_set_title (track, s);
376 			g_free (s);
377 		}
378 		s = g_key_file_get_string (f, group, "Artist", NULL);
379 		if (s != NULL) {
380 			track_info_set_artist (track, s, "");
381 			g_free (s);
382 		}
383 		g_free (group);
384 
385 		i++;
386 	}
387 
388 	g_key_file_free (f);
389 
390 	return TRUE;
391 }
392 
393 
394 void
album_info_save_to_cache(AlbumInfo * album,const char * disc_id)395 album_info_save_to_cache (AlbumInfo  *album,
396 			  const char *disc_id)
397 {
398 	GKeyFile *f;
399 	char     *data;
400 	gsize     length;
401 	GError   *error = NULL;
402 	int       i;
403 	GList    *scan;
404 
405 	f = g_key_file_new ();
406 
407 	if (album->id != NULL)
408 		g_key_file_set_string (f, "Album", "ID", album->id);
409 	if (album->title != NULL)
410 		g_key_file_set_string (f, "Album", "Title", album->title);
411 	if (album->artist != NULL)
412 		g_key_file_set_string (f, "Album", "Artist", album->artist);
413 	g_key_file_set_boolean (f, "Album", "VariousArtists", album->various_artist);
414 	if (album->genre != NULL)
415 		g_key_file_set_string (f, "Album", "Genre", album->genre);
416 	if (album->asin != NULL)
417 		g_key_file_set_string (f, "Album", "Asin", album->asin);
418 	if (g_date_valid (album->release_date)) {
419 		char s[64];
420 
421 		g_date_strftime (s, sizeof(s), "%d/%m/%Y", album->release_date);
422 		g_key_file_set_string (f, "Album", "ReleaseDate", s);
423 	}
424 
425 	i = 1;
426 	for (scan = album->tracks; scan; scan = scan->next) {
427 		TrackInfo *track = scan->data;
428 		char      *group;
429 
430 		group = g_strdup_printf ("Track %d", i);
431 		g_key_file_set_string (f, group, "Title", track->title);
432 		if (track->artist != NULL)
433 			g_key_file_set_string (f, group, "Artist", track->artist);
434 		g_free (group);
435 
436 		i++;
437 	}
438 
439 	data = g_key_file_to_data (f, &length, &error);
440 	if (data == NULL) {
441 		debug (DEBUG_INFO, "%s\n", error->message);
442 		g_clear_error (&error);
443 	}
444 	else {
445 		char *path;
446 
447 		path = get_cache_path (album, disc_id);
448 		if (path != NULL) {
449 			char *dir;
450 
451 			dir = g_path_get_dirname (path);
452 			g_mkdir_with_parents (dir, 0700);
453 
454 			if (! g_file_set_contents (path, data, length, &error)) {
455 				debug (DEBUG_INFO, "%s\n", error->message);
456 				g_clear_error (&error);
457 			}
458 
459 			g_free (dir);
460 			g_free (path);
461 		}
462 		g_free (data);
463 	}
464 
465 	g_key_file_free (f);
466 }
467 
468 
469 /* -- */
470 
471 
472 GList *
album_list_dup(GList * album_list)473 album_list_dup (GList *album_list)
474 {
475 	GList *new_list;
476 
477 	if (album_list == NULL)
478 		return NULL;
479 
480 	new_list = g_list_copy (album_list);
481 	g_list_foreach (new_list, (GFunc) album_info_ref, NULL);
482 
483 	return new_list;
484 }
485 
486 
487 void
album_list_free(GList * album_list)488 album_list_free (GList *album_list)
489 {
490 	if (album_list == NULL)
491 		return;
492 	g_list_foreach (album_list, (GFunc) album_info_unref, NULL);
493 	g_list_free (album_list);
494 }
495