summaryrefslogtreecommitdiffstats
path: root/src/itdb_track.c
blob: 16a07d8c35a39132578189ff7b3d55daf1dcaac7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
/*
|  Copyright (C) 2002-2007 Jorg Schuler <jcsjcs at users sourceforge net>
|  Part of the gtkpod project.
|
|  URL: http://www.gtkpod.org/
|  URL: http://gtkpod.sourceforge.net/
|
|  The code contained in this file is free software; you can redistribute
|  it and/or modify it under the terms of the GNU Lesser General Public
|  License as published by the Free Software Foundation; either version
|  2.1 of the License, or (at your option) any later version.
|
|  This file is distributed in the hope that it will be useful,
|  but WITHOUT ANY WARRANTY; without even the implied warranty of
|  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
|  Lesser General Public License for more details.
|
|  You should have received a copy of the GNU Lesser General Public
|  License along with this code; if not, write to the Free Software
|  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
|
|  iTunes and iPod are trademarks of Apple
|
|  This product is not supported/written/published by Apple!
|
|  $Id$
*/

#include <config.h>

#include "itdb_private.h"
#include "itdb_device.h"
#include "itdb_thumb.h"
#include <string.h>

/**
 * itdb_track_new:
 *
 * Creates an empty #Itdb_Track
 *
 * Returns: the new #Itdb_Track, free it with itdb_track_free() when no
 * longer needed
 */
Itdb_Track *itdb_track_new (void)
{
    Itdb_Track *track = g_new0 (Itdb_Track, 1);

    track->artwork = itdb_artwork_new ();
    track->chapterdata = itdb_chapterdata_new ();

    track->visible = 1;
    return track;
}

static gboolean haystack (gchar *filetype, gchar **desclist)
{
    gchar **dlp;
    if (!filetype || !desclist) return FALSE;
    for (dlp=desclist; *dlp; ++dlp)
    {
	    if (strstr (filetype, *dlp)) return TRUE;
    }
    return FALSE;
}

/* Attempt to set some of the unknowns to reasonable defaults */
static void itdb_track_set_defaults (Itdb_Track *tr)
{
    gchar *mp3_desc[] = {"MPEG", "MP3", "mpeg", "mp3", NULL};
    gchar *mp4_desc[] = {"AAC", "MP4", "aac", "mp4", NULL};
    gchar *audible_subdesc[] = {"Audible", "audible", "Book", "book", NULL};
    gchar *wav_desc[] = {"WAV", "wav", NULL};
    gchar *m4v_desc[] = {"M4V", "MP4", "MP4V", "m4v", "mp4", "mp4v", NULL};

    g_return_if_fail (tr);
    g_return_if_fail (tr->itdb);

    if (tr->mark_unplayed == 0)
    {
	/* don't have the iPod mark this track with a bullet as
	   unplayed. Should be set to 0x02 for podcasts that have not
	   yet been played. */
	tr->mark_unplayed = 0x01;
    }

    /* The exact meaning of unk126 is unknown, but always seems to be
       0xffff for MP3/AAC tracks, 0x0 for uncompressed tracks (like WAVE
       format), 0x1 for Audible. */
    if (tr->unk126 == 0)
    {
	if (haystack (tr->filetype, mp3_desc))
	{
	    tr->unk126 = 0xffff;
	}
	else if (haystack (tr->filetype, mp4_desc))
	{
	    if (haystack (tr->filetype, audible_subdesc))
	    {
		tr->unk126 = 0x01;
	    }
		else
	    {
		tr->unk126 = 0xffff;
	    }
	}
	else if (haystack (tr->filetype, wav_desc))
	{
	    tr->unk126 = 0x00;
	}
	else
	{
	    tr->unk126 = 0x00;  /* default value */
	}
    }
    /* The exact meaning of unk144 is unknown, but MP3 tracks appear to
       be always 0x0000000c or 0x0100000c (if played one or more times
       in iTunes), AAC tracks are always 0x01000033, Audible files are
       0x01000029, WAV files are 0x0. */
    if (tr->unk144 == 0)
    {
	if (haystack (tr->filetype, mp3_desc))
	{
	    tr->unk144 = 0x000c;
	}
	else if (haystack (tr->filetype, mp4_desc))
	{
	    if (haystack (tr->filetype, audible_subdesc))
	    {
		tr->unk144 = 0x0029;
	    }
		else
	    {
		tr->unk144 = 0x0033;
	    }
	}
	else if (haystack (tr->filetype, wav_desc))
	{
	    tr->unk144 = 0x0000;
	}
	else
	{
	    tr->unk144 = 0x0000;  /* default value */
	}
    }
    if (itdb_device_supports_video (tr->itdb->device))
    {
	/* The unk208 field seems to denote whether the file is a
	   video or not.  It seems that it must be set to 0x00000002
	   for video files. */
	/* Only doing that for iPod videos since it remains to be
	 * proven that setting unk208 to non-0 doesn't upset older
	 * ipod models
	 */
	if (tr->mediatype == 0)
	{
	    if (haystack (tr->filetype, m4v_desc))
	    {
		/* set type to video (0x00000002) */
		tr->mediatype = 0x00000002;
	    }
	    else
	    {
		/* set type to audio */
		tr->mediatype = 0x00000001;
	    }
	}
    }
    /* The sample rate of the track expressed as an IEEE 32 bit
       floating point number.  It's uncertain why this is here.  itdb
       will set this when adding a track */
    tr->samplerate2 = tr->samplerate;

    /* set unique ID when not yet set */
    if (tr->dbid == 0)
    {
	GList *gl;
	guint64 id;
	do
	{
	    id = ((guint64)g_random_int () << 32) |
		((guint64)g_random_int ());
	    /* check if id is really unique */
	    for (gl=tr->itdb->tracks; id && gl; gl=gl->next)
	    {
		Itdb_Track *g_tr = gl->data;
		g_return_if_fail (g_tr);
		if (id == g_tr->dbid)  id = 0;
	    }
	} while (id == 0);
	tr->dbid = id;
	tr->dbid2= id;
    }
    if (tr->dbid2 == 0)  tr->dbid2 = tr->dbid;
}

