[ohm] ohm: Branch 'master' - 4 commits

Rob Taylor robtaylor at kemper.freedesktop.org
Wed Aug 22 08:00:55 PDT 2007


 ohmd/Makefile.am    |    4 -
 ohmd/ohm-conf.c     |  193 +++++++++++++++++++---------------------------------
 ohmd/ohm-conf.h     |   20 ++---
 ohmd/ohm-keystore.c |   34 ++++-----
 ohmd/ohm-test.c     |    6 -
 5 files changed, 99 insertions(+), 158 deletions(-)

New commits:
diff-tree 466fc27d87f05fec747dbdffcfd7ecf0c06c54fd (from 8cbeb877f00f2e59080eb07cd422c40388474b60)
Author: Rob Taylor <rob.taylor at codethink.co.uk>
Date:   Thu Aug 9 14:19:15 2007 +0100

    remove test for OhmConfObj from ohm-test.c
    
    OhmConfObj exists no more, so remove the test.

diff --git a/ohmd/ohm-test.c b/ohmd/ohm-test.c
index db14c98..c100965 100644
--- a/ohmd/ohm-test.c
+++ b/ohmd/ohm-test.c
@@ -30,7 +30,6 @@
 #include "ohm-debug.h"
 #include "ohm-common.h"
 #include "ohm-conf.h"
