commit 53ca547db148108523cf3c7484b0db92db94736f
Author: Tristan Van Berkom <tristan.van.berkom@gmail.com>
Date:   Thu Jul 7 19:27:35 2011 -0400

    Make local addressbook backend store image data as URIs.
    
    Whenever the local address book receives a uri as a binary
    blob it proceeds to transform it to a uri accessbile on
    the system somewhere under $XDG_DATA_HOME. The local backend
    cleans up old photo uris when contacts are removed or modified
    to use new photos.

diff --git a/addressbook/backends/file/e-book-backend-file.c b/addressbook/backends/file/e-book-backend-file.c
index a65e126..f6c7fac 100644
--- a/addressbook/backends/file/e-book-backend-file.c
+++ b/addressbook/backends/file/e-book-backend-file.c
@@ -74,18 +74,70 @@ struct _EBookBackendFilePrivate {
 	gchar     *dirname;
 	gchar     *filename;
 	gchar     *summary_filename;
-	DB       *file_db;
-	DB_ENV   *env;
+	gchar     *photo_dirname;
+	DB        *file_db;
+	DB_ENV    *env;
 	EBookBackendSummary *summary;
 	guint     id;
 };
 
+typedef enum {
+	GET_PATH_DB_DIR,
+	GET_PATH_PHOTO_DIR
+} GetPathType;
+
+typedef enum {
+	STATUS_NORMAL = 0,
+	STATUS_MODIFIED,
+	STATUS_ERROR
+} PhotoModifiedStatus;
+
 G_LOCK_DEFINE_STATIC (global_env);
 static struct {
 	gint ref_count;
 	DB_ENV *env;
 } global_env;
 
+static gboolean
+remove_file (const gchar *filename, GError **error)
+{
+	if (-1 == g_unlink (filename)) {
+		if (errno == EACCES || errno == EPERM) {
+			g_propagate_error (error, EDB_ERROR (PERMISSION_DENIED));
+		} else {
+			g_propagate_error (error, e_data_book_create_error_fmt
+					   (E_DATA_BOOK_STATUS_OTHER_ERROR, 
+					    "Failed to remove file '%s': %s", 
+					    filename, g_strerror (errno)));
+		}
+		return FALSE;
+	}
+
+	return TRUE;
+}
+
+static gboolean
+create_directory (const gchar *dirname,
+		  GError     **error)
+{
+	gint rv;
+
+	rv = g_mkdir_with_parents (dirname, 0700);
+	if (rv == -1 && errno != EEXIST) {
+		g_warning ("failed to make directory %s: %s", dirname, g_strerror (errno));
+		if (errno == EACCES || errno == EPERM)
+			g_propagate_error (error, EDB_ERROR (PERMISSION_DENIED));
+		else
+			g_propagate_error (error, 
+					   e_data_book_create_error_fmt (E_DATA_BOOK_STATUS_OTHER_ERROR,
+									 "Failed to make directory %s: %s", 
+									 dirname, g_strerror (errno)));
+		return FALSE;
+	}
+	return TRUE;
+}
+
+
 static void
 db_error_to_gerror (const gint db_error, GError **perror)
 {
@@ -117,7 +169,7 @@ string_to_dbt(const gchar *str, DBT *dbt)
 }
 
 static EContact*
-create_contact (gchar *uid, const gchar *vcard)
+create_contact (const gchar *uid, const gchar *vcard)
 {
 	EContact *contact = e_contact_new_from_vcard (vcard);
 	if (!e_contact_get_const (contact, E_CONTACT_UID))
@@ -126,6 +178,265 @@ create_contact (gchar *uid, const gchar *vcard)
 	return contact;
 }
 