/**
 * itdb_track_add:
 * @itdb:   an #Itdb_iTunesDB
 * @track:  an #Itdb_Track
 * @pos:    position of the track to add
 *
 * Adds @track to @itdb->tracks at position @pos (or at the end if pos
 * is -1). The application is responsible to also add it to the master
 * playlist. The @itdb gets ownership of the @track and will take care of
 * freeing the memory it uses when it's no longer necessary.
 */
void itdb_track_add (Itdb_iTunesDB *itdb, Itdb_Track *track, gint32 pos)
{
    g_return_if_fail (itdb);
    g_return_if_fail (track);
    g_return_if_fail (!track->userdata || track->userdata_duplicate);

    track->itdb = itdb;

    itdb_track_set_defaults (track);

    itdb->tracks = g_list_insert (itdb->tracks, track, pos);
}

/**
 * itdb_track_free:
 * @track: an #Itdb_Track
 *
 * Frees the memory used by @track
 */
void itdb_track_free (Itdb_Track *track)
{
    g_return_if_fail (track);

    g_free (track->title);
    g_free (track->ipod_path);
    g_free (track->album);
    g_free (track->artist);
    g_free (track->genre);
    g_free (track->filetype);
    g_free (track->comment);
    g_free (track->category);
    g_free (track->composer);
    g_free (track->grouping);
    g_free (track->description);
    g_free (track->podcasturl);
    g_free (track->podcastrss);
    g_free (track->subtitle);
    g_free (track->tvshow);
    g_free (track->tvepisode);
    g_free (track->tvnetwork);
    g_free (track->albumartist);
    g_free (track->keywords);
    g_free (track->sort_artist);
    g_free (track->sort_title);
    g_free (track->sort_album);
    g_free (track->sort_albumartist);
    g_free (track->sort_composer);
    g_free (track->sort_tvshow);

    itdb_chapterdata_free (track->chapterdata);

    itdb_artwork_free (track->artwork);

    if (track->userdata && track->userdata_destroy)
	(*track->userdata_destroy) (track->userdata);

    g_free (track);
}

/**
 * itdb_track_remove:
 * @track: an #Itdb_Track
 *
 * Removes @track from the #Itdb_iTunesDB it's associated with, and frees the
 * memory it uses. It doesn't remove the track from the playlists it may have
 * been added to, in particular it won't be removed from the master playlist.
 */
void itdb_track_remove (Itdb_Track *track)
{
    Itdb_iTunesDB *itdb;

    g_return_if_fail (track);
    itdb = track->itdb;
    g_return_if_fail (itdb);

    itdb->tracks = g_list_remove (itdb->tracks, track);
    itdb_track_free (track);
}