-#include "ohm-confobj.h"
 
 /**
  * main:
@@ -39,7 +38,6 @@ int
 main (int argc, char *argv[])
 {
 	OhmConf *conf = NULL;
-	OhmConfObj *confobj = NULL;
 	gboolean ret;
 	GError *error;
 	gint value;
@@ -48,10 +46,6 @@ main (int argc, char *argv[])
 
 	ohm_debug_init (TRUE);
 
-	ohm_debug ("Tesing confobj");
-	confobj = ohm_confobj_new ();
-	g_object_unref (confobj);
-
 	ohm_debug ("Testing conf");
 	conf = ohm_conf_new ();
 
diff-tree 8cbeb877f00f2e59080eb07cd422c40388474b60 (from 34ca4e70414db7e75205500a7bbb10428d9e1e8e)
Author: Rob Taylor <rob.taylor at codethink.co.uk>
Date:   Thu Aug 9 14:18:36 2007 +0100

    covert OhmKeystore to use new OhmCOnf key iterator
    
    Covert OhmKeystore to use new ohm_conf_keys_foreach.

diff --git a/ohmd/ohm-keystore.c b/ohmd/ohm-keystore.c
index f680a83..caa94f7 100644
--- a/ohmd/ohm-keystore.c
+++ b/ohmd/ohm-keystore.c
@@ -99,6 +99,18 @@ ohm_keystore_set_key (OhmKeystore *keyst
 	(dbus_g_type_get_struct ("GValueArray", \
 	G_TYPE_STRING, G_TYPE_INT, G_TYPE_BOOLEAN, G_TYPE_INVALID))
 
+static void
+get_key_foreach (const char *key, gboolean public, gint value, gpointer user_data)
+{
+	GPtrArray *data = (GPtrArray *) user_data;
+	GValue gv ={0,};
+
+	g_value_init (&gv, OHM_DBUS_STRUCT_STRING_INT_BOOL);
+	g_value_take_boxed (&gv, dbus_g_type_specialized_construct (OHM_DBUS_STRUCT_STRING_INT_BOOL));
+	dbus_g_type_struct_set (&gv, 0, key, 1, value, 2, public, -1);
+	g_ptr_array_add (data, g_value_get_boxed (&gv));
+}
+
 /**
  * ohm_keystore_get_keys:
  *
@@ -109,30 +121,16 @@ ohm_keystore_get_keys (OhmKeystore *keys
 		       GPtrArray  **data,
 		       GError     **error)
 {
-	GSList *keys;
-	GSList *l;
-	OhmConfKeyValue *new;
-	GValue *value;
-
 	g_return_val_if_fail (keystore != NULL, FALSE);
 	g_return_val_if_fail (OHM_IS_KEYSTORE (keystore), FALSE);
 	g_return_val_if_fail (data != NULL, FALSE);
 
 	ohm_debug ("Getting list of keys");
 
-	keys = NULL;
-	ohm_conf_get_keys (keystore->priv->conf, &keys);
-	*data = g_ptr_array_sized_new (g_slist_length (keys));
-
-	for (l=keys; l != NULL; l=l->next) {
-		new = (OhmConfKeyValue *) l->data;
-		value = g_new0 (GValue, 1);
-		g_value_init (value, OHM_DBUS_STRUCT_STRING_INT_BOOL);
-		g_value_take_boxed (value, dbus_g_type_specialized_construct (OHM_DBUS_STRUCT_STRING_INT_BOOL));
-		dbus_g_type_struct_set (value, 0, new->name, 1, new->value, 2, new->public, -1);
-		g_ptr_array_add (*data, g_value_get_boxed (value));
-		g_free (value);
-	}
+	*data = g_ptr_array_sized_new (ohm_conf_keys_length (keystore->priv->conf));
+
+	ohm_conf_keys_foreach (keystore->priv->conf, get_key_foreach, *data);
+
 	return TRUE;
 }
 
diff-tree 34ca4e70414db7e75205500a7bbb10428d9e1e8e (from 7b7b9ee40483cfb2b31d6b198b3a4bed4f363345)
Author: Rob Taylor <rob.taylor at codethink.co.uk>
Date:   Thu Aug 9 14:17:41 2007 +0100

    Add ohm_conf_keys_length method
    
    Adds ohm_conf_keys_length method to OhmConf to return the number of keys stored.

diff --git a/ohmd/ohm-conf.c b/ohmd/ohm-conf.c
index ce0448d..b36259b 100644
--- a/ohmd/ohm-conf.c
+++ b/ohmd/ohm-conf.c
@@ -209,14 +209,19 @@ foreach_keys (gpointer key,
 }
 
 void
-ohm_conf_keys_foreach(OhmConf		 *conf,
-		      OhmConfForeachFunc  func,
-		      gpointer		  user_data)
+ohm_conf_keys_foreach (OhmConf		 *conf,
+		       OhmConfForeachFunc  func,
+		       gpointer		  user_data)
 {
 	ForeachData d = {func, user_data};
 	g_hash_table_foreach (conf->priv->keys, foreach_keys, &d);
 }
 
+gint
+ohm_conf_keys_length (OhmConf	*conf)
+{
+	return g_hash_table_size(conf->priv->keys);
+}
 
 /**
  * ohm_conf_set_key:
diff --git a/ohmd/ohm-conf.h b/ohmd/ohm-conf.h
index 1345899..33cd06f 100644
--- a/ohmd/ohm-conf.h
+++ b/ohmd/ohm-conf.h
@@ -72,6 +72,8 @@ void		 ohm_conf_keys_foreach			(OhmConf	
 							 OhmConfForeachFunc func,
 							 gpointer	user_data);
 
+gint		 ohm_conf_keys_length			(OhmConf	*conf);
+
 gboolean	 ohm_conf_set_key_internal		(OhmConf	*conf,
 							 const gchar	*key,
 							 gint		 value,
diff-tree 7b7b9ee40483cfb2b31d6b198b3a4bed4f363345 (from 5adfae30f655b11383c440938e9c08fc6ea8314a)
Author: Rob Taylor <rob.taylor at codethink.co.uk>
Date:   Thu Aug 9 13:14:19 2007 +0100

    replace ohm_conf_get_keys with ohm_conf_keys_foreach and remove OhmConfobj
    
    Replace the inefficent and abi-messy ohm_conf_get_keys with an g_hash style
    iterator. Remove OhmConfObj and replace with a small slice-allocated struct.

diff --git a/ohmd/Makefile.am b/ohmd/Makefile.am
index 03d2367..da966a1 100644
--- a/ohmd/Makefile.am
+++ b/ohmd/Makefile.am
@@ -23,8 +23,6 @@ noinst_PROGRAMS = ohmd-test
 ohmd_test_SOURCES = 						\
 	ohm-debug.c						\
 	ohm-debug.h						\
-	ohm-confobj.c						\
-	ohm-confobj.h						\
 	ohm-conf.c						\
 	ohm-conf.h						\
 	ohm-marshal.h						\
@@ -38,8 +36,6 @@ ohmd_SOURCES = 							\
 	ohm-common.h						\
 	ohm-debug.c						\
 	ohm-debug.h						\
-	ohm-confobj.c						\
-	ohm-confobj.h						\
 	ohm-conf.c						\
 	ohm-conf.h						\
 	ohm-marshal.h						\
diff --git a/ohmd/ohm-conf.c b/ohmd/ohm-conf.c
index a231c62..ce0448d 100644
--- a/ohmd/ohm-conf.c
+++ b/ohmd/ohm-conf.c
@@ -38,7 +38,6 @@
 
 #include "ohm-debug.h"
 #include "ohm-conf.h"
-#include "ohm-confobj.h"
 #include "ohm-marshal.h"
 
 #define OHM_CONF_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), OHM_TYPE_CONF, OhmConfPrivate))
@@ -58,111 +57,37 @@ static guint	     signals [LAST_SIGNAL] 
 
 G_DEFINE_TYPE (OhmConf, ohm_conf, G_TYPE_OBJECT)
 
-/**
- * ohm_conf_error_quark:
- * Return value: Our personal error quark.
- **/
-GQuark
-ohm_conf_error_quark (void)
+typedef struct ConfValue ConfValue;
+struct ConfValue
 {
-	static GQuark quark = 0;
-	if (!quark) {
-		quark = g_quark_from_static_string ("ohm_conf_error");
-	}
-	return quark;
-}
+	gboolean public;
+	gint value;
+};
 
