[couchdb-glib] Added basic DocumentContact 'object'



commit 248b0a66da1704791060df3b79aff5f6e75c098b
Author: Rodrigo Moya <rodrigo gnome-db org>
Date:   Fri Jun 12 18:08:26 2009 +0200

    Added basic DocumentContact 'object'

 couchdb-glib/Makefile.am                |    2 +
 couchdb-glib/couchdb-document-contact.c |  112 +++++++++++++++++++++++++++++++
 couchdb-glib/couchdb-document-contact.h |   34 +++++++++
 couchdb-glib/couchdb-document.c         |   22 ++++---
 couchdb-glib/couchdb-glib.h             |    7 ++-
 couchdb-glib/utils.h                    |    8 ++
 6 files changed, 174 insertions(+), 11 deletions(-)
---
diff --git a/couchdb-glib/Makefile.am b/couchdb-glib/Makefile.am
index 6f12519..efd5262 100644
--- a/couchdb-glib/Makefile.am
+++ b/couchdb-glib/Makefile.am
@@ -6,6 +6,7 @@ lib_LTLIBRARIES = libcouchdb-glib-1.0.la
 libcouchdb_glib_1_0_la_SOURCES =	\
 	couchdb.c			\
 	couchdb-document.c		\
+	couchdb-document-contact.c	\
 	couchdb-types.c			\
 	utils.c				\
 	utils.h
@@ -13,6 +14,7 @@ libcouchdb_glib_1_0_la_SOURCES =	\
 hdir = $(includedir)/couchdb-glib-1.0
 h_DATA = 				\
 	couchdb-glib.h			\
+	couchdb-document-contact.h	\
 	couchdb-types.h
 
 EXTRA_DIST = $(h_DATA)
diff --git a/couchdb-glib/couchdb-document-contact.c b/couchdb-glib/couchdb-document-contact.c
new file mode 100644
index 0000000..98335fa
--- /dev/null
+++ b/couchdb-glib/couchdb-document-contact.c
@@ -0,0 +1,112 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2009 Canonical Services Ltd (www.canonical.com)
+ *
+ * Authors: Rodrigo Moya <rodrigo moya canonical com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU Lesser General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include "couchdb-document-contact.h"
+#include "utils.h"
+
+static void
+set_simple_string_value_field (CouchDBDocument *document, const char *field, const char *value)
+{
+	JsonObject *object;
+
+	object = json_object_get_object_member (json_node_get_object (document->root_node), field);
+	if (object)
+		json_object_set_string_member (object, "v", value);
+	else {
+		object = json_object_new ();
+		json_object_set_string_member (object, "v", value);
+		json_object_set_object_member (json_node_get_object (document->root_node), field, object);
+
+		json_object_unref (object);
+	}
+}
+
+const char *
+couchdb_document_contact_get_first_name (CouchDBDocument *document)
+{
+	JsonObject *object;
+
+	g_return_val_if_fail (COUCHDB_IS_DOCUMENT (document), NULL);
+	g_return_val_if_fail (couchdb_document_is_contact (document), NULL);
+
+	object = json_object_get_object_member (json_node_get_object (document->root_node), "first_name");
+	if (object)
+		return json_object_get_string_member (object, "v");
+
+	return NULL;
+}
+
+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 (first_name != NULL);
+
+	set_simple_string_value_field (document, "first_name", first_name);
+}
+
+const char *
+couchdb_document_contact_get_last_name (CouchDBDocument *document)
+{
+	JsonObject *object;
+
+	g_return_val_if_fail (COUCHDB_IS_DOCUMENT (document), NULL);
+	g_return_val_if_fail (couchdb_document_is_contact (document), NULL);
+
+	object = json_object_get_object_member (json_node_get_object (document->root_node), "last_name");
+	if (object)
+		return json_object_get_string_member (object, "v");
+
+	return NULL;
+}
+
+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 (last_name != NULL);
+
+	set_simple_string_value_field (document, "last_name", last_name);
+}
+
+const char *
+couchdb_document_contact_get_birth_date (CouchDBDocument *document)
+{
+	JsonObject *object;
+
+	g_return_val_if_fail (COUCHDB_IS_DOCUMENT (document), NULL);
+	g_return_val_if_fail (couchdb_document_is_contact (document), NULL);
+
+	object = json_object_get_object_member (json_node_get_object (document->root_node), "birth_date");
+	if (object)
+		return json_object_get_string_member (object, "v");
+
+	return NULL;
+}
+
+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 (birth_date != NULL);
+
+	set_simple_string_value_field (document, "birth_date", birth_date);
+}
diff --git a/couchdb-glib/couchdb-document-contact.h b/couchdb-glib/couchdb-document-contact.h
new file mode 100644
index 0000000..17ef54c
--- /dev/null
+++ b/couchdb-glib/couchdb-document-contact.h
@@ -0,0 +1,34 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2009 Canonical Services Ltd (www.canonical.com)
+ *
+ * Authors: Rodrigo Moya <rodrigo moya canonical com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU Lesser General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifndef __COUCHDB_DOCUMENT_CONTACT_H__
+#define __COUCHDB_DOCUMENT_CONTACT_H__
+
+#include <couchdb-glib.h>
+
+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);
+void        couchdb_document_contact_set_last_name (CouchDBDocument *document, const char *last_name);
+const char *couchdb_document_contact_get_birth_date (CouchDBDocument *document);
+void        couchdb_document_contact_set_birth_date (CouchDBDocument *document, const char *birth_date);
+
+#endif
diff --git a/couchdb-glib/couchdb-document.c b/couchdb-glib/couchdb-document.c
index 26c8ac2..5b83ef0 100644
--- a/couchdb-glib/couchdb-document.c
+++ b/couchdb-glib/couchdb-document.c
@@ -27,14 +27,6 @@
 #include "couchdb-glib.h"
 #include "utils.h"
 