/**
 * itdb_track_unlink:
 * @track: an #Itdb_Track
 *
 * Removes @track from the #Itdb_iTunesDB it's associated with, but do not free
 * memory. It doesn't remove the track from the playlists it may have been
 * added to, in particular it won't be removed from the master playlist.
 * @track->itdb is set to NULL.
 */
void itdb_track_unlink (Itdb_Track *track)
{
    Itdb_iTunesDB *itdb;

    g_return_if_fail (track);
    itdb = track->itdb;
    g_return_if_fail (itdb);

    itdb->tracks = g_list_remove (itdb->tracks, track);
    track->itdb = NULL;
}

/**
 * itdb_track_duplicate:
 * @tr: an #Itdb_Track
 *
 * Duplicates an existing track
 *
 * Returns: a newly allocated #Itdb_Track
 */
Itdb_Track *itdb_track_duplicate (Itdb_Track *tr)
{
    Itdb_Track *tr_dup;

    g_return_val_if_fail (tr, NULL);

    tr_dup = g_new (Itdb_Track, 1);
    memcpy (tr_dup, tr, sizeof (Itdb_Track));

    /* clear itdb pointer */
    tr_dup->itdb = NULL;

    /* copy strings */
    tr_dup->title = g_strdup (tr->title);
    tr_dup->album = g_strdup (tr->album);
    tr_dup->artist = g_strdup (tr->artist);
    tr_dup->genre = g_strdup (tr->genre);
    tr_dup->filetype = g_strdup (tr->filetype);
    tr_dup->comment = g_strdup (tr->comment);
    tr_dup->category = g_strdup (tr->category);
    tr_dup->composer = g_strdup (tr->composer);
    tr_dup->grouping = g_strdup (tr->grouping);
    tr_dup->description = g_strdup (tr->description);
    tr_dup->podcasturl = g_strdup (tr->podcasturl);
    tr_dup->podcastrss = g_strdup (tr->podcastrss);
    tr_dup->subtitle = g_strdup (tr->subtitle);
    tr_dup->tvshow = g_strdup (tr->tvshow);
    tr_dup->tvepisode = g_strdup (tr->tvepisode);
    tr_dup->tvnetwork = g_strdup (tr->tvnetwork);
    tr_dup->albumartist = g_strdup (tr->albumartist);
    tr_dup->keywords = g_strdup (tr->keywords);
    tr_dup->ipod_path = g_strdup (tr->ipod_path);
    tr_dup->sort_artist = g_strdup (tr->sort_artist);
    tr_dup->sort_title = g_strdup (tr->sort_title);
    tr_dup->sort_album = g_strdup (tr->sort_album);
    tr_dup->sort_albumartist = g_strdup (tr->sort_albumartist);
    tr_dup->sort_composer = g_strdup (tr->sort_composer);
    tr_dup->sort_tvshow = g_strdup (tr->sort_tvshow);


    /* Copy chapterdata */
    tr_dup->chapterdata = itdb_chapterdata_duplicate (tr->chapterdata);

    /* Copy thumbnail data */
    if (tr->artwork != NULL) {
        tr_dup->artwork = itdb_artwork_duplicate (tr->artwork);
    }

    /* Copy userdata */
    if (tr->userdata && tr->userdata_duplicate)
	tr_dup->userdata = tr->userdata_duplicate (tr->userdata);

    return tr_dup;
}


/* called by itdb_track_set_thumbnails() and
   itdb_track_set_thumbnails_from_data() */
static gboolean itdb_track_set_thumbnails_internal (Itdb_Track *track,
						    const gchar *filename,
						    const guchar *image_data,
						    gsize image_data_len,
                                                    gpointer *pixbuf,
						    gint rotation,
						    GError **error)
{
    gboolean result = FALSE;

    g_return_val_if_fail (track, FALSE);
    g_return_val_if_fail (filename || image_data || pixbuf, FALSE);

    itdb_artwork_remove_thumbnails (track->artwork);
    /* clear artwork id */
    track->artwork->id = 0;

    if (filename)
    {
        result = itdb_artwork_set_thumbnail (track->artwork, filename,
                                             rotation, error);
    }
    if (image_data)
    {
        result = itdb_artwork_set_thumbnail_from_data (track->artwork,
                                                       image_data,
                                                       image_data_len,
                                                       rotation, error);
    }
    if (pixbuf)
    {
        result = itdb_artwork_set_thumbnail_from_pixbuf (track->artwork,
                                                         pixbuf, rotation,
                                                         error);
    }

    if (result == TRUE)
    {
	/* some black magic :-( */
	/* track->artwork_size should actually be the total size of
	   artwork packed into MP3 tags. We don't write mp3 tags... */
	track->artwork_size = track->artwork->artwork_size;
	/* track->artwork_count should actually be the number of
	   images packed into MP3 tags. */
	track->artwork_count = 1;
	/* for some reason artwork->artwork_size is always
	   track->artwork_size + track->artwork_count */
	track->artwork->artwork_size += track->artwork_count;
	/* indicate artwork is present */
	track->has_artwork = 0x01;
    }
    else
    {
	itdb_track_remove_thumbnails (track);
    }

    return result;
}