-/**
- * ohm_conf_compare_func
- * A GCompareFunc for comparing two OhmConfKeyValue objects by name.
- **/
-static gint
-ohm_conf_compare_func (gconstpointer a, gconstpointer b)
+static ConfValue *
+new_conf_value ()
 {
-	OhmConfKeyValue *entry1 = (OhmConfKeyValue *) a;
-	OhmConfKeyValue *entry2 = (OhmConfKeyValue *) b;
-	return strcmp (entry1->name, entry2->name);
+	return g_slice_new (ConfValue);
 }
 
-/**
- * ohm_conf_to_slist_iter:
- **/
 static void
-ohm_conf_to_slist_iter (gpointer    key,
-			OhmConfObj *confobj,
-			gpointer   *user_data)
-{
-	OhmConfKeyValue *keyvalue;
-	GSList **list;
-
-	/* copy key values into the ABI stable struct to export */
-	keyvalue = g_new0 (OhmConfKeyValue, 1);
-	keyvalue->name = ohm_confobj_get_key (confobj);
-	keyvalue->value = ohm_confobj_get_value (confobj);
-	keyvalue->public = ohm_confobj_get_public (confobj);
-
-	/* add to list */
-	list = (GSList **) user_data;
-	*list = g_slist_prepend (*list, (gpointer) keyvalue);
-}
-
-/**
- * ohm_conf_get_keys:
- * @conf: an #OhmConf object
- * @list: a pointer in which to return the list of keys
- *
- * Gets an ordered list of all the key values in #OhmConfKeyValue's.
- * Free the list with #ohm_conf_free_keys.
- *
- * Returns: FALSE if an error occured.
- **/
-gboolean
-ohm_conf_get_keys (OhmConf *conf,
-		   GSList **list)
+free_conf_value (ConfValue *cv)
 {
-	g_return_val_if_fail (OHM_IS_CONF (conf), FALSE);
-	g_return_val_if_fail (list != NULL, FALSE);
-	g_return_val_if_fail (*list == NULL, FALSE);
-
-	ohm_debug ("Get all keys and values in database");
-	if (conf->priv->keys == NULL) {
-		g_warning ("Conf invalid");
-		return FALSE;
-	}
-
-	/* add hash to unsorted SList */
-	g_hash_table_foreach (conf->priv->keys, (GHFunc) ohm_conf_to_slist_iter, list);
-
-	/* sort list */
-	*list = g_slist_sort (*list, ohm_conf_compare_func);
-	return TRUE;
+	g_slice_free (ConfValue, cv);
 }
 
 /**
- * ohm_conf_free_keys:
- * @conf: an #OhmConf store
- * @list: list of keys as returned by #ohm_conf_get_keys
- *
- * Frees a list of keys, as returned from #ohm_conf_get_keys.
+ * ohm_conf_error_quark:
+ * Return value: Our personal error quark.
  **/
