[evolution-couchdb] Add deleted flag to documents instead of removing them from the CouchDB database (only for desktopco



commit 37cf6610493ed388b5352a7d3ec16d7f0d979fc4
Author: Rodrigo Moya <rodrigo gnome-db org>
Date:   Thu Nov 12 16:14:18 2009 +0100

    Add deleted flag to documents instead of removing them from the CouchDB database (only for desktopcouch)

 addressbook/e-book-backend-couchdb.c |   85 ++++++++++++++++++++++++++++++----
 addressbook/e-book-backend-couchdb.h |    1 +
 2 files changed, 77 insertions(+), 9 deletions(-)
---
diff --git a/addressbook/e-book-backend-couchdb.c b/addressbook/e-book-backend-couchdb.c
index 17d49f7..6c957d8 100644
--- a/addressbook/e-book-backend-couchdb.c
+++ b/addressbook/e-book-backend-couchdb.c
@@ -40,10 +40,31 @@ vcard_from_couch_document (CouchDBDocument *document)
 	char *str;
 	GSList *list, *sl;
 	GList *attr_list;
+	CouchDBStructField *app_annotations;
 
 	if (!couchdb_document_is_contact (document))
 		return NULL;
 
+	/* Check if the contact is marked for deletion */
+	if ((app_annotations = couchdb_document_get_application_annotations (document))) {
+		CouchDBStructField *u1_annotations;
+
+		u1_annotations = couchdb_struct_field_get_struct_field (
+			app_annotations, "Ubuntu One");
+		if (u1_annotations != NULL) {
+			CouchDBStructField *private_annotations;
+
+			private_annotations = couchdb_struct_field_get_struct_field (
+				u1_annotations, "private_application_annotations");
+			if (private_annotations != NULL) {
+				if (couchdb_struct_field_has_field (private_annotations, "deleted")
+				    && couchdb_struct_field_get_boolean_field (private_annotations, "deleted"))
+					return NULL;
+			}
+		}
+	}
+
+	/* 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),
@@ -879,6 +900,7 @@ e_book_backend_couchdb_load_source (EBookBackend *backend,
 
 	/* create CouchDB main object */
 	couchdb_backend->dbname = g_strdup ("contacts");
+	couchdb_backend->using_desktopcouch = FALSE;
 
 	property = e_source_get_property (source, "couchdb_instance");
 	if (g_strcmp0 (property, "user") == 0) {
@@ -953,6 +975,8 @@ e_book_backend_couchdb_load_source (EBookBackend *backend,
 			return GNOME_Evolution_Addressbook_NoSuchBook;
 		}
 
+		couchdb_backend->using_desktopcouch = TRUE;
+
 	} else if (g_strcmp0 (property, "remote") == 0)
 		uri = g_strdup_printf ("http://%s";, e_source_get_property (source, "couchdb_remote_server"));
 	else
@@ -1113,14 +1137,53 @@ e_book_backend_couchdb_remove_contacts (EBookBackend *backend,
 
 		document = couchdb_document_get (couchdb_backend->couchdb, couchdb_backend->dbname, uid, &error);
 		if (document) {
-			if (couchdb_document_delete (document, &error))
-				deleted_ids = g_list_append (deleted_ids, (gpointer) uid);
-			else {
-				if (error != NULL) {
-					g_debug ("Error deleting document: %s", error->message);
-					g_error_free (error);
-				} else
-					g_debug ("Error deleting document");
+			if (couchdb_backend->using_desktopcouch) {
+				CouchDBStructField *app_annotations, *u1_annotations, *private_annotations;
+
+				/* For desktopcouch, we don't remove contacts, we just
+				 * mark them as deleted */
+				app_annotations = couchdb_document_get_application_annotations (document);
+				if (app_annotations == NULL)
+					app_annotations = couchdb_struct_field_new ();
+
+				u1_annotations = couchdb_struct_field_get_struct_field (app_annotations, "Ubuntu One");
+				if (u1_annotations == NULL)
+					u1_annotations = couchdb_struct_field_new ();
+
+				private_annotations = couchdb_struct_field_get_struct_field (u1_annotations, "private_application_annotations");
+				if (private_annotations == NULL)
+					private_annotations = couchdb_struct_field_new ();
+
+				couchdb_struct_field_set_boolean_field (private_annotations, "deleted", TRUE);
+				couchdb_struct_field_set_struct_field (u1_annotations, "private_application_annotations", private_annotations);
+				couchdb_struct_field_set_struct_field (app_annotations, "Ubuntu One", u1_annotations);
+				couchdb_document_set_application_annotations (document, app_annotations);
+
+				/* Now put the new revision of the document */
+				if (couchdb_document_put (document, couchdb_backend->dbname, &error))
+					deleted_ids = g_list_append (deleted_ids, (gpointer) uid);
+				else {
+					if (error != NULL) {
+						g_debug ("Error deleting document: %s", error->message);
+						g_error_free (error);
+					} else
+						g_debug ("Error deleting document");
+				}
+
+				/* Free memory */
+				couchdb_struct_field_unref (app_annotations);
+				couchdb_struct_field_unref (u1_annotations);
+				couchdb_struct_field_unref (private_annotations);
+			} else {
+				if (couchdb_document_delete (document, &error))
+					deleted_ids = g_list_append (deleted_ids, (gpointer) uid);
+				else {
+					if (error != NULL) {
+						g_debug ("Error deleting document: %s", error->message);
+						g_error_free (error);
+					} else
+						g_debug ("Error deleting document");
+				}
 			}
 		} else {
 			if (error != NULL) {
@@ -1221,6 +1284,7 @@ e_book_backend_couchdb_get_contact_list (EBookBackend *backend,
 					   couchdb_backend->dbname,
 					   &error);
 	for (sl = doc_list; sl != NULL; sl = sl->next) {
+		char *vcard;
 		CouchDBDocument *document;
 		CouchDBDocumentInfo *doc_info = (CouchDBDocumentInfo *) sl->data;
 
@@ -1233,7 +1297,10 @@ e_book_backend_couchdb_get_contact_list (EBookBackend *backend,
 		if (!document)
 			continue;
 
-		contacts = g_list_prepend (contacts, vcard_from_couch_document (document));
+		vcard = vcard_from_couch_document (document);
+		if (vcard != NULL)
+			contacts = g_list_prepend (contacts, vcard);
+
 		g_object_unref (G_OBJECT (document));
 	}
 
diff --git a/addressbook/e-book-backend-couchdb.h b/addressbook/e-book-backend-couchdb.h
index 2dee6f6..2a56875 100644
--- a/addressbook/e-book-backend-couchdb.h
+++ b/addressbook/e-book-backend-couchdb.h
@@ -39,6 +39,7 @@ typedef struct {
 
 	CouchDB *couchdb;
 	char *dbname;
+	gboolean using_desktopcouch;
 } EBookBackendCouchDB;
 
 typedef struct {



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