+
+static gchar *
+collect_uri_for_field (EContact         *old_contact,
+		       EContact         *new_contact,
+		       EContactField     field)
+{
+	EContactPhoto *old_photo, *new_photo;
+
+	old_photo = e_contact_get (old_contact, field);
+	if (!old_photo)
+		return NULL;
+
+	if (new_contact) {
+
+		new_photo = e_contact_get (new_contact, field);
+
+		if (new_photo == NULL ||
+		    strcmp (old_photo->data.uri, new_photo->data.uri))
+			return g_strdup (old_photo->data.uri);
+	} else {
+		return g_strdup (old_photo->data.uri);
+	}
+
+	return NULL;
+}
+
+static void
+maybe_delete_uri (EBookBackendFile *bf,
+		  const gchar      *uri)
+{
+	GError *error = NULL;
+	gchar  *filename;
+
+	/* A uri that does not give us a filename is certainly not
+	 * a uri that we created for a local file, just skip it */
+	if ((filename = g_filename_from_uri (uri, NULL, NULL)) == NULL)
+		return;
+
+	/* If the file is in our path it belongs to us and we need to delete it.
+	 */
+	if (!strncmp (bf->priv->photo_dirname, filename, strlen (bf->priv->photo_dirname))) {
+
+		/* Deleting uris should not cause the backend to fail to update
+		 * a contact so the best we can do from here is log warnings
+		 * when we fail to unlink a file from the disk.
+		 */
+		if (!remove_file (filename, &error)) {
+			g_warning ("Unable to cleanup photo uri: %s", error->message);
+			g_error_free (error);
+		}
+	}
+
+	g_free (filename);
+}
+
+static void
+maybe_delete_unused_uris (EBookBackendFile *bf,
+			  const gchar      *id,
+			  EContact         *new_contact)
+{
+	DB                *db = bf->priv->file_db;
+	DBT                id_dbt, vcard_dbt;
+	gchar             *uri_photo, *uri_logo, *vcard;
+	EContact          *old_contact;
+	gint               db_error;
+
+	/* Get the old contact from the db and compare the photo fields */
+	string_to_dbt (id, &id_dbt);
+	memset (&vcard_dbt, 0, sizeof (vcard_dbt));
+	vcard_dbt.flags = DB_DBT_MALLOC;
+		
+	db_error = db->get (db, NULL, &id_dbt, &vcard_dbt, 0);
+
+	if (db_error == 0) {
+		vcard = vcard_dbt.data;
+	} else {
+		g_warning (G_STRLOC ": db->get failed with %s", db_strerror (db_error));
+		return;
+	}
+
+	old_contact = create_contact (id, vcard);
+	g_free (vcard);
+
+	/* If there is no new contact, collect all the uris to delete from old_contact
+	 *
+	 * Otherwise, if any of the photo uri fields have changed in new_contact, then collect the
+	 * old uris for those fields from old_contact to delete
+	 */
+	uri_photo = collect_uri_for_field (old_contact, new_contact, E_CONTACT_PHOTO);
+	uri_logo  = collect_uri_for_field (old_contact, new_contact, E_CONTACT_LOGO);
+
+	if (uri_photo)
+		maybe_delete_uri (bf, uri_photo);
+
+	g_object_unref (old_contact);
+}
+
+static gchar *
+e_book_backend_file_extract_path_from_source (ESource      *source, 
+					      GetPathType   path_type)
+{
+	const gchar *user_data_dir;
+	const gchar *source_dir;
+	gchar *mangled_source_dir;
+	gchar *filename = NULL;
+
+	user_data_dir = e_get_user_data_dir ();
+	source_dir = e_source_peek_relative_uri (source);
+
+	if (!source_dir || !g_str_equal (source_dir, "system"))
+		source_dir = e_source_peek_uid (source);
+
+	/* Mangle the URI to not contain invalid characters. */
+	mangled_source_dir = g_strdelimit (g_strdup (source_dir), ":/", '_');
+
+	switch (path_type) {
+	case GET_PATH_DB_DIR:
+		filename = g_build_filename 
+			(user_data_dir, "addressbook", mangled_source_dir, NULL);
+		break;
+	case GET_PATH_PHOTO_DIR:
+		filename = g_build_filename 
+			(user_data_dir, "addressbook", mangled_source_dir, "photos", NULL);
+		break;
+	default:
+		break;
+	}
+	g_free (mangled_source_dir);
+
+	return filename;
+}
+
+static gchar *
+safe_name_for_photo (EBookBackendFile *bf,
+		     ESource          *source,
+		     EContact         *contact,
+		     EContactField     field)
+{
+	gchar *fullname = NULL, *uid, *str, *final;
+	gint   i = 0;
+
+	uid = g_strdup (e_contact_get_const (contact, E_CONTACT_UID));
+	uid = g_strdelimit (uid, NULL, '_');
+
+	str = g_strdup (e_contact_field_name (field));
+	str = g_strdelimit (str, NULL, '_');
+
+	final = g_strdup_printf ("%s_%s", uid, str);
+
+	do {
+		gchar *filename = NULL;
+
+		g_free (fullname);
+
+		filename = g_strdup_printf ("%s_%02d.data", final, i);
+		fullname = g_build_filename (bf->priv->photo_dirname, filename, NULL);
+		g_free (filename);
+
+		i++;
+	} while (g_file_test (fullname, G_FILE_TEST_EXISTS));
+
+	g_free (str);
+	g_free (uid);
+	g_free (final);
+
+	return fullname;
+}
+
+static PhotoModifiedStatus
+maybe_transform_vcard_field_for_photo (EBookBackendFile *bf,
+				       ESource          *source,
+				       EContact         *contact,
+				       EContactField     field,
+				       GError          **error)
+{
+	PhotoModifiedStatus  status = STATUS_NORMAL;
+	EContactPhoto       *photo;
+
+	if (field != E_CONTACT_PHOTO && field != E_CONTACT_LOGO)
+		return status;
+
+	photo = e_contact_get (contact, field);
+	if (!photo)
+		return status;
+
+	if (photo->type == E_CONTACT_PHOTO_TYPE_INLINED) {
+		EContactPhoto *new_photo;
+		gchar         *new_photo_path;
+		gchar         *uri;
+
+		/* XXX Create a good file extension based on mime type */
+		new_photo_path = safe_name_for_photo (bf, source, contact, field);
+
+		if ((uri = 
+		     g_filename_to_uri (new_photo_path, NULL, error)) == NULL) {
+
+			status = STATUS_ERROR;
+		} else if (!g_file_set_contents (new_photo_path,
+						 (const gchar *)photo->data.inlined.data,
+						 photo->data.inlined.length,
+						 error)) {
+
+			status = STATUS_ERROR;
+		} else {
+			new_photo           = g_new (EContactPhoto, 1);
+			new_photo->type     = E_CONTACT_PHOTO_TYPE_URI;
+			new_photo->data.uri = uri;
+
+			e_contact_set (contact, field, new_photo);
+
+			status = STATUS_MODIFIED;
+		}
+
+		g_free (uri);
+		g_free (new_photo_path);
+	}
+
+	return status;
+}
+
+/*
+ * When a contact is added or modified we receive a vCard,
+ * this function checks if we've received inline data
+ * and replaces it with a uri notation.
+ *
+ * Returns the actual data and stores the size in 'len'
+ */
+static PhotoModifiedStatus
+maybe_transform_vcard_for_photo (EBookBackendFile *bf,
+				 EContact         *contact,
+				 gchar           **vcard_ret,
+				 GError          **error)
+{
+	EBookBackend *backend = E_BOOK_BACKEND (bf);
+	ESource      *source  = e_book_backend_get_source (backend);
+	gboolean      modified = FALSE;
+	PhotoModifiedStatus status;
+
+	status   = maybe_transform_vcard_field_for_photo (bf, source, contact, E_CONTACT_PHOTO, error);
+	modified = (status == STATUS_MODIFIED);
+
+	if (status != STATUS_ERROR) {
+		status   = maybe_transform_vcard_field_for_photo (bf, source, contact, E_CONTACT_LOGO, error);
+		modified = modified || (status == STATUS_MODIFIED);
+	}
+
+	if (status != STATUS_ERROR) {
+		if (modified) {
+
+			if (vcard_ret)
+				*vcard_ret = e_vcard_to_string (E_VCARD (contact), EVC_FORMAT_VCARD_30);
+			status = STATUS_MODIFIED;
+		}
+	}
+
+	return status;
+}
+
+
 static gboolean
 build_summary (EBookBackendFilePrivate *bfpriv)
 {
@@ -195,18 +506,22 @@ do_create(EBookBackendFile  *bf,
 	  EContact **contact,
 	  GError **perror)
 {
-	DB             *db = bf->priv->file_db;
-	DBT            id_dbt, vcard_dbt;
-	gint            db_error;
-	gchar           *id;
-	gchar           *vcard;
-	const gchar *rev;
+	DB              *db = bf->priv->file_db;
+	DBT              id_dbt, vcard_dbt;
+	gint             db_error;
+	gchar           *id = NULL;
+	gchar           *vcard = NULL;
+	const gchar     *rev;
+	PhotoModifiedStatus status = STATUS_NORMAL;
 
 	g_assert (bf);
 	g_assert (vcard_req);
 	g_assert (contact);
 
  retry:
+	g_free (id);
+	g_free (vcard);
+
 	id = e_book_backend_file_create_next_id (bf);
 
 	string_to_dbt (id, &id_dbt);
@@ -223,9 +538,6 @@ do_create(EBookBackendFile  *bf,
 
 	db_error = db->put (db, NULL, &id_dbt, &vcard_dbt, DB_NOOVERWRITE);
 
-	g_free (vcard);
-	g_free (id);
-
 	if (0 == db_error) {
 		db_error = db->sync (db, 0);
 		if (db_error != 0) {
@@ -242,7 +554,40 @@ do_create(EBookBackendFile  *bf,
 		}
 	}
 
-	db_error_to_gerror (db_error, perror);
+	/* If we havent failed yet, now we might have to overwrite
+	 * the entry with a transformed contact.
+	 *
+	 * We wait until the last minute to re-enter any new contact
+	 * with photo data into the database because we need to know
+	 * the contact UID in order to know where to store the data
+	 * (and in order to derive the correct uri for the vCard).
+	 */
+	if (0 == db_error) {
+
+		gchar *transformed_vcard = NULL;
+
+		status = maybe_transform_vcard_for_photo (bf, *contact, &transformed_vcard, perror);
+
+		if (status == STATUS_MODIFIED && transformed_vcard) {
+
+			/* Here we just dump the new version of the vcard into
+			 * the DB, the returned EContact is correct and stores
+			 * images by uri already so the following notifications
+			 * to the views should be correct.
+			 */
+			string_to_dbt (id,                &id_dbt);
+			string_to_dbt (transformed_vcard, &vcard_dbt);
+
+			db_error = db->put (db, NULL, &id_dbt, &vcard_dbt, 0);
+		}
+		g_free (transformed_vcard);
+	}
+
+	g_free (vcard);
+	g_free (id);
+
+	if (perror && !perror)
+		db_error_to_gerror (db_error, perror);
 
 	return db_error == 0;
 }
@@ -275,12 +620,16 @@ e_book_backend_file_remove_contacts (EBookBackendSync *backend,
 	DBT            id_dbt;
 	gint            db_error;
 	gchar          *id;
-	GList         *l;
-	GList         *removed_cards = NULL;
+	GList          *l;
+	GList          *removed_cards = NULL;
 
 	for (l = id_list; l; l = l->next) {
 		id = l->data;
 
+		/* First collect uris to delete */
+		maybe_delete_unused_uris (bf, id, NULL);
+
+		/* Then go on to delete from the db */
 		string_to_dbt (id, &id_dbt);
 
 		db_error = db->del (db, NULL, &id_dbt, 0);
@@ -322,6 +671,7 @@ e_book_backend_file_modify_contact (EBookBackendSync *backend,
 	gint            db_error;
 	const gchar    *id, *lookup_id;
 	gchar          *vcard_with_rev;
+	PhotoModifiedStatus status;
 
 	*contact = e_contact_new_from_vcard (vcard);
 	id = e_contact_get_const (*contact, E_CONTACT_UID);
@@ -331,6 +681,15 @@ e_book_backend_file_modify_contact (EBookBackendSync *backend,
 		return;
 	}
 
+	/* Transform incomming photo blobs to uris before storing this to the DB */
+	status = maybe_transform_vcard_for_photo (bf, *contact, NULL, perror);
+	if (status == STATUS_ERROR)
+		return;
+
+	/* Delete old photo file uris if need be (this will compare the new contact
+	 * with the current copy in the BDB to extract the uris to delete) */
+	maybe_delete_unused_uris (bf, id, *contact);
+
 	/* update the revision (modified time of contact) */
 	set_revision (*contact);
 	vcard_with_rev = e_vcard_to_string (E_VCARD (*contact), EVC_FORMAT_VCARD_30);
@@ -787,8 +1146,9 @@ book_view_thread (gpointer data)
 
 	}
 done:
-	if (e_flag_is_set (closure->running))
+	if (e_flag_is_set (closure->running)) {
 		e_data_book_view_notify_complete (book_view, NULL /* Success */);
+	}
 
 	/* unref the */
 	printf("book_view file uref \n");
@@ -1018,31 +1378,6 @@ e_book_backend_file_get_changes (EBookBackendSync *backend,
 	e_dbhash_destroy (ehash);
 }
 
-static gchar *
-e_book_backend_file_extract_path_from_source (ESource *source)
-{
-	const gchar *user_data_dir;
-	const gchar *source_dir;
-	gchar *mangled_source_dir;
-	gchar *filename;
-
-	user_data_dir = e_get_user_data_dir ();
-	source_dir = e_source_peek_relative_uri (source);
-
-	if (!source_dir || !g_str_equal (source_dir, "system"))
-		source_dir = e_source_peek_uid (source);
-
-	/* Mangle the URI to not contain invalid characters. */
-	mangled_source_dir = g_strdelimit (g_strdup (source_dir), ":/", '_');
-
-	filename = g_build_filename (
-		user_data_dir, "addressbook", mangled_source_dir, NULL);
-
-	g_free (mangled_source_dir);
-
-	return filename;
-}
-
 static void
 e_book_backend_file_authenticate_user (EBookBackendSync *backend,
 				       EDataBook *book,
@@ -1255,7 +1590,7 @@ e_book_backend_file_load_source (EBookBackend           *backend,
 	struct stat sb;
 	DB_HASH_STAT *hashstat;
 
-	dirname = e_book_backend_file_extract_path_from_source (source);
+	dirname = e_book_backend_file_extract_path_from_source (source, GET_PATH_DB_DIR);
 	filename = g_build_filename (dirname, "addressbook.db", NULL);
 
 	db_error = e_db3_utils_maybe_recover (filename);
@@ -1369,18 +1704,11 @@ e_book_backend_file_load_source (EBookBackend           *backend,
 		db_error = (*db->open) (db, NULL, filename, NULL, DB_HASH, DB_RDONLY | DB_THREAD, 0666);
 
 		if (db_error != 0 && !only_if_exists) {
-			gint rv;
-
 			/* the database didn't exist, so we create the
 			   directory then the .db */
 			db->close (db, 0);
-			rv = g_mkdir_with_parents (dirname, 0700);
-			if (rv == -1 && errno != EEXIST) {
-				g_warning ("failed to make directory %s: %s", dirname, g_strerror (errno));
-				if (errno == EACCES || errno == EPERM)
-					g_propagate_error (perror, EDB_ERROR (PERMISSION_DENIED));
-				else
-					g_propagate_error (perror, e_data_book_create_error_fmt (E_DATA_BOOK_STATUS_OTHER_ERROR, "Failed to make directory %s: %s", dirname, g_strerror (errno)));
+
+			if (!create_directory (dirname, perror)) {
 				g_free (dirname);
 				g_free (filename);
 				return;
@@ -1474,6 +1802,12 @@ e_book_backend_file_load_source (EBookBackend           *backend,
 		}
 	}
 
+	/* Resolve the photo directory here */
+	dirname = e_book_backend_file_extract_path_from_source (source, GET_PATH_PHOTO_DIR);
+	if (!only_if_exists && !create_directory (dirname, perror))
+		return;
+	bf->priv->photo_dirname = dirname;
+
 	e_book_backend_set_is_loaded (backend, TRUE);
 	e_book_backend_set_is_writable (backend, writable);
 }
@@ -1497,6 +1831,28 @@ select_changes (const gchar *name)
 }
 
 static void
+remove_photos (const gchar *dirname)
+{
+	GDir *dir;
+
+	dir = g_dir_open (dirname, 0, NULL);
+	if (dir) {
+		const gchar *name;
+
+		while ((name = g_dir_read_name (dir))) {
+			gchar *full_path = g_build_filename (dirname, name, NULL);
+			if (-1 == g_unlink (full_path)) {
+				g_warning ("failed to remove photo file %s: %s",
+					   full_path, g_strerror (errno));
+			}
+			g_free (full_path);
+		}
+
+		g_dir_close (dir);
+	}
+}
+
+static void
 e_book_backend_file_remove (EBookBackendSync *backend,
 			    EDataBook        *book,
 			    guint32           opid,
@@ -1505,14 +1861,8 @@ e_book_backend_file_remove (EBookBackendSync *backend,
 	EBookBackendFile *bf = E_BOOK_BACKEND_FILE (backend);
 	GDir *dir;
 
-	if (-1 == g_unlink (bf->priv->filename)) {
-		if (errno == EACCES || errno == EPERM) {
-			g_propagate_error (perror, EDB_ERROR (PERMISSION_DENIED));
-		} else {
-			g_propagate_error (perror, e_data_book_create_error_fmt (E_DATA_BOOK_STATUS_OTHER_ERROR, "Failed to remove file '%s': %s", bf->priv->filename, g_strerror (errno)));
-		}
+	if (!remove_file (bf->priv->filename, perror))
 		return;
-	}
 
 	/* unref the summary before we remove the file so it's not written out again */
 	g_object_unref (bf->priv->summary);
@@ -1537,6 +1887,12 @@ e_book_backend_file_remove (EBookBackendSync *backend,
 		g_dir_close (dir);
 	}
 
+	/* Remove photos and the photo directory */
+	remove_photos (bf->priv->photo_dirname);
+	if (-1 == g_rmdir (bf->priv->photo_dirname))
+		g_warning ("failed to remove directory `%s`: %s", bf->priv->dirname, g_strerror (errno));
+
+	/* Try removing the base directory now */
 	if (-1 == g_rmdir (bf->priv->dirname))
 		g_warning ("failed to remove directory `%s`: %s", bf->priv->dirname, g_strerror (errno));
 
@@ -1630,6 +1986,7 @@ e_book_backend_file_finalize (GObject *object)
 	g_free (bf->priv->filename);
 	g_free (bf->priv->dirname);
 	g_free (bf->priv->summary_filename);
+	g_free (bf->priv->photo_dirname);
 
 	g_free (bf->priv);
 
@@ -1731,7 +2088,6 @@ e_book_backend_file_init (EBookBackendFile *backend)
 {
 	EBookBackendFilePrivate *priv;
 
-	priv             = g_new0 (EBookBackendFilePrivate, 1);
-
+	priv          = g_new0 (EBookBackendFilePrivate, 1);
 	backend->priv = priv;
 }
