Merge lp:~hikiko/unity-control-center/unity-control-center.replaced-gvariant-array-with-gvariant-builder into lp:unity-control-center

Proposed by Eleni Maria Stea
Status: Merged
Approved by: Sebastien Bacher
Approved revision: 12736
Merged at revision: 12736
Proposed branch: lp:~hikiko/unity-control-center/unity-control-center.replaced-gvariant-array-with-gvariant-builder
Merge into: lp:unity-control-center
Diff against target: 124 lines (+24/-46)
1 file modified
panels/display/cc-display-panel.c (+24/-46)
To merge this branch: bzr merge lp:~hikiko/unity-control-center/unity-control-center.replaced-gvariant-array-with-gvariant-builder
Reviewer Review Type Date Requested Status
PS Jenkins bot (community) continuous-integration Approve
Allison Karlitskaya (community) Approve
Sebastien Bacher Pending
Review via email: mp+208313@code.launchpad.net

Commit message

In display add_dict_entry:
replaced the gvariant arrays with gvariant builder to simplify the code

Description of the change

In display add_dict_entry:
replaced the gvariant arrays with gvariant builder to simplify the code

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Allison Karlitskaya (desrt) wrote :

+ if (!key || !value)

 This kind of checking is normally done with g_return_val_if_fail() and common practice here is to return NULL (since typically you want things like this to cause crashes rather than be silently ignored). In any case, you should definitely use g_critical() instead of g_warning() for "programmer error" type situations. g_warning() is more like "something unexpected happened, but it was not the programmer's fault" -- like an invalidly-formatted D-Bus message or something.

You also attempt to deal with a NULL dictionary. That should not be possible. g_settings_get_value() will always return non-NULL (assuming you give it a valid GSettings object and key).

36 if (!dict)
37 {
38 + GVariant *pair[2];
39 + GVariant *dict_entry;
40 +
41 + pair[0] = g_variant_new_string (key);
42 + pair[1] = g_variant_new_int32 (value);
43 + dict_entry = g_variant_new_dict_entry (pair[0], pair[1]);
44 dict = g_variant_new_array (G_VARIANT_TYPE_DICT_ENTRY, &dict_entry, 1);
45 +
46 return dict;
47 }

As per the comment above (dict can never be NULL) you should delete this, but (just as an aside) note that a much easier way to do the same would be:

  return g_variant_new_parsed ("{ %s: %i }", key, value);

You should change the cases of:

  g_settings_get (..., "@a{si}", &value);

to

  value = g_settings_get_value(...);

and similarly:

  g_settings_set (..., "@a{si}", value);

to

  g_settings_set_value (..., value);

because they are more typesafe (and also more efficient).

  add_dict_entry (dict, monitor_name, value);
  g_settings_set (self->priv->desktop_settings, "scale-factor", "@a{si}", dict);

This code doesn't work because 'dict' is not updated in place, but rather a new value is returned (which you ignore). You must use the new value. You must also free the old value when you're done with it, so you're probably going to need two variables (unless you nest the call to add_dict_entry() inside of the GSettings call).

Floating GVariant references only come from functions named g_Variant_new_* in the name or g_variant_*_end(), so your call to g_variant_builder_end() gives you a floating reference which will be taken care of automatically when you pass it to g_settings_set_value(). The same is not true of the value you get from GSettings. You must free this one for yourself.

    g_settings_get (self->priv->desktop_settings, "scale-factor", "@a{si}", &dict);
    dict = add_dict_entry (dict, monitor_name, value);
    g_settings_set (self->priv->desktop_settings, "scale-factor", "@a{si}", dict);

This one does the update properly, but it leaks the old value of dict (and should be changed to g_settings_get_value/g_settings_set_value as mentioned above).

review: Needs Fixing
Revision history for this message
Eleni Maria Stea (hikiko) wrote :

fixed :)

Revision history for this message
Allison Karlitskaya (desrt) wrote :

This looks great to me. Assuming you tested it, +1.

