[evolution-data-server/openismus-work-master: 9/11] Minor refactor of EBookBackendFile.
- From: Tristan Van Berkom <tvb src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [evolution-data-server/openismus-work-master: 9/11] Minor refactor of EBookBackendFile.
- Date: Tue, 26 Jul 2011 23:44:37 +0000 (UTC)
commit 302f8300963a2797effdb21665153c455a15ec56
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 | 141 ++++++++++++++---------
1 files changed, 84 insertions(+), 57 deletions(-)
---
diff --git a/addressbook/backends/file/e-book-backend-file.c b/addressbook/backends/file/e-book-backend-file.c
index 7378f61..2e1bddb 100644
--- a/addressbook/backends/file/e-book-backend-file.c
+++ b/addressbook/backends/file/e-book-backend-file.c
@@ -118,8 +118,47 @@ 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 (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))
@@ -128,6 +167,34 @@ create_contact (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_summary (EBookBackendFilePrivate *bfpriv)
{
@@ -441,28 +508,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
@@ -497,19 +548,15 @@ e_book_backend_file_get_contact_list (EBookBackendSync *backend,
for (i = 0; i < ids->len; i++) {
gchar *id = g_ptr_array_index (ids, i);
- string_to_dbt (id, &id_dbt);
- memset (&vcard_dbt, 0, sizeof (vcard_dbt));
- vcard_dbt.flags = DB_DBT_MALLOC;
+ gchar *vcard;
- db_error = db->get (db, NULL, &id_dbt, &vcard_dbt, 0);
- if (db_error == 0) {
- contact_list = g_slist_prepend (contact_list, vcard_dbt.data);
- } else {
- g_warning (G_STRLOC ": db->get failed with %s", db_strerror (db_error));
- db_error_to_gerror (db_error, perror);
+ vcard = load_vcard (bf, id, perror);
+ if (!vcard)
break;
- }
+
+ contact_list = g_slist_prepend (contact_list, vcard_dbt.data);
}
+
g_ptr_array_free (ids, TRUE);
} else {
search_needed = TRUE;
@@ -835,6 +882,7 @@ book_view_thread (gpointer data)
for (i = 0; i < ids->len; i++) {
gchar *id = g_ptr_array_index (ids, i);
+ gchar *vcard;
if (!e_flag_is_set (closure->running))
break;
@@ -844,18 +892,9 @@ book_view_thread (gpointer data)
continue;
}
- 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) {
- notify_update_vcard (book_view, TRUE, id, vcard_dbt.data);
- }
- else {
- g_warning (G_STRLOC ": db->get failed with %s", db_strerror (db_error));
- }
+ vcard = load_vcard (bf, id, NULL);
+ if (vcard)
+ notify_update_vcard (book_view, TRUE, id, vcard);
}
g_ptr_array_free (ids, TRUE);
@@ -1280,18 +1319,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;
@@ -1410,14 +1443,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);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]