summaryrefslogtreecommitdiffstats
path: root/src/itdb_photoalbum.c
blob: f7d6039023ac4fff1df17ac05f95b8df08deddfa (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
#include <config.h>

#include "itdb_private.h"
#include "itdb_device.h"
#include "db-artwork-parser.h"
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <glib/gi18n-lib.h>


/* Set @error with standard error message */
static void error_no_photos_dir (const gchar *mp, GError **error)
{
    gchar *str;

    g_return_if_fail (mp);

    if (error)
    {
	str = g_build_filename (mp, "iPod_Control", "Photos", NULL);
	g_set_error (error,
		     ITDB_FILE_ERROR,
		     ITDB_FILE_ERROR_NOTFOUND,
		     _("Photos directory not found: '%s' (or similar)."),
		     str);
	g_free (str);
    }
}


/**
 * itdb_get_photos_dir:
 * @mountpoint: mountpoint of iPod
 *
 * Retrieve the Photo directory by
 * first calling itdb_get_control_dir() and then adding 'Photos'
 *
 * Return value: path to the Artwork directory or NULL of
 * non-existent. Must g_free() after use.
 */
gchar *itdb_get_photos_dir (const gchar *mountpoint)
{
    gchar *p_ipod[] = {"Photos", NULL};
    /* Use an array with all possibilities, so further extension will
       be easy */
    gchar **paths[] = {p_ipod, NULL};
    gchar ***ptr;
    gchar *result = NULL;

    g_return_val_if_fail (mountpoint, NULL);

    for (ptr=paths; *ptr && !result; ++ptr)
    {
	result = itdb_resolve_path (mountpoint, (const gchar **)*ptr);
    }
    return result;
}

/**
 * itdb_get_photodb_path:
 * @mountpoint: the iPod mountpoint
 *
 * Retrieve a path to the Photo DB
 *
 * Return value: path to the PhotoDB or NULL if non-existent. Must
 * g_free() after use.
 **/
gchar *itdb_get_photodb_path (const gchar *mountpoint)
{
    gchar *photo_dir, *path=NULL;

    g_return_val_if_fail (mountpoint, NULL);

    photo_dir = itdb_get_photos_dir (mountpoint);

    if (photo_dir)
    {
	path = itdb_get_path (photo_dir, "Photo Database");
	g_free (photo_dir);
    }

    return path;
}

/**
 * itdb_get_photos_thumb_dir:
 * @mountpoint: the iPod mountpoint
 *
 * Retrieve the Photo Thumbnail directory by
 * first calling itdb_get_control_dir() and then adding 'Photos/Thumbs'
 *
 * Return value: path to the Artwork directory or NULL of
 * non-existent. Must g_free() after use.
 */
gchar *itdb_get_photos_thumb_dir (const gchar *mountpoint)
{
    gchar *control_dir;
    gchar *result = NULL;
    gchar *dir = "Thumbs";

    g_return_val_if_fail (mountpoint, NULL);
    g_return_val_if_fail (dir, NULL);

    control_dir = itdb_get_photos_dir (mountpoint);
    if (control_dir)
    {
	const gchar *p_dir[] = {NULL, NULL};
	p_dir[0] = dir;
	result = itdb_resolve_path (control_dir, p_dir);
	g_free (control_dir);
    }
    return result;
}


/**
 * itdb_photodb_parse:
 *
 * Parses the photo database of an iPod mounted at @mp.
 *
 * @mp: mountpoint of the iPod
 * @error: will contain the error description when an error occured.
 *
 * Return value: the imported PhotoDB or NULL in case of an error.
 **/
Itdb_PhotoDB *itdb_photodb_parse (const gchar *mp, GError **error)
{
    gchar *photos_dir;
    Itdb_PhotoDB *photodb = NULL;

    photos_dir = itdb_get_photos_dir (mp);

    if (!photos_dir)
    {
	error_no_photos_dir (mp, error);
	return NULL;
    }
    g_free (photos_dir);

    photodb = itdb_photodb_new ();
    itdb_device_set_mountpoint (photodb->device, mp);
    ipod_parse_photo_db (photodb);
    return photodb;
}


/**
 * itdb_photodb_new:
 *
 * Creates a new Itdb_PhotoDB
 *
 * Return value: a newly created Itdb_PhotoDB to be freed with
 * itdb_photodb_free() when it's no longer needed
 **/
Itdb_PhotoDB *itdb_photodb_new (void)
{
    Itdb_PhotoDB *photodb;

    photodb = g_new0 (Itdb_PhotoDB, 1);
    photodb->device = itdb_device_new ();

    return photodb;
}


/** 
 * itdb_photodb_free:
 * @photodb: an #Itdb_PhotoDB
 *
 * Free the memory taken by @photodb. 
 **/
void itdb_photodb_free (Itdb_PhotoDB *photodb)
{
	if (photodb)
	{
		g_list_foreach (photodb->photoalbums,
				(GFunc)(itdb_photodb_photoalbum_free), NULL);
		g_list_free (photodb->photoalbums);
		g_list_foreach (photodb->photos,
				(GFunc)(itdb_artwork_free), NULL);
		g_list_free (photodb->photos);
		itdb_device_free (photodb->device);

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

		g_free (photodb);
	}
}




G_GNUC_INTERNAL gint itdb_get_free_photo_id ( Itdb_PhotoDB *db ) 
{
	gint photo_id = 0;
	GList *it;

	for (it = db->photos; it != NULL; it = it->next) {
		Itdb_Artwork *artwork;

		artwork = (Itdb_Artwork *)it->data;
		if( artwork->id > photo_id )
			photo_id = artwork->id;
	}
	return photo_id + 1;
}

static gint itdb_get_free_photoalbum_id ( Itdb_PhotoDB *db )
{
	gint album_id = 0;
	GList *it;

	for (it = db->photoalbums; it != NULL; it = it->next) {
		Itdb_PhotoAlbum *album;

		album = (Itdb_PhotoAlbum *)it->data;
		if( album->album_id > album_id )
			album_id = album->album_id;
	}
	return album_id + 1;
}

static Itdb_PhotoAlbum *itdb_get_photoalbum ( Itdb_PhotoDB *db, const gchar *albumname )
{
	GList *it;

	if( strcmp( albumname, "master" ) == 0 )
	    return g_list_nth_data (db->photoalbums, 0);

	for (it = db->photoalbums; it != NULL; it = it->next) {
		Itdb_PhotoAlbum *album;

		album = (Itdb_PhotoAlbum *)it->data;
		if( strcmp(album->name, albumname) == 0 )
			return album;
	}
	return (Itdb_PhotoAlbum *)NULL;
}


void itdb_photodb_photoalbum_free (Itdb_PhotoAlbum *pa)
{
    if (pa)
    {
	g_free (pa->name);
	g_list_free (pa->members);

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

	g_free (pa);
    }
}

static gboolean itdb_photodb_add_photo_internal (Itdb_PhotoDB *db,
						 const gchar *albumname,
						 const gchar *filename,
						 const guchar *image_data,
						 gsize image_data_len)
{
	gboolean result;
	Itdb_Artwork *artwork;
	Itdb_PhotoAlbum *photoalbum;
	const Itdb_ArtworkFormat *format;
	gint photo_id;

	g_return_val_if_fail (db, FALSE);

	artwork = itdb_artwork_new ();

	photo_id = itdb_get_free_photo_id( db );
	artwork->id = photo_id;

	/* Add a thumbnail for every supported format */
	format = itdb_device_get_artwork_formats(db->device);
	for( result = TRUE; format->type != -1 && result == TRUE; format++)
	{
	    if((format->type == ITDB_THUMB_COVER_SMALL) ||
	       (format->type == ITDB_THUMB_COVER_LARGE))
			continue;
	    if (filename)
	    {
		result = itdb_artwork_add_thumbnail (artwork,
						     format->type,
						     filename);
	    }
	    if (image_data)
	    {
		result = itdb_artwork_add_thumbnail_from_data (artwork,
							       format->type,
							       image_data,
							       image_data_len);
	    }
	}

	if (result != TRUE)
	{
		itdb_artwork_remove_thumbnails (artwork);
		return result;
	}
	db->photos = g_list_append (db->photos, artwork);

	photoalbum = itdb_get_photoalbum( db, albumname );
	if( photoalbum == NULL )
		photoalbum = itdb_photodb_photoalbum_new( db, albumname );
	photoalbum->num_images++;
	photoalbum->members = g_list_append (photoalbum->members, GINT_TO_POINTER(photo_id));

	return result;
}


gboolean itdb_photodb_add_photo (Itdb_PhotoDB *db,
				 const gchar *albumname,
				 const gchar *filename)
{
    g_return_val_if_fail (db, FALSE);
    g_return_val_if_fail (filename, FALSE);

    return itdb_photodb_add_photo_internal (db, albumname, filename,
					    NULL, 0);
}


gboolean itdb_photodb_add_photo_from_data (Itdb_PhotoDB *db,
					   const gchar *albumname,
					   const guchar *image_data,
					   gsize image_data_len)
{
    g_return_val_if_fail (db, FALSE);
    g_return_val_if_fail (image_data, FALSE);

    return itdb_photodb_add_photo_internal (db, albumname, NULL,
					    image_data, image_data_len);
}


gboolean itdb_photodb_remove_photo (Itdb_PhotoDB *db,
		const gint photo_id )
{
	gboolean result = TRUE;
	GList *it;

	g_return_val_if_fail (db, FALSE);

	/* Remove the photo from the image list */
	for (it = db->photos; it != NULL; it = it->next) {
		Itdb_Artwork *artwork;

		artwork = (Itdb_Artwork *)it->data;
		if( artwork->id == photo_id ) {
			db->photos = g_list_remove (db->photos, artwork);
			break;
		}
	}

	/* Remove the photo from any albums containing it */
	for (it = db->photoalbums; it != NULL; it = it->next) {
		Itdb_PhotoAlbum *album;

		album = (Itdb_PhotoAlbum *)it->data;
		album->members = g_list_remove (album->members, GINT_TO_POINTER(photo_id));
		album->num_images = g_list_length( album->members );
	}
	return result;
}

/**
 * itdb_photodb_remove_photoalbum
 * @photodb: the #Itdb_PhotoDB to apply changes to
 * @album_name: the name of the photoalbum to remove from the database
 *
 * Return value: TRUE on success, FALSE if the album specified by @album_name could not
 *               be found in the database.
 */
gboolean itdb_photodb_remove_photoalbum (Itdb_PhotoDB *db, const gchar *album_name)
{
        gboolean result = TRUE;
        GList *it;

        g_return_val_if_fail (db, FALSE);
        g_return_val_if_fail (album_name, FALSE);

        /* Remove all the photos within that album from the library */
        for (it = db->photoalbums; it != NULL; it = it->next )
        {
                Itdb_PhotoAlbum *album;
                GList *it2;

                album = (Itdb_PhotoAlbum*)it->data;
                g_return_val_if_fail (album, FALSE);
                /* Have we found the desired album? */
                if ( strcmp (album->name, album_name) == 0 )
                {
                        /* iterate over the photos within that album and remove them from the database */
                        for (it2 = album->members; it2 != NULL; it2 = it2->next )
                        {
                                gint photo_id = GPOINTER_TO_INT(it2->data);
                                g_print (_("Deleting photo with id: %d\n"), photo_id);
                                result = itdb_photodb_remove_photo (db, photo_id);
                                if (result == FALSE)
                                        break;
                        }
                        /* remove the album only if all members were successfully removed */
                        if (result == TRUE )
                        {
                            g_print (_("Successfuly remove album photos, removing album (%s) now.\n"), album_name );
                            db->photoalbums = g_list_remove (db->photoalbums, album);
                        }
                        return result;
                }
        }
        /* If we made it here, then there was no album by that name */
        return FALSE;
}

/**
 * itdb_photodb_rename_photoalbum:
 * @album_name: The album name (in the database, @db) to be renamed
 * @new_album_name: The name which @album_name will be renamed to
 * 
 * Rename the album given by @album_name to @new_album_name
 *
 * Return TRUE if successful, or FALSE if the album @album_name could not be found
 */
gboolean itdb_photodb_rename_photoalbum (Itdb_PhotoDB *db,
		        const gchar *album_name, const gchar *new_album_name)
{
	GList *it;

	g_return_val_if_fail (db, FALSE);
	g_return_val_if_fail (album_name, FALSE);
	g_return_val_if_fail (new_album_name, FALSE);

	for (it = db->photoalbums; it != NULL; it = it->next )
	{
		Itdb_PhotoAlbum *album;

		album = (Itdb_PhotoAlbum*)it->data;
		g_return_val_if_fail (album, FALSE);
		/* Have we found the desired album? */
		if (strcmp (album->name, album_name) == 0 )
		{
			strcpy (album->name,new_album_name);
			return TRUE;
		}
	}
	/* Obviously the source album wasn't found */
	return FALSE;
}

Itdb_PhotoAlbum *itdb_photodb_photoalbum_new (Itdb_PhotoDB *db,
		const gchar *album_name)
{
	Itdb_PhotoAlbum *photoalbum;

	g_return_val_if_fail (db, FALSE);
	g_return_val_if_fail (album_name, FALSE);

	photoalbum = g_new0 (Itdb_PhotoAlbum, 1);
	photoalbum->album_id = itdb_get_free_photoalbum_id( db );
	photoalbum->prev_album_id = photoalbum->album_id - 1;
	photoalbum->name = g_strdup( album_name );
	db->photoalbums = g_list_append (db->photoalbums, photoalbum);

	return photoalbum;
}

/**
 * itdb_photodb_write:
 * @photodb: the #Itdb_PhotoDB to write to disk
 * @error: return location for a #GError or NULL
 *
 * Write out a PhotoDB.
 *
 * FIXME: error is not set yet.
 *
 * Return value: TRUE on success, FALSE on error, in which case @error is
 * set accordingly.
 **/
gboolean itdb_photodb_write (Itdb_PhotoDB *photodb, GError **error)
{
    gboolean result;

    g_return_val_if_fail (photodb, FALSE);
    g_return_val_if_fail (photodb->device, FALSE);

    if (photodb->device->byte_order == 0)
	itdb_device_autodetect_endianess (photodb->device);

    result = ipod_write_photo_db (photodb);

    /* Write SysInfo file if it has changed */
    if (!(*error) && photodb->device->sysinfo_changed)
    {
	itdb_device_write_sysinfo (photodb->device, error);
    }

    if (result == -1)
	return FALSE;
    else
	return TRUE;
}