review: Approve
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'panels/display/cc-display-panel.c'
2--- panels/display/cc-display-panel.c 2014-02-24 16:53:59 +0000
3+++ panels/display/cc-display-panel.c 2014-02-26 15:59:25 +0000
4@@ -566,49 +566,23 @@
5 static GVariant*
6 add_dict_entry (GVariant *dict, const char *key, int value)
7 {
8- GVariant *dict_entry;
9- GVariant **dict_entries;
10- GVariant *tmp;
11- GVariant *pair[2];
12+ GVariantBuilder builder;
13 GVariantIter iter;
14- unsigned long sz;
15- char str[512];
16- int i = 0;
17-
18- pair[0] = g_variant_new_string (key);
19- pair[1] = g_variant_new_int32 (value);
20- dict_entry = g_variant_new_dict_entry (pair[0], pair[1]);
21-
22- if (!dict)
23- {
24- dict = g_variant_new_array (G_VARIANT_TYPE_DICT_ENTRY, &dict_entry, 1);
25- return dict;
26- }
27-
28- sz = g_variant_n_children (dict);
29- dict_entries = malloc (sizeof(GVariant*) * (sz + 1));
30-
31+
32+ const gchar *k;
33+ guint32 v;
34+
35+ g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{si}"));
36 g_variant_iter_init (&iter, dict);
37- while (tmp = g_variant_iter_next_value (&iter))
38+
39+ while (g_variant_iter_next (&iter, "{&si}", &k, &v))
40 {
41- int val = 0; char *str;
42- g_variant_get_child (tmp, 0, "s", &str);
43- if (!str)
44- {
45- g_warning ("Invalid dictionary entry.\n");
46- break;
47- }
48-
49- if (strcmp (str, key) != 0)
50- {
51- dict_entries[i++] = tmp;
52- }
53- g_free(str);
54+ if (!g_str_equal (k, key))
55+ g_variant_builder_add (&builder, "{si}", k, v);
56 }
57- dict_entries[i++] = dict_entry;
58- dict = g_variant_new_array (NULL, dict_entries, i);
59+ g_variant_builder_add (&builder, "{si}", key, value);
60
61- return dict;
62+ return g_variant_builder_end (&builder);
63 }
64
65 static void
66@@ -618,12 +592,13 @@
67 float t;
68
69 GVariant *dict;
70+ GVariant *new_dict;
71
72 GtkAdjustment *adj = gtk_range_get_adjustment (GTK_RANGE(self->priv->ui_scale));
73 const char *monitor_name = gnome_rr_output_info_get_name (self->priv->current_output);
74 if (!monitor_name)
75 {
76- g_warning("Failed to get monitor name.\n");
77+ g_warning("Failed to get monitor name.");
78 return;
79 }
80
81@@ -632,15 +607,16 @@
82 gtk_scale_set_digits (GTK_SCALE(self->priv->ui_scale), 0);
83 gtk_scale_add_mark (GTK_SCALE(self->priv->ui_scale), 8, GTK_POS_TOP, NULL);
84
85- g_settings_get (self->priv->desktop_settings, "scale-factor", "@a{si}", &dict);
86+ dict = g_settings_get_value (self->priv->desktop_settings, "scale-factor");
87 if (!g_variant_lookup (dict, monitor_name, "i", &value))
88 {
89 value = 8;
90 self->priv->ui_prev_scale = value;
91 }
92- add_dict_entry (dict, monitor_name, value);
93- g_settings_set (self->priv->desktop_settings, "scale-factor", "@a{si}", dict);
94+ new_dict = add_dict_entry (dict, monitor_name, value);
95+ g_settings_set_value (self->priv->desktop_settings, "scale-factor", new_dict);
96 gtk_adjustment_set_value (adj, value);
97+ g_variant_unref (dict);
98 }
99
100 static int
101@@ -1050,17 +1026,19 @@
102 if (value != self->priv->ui_prev_scale)
103 {
104 GVariant *dict;
105+ GVariant *new_dict;
106
107 monitor_name = gnome_rr_output_info_get_name (self->priv->current_output);
108 if (!monitor_name)
109 {
110- g_warning("Failed to get monitor name.\n");
111+ g_warning("Failed to get monitor name.");
112 return FALSE;
113 }
114
115- g_settings_get (self->priv->desktop_settings, "scale-factor", "@a{si}", &dict);
116- dict = add_dict_entry (dict, monitor_name, value);
117- g_settings_set (self->priv->desktop_settings, "scale-factor", "@a{si}", dict);
118+ dict = g_settings_get_value (self->priv->desktop_settings, "scale-factor");
119+ new_dict = add_dict_entry (dict, monitor_name, value);
120+ g_settings_set_value (self->priv->desktop_settings, "scale-factor", new_dict);
121+ g_variant_unref (dict);
122 }
123
124 return FALSE; /* gtk should still process this event */

Subscribers

People subscribed via source and target branches