diff options
author | Ted Gould <ted@gould.cx> | 2009-12-09 21:50:25 -0600 |
---|---|---|
committer | Ted Gould <ted@gould.cx> | 2009-12-09 21:50:25 -0600 |
commit | 2997a508fd49ebbbfa53d1c5fc5d0d56250de760 (patch) | |
tree | 03b00f9a9d8cfb2fbc8f47340acc40bfcf139009 /libdbusmenu-glib | |
parent | 07ddbba1253463e159d13f65eabcfade699df2e8 (diff) | |
download | libdbusmenu-2997a508fd49ebbbfa53d1c5fc5d0d56250de760.tar.gz libdbusmenu-2997a508fd49ebbbfa53d1c5fc5d0d56250de760.tar.bz2 libdbusmenu-2997a508fd49ebbbfa53d1c5fc5d0d56250de760.zip |
Switching the menuitem internal storage to be a hashtable of values.
Diffstat (limited to 'libdbusmenu-glib')
-rw-r--r-- | libdbusmenu-glib/menuitem.c | 78 | ||||
-rw-r--r-- | libdbusmenu-glib/menuitem.h | 4 | ||||
-rw-r--r-- | libdbusmenu-glib/server.c | 16 |
3 files changed, 75 insertions, 23 deletions
diff --git a/libdbusmenu-glib/menuitem.c b/libdbusmenu-glib/menuitem.c index d486b53..77f8e90 100644 --- a/libdbusmenu-glib/menuitem.c +++ b/libdbusmenu-glib/menuitem.c @@ -213,6 +213,20 @@ dbusmenu_menuitem_class_init (DbusmenuMenuitemClass *klass) static guint menuitem_next_id = 1; +/* A small little function to both clear the insides of a + value as well as the memory it itself uses. */ +static void +g_value_free (gpointer data) +{ + if (data == NULL) return; + GValue * value = (GValue*)data; + g_value_unset(value); + g_free(data); + return; +} + +/* Initialize the values of the in the object, and build the + properties hash table. */ static void dbusmenu_menuitem_init (DbusmenuMenuitem *self) { @@ -221,7 +235,7 @@ dbusmenu_menuitem_init (DbusmenuMenuitem *self) priv->id = 0; priv->children = NULL; - priv->properties = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); + priv->properties = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_value_free); priv->root = FALSE; @@ -674,27 +688,58 @@ dbusmenu_menuitem_find_id (DbusmenuMenuitem * mi, guint id) gboolean dbusmenu_menuitem_property_set (DbusmenuMenuitem * mi, const gchar * property, const gchar * value) { + GValue val = {0}; + g_value_init(&val, G_TYPE_STRING); + g_value_set_static_string(&val, value); + return dbusmenu_menuitem_property_set_value(mi, property, &val); +} + +/** + dbusmenu_menuitem_property_set: + @mi: The #DbusmenuMenuitem to set the property on. + @property: Name of the property to set. + @value: The value of the property. + + Takes the pair of @property and @value and places them as a + property on @mi. If a property already exists by that name, + then the value is set to the new value. If not, the property + is added. If the value is changed or the property was previously + unset then the signal #DbusmenuMenuitem::prop-changed will be + emitted by this function. + + Return value: A boolean representing if the property value was set. +*/ +gboolean +dbusmenu_menuitem_property_set_value (DbusmenuMenuitem * mi, const gchar * property, const GValue * value) +{ g_return_val_if_fail(DBUSMENU_IS_MENUITEM(mi), FALSE); g_return_val_if_fail(property != NULL, FALSE); + g_return_val_if_fail(G_IS_VALUE(value), FALSE); DbusmenuMenuitemPrivate * priv = DBUSMENU_MENUITEM_GET_PRIVATE(mi); /* g_debug("Setting a property. ID: %d Prop: %s Value: %s", priv->id, property, value); */ + #if 0 gpointer lookup = g_hash_table_lookup(priv->properties, property); if (g_strcmp0((gchar *)lookup, value) == 0) { /* The value is the same as the value currently in the table so we don't really care. Just say everything's okay */ return TRUE; } + #endif gchar * lprop = g_strdup(property); - gchar * lval = g_strdup(value); + GValue * lval = g_new0(GValue, 1); + g_value_init(lval, G_VALUE_TYPE(value)); + g_value_copy(value, lval); g_hash_table_insert(priv->properties, lprop, lval); #ifdef MASSIVEDEBUGGING - g_debug("Menuitem %d (%s) signalling property '%s' changed to '%s'", ID(mi), LABEL(mi), property, g_utf8_strlen(value, 50) < 25 ? value : "<too long>"); + gchar * valstr = g_strdup_value_contents(lval); + g_debug("Menuitem %d (%s) signalling property '%s' changed to '%s'", ID(mi), LABEL(mi), property, g_utf8_strlen(valstr, 50) < 25 ? valstr : "<too long>"); + g_free(valstr); #endif - g_signal_emit(G_OBJECT(mi), signals[PROPERTY_CHANGED], 0, property, value, TRUE); + g_signal_emit(G_OBJECT(mi), signals[PROPERTY_CHANGED], 0, lprop, lval, TRUE); return TRUE; } @@ -710,19 +755,40 @@ dbusmenu_menuitem_property_set (DbusmenuMenuitem * mi, const gchar * property, c Return value: A string with the value of the property that shouldn't be free'd. Or #NULL if the property - is not set. + is not set or is not a string. */ const gchar * dbusmenu_menuitem_property_get (DbusmenuMenuitem * mi, const gchar * property) { + const GValue * value = dbusmenu_menuitem_property_get_value(mi, property); + if (value == NULL) return NULL; + if (G_VALUE_TYPE(value) != G_TYPE_STRING) return NULL; + return g_value_get_string(value); +} + +/** + dbusmenu_menuitem_property_get_value: + @mi: The #DbusmenuMenuitem to look for the property on. + @property: The property to grab. + + Look up a property on @mi and return the value of it if + it exits. #NULL will be returned if the property doesn't + exist. + + Return value: A GValue for the property. +*/ +const GValue * +dbusmenu_menuitem_property_get_value (DbusmenuMenuitem * mi, const gchar * property) +{ g_return_val_if_fail(DBUSMENU_IS_MENUITEM(mi), NULL); g_return_val_if_fail(property != NULL, NULL); DbusmenuMenuitemPrivate * priv = DBUSMENU_MENUITEM_GET_PRIVATE(mi); - return (const gchar *)g_hash_table_lookup(priv->properties, property); + return (const GValue *)g_hash_table_lookup(priv->properties, property); } + /** dbusmenu_menuitem_property_exit: @mi: The #DbusmenuMenuitem to look for the property on. diff --git a/libdbusmenu-glib/menuitem.h b/libdbusmenu-glib/menuitem.h index 27a86a2..f0615d7 100644 --- a/libdbusmenu-glib/menuitem.h +++ b/libdbusmenu-glib/menuitem.h @@ -94,7 +94,7 @@ struct _DbusmenuMenuitemClass GObjectClass parent_class; /* Signals */ - void (*property_changed) (gchar * property, gchar * value); + void (*property_changed) (gchar * property, GValue * value); void (*item_activated) (void); void (*child_added) (DbusmenuMenuitem * child, guint position); void (*child_removed) (DbusmenuMenuitem * child); @@ -129,7 +129,9 @@ DbusmenuMenuitem * dbusmenu_menuitem_child_find (DbusmenuMenuitem * mi, guint id DbusmenuMenuitem * dbusmenu_menuitem_find_id (DbusmenuMenuitem * mi, guint id); gboolean dbusmenu_menuitem_property_set (DbusmenuMenuitem * mi, const gchar * property, const gchar * value); +gboolean dbusmenu_menuitem_property_set_value (DbusmenuMenuitem * mi, const gchar * property, const GValue * value); const gchar * dbusmenu_menuitem_property_get (DbusmenuMenuitem * mi, const gchar * property); +const GValue * dbusmenu_menuitem_property_get_value (DbusmenuMenuitem * mi, const gchar * property); gboolean dbusmenu_menuitem_property_exist (DbusmenuMenuitem * mi, const gchar * property); GList * dbusmenu_menuitem_properties_list (DbusmenuMenuitem * mi) G_GNUC_WARN_UNUSED_RESULT; GHashTable * dbusmenu_menuitem_properties_copy (DbusmenuMenuitem * mi); diff --git a/libdbusmenu-glib/server.c b/libdbusmenu-glib/server.c index 73c21e2..f61b0fb 100644 --- a/libdbusmenu-glib/server.c +++ b/libdbusmenu-glib/server.c @@ -442,16 +442,6 @@ _dbusmenu_server_get_property (DbusmenuServer * server, guint id, gchar * proper return TRUE; } -static void -value_table (gpointer pkey, gpointer pvalue, gpointer phash) -{ - GValue * value = g_new0(GValue, 1); - g_value_init(value, G_TYPE_STRING); - g_value_set_string(value, (gchar *)pvalue); - g_hash_table_insert((GHashTable *)phash, g_strdup((gchar *)pkey), value); - return; -} - static gboolean _dbusmenu_server_get_properties (DbusmenuServer * server, guint id, GPtrArray * properties, GHashTable ** dict, GError ** error) { @@ -471,12 +461,6 @@ _dbusmenu_server_get_properties (DbusmenuServer * server, guint id, GPtrArray * *dict = dbusmenu_menuitem_properties_copy(mi); - GHashTable * newtable = g_hash_table_new(g_str_hash, g_str_equal); - g_hash_table_foreach(*dict, value_table, newtable); - g_hash_table_destroy(*dict); - - *dict = newtable; - return TRUE; } |