← Back to team overview

ayatana-commits team mailing list archive

[Branch ~dbusmenu-team/dbusmenu/trunk] Rev 176: Allow removing properties with NULL variants or strings.

 

Merge authors:
  Ted Gould (ted)
Related merge proposals:
  https://code.launchpad.net/~ted/dbusmenu/variant-removal/+merge/45322
  proposed by: Ted Gould (ted)
  review: Approve - Mikkel Kamstrup Erlandsen (kamstrup)
------------------------------------------------------------
revno: 176 [merge]
committer: Ted Gould <ted@xxxxxxxx>
branch nick: trunk
timestamp: Thu 2011-01-06 10:25:47 -0600
message:
  Allow removing properties with NULL variants or strings.
modified:
  libdbusmenu-glib/menuitem.c
  tests/test-glib-objects.c


--
lp:dbusmenu
https://code.launchpad.net/~dbusmenu-team/dbusmenu/trunk

Your team ayatana-commits is subscribed to branch lp:dbusmenu.
To unsubscribe from this branch go to https://code.launchpad.net/~dbusmenu-team/dbusmenu/trunk/+edit-subscription
=== modified file 'libdbusmenu-glib/menuitem.c'
--- libdbusmenu-glib/menuitem.c	2010-11-23 22:55:43 +0000
+++ libdbusmenu-glib/menuitem.c	2011-01-06 02:53:27 +0000
@@ -913,7 +913,10 @@
 gboolean
 dbusmenu_menuitem_property_set (DbusmenuMenuitem * mi, const gchar * property, const gchar * value)
 {
-	GVariant * variant = g_variant_new("s", value);
+	GVariant * variant = NULL;
+	if (value != NULL) {
+		variant = g_variant_new_string(value);
+	}
 	return dbusmenu_menuitem_property_set_variant(mi, property, variant);
 }
 
@@ -984,14 +987,22 @@
 
 	DbusmenuMenuitemPrivate * priv = DBUSMENU_MENUITEM_GET_PRIVATE(mi);
 
-	gchar * lprop = g_strdup(property);
-	g_variant_ref(value);
-
 	gboolean replaced = FALSE;
-	gpointer currentval = g_hash_table_lookup(priv->properties, lprop);
-	if (currentval == NULL || !g_variant_equal((GVariant*)currentval, value)) {
-		g_hash_table_replace(priv->properties, lprop, value);
-		replaced = TRUE;
+	gpointer currentval = g_hash_table_lookup(priv->properties, property);
+
+	if (value != NULL) {
+		gchar * lprop = g_strdup(property);
+		g_variant_ref(value);
+
+		if (currentval == NULL || !g_variant_equal((GVariant*)currentval, value)) {
+			g_hash_table_replace(priv->properties, lprop, value);
+			replaced = TRUE;
+		}
+	} else {
+		if (currentval != NULL) {
+			g_hash_table_remove(priv->properties, property);
+			replaced = TRUE;
+		}
 	}
 
 	/* NOTE: The actual value is invalid at this point
@@ -999,7 +1010,7 @@
 	   table.  But the fact that there was a value is
 	   the imporant part. */
 	if (currentval == NULL || replaced) {
-		g_signal_emit(G_OBJECT(mi), signals[PROPERTY_CHANGED], 0, lprop, value, TRUE);
+		g_signal_emit(G_OBJECT(mi), signals[PROPERTY_CHANGED], 0, property, value, TRUE);
 	}
 
 	return TRUE;

=== modified file 'tests/test-glib-objects.c'
--- tests/test-glib-objects.c	2010-11-18 15:00:45 +0000
+++ tests/test-glib-objects.c	2011-01-06 02:49:47 +0000
@@ -274,6 +274,37 @@
 	return;
 }
 
+/* Set and then remove a prop */
+static void
+test_object_menuitem_props_removal (void)
+{
+	/* Build a menu item */
+	DbusmenuMenuitem * item = dbusmenu_menuitem_new();
+
+	/* Test to make sure it's a happy object */
+	g_assert(item != NULL);
+
+	/* Set the property and ensure that it's set */
+	dbusmenu_menuitem_property_set_variant(item, "myprop", g_variant_new_int32(34));
+	g_assert(dbusmenu_menuitem_property_get_variant(item, "myprop") != NULL);
+
+	/* Remove the property and ensure it goes away */
+	dbusmenu_menuitem_property_set_variant(item, "myprop", NULL);
+	g_assert(dbusmenu_menuitem_property_get_variant(item, "myprop") == NULL);
+
+	/* Set the property again */
+	dbusmenu_menuitem_property_set_variant(item, "myprop", g_variant_new_int32(34));
+	g_assert(dbusmenu_menuitem_property_get_variant(item, "myprop") != NULL);
+
+	/* Remove the property with a NULL string */
+	dbusmenu_menuitem_property_set(item, "myprop", NULL);
+	g_assert(dbusmenu_menuitem_property_get_variant(item, "myprop") == NULL);
+
+	g_object_unref(item);
+
+	return;
+}
+
 /* Build the test suite */
 static void
 test_glib_objects_suite (void)
@@ -286,6 +317,7 @@
 	g_test_add_func ("/dbusmenu/glib/objects/menuitem/props_swap",    test_object_menuitem_props_swap);
 	g_test_add_func ("/dbusmenu/glib/objects/menuitem/props_signals", test_object_menuitem_props_signals);
 	g_test_add_func ("/dbusmenu/glib/objects/menuitem/props_boolstr", test_object_menuitem_props_boolstr);
+	g_test_add_func ("/dbusmenu/glib/objects/menuitem/props_removal", test_object_menuitem_props_removal);
 	return;
 }