-gboolean
-ohm_conf_free_keys (OhmConf *conf,
-		    GSList  *list)
+GQuark
+ohm_conf_error_quark (void)
 {
-	GSList *l;
-	OhmConfKeyValue *keyvalue;
-
-	g_return_val_if_fail (OHM_IS_CONF (conf), FALSE);
-	g_return_val_if_fail (list != NULL, FALSE);
-
-	/* free the object only, text is an internal pointer */
-	for (l=list; l != NULL; l=l->next) {
-		keyvalue = (OhmConfKeyValue *) l->data;
-		g_free (keyvalue);
+	static GQuark quark = 0;
+	if (!quark) {
+		quark = g_quark_from_static_string ("ohm_conf_error");
 	}
-	g_slist_free (list);
-
-	return TRUE;
+	return quark;
 }
 
 /**
@@ -174,7 +99,8 @@ ohm_conf_get_key (OhmConf     *conf,
 		  gint        *value,
 		  GError     **error)
 {
-	OhmConfObj *confobj;
+	ConfValue *cv;
+
 	g_return_val_if_fail (OHM_IS_CONF (conf), FALSE);
 	g_return_val_if_fail (key != NULL, FALSE);
 	g_return_val_if_fail (value != NULL, FALSE);
@@ -190,8 +116,8 @@ ohm_conf_get_key (OhmConf     *conf,
 	}
 
 	/* try to find the key in the global conf */
-	confobj = g_hash_table_lookup (conf->priv->keys, key);
-	if (confobj == NULL) {
+	cv = g_hash_table_lookup (conf->priv->keys, key);
+	if (cv == NULL) {
 		*error = g_error_new (ohm_conf_error_quark (),
 				      OHM_CONF_ERROR_KEY_MISSING,
 				      "Key %s missing", key);
@@ -200,7 +126,7 @@ ohm_conf_get_key (OhmConf     *conf,
 	}
 
 	/* copy value from key */
-	*value = ohm_confobj_get_value (confobj);
+	*value = cv->value;
 	return TRUE;
 }
 
@@ -223,8 +149,7 @@ ohm_conf_add_key (OhmConf     *conf,
 		  gboolean     public,
 		  GError     **error)
 {
-	OhmConfObj *confobj;
-	gchar *confkey;
+	ConfValue *cv;
 
 	g_return_val_if_fail (OHM_IS_CONF (conf), FALSE);
 	g_return_val_if_fail (key != NULL, FALSE);
@@ -237,8 +162,8 @@ ohm_conf_add_key (OhmConf     *conf,
 	}
 
 	/* try to find the key in the global conf */
-	confobj = g_hash_table_lookup (conf->priv->keys, key);
-	if (confobj != NULL) {
+	cv = g_hash_table_lookup (conf->priv->keys, key);
+	if (cv != NULL) {
 		*error = g_error_new (ohm_conf_error_quark (),
 				      OHM_CONF_ERROR_KEY_ALREADY_EXISTS,
 				      "Key %s already exists", key);
@@ -247,12 +172,10 @@ ohm_conf_add_key (OhmConf     *conf,
 
 	/* create a new key */
 	ohm_debug ("create key '%s' : %i", key, value);
-	confobj = ohm_confobj_new ();
+	cv = new_conf_value();
 
-	/* maybe point to the key in the hashtable to save memory? */
-	ohm_confobj_set_key (confobj, key);
-	ohm_confobj_set_public (confobj, public);
-	ohm_confobj_set_value (confobj, value);
+	cv->public = public;
+	cv->value = value;
 
 	/* we need to create new objects in the store for each added user */
 
@@ -260,13 +183,41 @@ ohm_conf_add_key (OhmConf     *conf,
 	ohm_debug ("emit key-added : %s", key);
 	g_signal_emit (conf, signals [KEY_ADDED], 0, key, value);
 
-	/* add as the strdup'd value as key is constant */
-	confkey = ohm_confobj_get_key (confobj);
-	g_hash_table_insert (conf->priv->keys, (gpointer) g_strdup(confkey), (gpointer) confobj);
+	/* It'd be nice to have a cunning way to not strdup if the key was from
+	 * static data. Maybe a field in ConfValue to put the key in if it wasnt,
+	 * and an ohm_conf_add_key_static?
+	 */
+	g_hash_table_insert (conf->priv->keys, (gpointer) g_strdup(key), (gpointer) cv);
 
 	return TRUE;
 }
 
+typedef struct ForeachData ForeachData;
+struct ForeachData {
+	OhmConfForeachFunc func;
+	gpointer user_data;
+};
+
+static void
+foreach_keys (gpointer key,
+	      gpointer value,
+	      gpointer user_data)
+{
+	ForeachData *d = (ForeachData*) user_data;
+	ConfValue *cv = (ConfValue *)value;
+	d->func ((const char*)key, cv->public, cv->value, d->user_data);
+}
+
+void
+ohm_conf_keys_foreach(OhmConf		 *conf,
+		      OhmConfForeachFunc  func,
+		      gpointer		  user_data)
+{
+	ForeachData d = {func, user_data};
+	g_hash_table_foreach (conf->priv->keys, foreach_keys, &d);
+}
+
+
 /**
  * ohm_conf_set_key:
  *
@@ -279,9 +230,7 @@ ohm_conf_set_key_internal (OhmConf     *
 		           gboolean     internal,
 		           GError     **error)
 {
-	OhmConfObj *confobj;
-	gboolean confpublic;
-	gint confobjvalue;
+	ConfValue *cv;
 
 	g_return_val_if_fail (OHM_IS_CONF (conf), FALSE);
 	g_return_val_if_fail (key != NULL, FALSE);
@@ -294,8 +243,8 @@ ohm_conf_set_key_internal (OhmConf     *
 	}
 
 	/* try to find the key in the global conf */
-	confobj = g_hash_table_lookup (conf->priv->keys, key);
-	if (confobj == NULL) {
+	cv = g_hash_table_lookup (conf->priv->keys, key);
+	if (cv == NULL) {
 		*error = g_error_new (ohm_conf_error_quark (),
 				      OHM_CONF_ERROR_KEY_MISSING,
 				      "Key %s missing", key);
@@ -304,8 +253,7 @@ ohm_conf_set_key_internal (OhmConf     *
 
 	/* if we are externally calling this key, check to see if
 	   we are allowed to set this key */
-	confpublic = ohm_confobj_get_public (confobj);
-	if (internal == FALSE && confpublic == FALSE) {
+	if (internal == FALSE && cv->public == FALSE) {
 		ohm_debug ("tried to set private key : %s", key);
 		*error = g_error_new (ohm_conf_error_quark (),
 				      OHM_CONF_ERROR_KEY_OVERRIDE,
@@ -317,9 +265,8 @@ ohm_conf_set_key_internal (OhmConf     *
 	ohm_debug ("set existing key '%s' : %i", key, value);
 
 	/* Only force signal if different */
-	confobjvalue = ohm_confobj_get_value (confobj);
-	if (confobjvalue != value) {
-		ohm_confobj_set_value (confobj, value);
+	if (cv->value != value) {
+		cv->value = value;
 		ohm_debug ("emit key-changed : %s", key);
 		g_signal_emit (conf, signals [KEY_CHANGED], 0, key, value);
 	}
@@ -491,7 +438,6 @@ ohm_conf_finalize (GObject *object)
 	g_hash_table_unref (conf->priv->keys);
 	conf->priv->keys = NULL;
 
-	g_return_if_fail (conf->priv != NULL);
 	G_OBJECT_CLASS (ohm_conf_parent_class)->finalize (object);
 }
 
@@ -532,7 +478,7 @@ ohm_conf_init (OhmConf *conf)
 {
 	conf->priv = OHM_CONF_GET_PRIVATE (conf);
 	conf->priv->keys = g_hash_table_new_full (g_str_hash, g_str_equal,
-						  g_free, g_object_unref);
+						  g_free, (GDestroyNotify) free_conf_value);
 }
 
 /**
diff --git a/ohmd/ohm-conf.h b/ohmd/ohm-conf.h
index 726a586..1345899 100644
--- a/ohmd/ohm-conf.h
+++ b/ohmd/ohm-conf.h
@@ -45,13 +45,6 @@ typedef struct
 	GObjectClass	parent_class;
 } OhmConfClass;
 
-/* ABI stable representation suitable for consumption by session apps */
-typedef struct {
-	gchar		*name;
-	gint		 value;
-	gboolean	 public;
-} OhmConfKeyValue;
-
 typedef enum
 {
 	 OHM_CONF_ERROR_INVALID,
@@ -62,16 +55,23 @@ typedef enum
 	 OHM_CONF_ERROR_KEY_LAST
 } OhmConfError;
 
+typedef void (*OhmConfForeachFunc)			(const char *key,
+							 gboolean public,
+							 gint value,
+							 gpointer user_data);
+
 GType		 ohm_conf_get_type			(void);
 GQuark		 ohm_conf_error_quark			(void);
 OhmConf 	*ohm_conf_new				(void);
 
-gboolean	 ohm_conf_get_keys			(OhmConf	*conf,
-							 GSList		**list);
 gboolean	 ohm_conf_get_key			(OhmConf	*conf,
 							 const gchar	*key,
 							 gint		*value,
 							 GError		**error);
+void		 ohm_conf_keys_foreach			(OhmConf	*conf,
+							 OhmConfForeachFunc func,
+							 gpointer	user_data);
+
 gboolean	 ohm_conf_set_key_internal		(OhmConf	*conf,
 							 const gchar	*key,
 							 gint		 value,


More information about the Ohm-devel mailing list