From 5377af7a8ee92aee489c3295c1a38ac4c3caffb1 Mon Sep 17 00:00:00 2001
From: Nikias Bassen <nikias@gmx.li>
Date: Mon, 3 Aug 2009 22:19:42 +0200
Subject: [PATCH 04/14] Improved artwork (ithumb) writing.

Removed mountpoint from ithumb-writer and instead use ithumb_dir which
will be set _before_ the writing of the ithumb files takes place.
Thus the path is not evaluated again and again for each ithumb file.
---
 src/db-artwork-writer.c |   92 +++++++++++++++++++++++++++-----
 src/db-image-parser.h   |    3 +-
 src/ithumb-writer.c     |  133 +++++------------------------------------------
 3 files changed, 93 insertions(+), 135 deletions(-)

diff --git a/src/db-artwork-writer.c b/src/db-artwork-writer.c
index a3c006e..a715a76 100644
--- a/src/db-artwork-writer.c
+++ b/src/db-artwork-writer.c
@@ -1151,6 +1151,29 @@ ipod_write_artwork_db (Itdb_iTunesDB *itdb)
 	int id_max;
 	Itdb_DB db;
 	int status;
+	const gchar *mount_point;
+	gchar *artwork_dir;
+
+	mount_point = itdb_get_mountpoint (itdb);
+	if (!mount_point) {
+		return -1;
+	}
+
+	/* this will create the Artwork directory */
+	filename = ipod_db_get_artwork_db_path ( mount_point );
+	if (filename == NULL) {
+		/* FIXME: the iTunesDB will be inconsistent wrt artwork_count
+		 * it might be better to 0 out this field in all tracks
+		 * when we encounter an error
+		 */
+		return -1;
+	}
+
+	artwork_dir = itdb_get_artwork_dir( mount_point );
+	if (!artwork_dir) {
+		g_free(filename);
+		return -1;
+	}
 
 	db.db_type = DB_TYPE_ITUNES;
 	db.db.itdb = itdb;
@@ -1160,26 +1183,18 @@ ipod_write_artwork_db (Itdb_iTunesDB *itdb)
 	/* First, let's write the .ithmb files, this will create the
 	 * various thumbnails as well */
 
-	status = itdb_write_ithumb_files (&db);
+	status = itdb_write_ithumb_files (&db, artwork_dir);
+	g_free(artwork_dir);
 	if (status != 0) {
 		return -1;
 	}
 
-	filename = ipod_db_get_artwork_db_path (itdb_get_mountpoint (itdb));
-	if (filename == NULL) {
-		/* FIXME: the iTunesDB will be inconsistent wrt artwork_count
-		 * it might be better to 0 out this field in all tracks
-		 * when we encounter an error
-		 */
-		return -1;
-	}
 	buf = ipod_buffer_new (filename, itdb->device->byte_order, DB_TYPE_ITUNES);
 	if (buf == NULL) {
 		g_print ("Couldn't create %s\n", filename);
 	        g_free (filename);
 		return -1;
 	}
-	g_free (filename);
 	bytes_written = write_mhfd (&db, buf, id_max);
 
 	/* Refcount of the shared buffer should drop to 0 and this should
@@ -1190,11 +1205,41 @@ ipod_write_artwork_db (Itdb_iTunesDB *itdb)
 	if (bytes_written == -1) {
 		g_print ("Failed to save %s\n", filename);
 		/* FIXME: maybe should unlink the file we may have created */
+		g_free (filename);
 		return -1;
 	}
+	g_free (filename);
 	return 0;
 }
 
