[couchdb-glib] Use a private structure instead of hiding the Couchdb object declaration



commit baa1abc5d65f8eba9fe016ed9a2cea1a9e62b7c0
Author: Rodrigo Moya <rodrigo gnome-db org>
Date:   Fri Jan 8 14:04:10 2010 +0100

    Use a private structure instead of hiding the Couchdb object declaration

 couchdb-glib/couchdb.c |  163 ++++++++++++++++++++++++------------------------
 couchdb-glib/couchdb.h |    8 +++
 2 files changed, 90 insertions(+), 81 deletions(-)
---
diff --git a/couchdb-glib/couchdb.c b/couchdb-glib/couchdb.c
index 15ae043..1530ed9 100644
--- a/couchdb-glib/couchdb.c
+++ b/couchdb-glib/couchdb.c
@@ -35,7 +35,7 @@
 #include "oauth.h"
 #endif
 
-struct _Couchdb {
+struct _CouchdbPrivate {
 	GObject parent;
 
 	char *uri;
@@ -72,19 +72,21 @@ couchdb_finalize (GObject *object)
 {
 	Couchdb *couchdb = COUCHDB (object);
 
-	g_hash_table_destroy (couchdb->db_watchlist);
+	g_hash_table_destroy (couchdb->priv->db_watchlist);
 
-	g_free (couchdb->uri);
-	g_object_unref (couchdb->http_session);
+	g_free (couchdb->priv->uri);
+	g_object_unref (couchdb->priv->http_session);
 
-	if (couchdb->oauth_consumer_key)
-		g_free (couchdb->oauth_consumer_key);
-	if (couchdb->oauth_consumer_secret)
-		g_free (couchdb->oauth_consumer_secret);
-	if (couchdb->oauth_token_key)
-		g_free (couchdb->oauth_token_key);
-	if (couchdb->oauth_token_secret)
-		g_free (couchdb->oauth_token_secret);
+	if (couchdb->priv->oauth_consumer_key)
+		g_free (couchdb->priv->oauth_consumer_key);
+	if (couchdb->priv->oauth_consumer_secret)
+		g_free (couchdb->priv->oauth_consumer_secret);
+	if (couchdb->priv->oauth_token_key)
+		g_free (couchdb->priv->oauth_token_key);
+	if (couchdb->priv->oauth_token_secret)
+		g_free (couchdb->priv->oauth_token_secret);
+
+	g_free (couchdb->priv);
 
 	G_OBJECT_CLASS (couchdb_parent_class)->finalize (object);
 }
@@ -96,18 +98,17 @@ couchdb_set_property (GObject         *object,
                       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;
-    }
+	Couchdb *couchdb = COUCHDB (object);
+
+	switch (prop_id) {
+	case PROP_URI:
+		g_free(couchdb->priv->uri);
+		couchdb->priv->uri = g_value_dup_string(value);
+		break;
+	default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+		break;
+	}
 }
 
 static void
@@ -116,17 +117,16 @@ couchdb_get_property (GObject         *object,
                       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;
-    }
+	Couchdb *couchdb = COUCHDB (object);
+
+	switch (prop_id) {
+	case PROP_URI:
+		g_value_set_string (value, couchdb->priv->uri);
+		break;
+	default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+		break;
+	}
 }
 
 static void
