[evolution-couchdb] Store the document revisions separately, to avoid having VCARDs carrying it over when copied to othe



commit 662fa90174228e42b0a320bb080ed68698ac8d7e
Author: Rodrigo Moya <rodrigo gnome-db org>
Date:   Fri Oct 15 11:11:41 2010 +0200

    Store the document revisions separately, to avoid having VCARDs carrying it over when copied to other addressbooks

 addressbook/e-book-backend-couchdb.c |   51 +++++++++++++++++++++++----------
 addressbook/e-book-backend-couchdb.h |    1 +
 2 files changed, 36 insertions(+), 16 deletions(-)
---
diff --git a/addressbook/e-book-backend-couchdb.c b/addressbook/e-book-backend-couchdb.c
index 49c292e..3d2be8a 100644
--- a/addressbook/e-book-backend-couchdb.c
+++ b/addressbook/e-book-backend-couchdb.c
@@ -28,7 +28,6 @@
 #include <dbus/dbus-glib.h>
 #include <gnome-keyring.h>
 
-#define COUCHDB_REVISION_PROP                "X-COUCHDB-REVISION"
 #define COUCHDB_UUID_PROP                    "X-COUCHDB-UUID"
 #define COUCHDB_APPLICATION_ANNOTATIONS_PROP "X-COUCHDB-APPLICATION-ANNOTATIONS"
 
@@ -47,7 +46,7 @@ get_current_time (gchar time_string[100])
 }
 
 static EContact *
-contact_from_couch_document (CouchdbDocument *document)
+contact_from_couch_document (EBookBackendCouchDB *couchdb_backend, CouchdbDocument *document)
 {
 	EContact *contact;
 	char *str;
@@ -79,12 +78,13 @@ contact_from_couch_document (CouchdbDocument *document)
 		}
 	}
 
+	/* Add entry to the revisions table */
+	g_hash_table_insert (couchdb_backend->revisions,
+			     g_strdup (couchdb_document_get_id (document)),
+			     g_strdup (couchdb_document_get_revision (document)));
+
 	/* Fill in the EContact with the data from the CouchDBDocument */
 	contact = e_contact_new ();
-	e_vcard_add_attribute_with_value (E_VCARD (contact),
-					  e_vcard_attribute_new (NULL, COUCHDB_REVISION_PROP),
-					  couchdb_document_get_revision (document));
-
 	e_contact_set (contact, E_CONTACT_UID, (const gpointer) couchdb_document_get_id (document));
 
 	contact_name.family = (char *) couchdb_document_contact_get_last_name (COUCHDB_DOCUMENT_CONTACT (document));