+static gchar *db_get_photos_thumb_dir(const char *mount_point)
+{
+	gchar *photos_thumb_dir;
+
+	g_return_val_if_fail (mount_point, NULL);
+
+	photos_thumb_dir = itdb_get_photos_thumb_dir (mount_point);
+	if (!photos_thumb_dir) {
+	    /* attempt to create Thumbs dir */
+	    gchar *photos_dir = itdb_get_photos_dir (mount_point);
+	    gchar *dir;
+	    if (!photos_dir) {   /* give up */
+		return NULL;
+	    }
+	    dir = g_build_filename (photos_dir, "Thumbs", NULL);
+	    mkdir (dir, 0777);
+	    g_free (dir);
+	    g_free (photos_dir);
+
+	    /* try again */
+	    photos_thumb_dir = itdb_get_photos_thumb_dir (mount_point);
+	    if (!photos_thumb_dir) {   /* give up */
+		return NULL;
+	    }
+	}
+	return photos_thumb_dir;
+}
+
 int
 ipod_write_photo_db (Itdb_PhotoDB *photodb)
 {
@@ -1204,20 +1249,35 @@ ipod_write_photo_db (Itdb_PhotoDB *photodb)
 	int id_max;
 	Itdb_DB db;
 	int status;
+	const gchar *mount_point;
+	gchar *photo_thumbs_dir;
+
+	mount_point = db_get_mountpoint (&db);
+	if (!mount_point) {
+		return -1;
+	}
 
 	db.db_type = DB_TYPE_PHOTO;
 	db.db.photodb = photodb;
 
-	filename = ipod_db_get_photos_db_path (db_get_mountpoint (&db));
+	filename = ipod_db_get_photos_db_path (mount_point);
+	if (filename == NULL) {
+		return -1;
+	}
 
-	status = itdb_write_ithumb_files (&db);
-	if (status != 0) {
+	photo_thumbs_dir = db_get_photos_thumb_dir (mount_point);
+	if (!photo_thumbs_dir) {
+		g_free (filename);
 		return -1;
 	}
+	status = itdb_write_ithumb_files (&db, photo_thumbs_dir);
+	g_free (photo_thumbs_dir);
 
-	if (filename == NULL) {
+	if (status != 0) {
+		g_free (filename);
 		return -1;
 	}
+
 	buf = ipod_buffer_new (filename, photodb->device->byte_order, DB_TYPE_PHOTO);
 	if (buf == NULL) {
 		g_print ("Couldn't create %s\n", filename);
@@ -1231,13 +1291,15 @@ ipod_write_photo_db (Itdb_PhotoDB *photodb)
 	 * sync buffered data to disk
 	 */
 	ipod_buffer_destroy (buf);
-        g_free (filename);
 
 	if (bytes_written == -1) {
 		g_print ("Failed to save %s\n", filename);
 		/* FIXME: maybe should unlink the file we may have created */
+	        g_free (filename);
 		return -1;
 	}
+        g_free (filename);
+
 	return 0;
 }
 #else
diff --git a/src/db-image-parser.h b/src/db-image-parser.h
index 606bb4f..1f1af8a 100644
--- a/src/db-image-parser.h
+++ b/src/db-image-parser.h
@@ -81,5 +81,6 @@
 G_GNUC_INTERNAL Itdb_Thumb_Ipod_Item *ipod_image_new_from_mhni (MhniHeader *mhni, 
 			        			   Itdb_DB *db);
 
-G_GNUC_INTERNAL int itdb_write_ithumb_files (Itdb_DB *db);
+G_GNUC_INTERNAL int itdb_write_ithumb_files (Itdb_DB *db, const gchar *thumbs_dir);
+
 #endif
diff --git a/src/ithumb-writer.c b/src/ithumb-writer.c
index ea267a9..610777d 100644
--- a/src/ithumb-writer.c
+++ b/src/ithumb-writer.c
@@ -57,9 +57,7 @@
 struct _iThumbWriter {
 	off_t cur_offset;
 	FILE *f;
-        gchar *mountpoint;
-	gchar *artwork_dir;
-	gchar *photos_thumb_dir;
+	gchar *thumbs_dir;
         gchar *filename;
         gint current_file_index;
 	const Itdb_ArtworkFormat *img_info;
@@ -1046,20 +1044,10 @@ ithumb_writer_update (iThumbWriter *writer)
 	/* increment index for filename */
 	++writer->current_file_index;
 
-	switch (writer->db_type) {
-	    case DB_TYPE_PHOTO:
-		writer->filename =
-			ipod_image_get_ithmb_filename (writer->photos_thumb_dir,
+	writer->filename =
+			ipod_image_get_ithmb_filename (writer->thumbs_dir,
 						writer->img_info->format_id,
 						writer->current_file_index);
-		break;
-	    case DB_TYPE_ITUNES:
-		writer->filename =
-			ipod_image_get_ithmb_filename (writer->artwork_dir,
-						writer->img_info->format_id,
-						writer->current_file_index);
-		break;
-	}
 
 	if (writer->filename == NULL)
 	{
@@ -1115,68 +1103,12 @@ ithumb_writer_free (iThumbWriter *writer)
 	    }
 	}
 	g_free (writer->filename);
-	g_free (writer->photos_thumb_dir);
-	g_free (writer->artwork_dir);
-	g_free (writer->mountpoint);
+	g_free (writer->thumbs_dir);
 	g_free (writer);
 }
 
-static gchar *ithumb_get_artwork_dir (const char *mount_point)
-{
-	gchar *artwork_dir;
-
-	g_return_val_if_fail (mount_point, NULL);
-
-	artwork_dir = itdb_get_artwork_dir (mount_point);
-	if (!artwork_dir) {
-		/* attempt to create Artwork dir */
-		gchar *control_dir = itdb_get_control_dir (mount_point);
-		gchar *dir;
-		if (!control_dir)
-		{   /* give up */
-			return NULL;
-		}
-		dir = g_build_filename (control_dir, "Artwork", NULL);
-		mkdir (dir, 0777);
-		g_free (dir);
-		g_free (control_dir);
-
-		/* try again */
-		artwork_dir = itdb_get_artwork_dir (mount_point);
-	}
-	return artwork_dir;
-}
-
-static gchar *ithumb_get_photos_thumb_dir(const char *mount_point)
-{
-	gchar *photos_thumb_dir;
-
-	g_return_val_if_fail (mount_point, NULL);
-
-	photos_thumb_dir = itdb_get_photos_thumb_dir (mount_point);
-	if (!photos_thumb_dir) {
-	    /* attempt to create Thumbs dir */
-	    gchar *photos_dir = itdb_get_photos_dir (mount_point);
-	    gchar *dir;
-	    if (!photos_dir) {   /* give up */
-		return NULL;
-	    }
-	    dir = g_build_filename (photos_dir, "Thumbs", NULL);
-	    mkdir (dir, 0777);
-	    g_free (dir);
-	    g_free (photos_dir);
-
-	    /* try again */
-	    photos_thumb_dir = itdb_get_photos_thumb_dir (mount_point);
-	    if (!photos_thumb_dir) {   /* give up */
-		return NULL;
-	    }
-	}
-	return photos_thumb_dir;
-}
-
 static iThumbWriter *
-ithumb_writer_new (const char *mount_point,
+ithumb_writer_new (const char *thumbs_dir,
 		   const Itdb_ArtworkFormat *info,
 		   DbType db_type,
 		   guint byte_order)
@@ -1189,18 +1121,8 @@ ithumb_writer_new (const char *mount_point,
 
 	writer->byte_order = byte_order;
 	writer->db_type = db_type;
-	/* TODO this hopefully can be removed soon */
-	writer->mountpoint = g_strdup (mount_point);
-	/* get artwork directory */
-	writer->artwork_dir = ithumb_get_artwork_dir (mount_point);
-	if (!writer->artwork_dir)
-	{   /* give up */
-		ithumb_writer_free(writer);
-		return NULL;
-	}
-	/* get photos thumb directory */
-	writer->photos_thumb_dir = ithumb_get_photos_thumb_dir (mount_point);
-	if (!writer->photos_thumb_dir) {   /* give up */
+	writer->thumbs_dir = g_strdup (thumbs_dir);
+	if (!writer->thumbs_dir) {   /* give up */
 		ithumb_writer_free(writer);
 		return NULL;
 	}
@@ -1423,7 +1345,8 @@ static gboolean ithumb_rearrange_thumbnail_file (gpointer _key,
    slots are filled, the file is truncated to the new length.
 */
 static gboolean
-ithmb_rearrange_existing_thumbnails (Itdb_DB *db,
+ithmb_rearrange_existing_thumbnails (const gchar *thumbs_dir,
+				     Itdb_DB *db,
 				     const Itdb_ArtworkFormat *info)
 {
     GList *gl;
@@ -1432,22 +1355,11 @@ ithmb_rearrange_existing_thumbnails (Itdb_DB *db,
     GList *thumbs;
     gint i;
     gchar *filename;
-    const gchar *mountpoint;
-    gchar *photos_thumb_dir, *artwork_dir;
 
     g_return_val_if_fail (db, FALSE);
     g_return_val_if_fail (info, FALSE);
     g_return_val_if_fail (db_get_device(db), FALSE);
 
-    mountpoint = db_get_mountpoint (db);
-    g_return_val_if_fail (mountpoint, FALSE);
-
-    artwork_dir = ithumb_get_artwork_dir(mountpoint);
-    g_return_val_if_fail (artwork_dir, FALSE);
-
-    photos_thumb_dir = ithumb_get_photos_thumb_dir(mountpoint);
-    g_return_val_if_fail (photos_thumb_dir, FALSE);
-
     filenamehash = g_hash_table_new_full (g_str_hash, g_str_equal, 
 					  g_free, NULL);
 
@@ -1526,19 +1438,9 @@ ithmb_rearrange_existing_thumbnails (Itdb_DB *db,
 
     for (i=0; i<50; ++i)
     {
-	filename = NULL;
-	switch (db->db_type) {
-	    case DB_TYPE_PHOTO:
-		filename = ipod_image_get_ithmb_filename (photos_thumb_dir,
-						  info->format_id,
-						  i);
-		break;
-	    case DB_TYPE_ITUNES:
-		filename = ipod_image_get_ithmb_filename (artwork_dir,
+	filename = ipod_image_get_ithmb_filename (thumbs_dir,
 						  info->format_id,
 						  i);
-		break;
-	}
 	if (g_file_test (filename, G_FILE_TEST_EXISTS))
 	{
 	    if (g_hash_table_lookup (filenamehash, filename) == NULL)
@@ -1563,32 +1465,25 @@ ithmb_rearrange_existing_thumbnails (Itdb_DB *db,
 				 ithumb_rearrange_thumbnail_file, &result);
     g_hash_table_destroy (filenamehash);
 
-    g_free(artwork_dir);
-    g_free(photos_thumb_dir);
-
     return result;
 }
 
 #endif
 
 G_GNUC_INTERNAL int
-itdb_write_ithumb_files (Itdb_DB *db) 
+itdb_write_ithumb_files (Itdb_DB *db, const gchar *thumbs_dir)
 {
 #ifdef HAVE_GDKPIXBUF
 	GList *writers;
 	Itdb_Device *device;
         GList *formats;
         GList *it;
-	const gchar *mount_point;
 
 	g_return_val_if_fail (db, -1);
 	device = db_get_device(db);
 	g_return_val_if_fail (device, -1);
 
-	mount_point = db_get_mountpoint (db);
-	/* FIXME: support writing to directory rather than writing to
-	   iPod */
-	if (mount_point == NULL)
+	if (thumbs_dir == NULL)
 	    return -1;
 	
         formats = NULL;
@@ -1606,8 +1501,8 @@ itdb_write_ithumb_files (Itdb_DB *db)
                 const Itdb_ArtworkFormat *format;
 
                 format = (const Itdb_ArtworkFormat *)it->data;
-                ithmb_rearrange_existing_thumbnails (db, format);
-                writer = ithumb_writer_new (mount_point, format,
+                ithmb_rearrange_existing_thumbnails (thumbs_dir, db, format);
+                writer = ithumb_writer_new (thumbs_dir, format,
                                             db->db_type, device->byte_order);
                 if (writer != NULL) {
                         writers = g_list_prepend (writers, writer);
-- 
1.6.4.4

