[evolution-data-server/openismus-work-master: 6/8] 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: 6/8] Minor refactor of EBookBackendFile.
- Date: Wed, 3 Aug 2011 01:35:59 +0000 (UTC)
commit 36f11dc3d33f67f6c29a11cebea0009aa5771fac
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 | 117 +++++++++++++++--------
1 files changed, 78 insertions(+), 39 deletions(-)
---
diff --git a/addressbook/backends/file/e-book-backend-file.c b/addressbook/backends/file/e-book-backend-file.c
index c9b3cda..17fcfcb 100644
--- a/addressbook/backends/file/e-book-backend-file.c
+++ b/addressbook/backends/file/e-book-backend-file.c
@@ -123,6 +123,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)
{
@@ -133,6 +172,39 @@ 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;
+
+ if (!db) {
+ g_propagate_error (perror, EDB_NOT_OPENED_ERROR);
+ return NULL;
+ }
+
+ /* 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)
{
@@ -508,33 +580,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;
-
- if (!db) {
- g_propagate_error (perror, EDB_NOT_OPENED_ERROR);
- return;
- }
-
- 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
@@ -1271,18 +1322,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;
@@ -1403,14 +1448,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]