[couchdb-glib] Added functions to deal with URLs in contact documents



commit c77c33364be5544bf23b90931086fc84297310a3
Author: Rodrigo Moya <rodrigo gnome-db org>
Date:   Tue Aug 25 18:06:36 2009 +0200

    Added functions to deal with URLs in contact documents

 couchdb-glib/couchdb-document-contact.c |  108 +++++++++++++++++++++++++++++++
 couchdb-glib/couchdb-document-contact.h |   11 +++
 2 files changed, 119 insertions(+), 0 deletions(-)
---
diff --git a/couchdb-glib/couchdb-document-contact.c b/couchdb-glib/couchdb-document-contact.c
index d2944b4..d0cba74 100644
--- a/couchdb-glib/couchdb-document-contact.c
+++ b/couchdb-glib/couchdb-document-contact.c
@@ -498,6 +498,59 @@ couchdb_document_contact_set_addresses (CouchDBDocument *document, GSList *list)
 	/* FIXME: crashes if we _unref json_object_unref (addresses); */
 }
 
+GSList *
+couchdb_document_contact_get_urls (CouchDBDocument *document)
+{
+	GSList *list = NULL;
+	JsonObject *urls;
+
+	g_return_val_if_fail (COUCHDB_IS_DOCUMENT (document), NULL);
+	g_return_val_if_fail (couchdb_document_is_contact (document), NULL);
+
+	urls = json_object_get_object_member (
+		json_node_get_object (document->root_node), "urls");
+	if (urls) {
+		json_object_foreach_member (urls,
+					    (JsonObjectForeach) foreach_object_cb,
+					    &list);
+	}
+
+	return list;
+}
+
+void
+couchdb_document_contact_set_urls (CouchDBDocument *document, GSList *list)
+{
+	GSList *l;
+	JsonObject *urls_json;
+
+	g_return_if_fail (COUCHDB_IS_DOCUMENT (document));
+
+	urls_json = json_object_new ();
+	for (l = list; l != NULL; l = l->next) {
+		const gchar *address_str;
+		CouchDBStructField *sf = (CouchDBStructField *) l->data;
+
+		address_str = couchdb_document_contact_url_get_address (sf);
+		if (address_str) {
+			JsonObject *this_url;
+
+			this_url = json_object_new ();
+			json_object_set_string_member (this_url, "address", address_str);
+			json_object_set_string_member (this_url, "description",
+						       couchdb_document_contact_url_get_description (sf));
+
+			json_object_set_object_member (urls_json,
+						       couchdb_struct_field_get_uuid (sf),
+						       this_url);
+			/* FIXME: crashes if we _unref json_object_unref (this_url); */
+		}
+	}
+
+	json_object_set_object_member (json_node_get_object (document->root_node), "urls", urls_json);
+	/* FIXME: crashes if we _unref json_object_unref (urls_json); */
+}
+
 const char *
 couchdb_document_contact_get_notes (CouchDBDocument *document)
 {
@@ -794,3 +847,58 @@ couchdb_document_contact_address_set_description (CouchDBStructField *sf, const
 
 	couchdb_struct_field_set_string_field (sf, "description", description);
 }
+
+CouchDBStructField *
+couchdb_document_contact_url_new (const char *uuid, const char *address, const char *description)
+{
+	CouchDBStructField *sf;
+
+	sf = couchdb_struct_field_new_from_json_object (json_object_new ());
+	if (uuid != NULL)
+		couchdb_struct_field_set_uuid (sf, uuid);
+	else {
+		char *new_uuid = generate_uuid ();
+		couchdb_struct_field_set_uuid (sf, new_uuid);
+		g_free (new_uuid);
+	}
+
+	if (address)
+		couchdb_document_contact_url_set_address (sf, address);
+	if (description)
+		couchdb_document_contact_url_set_description (sf, description);
+
+	return sf;
+}
+
+const char *
+couchdb_document_contact_url_get_address (CouchDBStructField *sf)
+{
+	g_return_val_if_fail (sf != NULL, NULL);
+
+	return couchdb_struct_field_get_string_field (sf, "address");
+}
+
+void
+couchdb_document_contact_url_set_address (CouchDBStructField *sf, const char *url)
+{
+	g_return_if_fail (sf != NULL);
+	g_return_if_fail (url != NULL);
+
+	couchdb_struct_field_set_string_field (sf, "address", url);
+}
+
+const char *
+couchdb_document_contact_url_get_description (CouchDBStructField *sf)
+{
+	g_return_val_if_fail (sf != NULL, NULL);
+
+	return couchdb_struct_field_get_string_field (sf, "description");
+}
+
+void
+couchdb_document_contact_url_set_description (CouchDBStructField *sf, const char *description)
+{
+	g_return_if_fail (sf != NULL);
+
+	couchdb_struct_field_set_string_field (sf, "description", description);
+}
diff --git a/couchdb-glib/couchdb-document-contact.h b/couchdb-glib/couchdb-document-contact.h
index 4637723..b94bc0f 100644
--- a/couchdb-glib/couchdb-document-contact.h
+++ b/couchdb-glib/couchdb-document-contact.h
@@ -70,6 +70,9 @@ void        couchdb_document_contact_set_phone_numbers (CouchDBDocument *documen
 GSList     *couchdb_document_contact_get_addresses (CouchDBDocument *document);
 void        couchdb_document_contact_set_addresses (CouchDBDocument *document, GSList *list);
 
+GSList     *couchdb_document_contact_get_urls (CouchDBDocument *document);
+void        couchdb_document_contact_set_urls (CouchDBDocument *document, GSList *list);
+
 const char *couchdb_document_contact_get_notes (CouchDBDocument *document);
 void        couchdb_document_contact_set_notes (CouchDBDocument *document, const char *notes);
 
@@ -121,4 +124,12 @@ void                couchdb_document_contact_address_set_pobox (CouchDBStructFie
 const char         *couchdb_document_contact_address_get_description (CouchDBStructField *sf);
 void                couchdb_document_contact_address_set_description (CouchDBStructField *sf, const char *description);
 
+/*
+ * Utility functions to manipulate URLs
+ */
+CouchDBStructField *couchdb_document_contact_url_new (const char *uuid, const char *address, const char *description);
+const char         *couchdb_document_contact_url_get_address  (CouchDBStructField *sf);
+void                couchdb_document_contact_url_set_address (CouchDBStructField *sf, const char *address);
+const char         *couchdb_document_contact_url_get_description (CouchDBStructField *sf);
+void                couchdb_document_contact_url_set_description (CouchDBStructField *sf, const char *description);
 #endif



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