[couchdb-glib] Added code to manage email addresses in contact documents



commit 6b412adfda1f3daeeadef01444657f9abf58909a
Author: Rodrigo Moya <rodrigo gnome-db org>
Date:   Fri Jun 12 21:21:05 2009 +0200

    Added code to manage email addresses in contact documents

 couchdb-glib/couchdb-document-contact.c |  129 +++++++++++++++++++++++++++++++
 couchdb-glib/couchdb-document-contact.h |   16 ++++
 couchdb-glib/couchdb-document.c         |    4 +-
 3 files changed, 147 insertions(+), 2 deletions(-)
---
diff --git a/couchdb-glib/couchdb-document-contact.c b/couchdb-glib/couchdb-document-contact.c
index 98335fa..48539d6 100644
--- a/couchdb-glib/couchdb-document-contact.c
+++ b/couchdb-glib/couchdb-document-contact.c
@@ -58,6 +58,7 @@ void
 couchdb_document_contact_set_first_name (CouchDBDocument *document, const char *first_name)
 {
 	g_return_if_fail (COUCHDB_IS_DOCUMENT (document));
+	g_return_if_fail (couchdb_document_is_contact (document));
 	g_return_if_fail (first_name != NULL);
 
 	set_simple_string_value_field (document, "first_name", first_name);
@@ -82,6 +83,7 @@ void
 couchdb_document_contact_set_last_name (CouchDBDocument *document, const char *last_name)
 {
       	g_return_if_fail (COUCHDB_IS_DOCUMENT (document));
+	g_return_if_fail (couchdb_document_is_contact (document));
 	g_return_if_fail (last_name != NULL);
 
 	set_simple_string_value_field (document, "last_name", last_name);
@@ -106,7 +108,134 @@ void
 couchdb_document_contact_set_birth_date (CouchDBDocument *document, const char *birth_date)
 {
 	g_return_if_fail (COUCHDB_IS_DOCUMENT (document));
+	g_return_if_fail (couchdb_document_is_contact (document));
 	g_return_if_fail (birth_date != NULL);
 
 	set_simple_string_value_field (document, "birth_date", birth_date);
 }
+
+static void
+foreach_email_cb (JsonObject *object,
+		  const char *member_name,
+		  JsonNode *member_node,
+		  gpointer user_data)
+{
+	GSList **list = (GSList **) user_data;
+
+	if (json_node_get_node_type (member_node) == JSON_NODE_OBJECT) {
+		CouchDBStructField *sf;
+
+		sf = couchdb_struct_field_new_from_json_object (json_object_ref (object));
+		*list = g_slist_prepend (*list, sf);
+	}
+}
+
+/**
+ * couchdb_document_contact_get_email_addresses:
+ * @document: A #CouchDBDocument object representing a contact
+ *
+ * Retrieve a list of email addresses from the specified contact document.
+ * Email addresses are returned in a GSList of #CouchDBStructField objects,
+ * which can be manipulated with the couchdb_document_contact_email_* functions
+ * and freed with:
+ *     g_slist_foreach (list, (GFunc) couchdb_struct_field_unref, NULL);
+ *     g_slist_free (list);
+ *
+ * Return value: a #GSList of #CouchDBStructField objects.
+ */
+GSList *
+couchdb_document_contact_get_email_addresses (CouchDBDocument *document)
+{
+	GSList *list = NULL;
+	JsonObject *addresses_json;
+
+	g_return_val_if_fail (COUCHDB_IS_DOCUMENT (document), NULL);
+	g_return_val_if_fail (couchdb_document_is_contact (document), NULL);
+
+	/* first, retrieve the email_addresses object */
+	addresses_json = json_object_get_object_member (json_node_get_object (document->root_node),
+							"email_addresses");
+	if (addresses_json) {
+		JsonObject *json_value;
+
+		json_value = json_object_get_object_member (addresses_json, "v");
+		if (json_value) {
+			json_object_foreach_member (json_value,
+						    (JsonObjectForeach) foreach_email_cb,
+						    &list);
+		}
+	}
+
+	return list;
+}
+
+void
+couchdb_document_contact_set_email_addresses (CouchDBDocument *document, GSList *list)
+{
+}
+
+const char *
+couchdb_document_contact_email_get_address (CouchDBStructField *sf)
+{
+	JsonObject *top_level, *parent;
+
+	g_return_val_if_fail (sf != NULL, NULL);
+
+	top_level = json_object_get_object_member (sf->json_object, "v");
+	if (top_level) {
+		parent = json_object_get_object_member (top_level, "address");
+		if (parent)
+			return json_object_get_string_member (parent, "v");
+	}
+
+	return NULL;
+}
+
+void
+couchdb_document_contact_email_set_address (CouchDBStructField *sf, const char *email)
+{
+	JsonObject *top_level, *parent;
+
+	g_return_if_fail (sf != NULL);
+	g_return_if_fail (email != NULL);
+
+	top_level = json_object_get_object_member (sf->json_object, "v");
+	if (top_level) {
+		parent = json_object_get_object_member (top_level, "address");
+		if (parent)
+			json_object_set_string_member (parent, "v", email);
+	}
+}
+
+const char *
+couchdb_document_contact_email_get_description (CouchDBStructField *sf)
+{
+	JsonObject *top_level, *parent;
+
+	g_return_val_if_fail (sf != NULL, NULL);
+
+	top_level = json_object_get_object_member (sf->json_object, "v");
+	if (top_level) {
+		parent = json_object_get_object_member (top_level, "description");
+		if (parent)
+			return json_object_get_string_member (parent, "v");
+	}
+
+	return NULL;
+}
+
+void
+couchdb_document_contact_email_set_description (CouchDBStructField *sf, const char *description)
+{
+	JsonObject *top_level, *parent;
+
+	g_return_if_fail (sf != NULL);
+	g_return_if_fail (description != NULL);
+
+	top_level = json_object_get_object_member (sf->json_object, "v");
+	if (top_level) {
+		parent = json_object_get_object_member (top_level, "description");
+		if (parent)
+			json_object_set_string_member (parent, "v", description);
+	}
+}
diff --git a/couchdb-glib/couchdb-document-contact.h b/couchdb-glib/couchdb-document-contact.h
index 17ef54c..0b34dcc 100644
--- a/couchdb-glib/couchdb-document-contact.h
+++ b/couchdb-glib/couchdb-document-contact.h
@@ -24,6 +24,10 @@
 
 #include <couchdb-glib.h>
 
+/*
+ * Top level functions to manipulate documents representing a contact
+ */
+
 const char *couchdb_document_contact_get_first_name (CouchDBDocument *document);
 void        couchdb_document_contact_set_first_name (CouchDBDocument *document, const char *first_name);
 const char *couchdb_document_contact_get_last_name (CouchDBDocument *document);
@@ -31,4 +35,16 @@ void        couchdb_document_contact_set_last_name (CouchDBDocument *document, c
 const char *couchdb_document_contact_get_birth_date (CouchDBDocument *document);
 void        couchdb_document_contact_set_birth_date (CouchDBDocument *document, const char *birth_date);
 
+GSList     *couchdb_document_contact_get_email_addresses (CouchDBDocument *document);
+void        couchdb_document_contact_set_email_addresses (CouchDBDocument *document, GSList *list);
+
+/*
+ * Utility functions to manipulate email addresses fields
+ */
+
+const char *couchdb_document_contact_email_get_address (CouchDBStructField *sf);
+void        couchdb_document_contact_email_set_address (CouchDBStructField *sf, const char *email);
+const char *couchdb_document_contact_email_get_description (CouchDBStructField *sf);
+void        couchdb_document_contact_email_set_description (CouchDBStructField *sf, const char *description);
+
 #endif
diff --git a/couchdb-glib/couchdb-document.c b/couchdb-glib/couchdb-document.c
index 5b83ef0..8884f18 100644
--- a/couchdb-glib/couchdb-document.c
+++ b/couchdb-glib/couchdb-document.c
@@ -260,8 +260,8 @@ couchdb_document_get_struct_field (CouchDBDocument *document, const char *field)
 	g_return_val_if_fail (field != NULL, NULL);
 
 	return couchdb_struct_field_new_from_json_object (
-		json_object_get_object_member (json_node_get_object (document->root_node),
-					       field));
+		json_object_ref (json_object_get_object_member (json_node_get_object (document->root_node),
+								field)));
 }
 
 void



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