[couchdb-glib] Add functions to convert struct fields to and from strings



commit cb6340d14396a16ec486bcda616f167ec851404c
Author: Rodrigo Moya <rodrigo gnome-db org>
Date:   Wed Aug 19 18:43:24 2009 +0200

    Add functions to convert struct fields to and from strings
    Add contact records creation convenience function
    Add support for application_annotations field

 couchdb-glib/couchdb-document-contact.c |   23 ++++++++++++++
 couchdb-glib/couchdb-document-contact.h |    5 +++
 couchdb-glib/couchdb-document.c         |   44 ++++++++++++++++++++++++----
 couchdb-glib/couchdb-glib.h             |   10 ++++--
 couchdb-glib/couchdb-types.c            |   49 +++++++++++++++++++++++++++++++
 couchdb-glib/couchdb-types.h            |    3 ++
 6 files changed, 124 insertions(+), 10 deletions(-)
---
diff --git a/couchdb-glib/couchdb-document-contact.c b/couchdb-glib/couchdb-document-contact.c
index e0c1a54..31e2c5d 100644
--- a/couchdb-glib/couchdb-document-contact.c
+++ b/couchdb-glib/couchdb-document-contact.c
@@ -22,6 +22,29 @@
 #include "couchdb-document-contact.h"
 #include "utils.h"
 
+CouchDBDocument *
+couchdb_document_contact_new (CouchDB *couchdb)
+{
+	CouchDBDocument *document;
+
+	g_return_val_if_fail (COUCHDB_IS (couchdb), NULL);
+
+	document = couchdb_document_new (couchdb);
+	if (document)
+		couchdb_document_set_record_type (document, COUCHDB_RECORD_TYPE_CONTACT);
+
+	return document;
+}
+
+gboolean
+couchdb_document_is_contact (CouchDBDocument *document)
+{
+	g_return_val_if_fail (COUCHDB_IS_DOCUMENT (document), FALSE);
+
+	return !g_ascii_strcasecmp (couchdb_document_get_record_type (document),
+				    COUCHDB_RECORD_TYPE_CONTACT);
+}
+
 const char *
 couchdb_document_contact_get_first_name (CouchDBDocument *document)
 {
diff --git a/couchdb-glib/couchdb-document-contact.h b/couchdb-glib/couchdb-document-contact.h
index 2fd786a..06cccfd 100644
--- a/couchdb-glib/couchdb-document-contact.h
+++ b/couchdb-glib/couchdb-document-contact.h
@@ -24,6 +24,11 @@
 
 #include <couchdb-glib.h>
 
+#define COUCHDB_RECORD_TYPE_CONTACT "http://www.freedesktop.org/wiki/Specifications/desktopcouch/contact";
+
+CouchDBDocument *couchdb_document_contact_new (CouchDB *couchdb);
+gboolean         couchdb_document_is_contact (CouchDBDocument *document);
+
 /*
  * Top level functions to manipulate documents representing a contact
  */
diff --git a/couchdb-glib/couchdb-document.c b/couchdb-glib/couchdb-document.c
index 50d4d5f..243a019 100644
--- a/couchdb-glib/couchdb-document.c
+++ b/couchdb-glib/couchdb-document.c
@@ -227,14 +227,30 @@ couchdb_document_set_revision (CouchDBDocument *document, const char *revision)
 				       revision);
 }
 
-gboolean
-couchdb_document_is_contact (CouchDBDocument *document)
+const char *
+couchdb_document_get_record_type (CouchDBDocument *document)
 {
-	g_return_val_if_fail (COUCHDB_IS_DOCUMENT (document), FALSE);
+	g_return_val_if_fail (COUCHDB_IS_DOCUMENT (document), NULL);
+
+	if (document->root_node &&
+	    json_node_get_node_type (document->root_node) == JSON_NODE_OBJECT) {
+		return json_object_get_string_member (
+			json_node_get_object (document->root_node),
+			"record_type");
+	}
+
+	return NULL;
+}
+
+void
+couchdb_document_set_record_type (CouchDBDocument *document, const char *record_type)
+{
+	g_return_if_fail (COUCHDB_IS_DOCUMENT (document));
+	g_return_if_fail (record_type != NULL);
 
-	return !g_ascii_strcasecmp (json_object_get_string_member (json_node_get_object (document->root_node),
-								   "record_type"),
-				    "http://www.freedesktop.org/wiki/Specifications/desktopcouch/contact";);
+	json_object_set_string_member (json_node_get_object (document->root_node),
+				       "record_type",
+				       record_type);
 }
 
 gboolean
@@ -369,6 +385,22 @@ couchdb_document_set_struct_field (CouchDBDocument *document, const char *field,
 				       json_object_ref (value->json_object));
 }
 
