[couchdb-glib: 5/6] Rename hostname to uri and GObjectify constructor



commit 7a0e84e048e3aa7fefb96c572b26ec56fcb15335
Author: Johan Dahlin <johan gnome org>
Date:   Wed Dec 9 09:28:18 2009 -0200

    Rename hostname to uri and GObjectify constructor

 couchdb-glib/couchdb-document.c |    8 ++--
 couchdb-glib/couchdb.c          |  101 +++++++++++++++++++++++++++++++--------
 couchdb-glib/couchdb.h          |    4 +-
 couchdb-glib/dbwatch.c          |    4 +-
 couchdb-glib/oauth.c            |    2 +-
 5 files changed, 89 insertions(+), 30 deletions(-)
---
diff --git a/couchdb-glib/couchdb-document.c b/couchdb-glib/couchdb-document.c
index 9f22923..9068f01 100644
--- a/couchdb-glib/couchdb-document.c
+++ b/couchdb-glib/couchdb-document.c
@@ -95,7 +95,7 @@ couchdb_document_get (Couchdb *couchdb,
 	g_return_val_if_fail (docid != NULL, NULL);
 
 	encoded_docid = soup_uri_encode (docid, NULL);
-	url = g_strdup_printf ("%s/%s/%s", couchdb_get_hostname (couchdb), dbname, encoded_docid);
+	url = g_strdup_printf ("%s/%s/%s", couchdb_get_uri (couchdb), dbname, encoded_docid);
 	parser = json_parser_new ();
 	if (couchdb_send_message (couchdb, SOUP_METHOD_GET, url, NULL, parser, error)) {
 		document = g_object_new (COUCHDB_TYPE_DOCUMENT, NULL);
@@ -132,12 +132,12 @@ couchdb_document_put (CouchdbDocument *document,
 		char *encoded_docid;
 
 		encoded_docid = soup_uri_encode (id, NULL);
-		url = g_strdup_printf ("%s/%s/%s", couchdb_get_hostname (document->couchdb), dbname, encoded_docid);		
+		url = g_strdup_printf ("%s/%s/%s", couchdb_get_uri (document->couchdb), dbname, encoded_docid);		
 		send_ok = couchdb_send_message (document->couchdb, SOUP_METHOD_PUT, url, body, parser, error);
 
 		g_free (encoded_docid);
 	} else {
-		url = g_strdup_printf ("%s/%s/", couchdb_get_hostname (document->couchdb), dbname);
+		url = g_strdup_printf ("%s/%s/", couchdb_get_uri (document->couchdb), dbname);
 		send_ok = couchdb_send_message (document->couchdb, SOUP_METHOD_POST, url, body, parser, error);
 	}
 
@@ -184,7 +184,7 @@ couchdb_document_delete (CouchdbDocument *document, GError **error)
 	if (!id || !revision) /* we can't remove a document without an ID and/or a REVISION */
 		return FALSE;
 
-	url = g_strdup_printf ("%s/%s/%s?rev=%s", couchdb_get_hostname (document->couchdb), document->dbname, id, revision);
+	url = g_strdup_printf ("%s/%s/%s?rev=%s", couchdb_get_uri (document->couchdb), document->dbname, id, revision);
 	
 	/* We don't parse the http response, therefore the parser arg is NULL */
 	if (couchdb_send_message (document->couchdb, SOUP_METHOD_DELETE, url, NULL, NULL, error)) {
diff --git a/couchdb-glib/couchdb.c b/couchdb-glib/couchdb.c
index d2131ec..15ae043 100644
--- a/couchdb-glib/couchdb.c
+++ b/couchdb-glib/couchdb.c
@@ -38,7 +38,7 @@
 struct _Couchdb {
 	GObject parent;
 
-	char *hostname;
+	char *uri;
 	SoupSession *http_session;
 
 	GHashTable *db_watchlist;
@@ -62,6 +62,11 @@ enum {
 };
 static guint couchdb_signals[LAST_SIGNAL];
 
+enum {
+    PROP_0,
+    PROP_URI
+};
+
 static void
 couchdb_finalize (GObject *object)
 {
@@ -69,7 +74,7 @@ couchdb_finalize (GObject *object)
 
 	g_hash_table_destroy (couchdb->db_watchlist);
 
-	g_free (couchdb->hostname);
+	g_free (couchdb->uri);
 	g_object_unref (couchdb->http_session);
 
 	if (couchdb->oauth_consumer_key)
@@ -85,11 +90,62 @@ couchdb_finalize (GObject *object)
 }
 
 static void
+couchdb_set_property (GObject         *object,
+                      guint            prop_id,
+                      const GValue    *value,
+                      GParamSpec      *pspec)
+
+{
+    Couchdb *couchdb = COUCHDB (object);
+
+    switch (prop_id)
+    {
+    case PROP_URI:
+        g_free(couchdb->uri);
+        couchdb->uri = g_value_dup_string(value);
+        break;
+    default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+        break;
+    }
+}
+
+static void
+couchdb_get_property (GObject         *object,
+                      guint            prop_id,
+                      GValue          *value,
+                      GParamSpec      *pspec)
+{
+    Couchdb *couchdb = COUCHDB (object);
+
+    switch (prop_id)
+    {
+    case PROP_URI:
+        g_value_set_string (value, couchdb->uri);
+        break;
+    default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+        break;
+    }
+}
+
+static void
 couchdb_class_init (CouchdbClass *klass)
 {
 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
 	object_class->finalize = couchdb_finalize;
+    object_class->set_property = couchdb_set_property;
+    object_class->get_property = couchdb_get_property;
+
+    g_object_class_install_property (object_class,
+                                     PROP_URI,
+                                     g_param_spec_string ("uri",
+                                                          "Uri",
+                                                          "Uri pointing to the host to connect to",
+                                                          NULL,
+                                                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
+
 
 	/* Signals */
 	couchdb_signals[DATABASE_CREATED] =
@@ -148,36 +204,34 @@ couchdb_init (Couchdb *couchdb)
 	couchdb->db_watchlist = g_hash_table_new_full (g_str_hash, g_str_equal,
 						       (GDestroyNotify) g_free,
 						       (GDestroyNotify) dbwatch_free);
-}
-
-Couchdb *
-couchdb_new (const char *hostname)
-{
-	Couchdb *couchdb;
+    if (couchdb->uri == NULL)
+        couchdb->uri = g_strdup("http://127.0.0.1:5984";);
 
-	couchdb = g_object_new (COUCHDB_TYPE, NULL);
-	couchdb->hostname = hostname ? g_strdup (hostname) : g_strdup ("http://127.0.0.1:5984";);
 	couchdb->http_session = soup_session_sync_new_with_options (
 		SOUP_SESSION_ADD_FEATURE_BY_TYPE, SOUP_TYPE_GNOME_FEATURES_2_26,
                 NULL);
-	
-	couchdb->oauth_consumer_key = NULL;        
+
+	couchdb->oauth_consumer_key = NULL;
 	couchdb->oauth_consumer_secret = NULL;
 	couchdb->oauth_token_key = NULL;
 	couchdb->oauth_token_secret = NULL;
 	couchdb->oauth_enabled = FALSE;
-	
+
 	soup_session_add_feature_by_type (couchdb->http_session, SOUP_TYPE_LOGGER);
+}
 
-	return couchdb;
+Couchdb *
+couchdb_new (const char *uri)
+{
+	return g_object_new (COUCHDB_TYPE, "uri", uri, NULL);
 }
 
 const char *
-couchdb_get_hostname (Couchdb *couchdb)
+couchdb_get_uri (Couchdb *couchdb)
 {
 	g_return_val_if_fail (COUCHDB_IS (couchdb), NULL);
 
-	return (const char *) couchdb->hostname;
+	return (const char *) couchdb->uri;
 }
 
 GSList *
@@ -190,7 +244,7 @@ couchdb_list_databases (Couchdb *couchdb, GError **error)
 	g_return_val_if_fail (COUCHDB_IS (couchdb), NULL);
 
 	/* Prepare request */
-	url = g_strdup_printf ("%s/_all_dbs", couchdb->hostname);
+	url = g_strdup_printf ("%s/_all_dbs", couchdb->uri);
 	parser = json_parser_new ();
 	if (couchdb_send_message (couchdb, SOUP_METHOD_GET, url, NULL, parser, error)) {
 		JsonNode *root_node;
@@ -225,7 +279,7 @@ couchdb_get_database_info (Couchdb *couchdb, const char *dbname, GError **error)
 	g_return_val_if_fail (COUCHDB_IS (couchdb), NULL);
 	g_return_val_if_fail (dbname != NULL, NULL);
 
-	url = g_strdup_printf ("%s/%s/", couchdb->hostname, dbname);
+	url = g_strdup_printf ("%s/%s/", couchdb->uri, dbname);
 	parser = json_parser_new ();
 	if (couchdb_send_message (couchdb, SOUP_METHOD_GET, url, NULL, parser, error)) {
 		JsonNode *root_node;
@@ -257,7 +311,7 @@ couchdb_create_database (Couchdb *couchdb, const char *dbname, GError **error)
 	g_return_val_if_fail (COUCHDB_IS (couchdb), FALSE);
 	g_return_val_if_fail (dbname != NULL, FALSE);
 
-	url = g_strdup_printf ("%s/%s/", couchdb->hostname, dbname);
+	url = g_strdup_printf ("%s/%s/", couchdb->uri, dbname);
 	parser = json_parser_new ();
 	if (couchdb_send_message (couchdb, SOUP_METHOD_PUT, url, NULL, parser, error)) {
 		JsonNode *root_node;
@@ -287,7 +341,7 @@ couchdb_delete_database (Couchdb *couchdb, const char *dbname, GError **error)
 	g_return_val_if_fail (COUCHDB_IS (couchdb), FALSE);
 	g_return_val_if_fail (dbname != NULL, FALSE);
 
-	url = g_strdup_printf ("%s/%s/", couchdb->hostname, dbname);
+	url = g_strdup_printf ("%s/%s/", couchdb->uri, dbname);
 	parser = json_parser_new ();
 	if (couchdb_send_message (couchdb, SOUP_METHOD_DELETE, url, NULL, parser, error)) {
 		JsonNode *root_node;
@@ -321,6 +375,11 @@ couchdb_free_database_list (GSList *dblist)
 	g_slist_free (dblist);
 }
 
+/**
+ * couchdb_list_documents:
+ *
+ * Return Value: (element-type Couchdb.DocumentInfo) (transfer full)
+ */
 GSList *
 couchdb_list_documents (Couchdb *couchdb, const char *dbname, GError **error)
 {
@@ -331,7 +390,7 @@ couchdb_list_documents (Couchdb *couchdb, const char *dbname, GError **error)
 	g_return_val_if_fail (COUCHDB_IS (couchdb), NULL);
 	g_return_val_if_fail (dbname != NULL, NULL);
 
-	url = g_strdup_printf ("%s/%s/_all_docs", couchdb->hostname, dbname);
+	url = g_strdup_printf ("%s/%s/_all_docs", couchdb->uri, dbname);
 	parser = json_parser_new ();
 	if (couchdb_send_message (couchdb, SOUP_METHOD_GET, url, NULL, parser, error)) {
 		JsonNode *root_node;
diff --git a/couchdb-glib/couchdb.h b/couchdb-glib/couchdb.h
index 4a1e820..511336a 100644
--- a/couchdb-glib/couchdb.h
+++ b/couchdb-glib/couchdb.h
@@ -51,9 +51,9 @@ typedef struct {
 } CouchdbClass;
 
 GType       couchdb_get_type (void);
-Couchdb    *couchdb_new (const char *hostname);
+Couchdb    *couchdb_new (const char *uri);
 
-const char *couchdb_get_hostname (Couchdb *couchdb);
+const char *couchdb_get_uri (Couchdb *couchdb);
 
 GSList              *couchdb_list_databases (Couchdb *couchdb, GError **error);
 void                 couchdb_free_database_list (GSList *dblist);
diff --git a/couchdb-glib/dbwatch.c b/couchdb-glib/dbwatch.c
index a2ea673..1384303 100644
--- a/couchdb-glib/dbwatch.c
+++ b/couchdb-glib/dbwatch.c
@@ -80,7 +80,7 @@ watch_timeout_cb (gpointer user_data)
 	DBWatch *watch = (DBWatch *) user_data;
 
 	url = g_strdup_printf ("%s/%s/_changes?since=%d",
-			       couchdb_get_hostname (watch->couchdb),
+			       couchdb_get_uri (watch->couchdb),
 			       watch->dbname,
 			       watch->last_update_seq);
 	parser = json_parser_new ();
@@ -124,7 +124,7 @@ dbwatch_new (Couchdb *couchdb, const gchar *dbname, gint update_seq)
 	watch->last_update_seq = update_seq;
 
 	/* Set timeout to check for changes every 5 minutes*/
-	if (g_str_has_prefix (couchdb_get_hostname (watch->couchdb), "http://127.0.0.1";))
+	if (g_str_has_prefix (couchdb_get_uri (watch->couchdb), "http://127.0.0.1";))
 		timeout = LOCAL_TIMEOUT_SECONDS;
 	else
 		timeout = REMOTE_TIMEOUT_SECONDS;
diff --git a/couchdb-glib/oauth.c b/couchdb-glib/oauth.c
index 3b5ee05..ee852d3 100644
--- a/couchdb-glib/oauth.c
+++ b/couchdb-glib/oauth.c
@@ -488,7 +488,7 @@ int oauth_split_post_paramters(const char *url, char ***argv, short qesc) {
 			while (slash && *(++slash) == '/')  ; // skip slashes eg /xxx:[\/]*/
 #if 0
 			// skip possibly unescaped slashes in the userinfo - they're not allowed by RFC2396 but have been seen.
-			// the hostname/IP may only contain alphanumeric characters - so we're safe there.
+			// the uri/IP may only contain alphanumeric characters - so we're safe there.
 			if (slash && strchr(slash,'@')) slash=strchr(slash,'@'); 
 #endif
 			if (slash && !strchr(slash,'/')) {



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