@@ -716,11 +716,11 @@ couch_document_from_contact (EBookBackendCouchDB *couchdb_backend, EContact *con
 	document = couchdb_document_contact_new ();
 
 	str = e_contact_get_const (contact, E_CONTACT_UID);
-	if (str)
+	if (str != NULL)
 		couchdb_document_set_id (COUCHDB_DOCUMENT (document), str);
 
-	str = e_vcard_attribute_get_value (e_vcard_get_attribute (E_VCARD (contact), COUCHDB_REVISION_PROP));
-	if (str)
+	str = g_hash_table_lookup (couchdb_backend->revisions, e_contact_get_const (contact, E_CONTACT_UID));
+	if (str != NULL)
 		couchdb_document_set_revision (COUCHDB_DOCUMENT (document), str);
 
 	contact_name = (EContactName *) e_contact_get (contact, E_CONTACT_NAME);
@@ -927,14 +927,18 @@ document_updated_cb (CouchdbDatabase *database, CouchdbDocument *document, gpoin
 	EContact *contact;
 	EBookBackendCouchDB *couchdb_backend = E_BOOK_BACKEND_COUCHDB (user_data);
 
-	contact = contact_from_couch_document (document);
+	contact = contact_from_couch_document (couchdb_backend, document);
 	if (!contact)
 		return;
 
-	e_book_backend_notify_update (E_BOOK_BACKEND (couchdb_backend), contact);
-
 	/* Add the contact to the cache */
 	e_book_backend_cache_add_contact (couchdb_backend->cache, contact);
+	g_hash_table_insert (couchdb_backend->revisions,
+			     g_strdup (couchdb_document_get_id (document)),
+			     g_strdup (couchdb_document_get_revision (document)));
+
+	/* Notify listeners */
+	e_book_backend_notify_update (E_BOOK_BACKEND (couchdb_backend), contact);
 
 	g_object_unref (G_OBJECT (contact));
 }
@@ -944,10 +948,12 @@ document_deleted_cb (CouchdbDatabase *database, const char *docid, gpointer user
 {
 	EBookBackendCouchDB *couchdb_backend = E_BOOK_BACKEND_COUCHDB (user_data);
 
-	e_book_backend_notify_remove (E_BOOK_BACKEND (couchdb_backend), docid);
-
 	/* Remove the contact from the cache */
 	e_book_backend_cache_remove_contact (couchdb_backend->cache, docid);
+	g_hash_table_remove (couchdb_backend->revisions, docid);
+
+	/* Notify listeners */
+	e_book_backend_notify_remove (E_BOOK_BACKEND (couchdb_backend), docid);
 }
 
 static GNOME_Evolution_Addressbook_CallStatus
@@ -969,6 +975,10 @@ e_book_backend_couchdb_load_source (EBookBackend *backend,
 		g_object_unref (G_OBJECT (couchdb_backend->database));
 	if (couchdb_backend->cache != NULL)
 		g_object_unref (G_OBJECT (couchdb_backend->cache));
+	if (couchdb_backend->revisions != NULL) {
+		/* Remove all IDs<->revisions pairs */
+		g_hash_table_remove_all (couchdb_backend->revisions);
+	}
 
 	/* create CouchDB main object */
 	couchdb_backend->using_desktopcouch = FALSE;
@@ -1074,6 +1084,8 @@ e_book_backend_couchdb_remove (EBookBackend *backend, EDataBook *book, guint32 o
 		couchdb_backend->cache = NULL;
 	}
 
+	g_hash_table_remove_all (couchdb_backend->revisions);
+
 	/* We don't remove data from CouchDB, since it would affect other apps,
 	   so just report success */
 	e_data_book_respond_remove (book, opid, GNOME_Evolution_Addressbook_Success);
@@ -1094,7 +1106,7 @@ put_document (EBookBackendCouchDB *couchdb_backend, CouchdbDocument *document)
 		EContact *new_contact;
 
 		/* couchdb_document_put sets the ID for new documents, so need to send that back */
-		new_contact = contact_from_couch_document (document);
+		new_contact = contact_from_couch_document (couchdb_backend, document);
 
 		/* Add the new contact to the cache */
 		e_book_backend_cache_add_contact (couchdb_backend->cache, new_contact);
@@ -1203,6 +1215,7 @@ e_book_backend_couchdb_remove_contacts (EBookBackend *backend,
 				if (couchdb_database_delete_document (couchdb_backend->database, document, &error)) {
 					deleted_ids = g_list_append (deleted_ids, (gpointer) uid);
 					e_book_backend_cache_remove_contact (couchdb_backend->cache, uid);
+					g_hash_table_remove (couchdb_backend->revisions, uid);
 				} else {
 					if (error != NULL) {
 						g_debug ("Error deleting document: %s", error->message);
@@ -1518,10 +1531,15 @@ e_book_backend_couchdb_dispose (GObject *object)
 		couchdb_backend->cache = NULL;
 	}
 
-	if (couchdb_backend->database) {
+	if (couchdb_backend->database != NULL) {
 		g_object_unref (G_OBJECT (couchdb_backend->database));
 		couchdb_backend->database = NULL;
 	}
+
+	if (couchdb_backend->revisions != NULL) {
+		g_hash_table_destroy (couchdb_backend->revisions);
+		couchdb_backend->revisions = NULL;
+	}
 }
 
 static void
@@ -1560,4 +1578,5 @@ e_book_backend_couchdb_init (EBookBackendCouchDB *backend)
 	backend->couchdb = NULL;
 	backend->database = NULL;
 	backend->cache = NULL;
+	backend->revisions = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
 }
diff --git a/addressbook/e-book-backend-couchdb.h b/addressbook/e-book-backend-couchdb.h
index 54e4589..561ad8d 100644
--- a/addressbook/e-book-backend-couchdb.h
+++ b/addressbook/e-book-backend-couchdb.h
@@ -41,6 +41,7 @@ typedef struct {
 	CouchdbSession *couchdb;
 	CouchdbDatabase *database;
 	EBookBackendCache *cache;
+	GHashTable *revisions;
 	gboolean using_desktopcouch;
 } EBookBackendCouchDB;
 



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