[gnome-control-center] Simplify usage of g_variant_iter_loop



commit f1e0666eca8607c1f8e9fcd92c579c9dfe6580df
Author: Robert Ancell <robert ancell canonical com>
Date:   Thu Nov 21 21:06:36 2019 +1300

    Simplify usage of g_variant_iter_loop
    
    Access strings via pointers, which reduces copying and the chances of leaks.
    Simplify cases where string arrays were modified - no need to replace the value as it's no longer 
allocated.
    Always complete the loop so references are freed.

 panels/applications/cc-applications-panel.c    | 16 +++++++---------
 panels/camera/cc-camera-panel.c                |  8 ++++----
 panels/location/cc-location-panel.c            | 18 +++++-------------
 panels/microphone/cc-microphone-panel.c        | 18 +++++-------------
 panels/user-accounts/cc-login-history-dialog.c |  2 +-
 5 files changed, 22 insertions(+), 40 deletions(-)
---
diff --git a/panels/applications/cc-applications-panel.c b/panels/applications/cc-applications-panel.c
index 139da3ef9..91ec65979 100644
--- a/panels/applications/cc-applications-panel.c
+++ b/panels/applications/cc-applications-panel.c
@@ -170,8 +170,9 @@ get_portal_permissions (CcApplicationsPanel *self,
 {
   g_autoptr(GVariant) ret = NULL;
   g_autoptr(GVariantIter) iter = NULL;
-  g_autoptr(GVariant) val = NULL;
-  g_autofree gchar *key = NULL;
+  const gchar *key = NULL;
+  GStrv val;
+  GStrv result = NULL;
 
   ret = g_dbus_proxy_call_sync (self->perm_store,
                                 "Lookup",
@@ -182,16 +183,13 @@ get_portal_permissions (CcApplicationsPanel *self,
 
   g_variant_get (ret, "(a{sas}v)", &iter, NULL);
 
-  while (g_variant_iter_loop (iter, "{s@as}", &key, &val))
+  while (g_variant_iter_loop (iter, "{&s^a&s}", &key, &val))
     {
-      if (strcmp (key, app_id) == 0)
-        return g_variant_dup_strv (val, NULL);
+      if (strcmp (key, app_id) == 0 && result == NULL)
+        result = g_strdupv (val);
     }
 
-  val = NULL; /* freed by g_variant_iter_loop */
-  key = NULL;
-
-  return NULL;
+  return result;
 }
 
 static void
diff --git a/panels/camera/cc-camera-panel.c b/panels/camera/cc-camera-panel.c
index 4050590f5..9dee58d4d 100644
--- a/panels/camera/cc-camera-panel.c
+++ b/panels/camera/cc-camera-panel.c
@@ -100,7 +100,7 @@ on_camera_app_state_set (GtkSwitch *widget,
   CcCameraPanel *self;
   GVariantIter iter;
   GVariant *params;
-  gchar *key;
+  const gchar *key;
   gchar **value;
 
   self = data->self;
@@ -113,7 +113,7 @@ on_camera_app_state_set (GtkSwitch *widget,
 
   g_variant_iter_init (&iter, self->camera_apps_perms);
   g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
-  while (g_variant_iter_loop (&iter, "{s^as}", &key, &value))
+  while (g_variant_iter_loop (&iter, "{&s^a&s}", &key, &value))
     {
       gchar *tmp = NULL;
 
@@ -253,7 +253,7 @@ update_perm_store (CcCameraPanel *self,
                    GVariant      *permissions_data)
 {
   GVariantIter iter;
-  gchar *key;
+  const gchar *key;
   gchar **value;
 
   g_clear_pointer (&self->camera_apps_perms, g_variant_unref);
@@ -262,7 +262,7 @@ update_perm_store (CcCameraPanel *self,
   self->camera_apps_data = permissions_data;
 
   g_variant_iter_init (&iter, permissions);
-  while (g_variant_iter_loop (&iter, "{s^as}", &key, &value))
+  while (g_variant_iter_loop (&iter, "{&s^a&s}", &key, &value))
     {
       gboolean enabled;
 
diff --git a/panels/location/cc-location-panel.c b/panels/location/cc-location-panel.c
index 1f5cc7bce..dc8f3773f 100644
--- a/panels/location/cc-location-panel.c
+++ b/panels/location/cc-location-panel.c
@@ -101,7 +101,7 @@ on_location_app_state_set (GtkSwitch *widget,
   CcLocationPanel *self = data->self;
   GVariant *params;
   GVariantIter iter;
-  gchar *key;
+  const gchar *key;
   gchar **value;
   GVariantBuilder builder;
 
@@ -113,24 +113,16 @@ on_location_app_state_set (GtkSwitch *widget,
 
   g_variant_iter_init (&iter, self->location_apps_perms);
   g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
-  while (g_variant_iter_loop (&iter, "{s^as}", &key, &value))
+  while (g_variant_iter_loop (&iter, "{&s^a&s}", &key, &value))
     {
-      gchar *tmp = NULL;
-
       /* It's OK to drop the entry if it's not in expected format */
       if (g_strv_length (value) < 2)
         continue;
 
       if (g_strcmp0 (data->app_id, key) == 0)
-        {
-          tmp = value[0];
-          value[0] = state ? "EXACT" : "NONE";
-        }
+        value[0] = state ? "EXACT" : "NONE";
 
       g_variant_builder_add (&builder, "{s^as}", key, value);
-
-      if (tmp != NULL)
-        value[0] = tmp;
     }
 
   params = g_variant_new ("(sbsa{sas}v)",
@@ -266,7 +258,7 @@ update_perm_store (CcLocationPanel *self,
                    GVariant        *permissions_data)
 {
   GVariantIter iter;
-  gchar *key;
+  const gchar *key;
   gchar **value;
 
   g_clear_pointer (&self->location_apps_perms, g_variant_unref);
@@ -276,7 +268,7 @@ update_perm_store (CcLocationPanel *self,
   self->location_apps_data = permissions_data;
 
   g_variant_iter_init (&iter, permissions);
-  while (g_variant_iter_loop (&iter, "{s^as}", &key, &value))
+  while (g_variant_iter_loop (&iter, "{&s^a&s}", &key, &value))
     {
       gboolean enabled;
       gint64 last_used;
diff --git a/panels/microphone/cc-microphone-panel.c b/panels/microphone/cc-microphone-panel.c
index b02b9d8d4..7168786e2 100644
--- a/panels/microphone/cc-microphone-panel.c
+++ b/panels/microphone/cc-microphone-panel.c
@@ -102,7 +102,7 @@ on_microphone_app_state_set (GtkSwitch *widget,
   CcMicrophonePanel *self = data->self;
   GVariant *params;
   GVariantIter iter;
-  gchar *key;
+  const gchar *key;
   gchar **value;
   GVariantBuilder builder;
 
@@ -114,24 +114,16 @@ on_microphone_app_state_set (GtkSwitch *widget,
 
   g_variant_iter_init (&iter, self->microphone_apps_perms);
   g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
-  while (g_variant_iter_loop (&iter, "{s^as}", &key, &value))
+  while (g_variant_iter_loop (&iter, "{&s^a&s}", &key, &value))
     {
-      gchar *tmp = NULL;
-
       if (g_strv_length (value) != 1)
         /* It's OK to drop the entry if it's not in expected format */
         continue;
 
       if (g_strcmp0 (data->app_id, key) == 0)
-        {
-          tmp = value[0];
-          value[0] = state ? "yes" : "no";
-        }
+        value[0] = state ? "yes" : "no";
 
       g_variant_builder_add (&builder, "{s^as}", key, value);
-
-      if (tmp != NULL)
-        value[0] = tmp;
     }
 
   params = g_variant_new ("(sbsa{sas}v)",
@@ -255,7 +247,7 @@ update_perm_store (CcMicrophonePanel *self,
                    GVariant *permissions_data)
 {
   GVariantIter iter;
-  gchar *key;
+  const gchar *key;
   gchar **value;
 
   g_clear_pointer (&self->microphone_apps_perms, g_variant_unref);
@@ -264,7 +256,7 @@ update_perm_store (CcMicrophonePanel *self,
   self->microphone_apps_data = permissions_data;
 
   g_variant_iter_init (&iter, permissions);
-  while (g_variant_iter_loop (&iter, "{s^as}", &key, &value))
+  while (g_variant_iter_loop (&iter, "{&s^a&s}", &key, &value))
     {
       gboolean enabled;
 
diff --git a/panels/user-accounts/cc-login-history-dialog.c b/panels/user-accounts/cc-login-history-dialog.c
index 4ccf15dac..6670f757a 100644
--- a/panels/user-accounts/cc-login-history-dialog.c
+++ b/panels/user-accounts/cc-login-history-dialog.c
@@ -125,7 +125,7 @@ get_login_history (ActUser *user)
        value = act_user_get_login_history (user);
        g_variant_get ((GVariant *) value, "a(xxa{sv})", &iter);
        while (g_variant_iter_loop (iter, "(xxa{sv})", &history.login_time, &history.logout_time, &iter2)) {
-               while (g_variant_iter_loop (iter2, "{sv}", &key, &variant)) {
+               while (g_variant_iter_loop (iter2, "{&sv}", &key, &variant)) {
                        if (g_strcmp0 (key, "type") == 0) {
                                history.type = g_variant_get_string (variant, NULL);
                        }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]