+CouchDBStructField *
+couchdb_document_get_application_annotations (CouchDBDocument *document)
+{
+	g_return_val_if_fail (COUCHDB_IS_DOCUMENT (document), NULL);
+
+	return couchdb_document_get_struct_field (document, "application_annotations");
+}
+
+void
+couchdb_document_set_application_annotations (CouchDBDocument *document, CouchDBStructField *annotations)
+{
+	g_return_if_fail (COUCHDB_IS_DOCUMENT (document));
+
+	couchdb_document_set_struct_field (document, "application_annotations", annotations);
+}
+
 char *
 couchdb_document_to_string (CouchDBDocument *document)
 {
diff --git a/couchdb-glib/couchdb-glib.h b/couchdb-glib/couchdb-glib.h
index d8d270d..bc8dc6a 100644
--- a/couchdb-glib/couchdb-glib.h
+++ b/couchdb-glib/couchdb-glib.h
@@ -85,11 +85,10 @@ gboolean         couchdb_document_delete (CouchDBDocument *document, GError **er
 
 const char      *couchdb_document_get_id (CouchDBDocument *document);
 void             couchdb_document_set_id (CouchDBDocument *document, const char *id);
-
 const char      *couchdb_document_get_revision (CouchDBDocument *document);
 void             couchdb_document_set_revision (CouchDBDocument *document, const char *revision);
-
-gboolean         couchdb_document_is_contact (CouchDBDocument *document);
+const char      *couchdb_document_get_record_type (CouchDBDocument *document);
+void             couchdb_document_set_record_type (CouchDBDocument *document, const char *record_type);
 
 gboolean         couchdb_document_has_field (CouchDBDocument *document, const char *field);
 void             couchdb_document_remove_field (CouchDBDocument *document, const char *field);
@@ -107,7 +106,10 @@ void             couchdb_document_set_struct_field (CouchDBDocument *document,
 						    const char *field,
 						    CouchDBStructField *value);
 
-char            *couchdb_document_to_string (CouchDBDocument *document);
+CouchDBStructField *couchdb_document_get_application_annotations (CouchDBDocument *document);
+void             couchdb_document_set_application_annotations (CouchDBDocument *document, CouchDBStructField *annotations);
+      
+char *couchdb_document_to_string (CouchDBDocument *document);
 
 
 #endif
diff --git a/couchdb-glib/couchdb-types.c b/couchdb-glib/couchdb-types.c
index 8ddc8aa..f332cf6 100644
--- a/couchdb-glib/couchdb-types.c
+++ b/couchdb-glib/couchdb-types.c
@@ -242,6 +242,31 @@ couchdb_struct_field_new (void)
 }
 
 CouchDBStructField *
+couchdb_struct_field_new_from_string (const char *str)
+{
+	JsonParser *parser;
+	GError *error = NULL;
+	CouchDBStructField *sf = NULL;
+
+	g_return_val_if_fail (str != NULL, NULL);
+
+	parser = json_parser_new ();
+	if (json_parser_load_from_data (parser, str, strlen (str), &error)) {
+		JsonNode *node = json_parser_get_root (parser);
+
+		if (json_node_get_node_type (node) == JSON_NODE_OBJECT)
+			sf = couchdb_struct_field_new_from_json_object (json_node_get_object (node));
+	} else {
+		g_warning ("Could not parse string: %s", error->message);
+		g_error_free (error);
+	}
+
+	g_object_unref (G_OBJECT (parser));
+
+	return sf;
+}
+
+CouchDBStructField *
 couchdb_struct_field_new_from_json_object (JsonObject *json_object)
 {
 	CouchDBStructField *sf;
@@ -415,3 +440,27 @@ couchdb_struct_field_set_uuid (CouchDBStructField *sf, const char *uuid)
 
 	sf->uuid = g_strdup (uuid);
 }
+
+char *
+couchdb_struct_field_to_string (CouchDBStructField *sf)
+{
+	JsonNode *node;
+	JsonGenerator *generator;
+	gsize size;
+	char *str = NULL;
+
+	g_return_val_if_fail (sf != NULL, NULL);
+
+	node = json_node_new (JSON_NODE_OBJECT);
+	json_node_set_object (node, sf->json_object);
+
+	generator = json_generator_new ();
+	json_generator_set_root (generator, node);
+
+	str = json_generator_to_data (generator, &size);
+	g_object_unref (G_OBJECT (generator));
+
+	json_node_free (node);
+
+	return str;
+}
diff --git a/couchdb-glib/couchdb-types.h b/couchdb-glib/couchdb-types.h
index 3daf537..fafb9e0 100644
--- a/couchdb-glib/couchdb-types.h
+++ b/couchdb-glib/couchdb-types.h
@@ -63,6 +63,7 @@ typedef struct _CouchDBStructField CouchDBStructField;
 
 GType               couchdb_struct_field_get_type (void);
 CouchDBStructField *couchdb_struct_field_new (void);
+CouchDBStructField *couchdb_struct_field_new_from_string (const char *str);
 CouchDBStructField *couchdb_struct_field_ref (CouchDBStructField *sf);
 void                couchdb_struct_field_unref (CouchDBStructField *sf);
 
@@ -83,4 +84,6 @@ void                couchdb_struct_field_set_struct_field (CouchDBStructField *s
 const char         *couchdb_struct_field_get_uuid (CouchDBStructField *sf);
 void                couchdb_struct_field_set_uuid (CouchDBStructField *sf, const char *uuid);
 
+char               *couchdb_struct_field_to_string (CouchDBStructField *sf);
+
 #endif



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