← Back to team overview

ayatana-commits team mailing list archive

[Merge] lp:~ted/dbusmenu/variant-removal into lp:dbusmenu

 

Ted Gould has proposed merging lp:~ted/dbusmenu/variant-removal into lp:dbusmenu.

Requested reviews:
  DBus Menu Team (dbusmenu-team)

For more details, see:
https://code.launchpad.net/~ted/dbusmenu/variant-removal/+merge/45322

Makes it so that properties can be removed by setting the variant to NULL.  Also works for NULL strings.
-- 
https://code.launchpad.net/~ted/dbusmenu/variant-removal/+merge/45322
Your team ayatana-commits is subscribed to branch lp:dbusmenu.
=== 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:56:57 +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:56:57 +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;
 }
 


Follow ups