-struct _CouchDBDocument {
-	GObject parent;
-
-	CouchDB *couchdb;
-	char *dbname;
-	JsonNode *root_node;
-};
-
 G_DEFINE_TYPE(CouchDBDocument, couchdb_document, G_TYPE_OBJECT);
 
 static void
@@ -59,6 +51,9 @@ couchdb_document_class_init (CouchDBDocumentClass *klass)
 static void
 couchdb_document_init (CouchDBDocument *document)
 {
+	document->couchdb = NULL;
+	document->root_node = NULL;
+	document->dbname = NULL;
 }
 
 CouchDBDocument *
@@ -68,7 +63,6 @@ couchdb_document_new (CouchDB *couchdb)
 
 	document = g_object_new (COUCHDB_TYPE_DOCUMENT, NULL);
 	document->couchdb = couchdb;
-	document->dbname = NULL;
 	document->root_node = json_node_new (JSON_NODE_OBJECT);
 
 	return document;
@@ -146,6 +140,16 @@ couchdb_document_get_revision (CouchDBDocument *document)
 }
 
 gboolean
+couchdb_document_is_contact (CouchDBDocument *document)
+{
+	g_return_val_if_fail (COUCHDB_IS_DOCUMENT (document), FALSE);
+
+	return !g_ascii_strcasecmp (json_object_get_string_member (json_node_get_object (document->root_node),
+								   "u1_record_type"),
+				    "http://api.ubuntuone.com/schema/contact/0.1";);
+}
+
+gboolean
 couchdb_document_has_field (CouchDBDocument *document, const char *field)
 {
 	JsonNode *node;
diff --git a/couchdb-glib/couchdb-glib.h b/couchdb-glib/couchdb-glib.h
index 2da9195..2cdf590 100644
--- a/couchdb-glib/couchdb-glib.h
+++ b/couchdb-glib/couchdb-glib.h
@@ -67,9 +67,10 @@ typedef struct {
 	GObjectClass parent_class;
 } CouchDBDocumentClass;
 
-GSList         *couchdb_list_documents (CouchDB *couchdb, const char *dbname, GError **error);
-void            couchdb_free_document_list (GSList *doclist);
+GSList          *couchdb_list_documents (CouchDB *couchdb, const char *dbname, GError **error);
+void             couchdb_free_document_list (GSList *doclist);
 
+GType            couchdb_document_get_type (void); 
 CouchDBDocument *couchdb_document_new (CouchDB *couchdb);
 CouchDBDocument *couchdb_document_get (CouchDB *couchdb,
 				       const char *dbname,
@@ -80,6 +81,8 @@ void             couchdb_document_set_id (CouchDBDocument *document, const char
 
 const char      *couchdb_document_get_revision (CouchDBDocument *document);
 
+gboolean         couchdb_document_is_contact (CouchDBDocument *document);
+
 gboolean         couchdb_document_has_field (CouchDBDocument *document, const char *field);
 void             couchdb_document_remove_field (CouchDBDocument *document, const char *field);
 
diff --git a/couchdb-glib/utils.h b/couchdb-glib/utils.h
index 1ea6db8..82c49b2 100644
--- a/couchdb-glib/utils.h
+++ b/couchdb-glib/utils.h
@@ -32,6 +32,14 @@ struct _CouchDB {
 	SoupSession *http_session;
 };
 
+struct _CouchDBDocument {
+	GObject parent;
+
+	CouchDB *couchdb;
+	char *dbname;
+	JsonNode *root_node;
+};
+
 struct _CouchDBDatabaseInfo {
 	gint ref_count;
 



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