/**
 * itdb_track_set_thumbnails:
 * @track:    an #Itdb_Track
 * @filename: image file to use as a thumbnail
 *
 * Uses the image contained in @filename to generate iPod thumbnails. The image
 * can be in any format supported by gdk-pixbuf. To save memory, the thumbnails
 * will only be generated when necessary, i.e. when itdb_save() or a similar
 * function is called.
 *
 * Returns: TRUE if the thumbnail could be added, FALSE otherwise.
 *
 * Since: 0.3.0
 */
gboolean itdb_track_set_thumbnails (Itdb_Track *track,
				    const gchar *filename)
{
    g_return_val_if_fail (track, FALSE);
    g_return_val_if_fail (filename, FALSE);

    return itdb_track_set_thumbnails_internal (track, filename, NULL, 0, NULL,
					       0, NULL);
}

/**
 * itdb_track_set_thumbnails_from_data:
 * @track:          an #Itdb_Track
 * @image_data:     data used to create the thumbnail (the raw contents of
 *                  an image file)
 * @image_data_len: length of above data block
 *
 * Uses @image_data to generate iPod thumbnails. The image can be in
 * any format supported by gdk-pixbuf. To save memory, the thumbnails
 * will only be generated when necessary, i.e. when itdb_save() or a
 * similar function is called.
 *
 * Returns: TRUE if the thumbnail could be added, FALSE otherwise.
 *
 * Since: 0.4.0
 */
gboolean itdb_track_set_thumbnails_from_data (Itdb_Track *track,
					      const guchar *image_data,
					      gsize image_data_len)
{
    g_return_val_if_fail (track, FALSE);
    g_return_val_if_fail (image_data, FALSE);

    return itdb_track_set_thumbnails_internal (track, NULL,
					       image_data, image_data_len,
                                               NULL, 0, NULL);
}

/**
 * itdb_track_set_thumbnails_from_pixbuf:
 * @track:  an #Itdb_Track
 * @pixbuf: a #GdkPixbuf used to generate the thumbnail
 *
 * Uses @pixbuf to generate iPod thumbnails. To save memory, the thumbnails
 * will only be generated when necessary, i.e. when itdb_save() or a
 * similar function is called.
 *
 * Returns: TRUE if the thumbnail could be added, FALSE otherwise.
 *
 * Since: 0.5.0
 */
gboolean itdb_track_set_thumbnails_from_pixbuf (Itdb_Track *track,
                                                gpointer pixbuf)
{
    g_return_val_if_fail (track, FALSE);
    g_return_val_if_fail (pixbuf, FALSE);

    return itdb_track_set_thumbnails_internal (track, NULL, NULL, 0,
                                               pixbuf, 0, NULL);
}

/**
 * itdb_track_remove_thumbnails:
 * @track: an #Itdb_Track
 *
 * Removes the thumbnails associated with @track
 *
 * Since: 0.3.0
 */
void itdb_track_remove_thumbnails (Itdb_Track *track)
{
    g_return_if_fail (track);
    itdb_artwork_remove_thumbnails (track->artwork);
    track->artwork_size = 0;
    track->artwork_count = 0;
    track->mhii_link = 0;
    /* indicate no artwork is present */
    track->has_artwork = 0x02;
}

/**
 * itdb_track_by_id:
 * @itdb: an #Itdb_iTunesDB
 * @id:   ID of the track to look for
 *
 * Looks up a track using its ID in @itdb.
 *
 * Looking up tracks by ID is not really a good idea because the IDs
 * are created by itdb just before export. The functions are here
 * because they are needed during import of the iTunesDB which is
 * referencing tracks by IDs.
 *
 * This function is very slow (linear in the number of tracks
 * contained in the database). If you need to lookup many IDs use
 * itdb_track_id_tree_create(), itdb_track_id_tree_destroy(), and
 * itdb_track_id_tree_by_id().
 *
 * Returns: #Itdb_Track with the ID @id or NULL if the ID cannot be
 * found.
 */
