[evolution-data-server/openismus-work-master: 10/12] Minor refactor of EBookBackendFile.



commit 47a15ba339f3a1e1bf66cdb95697fe563afb8a4b
Author: Tristan Van Berkom <tristan van berkom gmail com>
Date:   Tue Jul 26 19:06:16 2011 -0400

    Minor refactor of EBookBackendFile.
    
    This minor refactor adds the functions:
      remove_file(), create_directory() and load_vcard().
    
    Mostly this just improves readability, for the case of load_vcard()
    a considerable amount of redundant lines of code are removed.

 addressbook/backends/file/e-book-backend-file.c |  107 +++++++++++++++-------
 1 files changed, 73 insertions(+), 34 deletions(-)
---
diff --git a/addressbook/backends/file/e-book-backend-file.c b/addressbook/backends/file/e-book-backend-file.c
index ff5a9d8..8a68479 100644
--- a/addressbook/backends/file/e-book-backend-file.c
+++ b/addressbook/backends/file/e-book-backend-file.c
@@ -122,6 +122,45 @@ string_to_dbt (const gchar *str, DBT *dbt)
 	dbt->flags = DB_DBT_USERMEM;
 }
 
+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 EContact*
 create_contact (const gchar *uid, const gchar *vcard)
 {
@@ -132,6 +171,34 @@ create_contact (const gchar *uid, const gchar *vcard)
 	return contact;
 }
 
+static gchar *
+load_vcard (EBookBackendFile *bf,
+	    const gchar      *uid,
+	    GError          **error)
+{
+	DB     *db = bf->priv->file_db;
+	DBT     id_dbt, vcard_dbt;
+	gchar  *vcard;
+	gint    db_error;
+
+	/* Get the old contact from the db and compare the photo fields */
+	string_to_dbt (uid, &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));
+		g_propagate_error (error, EDB_ERROR (CONTACT_NOT_FOUND));
+		return NULL;
+	}
+
+	return vcard;
+}
+
 static gboolean
 build_sqlitedb (EBookBackendFilePrivate *bfpriv)
 {
@@ -487,28 +554,12 @@ e_book_backend_file_get_contact (EBookBackendSync *backend,
 				 gchar **vcard,
 				 GError **perror)
 {
-	EBookBackendFile *bf;
-	DB             *db;
-	DBT             id_dbt, vcard_dbt;
-	gint             db_error = 0;
-
-	bf = E_BOOK_BACKEND_FILE (backend);
-	db = bf->priv->file_db;
-
-	string_to_dbt (id, &id_dbt);
-	memset (&vcard_dbt, 0, sizeof (vcard_dbt));
-	vcard_dbt.flags = DB_DBT_MALLOC;
+	EBookBackendFile *bf = E_BOOK_BACKEND_FILE (backend);
 
-	db_error = db->get (db, NULL, &id_dbt, &vcard_dbt, 0);
+	*vcard = load_vcard (bf, id, perror);
 
-	if (db_error == 0) {
-		*vcard = vcard_dbt.data;
-	} else {
-		g_warning (G_STRLOC ": db->get failed with %s", db_strerror (db_error));
+	if (!*vcard)
 		*vcard = g_strdup ("");
-
-		g_propagate_error (perror, EDB_ERROR (CONTACT_NOT_FOUND));
-	}
 }
 
 static void
@@ -1221,18 +1272,12 @@ e_book_backend_file_open (EBookBackendSync       *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;
@@ -1353,14 +1398,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;
-	}
 
 	if (!e_book_backend_sqlitedb_remove (bf->priv->sqlitedb, perror))
 		return;



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