@@ -135,17 +135,16 @@ 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));
+	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] =
@@ -201,23 +200,25 @@ couchdb_class_init (CouchdbClass *klass)
 static void
 couchdb_init (Couchdb *couchdb)
 {
-	couchdb->db_watchlist = g_hash_table_new_full (g_str_hash, g_str_equal,
+	couchdb->priv = g_new0 (CouchdbPrivate, 1);
+
+	couchdb->priv->db_watchlist = g_hash_table_new_full (g_str_hash, g_str_equal,
 						       (GDestroyNotify) g_free,
 						       (GDestroyNotify) dbwatch_free);
-    if (couchdb->uri == NULL)
-        couchdb->uri = g_strdup("http://127.0.0.1:5984";);
+	if (couchdb->priv->uri == NULL)
+		couchdb->priv->uri = g_strdup("http://127.0.0.1:5984";);
 
-	couchdb->http_session = soup_session_sync_new_with_options (
+	couchdb->priv->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_secret = NULL;
-	couchdb->oauth_token_key = NULL;
-	couchdb->oauth_token_secret = NULL;
-	couchdb->oauth_enabled = FALSE;
+	couchdb->priv->oauth_consumer_key = NULL;
+	couchdb->priv->oauth_consumer_secret = NULL;
+	couchdb->priv->oauth_token_key = NULL;
+	couchdb->priv->oauth_token_secret = NULL;
+	couchdb->priv->oauth_enabled = FALSE;
 
-	soup_session_add_feature_by_type (couchdb->http_session, SOUP_TYPE_LOGGER);
+	soup_session_add_feature_by_type (couchdb->priv->http_session, SOUP_TYPE_LOGGER);
 }
 
 Couchdb *
@@ -231,7 +232,7 @@ couchdb_get_uri (Couchdb *couchdb)
 {
 	g_return_val_if_fail (COUCHDB_IS (couchdb), NULL);
 
-	return (const char *) couchdb->uri;
+	return (const char *) couchdb->priv->uri;
 }
 
 GSList *
@@ -244,7 +245,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->uri);
+	url = g_strdup_printf ("%s/_all_dbs", couchdb->priv->uri);
 	parser = json_parser_new ();
 	if (couchdb_send_message (couchdb, SOUP_METHOD_GET, url, NULL, parser, error)) {
 		JsonNode *root_node;
@@ -279,7 +280,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->uri, dbname);
+	url = g_strdup_printf ("%s/%s/", couchdb->priv->uri, dbname);
 	parser = json_parser_new ();
 	if (couchdb_send_message (couchdb, SOUP_METHOD_GET, url, NULL, parser, error)) {
 		JsonNode *root_node;
@@ -311,7 +312,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->uri, dbname);
+	url = g_strdup_printf ("%s/%s/", couchdb->priv->uri, dbname);
 	parser = json_parser_new ();
 	if (couchdb_send_message (couchdb, SOUP_METHOD_PUT, url, NULL, parser, error)) {
 		JsonNode *root_node;
@@ -341,7 +342,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->uri, dbname);
+	url = g_strdup_printf ("%s/%s/", couchdb->priv->uri, dbname);
 	parser = json_parser_new ();
 	if (couchdb_send_message (couchdb, SOUP_METHOD_DELETE, url, NULL, parser, error)) {
 		JsonNode *root_node;
@@ -357,8 +358,8 @@ couchdb_delete_database (Couchdb *couchdb, const char *dbname, GError **error)
 
 	if (result) {
 		/* If we're listening for changes on this database, stop doing so */
-		if (g_hash_table_lookup (couchdb->db_watchlist, dbname))
-			g_hash_table_remove (couchdb->db_watchlist, dbname);
+		if (g_hash_table_lookup (couchdb->priv->db_watchlist, dbname))
+			g_hash_table_remove (couchdb->priv->db_watchlist, dbname);
 
 		g_signal_emit_by_name (couchdb, "database_deleted", dbname);
 	}
@@ -390,7 +391,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->uri, dbname);
+	url = g_strdup_printf ("%s/%s/_all_docs", couchdb->priv->uri, dbname);
 	parser = json_parser_new ();
 	if (couchdb_send_message (couchdb, SOUP_METHOD_GET, url, NULL, parser, error)) {
 		JsonNode *root_node;
@@ -444,7 +445,7 @@ couchdb_listen_for_changes (Couchdb *couchdb, const char *dbname)
 	g_return_if_fail (COUCHDB_IS (couchdb));
 	g_return_if_fail (dbname != NULL);
 
-	watch = g_hash_table_lookup (couchdb->db_watchlist, dbname);
+	watch = g_hash_table_lookup (couchdb->priv->db_watchlist, dbname);
 	if (watch) {
 		g_warning ("Already listening for changes in '%s' database", dbname);
 		return;
@@ -464,7 +465,7 @@ couchdb_listen_for_changes (Couchdb *couchdb, const char *dbname)
 			     dbname,
 			     couchdb_database_info_get_update_sequence (db_info));
 	if (watch)
-		g_hash_table_insert (couchdb->db_watchlist, g_strdup (dbname), watch);
+		g_hash_table_insert (couchdb->priv->db_watchlist, g_strdup (dbname), watch);
 
 	/* Free memory */
 	couchdb_database_info_unref (db_info);
@@ -492,18 +493,18 @@ couchdb_enable_oauth (Couchdb *couchdb,
 	g_return_val_if_fail (COUCHDB_IS (couchdb), FALSE);
 
 #ifdef HAVE_OAUTH
-	if (couchdb->oauth_enabled) {
-		g_free (couchdb->oauth_consumer_key);
-		g_free (couchdb->oauth_consumer_secret);
-		g_free (couchdb->oauth_token_key);
-		g_free (couchdb->oauth_token_secret);
+	if (couchdb->priv->oauth_enabled) {
+		g_free (couchdb->priv->oauth_consumer_key);
+		g_free (couchdb->priv->oauth_consumer_secret);
+		g_free (couchdb->priv->oauth_token_key);
+		g_free (couchdb->priv->oauth_token_secret);
 	}
 
-	couchdb->oauth_consumer_key = g_strdup (consumer_key);
-	couchdb->oauth_consumer_secret = g_strdup (consumer_secret);
-	couchdb->oauth_token_key = g_strdup (token_key);
-	couchdb->oauth_token_secret = g_strdup (token_secret);
-	couchdb->oauth_enabled = TRUE;
+	couchdb->priv->oauth_consumer_key = g_strdup (consumer_key);
+	couchdb->priv->oauth_consumer_secret = g_strdup (consumer_secret);
+	couchdb->priv->oauth_token_key = g_strdup (token_key);
+	couchdb->priv->oauth_token_secret = g_strdup (token_secret);
+	couchdb->priv->oauth_enabled = TRUE;
 
 	return TRUE;
 #else
@@ -516,7 +517,7 @@ couchdb_is_oauth_enabled (Couchdb *couchdb)
 {
 	g_return_val_if_fail (COUCHDB_IS (couchdb), FALSE);
 	
-	return couchdb->oauth_enabled;
+	return couchdb->priv->oauth_enabled;
 }
 
 
@@ -528,10 +529,10 @@ couchdb_add_oauth_signature (Couchdb *couchdb, SoupMessage *http_message, const
 	char *signed_url;
 
 	signed_url = oauth_sign_url2 (url, NULL, OA_HMAC, method,
-				      couchdb->oauth_consumer_key,
-				      couchdb->oauth_consumer_secret,
-				      couchdb->oauth_token_key,
-				      couchdb->oauth_token_secret);
+				      couchdb->priv->oauth_consumer_key,
+				      couchdb->priv->oauth_consumer_secret,
+				      couchdb->priv->oauth_token_key,
+				      couchdb->priv->oauth_token_secret);
 	if (signed_url != NULL) {
 		char **parsed_url;
 		GString *header = NULL;
@@ -652,7 +653,7 @@ couchdb_send_message (Couchdb *couchdb, const char *method, const char *url, con
 				      (SoupMessageHeadersForeachFunc) debug_print_headers,
 				      NULL);
 
-	status = soup_session_send_message (couchdb->http_session, http_message);
+	status = soup_session_send_message (couchdb->priv->http_session, http_message);
 	if (SOUP_STATUS_IS_SUCCESSFUL (status)) {
 		if (parser != NULL)
 		       	parse_json_response (couchdb, parser, http_message, error);
diff --git a/couchdb-glib/couchdb.h b/couchdb-glib/couchdb.h
index 511336a..2f57fa3 100644
--- a/couchdb-glib/couchdb.h
+++ b/couchdb-glib/couchdb.h
@@ -39,6 +39,14 @@ G_BEGIN_DECLS
 #define COUCHDB_IS_CLASS(klass)     (G_TYPE_CHECK_CLASS_TYPE ((klass), COUCHDB_TYPE))
 #define COUCHDB_GET_CLASS(obj)      (G_TYPE_INSTANCE_GET_CLASS ((obj), COUCHDB_TYPE, CouchdbClass))
 
+typedef struct _CouchdbPrivate CouchdbPrivate;
+
+typedef struct {
+	GObject parent;
+
+	CouchdbPrivate *priv;
+} Couchdb;
+
 typedef struct {
 	GObjectClass parent_class;
 



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