Itdb_Track *itdb_track_by_id (Itdb_iTunesDB *itdb, guint32 id)
{
    GList *gl;

    g_return_val_if_fail (itdb, NULL);

    for (gl=itdb->tracks; gl; gl=gl->next)
    {
	Itdb_Track *track = gl->data;
	if (track->id == id)  return track;
    }
    return NULL;
}

static gint track_id_compare (gconstpointer a, gconstpointer b)
{
    if (*(guint32*) a == *(guint32*) b)
	return 0;
    if (*(guint32*) a > *(guint32*) b)
	return 1;
    return -1;
}

/**
 * itdb_track_id_tree_create:
 * @itdb: an #Itdb_iTunesDB
 *
 * Creates a balanced-binary tree for quick ID lookup that is used in
 * itdb_track_by_id_tree()
 *
 * Returns: a #GTree indexed by track IDs to be freed with
 * itdb_track_id_tree_destroy() when no longer used
 */
GTree *itdb_track_id_tree_create (Itdb_iTunesDB *itdb)
{
    GTree *idtree;
    GList *gl;

    g_return_val_if_fail (itdb, NULL);

    idtree = g_tree_new (track_id_compare);

    for (gl=itdb->tracks; gl; gl=gl->next)
    {
	Itdb_Track *tr = gl->data;
	g_return_val_if_fail (tr, NULL);
	g_tree_insert (idtree, &tr->id, tr);
    }
    return idtree;
}

/**
 * itdb_track_id_tree_destroy:
 * @idtree: a #GTree
 *
 * Frees the memory used by @idtree
 */
void itdb_track_id_tree_destroy (GTree *idtree)
{
    g_return_if_fail (idtree);

    g_tree_destroy (idtree);
}

/**
 * itdb_track_id_tree_by_id:
 * @idtree: a #GTree created using itdb_track_id_tree_create()
 * @id:     the ID of the track to search for
 *
 * Lookup an #Itdb_Track by @id using @idtree for faster lookup
 * (compared to itdb_track_by_id())
 *
 * Returns: the #Itdb_Track whose ID is @id, or NULL if such a track
 * couldn't be found
 */
Itdb_Track *itdb_track_id_tree_by_id (GTree *idtree, guint32 id)
{
    g_return_val_if_fail (idtree, NULL);

    return (Itdb_Track *)g_tree_lookup (idtree, &id);
}

/**
 * itdb_track_has_thumbnails:
 * @track: an #Itdb_Track
 *
 * Determine if a @track has thumbnails
 *
 * Returns: TRUE if @track has artwork available, FALSE otherwise
 *
 * Since: 0.7.0
 */
gboolean itdb_track_has_thumbnails (Itdb_Track *track)
{
    g_return_val_if_fail (track != NULL, FALSE);
    return ((track->artwork) && (track->artwork->thumbnail));
}

/**
 * itdb_track_get_thumbnail:
 * @track:  an #Itdb_Track
 * @width:  width of the pixbuf to retrieve, -1 for the biggest possible size
 *          (with no scaling)
 * @height: height of the pixbuf to retrieve, -1 for the biggest possible size
 *          (with no scaling)
 *
 * Get a thumbnail representing the cover associated with the current track,
 * scaling it if appropriate. If either height or width is -1, then the biggest
 * unscaled thumbnail available will be returned.
 *
 * Returns: a #GdkPixbuf that must be unreffed when no longer used, NULL
 * if no artwork could be found or if libgpod is compiled without GdkPixbuf
 * support
 *
 * Since: 0.7.0
 */
gpointer itdb_track_get_thumbnail (Itdb_Track *track, gint width, gint height)
{
    g_return_val_if_fail (track != NULL, NULL);
    if (!itdb_track_has_thumbnails (track)) {
        return NULL;
    }
    if (track->itdb != NULL) {
        return itdb_thumb_to_pixbuf_at_size (track->itdb->device,
                                             track->artwork->thumbnail,
                                             width, height);
    } else {
        return itdb_thumb_to_pixbuf_at_size (NULL, track->artwork->thumbnail,
                                             width, height);
    }
}