[evolution-data-server/openismus-work-master: 11/12] Make local addressbook backend store image data as URIs.



commit 542423bf26e8f6e77615717845992fc4a581eb2f
Author: Tristan Van Berkom <tristan van berkom gmail com>
Date:   Fri Jul 22 20:05:21 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.
    
    Additionally, whenever it is detected that the user is
    cross-referencing a uri which belongs to another contact,
    a hard-link will be created to the addressbook owned file
    on disk and a new uri will be assigned to any additional
    contacts which try to share a uri owned by the addressbook.

 addressbook/backends/file/e-book-backend-file.c |  605 ++++++++++++++++++++---
 1 files changed, 546 insertions(+), 59 deletions(-)
---
diff --git a/addressbook/backends/file/e-book-backend-file.c b/addressbook/backends/file/e-book-backend-file.c
index 8a68479..641098c 100644
--- a/addressbook/backends/file/e-book-backend-file.c
+++ b/addressbook/backends/file/e-book-backend-file.c
@@ -79,6 +79,7 @@ G_DEFINE_TYPE (EBookBackendFile, e_book_backend_file, E_TYPE_BOOK_BACKEND_SYNC)
 struct _EBookBackendFilePrivate {
 	gchar     *dirname;
 	gchar     *filename;
+	gchar     *photo_dirname;
 	gchar     *revision;
 	DB        *file_db;
 	DB_ENV    *env;
@@ -86,6 +87,17 @@ struct _EBookBackendFilePrivate {
 	EBookBackendSqliteDB *sqlitedb;
 };
 
+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;
@@ -199,6 +211,436 @@ load_vcard (EBookBackendFile *bf,
 	return vcard;
 }
 
+static EContact *
+load_contact (EBookBackendFile *bf,
+	      const gchar      *uid,
+	      GError          **error)
+{
+	EContact *contact = NULL;
+	gchar    *vcard;
+
+	if ((vcard = load_vcard (bf, uid, error)) != NULL) {
+		contact = create_contact (uid, vcard);
+		g_free (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))) {
+
+		d(g_print ("Deleting uri file: %s\n", filename));
+
+		/* 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         *old_contact,
+			  EContact         *new_contact)
+{
+	GError            *error = NULL;
+	gchar             *uri_photo, *uri_logo;
+
+	if (!old_contact) {
+		old_contact = load_contact (bf, id, &error);
+
+		if (!old_contact) {
+			g_warning (G_STRLOC ": failed to load contact %s", error->message);
+			g_error_free (error);
+			return;
+		}
+	} else
+		g_object_ref (old_contact);
+
+	/* 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_free (uri_photo);
+	}
+
+	if (uri_logo) {
+		maybe_delete_uri (bf, uri_logo);
+		g_free (uri_logo);
+	}
+
+	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,
+		     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 gchar *
+hard_link_photo (EBookBackendFile *bf,
+		 EContact         *contact,
+		 EContactField     field,
+		 const gchar      *src_filename,
+		 GError          **error)
+{
+	gchar *fullname = NULL, *uid, *str, *final;
+	gint   i = 0, ret;
+
+	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++;
+
+		ret = link (src_filename, fullname);
+
+	} while (ret < 0 && errno == EEXIST);
+
+	if (ret < 0) {
+		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 create hardlink for resource '%s': %s", 
+					    src_filename, g_strerror (errno)));
+		}
+		g_free (fullname);
+		fullname = NULL;
+	}
+
+	g_free (str);
+	g_free (uid);
+	g_free (final);
+
+	return fullname;
+}
+
+
+static gboolean 
+is_backend_owned_uri (EBookBackendFile *bf, 
+		      const gchar      *uri)
+{
+	gchar     *filename;
+	gchar     *dirname;
+	gboolean   owned_uri;
+
+	/* Errors converting from uri definitily indicate it was
+	 * not our uri to begin with, so just disregard this error. */
+	filename = g_filename_from_uri (uri, NULL, NULL);
+	if (!filename)
+		return FALSE;
+
+	dirname = g_path_get_dirname (filename);
+
+	owned_uri = (strcmp (dirname, bf->priv->photo_dirname) == 0);
+
+	g_free (filename);
+	g_free (dirname);
+
+	return owned_uri;
+}
+
+
+static PhotoModifiedStatus
+maybe_transform_vcard_field_for_photo (EBookBackendFile *bf,
+				       EContact         *old_contact,
+				       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, 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_slice_new0 (EContactPhoto);
+			new_photo->type     = E_CONTACT_PHOTO_TYPE_URI;
+			new_photo->data.uri = uri;
+
+			e_contact_set (contact, field, new_photo);
+
+			d(g_print ("Backend modified incomming binary blob to be %s:\n", uri));
+
+			status = STATUS_MODIFIED;
+
+			g_slice_free (EContactPhoto, new_photo);
+		}
+
+		g_free (uri);
+		g_free (new_photo_path);
+
+	} else { /* E_CONTACT_PHOTO_TYPE_URI */
+		const gchar       *uid;
+		EContactPhoto     *old_photo = NULL, *new_photo;
+
+		/* First determine that the new contact uri points to our 'photos' directory,
+		 * if not then we do nothing
+		 */
+		if (!is_backend_owned_uri (bf, photo->data.uri))
+			return status;
+
+		/* Now check if the uri is changed from the BDB copy
+		 */
+		uid = e_contact_get_const (contact, E_CONTACT_UID);
+		if (uid == NULL) {
+			g_propagate_error (error, EDB_ERROR_EX (OTHER_ERROR, "No UID in the contact"));
+			return STATUS_ERROR;
+		}
+
+		if (old_contact)
+			old_photo = e_contact_get (old_contact, field);
+
+		/* Unless we are receiving the same uri that we already have
+		 * stored in the BDB... */
+		if (!old_photo || old_photo->type == E_CONTACT_PHOTO_TYPE_INLINED ||
+		    strcmp (old_photo->data.uri, photo->data.uri) != 0) {
+			gchar *filename;
+			gchar *new_filename;
+			gchar *new_uri = NULL;
+
+			/* ... Assume that the incomming uri belongs to another contact
+			 * still in the BDB. Lets go ahead and create a hard link to the 
+			 * photo file and create a new name for the incomming uri, and
+			 * use that in the incomming contact to save in place.
+			 *
+			 * This piece of code is here to ensure there are no problems if
+			 * the libebook user decides to cross-reference and start "sharing"
+			 * uris that we've previously stored in the photo directory.
+			 *
+			 * We use the hard-link here to off-load the necessary ref-counting
+			 * logic to the file-system.
+			 */
+			filename = g_filename_from_uri (photo->data.uri, NULL, NULL);
+			g_assert (filename); /* we already checked this with 'is_backend_owned_uri()' */
+
+			new_filename = hard_link_photo (bf, contact, field, filename, error);
+
+			if (!new_filename)
+				status = STATUS_ERROR;
+			else if ((new_uri = g_filename_to_uri (new_filename, NULL, error)) == NULL) {
+				/* If we fail here... we need to clean up the hardlink we just created */
+				GError *local_err = NULL;
+				if (!remove_file (new_filename, &local_err)) {
+					g_warning ("Unable to cleanup photo uri: %s", local_err->message);
+					g_error_free (local_err);
+				}
+				status = STATUS_ERROR;
+			} else {
+
+				new_photo           = g_slice_new0 (EContactPhoto);
+				new_photo->type     = E_CONTACT_PHOTO_TYPE_URI;
+				new_photo->data.uri = new_uri;
+
+				e_contact_set (contact, field, new_photo);
+
+				d(g_print ("Backend modified incomming shared uri to be %s:\n", new_uri));
+
+				g_slice_free (EContactPhoto, new_photo);
+				status = STATUS_MODIFIED;
+			}
+			g_free (new_uri);
+			g_free (new_filename);
+			g_free (filename);
+		}
+	}
+
+	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         *old_contact,
+				 EContact         *contact,
+				 gchar           **vcard_ret,
+				 GError          **error)
+{
+	PhotoModifiedStatus status;
+	gboolean            modified = FALSE;
+
+	status   = maybe_transform_vcard_field_for_photo (bf, old_contact, contact,
+							  E_CONTACT_PHOTO, error);
+	modified = (status == STATUS_MODIFIED);
+
+	if (status != STATUS_ERROR) {
+		status   = maybe_transform_vcard_field_for_photo (bf, old_contact, 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_sqlitedb (EBookBackendFilePrivate *bfpriv)
 {
@@ -353,12 +795,13 @@ 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);
@@ -380,8 +823,6 @@ do_create (EBookBackendFile  *bf,
 
 	db_error = db->put (db, NULL, &id_dbt, &vcard_dbt, 0);
 
-	g_free (vcard);
-
 	if (0 == db_error) {
 		db_error = db->sync (db, 0);
 		if (db_error != 0) {
@@ -393,10 +834,42 @@ do_create (EBookBackendFile  *bf,
 		*contact = NULL;
 	}
 
+	/* 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, NULL, *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);
-	db_error_to_gerror (db_error, perror);
 
-	return db_error == 0;
+	if (db_error && !perror)
+		db_error_to_gerror (db_error, perror);
+
+	return db_error == 0 && status != STATUS_ERROR;
 }
 
 static void
@@ -443,6 +916,10 @@ e_book_backend_file_remove_contacts (EBookBackendSync *backend,
 	for (l = id_list; l; l = l->next) {
 		id = l->data;
 
+		/* First collect uris to delete */
+		maybe_delete_unused_uris (bf, id, NULL, NULL);
+
+		/* Then go on to delete from the db */
 		string_to_dbt (id, &id_dbt);
 
 		db_error = db->del (db, NULL, &id_dbt, 0);
@@ -488,6 +965,8 @@ e_book_backend_file_modify_contact (EBookBackendSync *backend,
 	gint            db_error;
 	const gchar    *id, *lookup_id;
 	gchar          *vcard_with_rev;
+	PhotoModifiedStatus status;
+	EContact       *old_contact;
 
 	*contact = e_contact_new_from_vcard (vcard);
 	id = e_contact_get_const (*contact, E_CONTACT_UID);
@@ -497,6 +976,24 @@ e_book_backend_file_modify_contact (EBookBackendSync *backend,
 		return;
 	}
 
+	old_contact = load_contact (bf, id, perror);
+	if (!old_contact) {
+		g_warning (G_STRLOC ": Failed to load contact %s", id);
+		return;
+	}
+
+	/* Transform incomming photo blobs to uris before storing this to the DB */
+	status = maybe_transform_vcard_for_photo (bf, old_contact, *contact, NULL, perror);
+	if (status == STATUS_ERROR) {
+		g_object_unref (old_contact);
+		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, old_contact, *contact);
+	g_object_unref (old_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);
@@ -870,7 +1367,7 @@ book_view_thread (gpointer data)
 				if (!e_flag_is_set (closure->running))
 					break;
 
-				/* don't include the version in the list of cards */
+				/* don't include the version or revision in the list of cards */
 				if (strcmp (id_dbt.data, E_BOOK_BACKEND_FILE_VERSION_NAME) &&
 				    strcmp (id_dbt.data, E_BOOK_BACKEND_FILE_REVISION_NAME)) {
 					notify_update_vcard (book_view, allcontacts, 
@@ -898,7 +1395,7 @@ book_view_thread (gpointer data)
 	if (e_flag_is_set (closure->running))
 		e_data_book_view_notify_complete (book_view, NULL /* Success */);
 
-	/* unref the */
+	/* unref the view */
 	printf("book_view file uref \n");
 	e_data_book_view_unref (book_view);
 
@@ -940,50 +1437,6 @@ e_book_backend_file_stop_book_view (EBookBackend  *backend,
 		g_thread_join (closure->thread);
 }
 
-static gchar *
-e_book_backend_file_extract_path_from_source (ESource *source)
-{
-	gchar *filename = NULL;
-	const gchar *absolute_uri;
-
-	absolute_uri = e_source_peek_absolute_uri (source);
-
-	if (absolute_uri && g_str_has_prefix (absolute_uri, "local://")) {
-		gchar *uri;
-
-		uri = g_strconcat ("file://", absolute_uri + 8, NULL);
-		filename = g_filename_from_uri (uri, NULL, NULL);
-		g_free (uri);
-
-		if (!g_file_test (filename, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
-			g_free (filename);
-			filename = NULL;
-		}
-	}
-
-	if (!filename) {
-		const gchar *user_data_dir;
-		const gchar *source_dir;
-		gchar *mangled_source_dir;
-
-		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,
 				       GCancellable *cancellable,
@@ -1158,7 +1611,7 @@ e_book_backend_file_open (EBookBackendSync       *backend,
 	gboolean create_default_vcard = FALSE;
 #endif
 
-	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);
@@ -1364,6 +1817,12 @@ e_book_backend_file_open (EBookBackendSync       *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_file_load_revision (bf);
 
 	e_book_backend_notify_online (E_BOOK_BACKEND (backend), TRUE);
@@ -1390,6 +1849,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,
 			    GCancellable *cancellable,
@@ -1425,6 +1906,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));
 
@@ -1544,6 +2031,7 @@ e_book_backend_file_finalize (GObject *object)
 	g_free (bf->priv->filename);
 	g_free (bf->priv->dirname);
 	g_free (bf->priv->revision);
+	g_free (bf->priv->photo_dirname);
 
 	g_free (bf->priv);
 
@@ -1642,7 +2130,6 @@ e_book_backend_file_init (EBookBackendFile *backend)
 {
 	EBookBackendFilePrivate *priv;
 
-	priv             = g_new0 (EBookBackendFilePrivate, 1);
-
+	priv          = g_new0 (EBookBackendFilePrivate, 1);
 	backend->priv = priv;
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]