Merge lp:~ted/libdbusmenu/variant-removal into lp:libdbusmenu/0.5

Proposed by Ted Gould
Status: Merged
Merged at revision: 176
Proposed branch: lp:~ted/libdbusmenu/variant-removal
Merge into: lp:libdbusmenu/0.5
Diff against target: 103 lines (+52/-9)
2 files modified
libdbusmenu-glib/menuitem.c (+20/-9)
tests/test-glib-objects.c (+32/-0)
To merge this branch: bzr merge lp:~ted/libdbusmenu/variant-removal
Reviewer Review Type Date Requested Status
Mikkel Kamstrup Erlandsen (community) Approve
Review via email: mp+45322@code.launchpad.net

Description of the change

Makes it so that properties can be removed by setting the variant to NULL. Also works for NULL strings.

To post a comment you must log in.
Revision history for this message
Mikkel Kamstrup Erlandsen (kamstrup) wrote :

 review approve

Looks fine to me

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'libdbusmenu-glib/menuitem.c'
2--- libdbusmenu-glib/menuitem.c 2010-11-23 22:55:43 +0000
3+++ libdbusmenu-glib/menuitem.c 2011-01-06 02:56:57 +0000
4@@ -913,7 +913,10 @@
5 gboolean
6 dbusmenu_menuitem_property_set (DbusmenuMenuitem * mi, const gchar * property, const gchar * value)
7 {
8- GVariant * variant = g_variant_new("s", value);
9+ GVariant * variant = NULL;
10+ if (value != NULL) {
11+ variant = g_variant_new_string(value);
12+ }
13 return dbusmenu_menuitem_property_set_variant(mi, property, variant);
14 }
15
16@@ -984,14 +987,22 @@
17
18 DbusmenuMenuitemPrivate * priv = DBUSMENU_MENUITEM_GET_PRIVATE(mi);
19
20- gchar * lprop = g_strdup(property);
21- g_variant_ref(value);
22-
23 gboolean replaced = FALSE;
24- gpointer currentval = g_hash_table_lookup(priv->properties, lprop);
25- if (currentval == NULL || !g_variant_equal((GVariant*)currentval, value)) {
26- g_hash_table_replace(priv->properties, lprop, value);
27- replaced = TRUE;
28+ gpointer currentval = g_hash_table_lookup(priv->properties, property);
29+
30+ if (value != NULL) {
31+ gchar * lprop = g_strdup(property);
32+ g_variant_ref(value);
33+
34+ if (currentval == NULL || !g_variant_equal((GVariant*)currentval, value)) {
35+ g_hash_table_replace(priv->properties, lprop, value);
36+ replaced = TRUE;
37+ }
38+ } else {
39+ if (currentval != NULL) {
40+ g_hash_table_remove(priv->properties, property);
41+ replaced = TRUE;
42+ }
43 }
44
45 /* NOTE: The actual value is invalid at this point
46@@ -999,7 +1010,7 @@
47 table. But the fact that there was a value is
48 the imporant part. */
49 if (currentval == NULL || replaced) {
50- g_signal_emit(G_OBJECT(mi), signals[PROPERTY_CHANGED], 0, lprop, value, TRUE);
51+ g_signal_emit(G_OBJECT(mi), signals[PROPERTY_CHANGED], 0, property, value, TRUE);
52 }
53
54 return TRUE;
55
56=== modified file 'tests/test-glib-objects.c'
57--- tests/test-glib-objects.c 2010-11-18 15:00:45 +0000
58+++ tests/test-glib-objects.c 2011-01-06 02:56:57 +0000
59@@ -274,6 +274,37 @@
60 return;
61 }
62
63+/* Set and then remove a prop */
64+static void
65+test_object_menuitem_props_removal (void)
66+{
67+ /* Build a menu item */
68+ DbusmenuMenuitem * item = dbusmenu_menuitem_new();
69+
70+ /* Test to make sure it's a happy object */
71+ g_assert(item != NULL);
72+
73+ /* Set the property and ensure that it's set */
74+ dbusmenu_menuitem_property_set_variant(item, "myprop", g_variant_new_int32(34));
75+ g_assert(dbusmenu_menuitem_property_get_variant(item, "myprop") != NULL);
76+
77+ /* Remove the property and ensure it goes away */
78+ dbusmenu_menuitem_property_set_variant(item, "myprop", NULL);
79+ g_assert(dbusmenu_menuitem_property_get_variant(item, "myprop") == NULL);
80+
81+ /* Set the property again */
82+ dbusmenu_menuitem_property_set_variant(item, "myprop", g_variant_new_int32(34));
83+ g_assert(dbusmenu_menuitem_property_get_variant(item, "myprop") != NULL);
84+
85+ /* Remove the property with a NULL string */
86+ dbusmenu_menuitem_property_set(item, "myprop", NULL);
87+ g_assert(dbusmenu_menuitem_property_get_variant(item, "myprop") == NULL);
88+
89+ g_object_unref(item);
90+
91+ return;
92+}
93+
94 /* Build the test suite */
95 static void
96 test_glib_objects_suite (void)
97@@ -286,6 +317,7 @@
98 g_test_add_func ("/dbusmenu/glib/objects/menuitem/props_swap", test_object_menuitem_props_swap);
99 g_test_add_func ("/dbusmenu/glib/objects/menuitem/props_signals", test_object_menuitem_props_signals);
100 g_test_add_func ("/dbusmenu/glib/objects/menuitem/props_boolstr", test_object_menuitem_props_boolstr);
101+ g_test_add_func ("/dbusmenu/glib/objects/menuitem/props_removal", test_object_menuitem_props_removal);
102 return;
103 }
104

Subscribers

People subscribed via source and target branches