[glib] gsettings tool: use schema for listing keys



commit bb8eea6148fab965969c74e80a936864fc65e671
Author: Ryan Lortie <desrt desrt ca>
Date:   Wed Nov 19 12:42:10 2014 -0500

    gsettings tool: use schema for listing keys
    
    Use the newly added g_settings_schema_list_keys() API instead of
    g_settings_list_keys() in order to list keys.
    
    Doing this allows the 'list-keys' command to work without creating a
    GSettings object, which is more efficient.  It also means that we don't
    have to provide a (meaningless and ignored) path when listing keys on
    relocatable schemas.
    
    While we're at it, update the 'range' command not to require creation of
    a GSettings object, in a similar way.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=740308

 gio/gsettings-tool.c |   75 ++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 57 insertions(+), 18 deletions(-)
---
diff --git a/gio/gsettings-tool.c b/gio/gsettings-tool.c
index cf3ef19..55c24b0 100644
--- a/gio/gsettings-tool.c
+++ b/gio/gsettings-tool.c
@@ -155,7 +155,7 @@ gsettings_list_keys (void)
 {
   gchar **keys;
 
-  keys = g_settings_list_keys (global_settings);
+  keys = g_settings_schema_list_keys (global_schema);
   output_list (keys);
   g_strfreev (keys);
 }
@@ -201,12 +201,12 @@ static void
 enumerate (GSettings *settings)
 {
   gchar **keys;
-  gchar *schema;
+  GSettingsSchema *schema;
   gint i;
 
-  g_object_get (settings, "schema-id", &schema, NULL);
+  g_object_get (settings, "settings-schema", &schema, NULL);
 
-  keys = g_settings_list_keys (settings);
+  keys = g_settings_schema_list_keys (schema);
   for (i = 0; keys[i]; i++)
     {
       GVariant *value;
@@ -214,12 +214,12 @@ enumerate (GSettings *settings)
 
       value = g_settings_get_value (settings, keys[i]);
       printed = g_variant_print (value, TRUE);
-      g_print ("%s %s %s\n", schema, keys[i], printed);
+      g_print ("%s %s %s\n", g_settings_schema_get_id (schema), keys[i], printed);
       g_variant_unref (value);
       g_free (printed);
     }
 
-  g_free (schema);
+  g_settings_schema_unref (schema);
   g_strfreev (keys);
 }
 
@@ -344,15 +344,19 @@ gsettings_reset (void)
 static void
 reset_all_keys (GSettings *settings)
 {
+  GSettingsSchema *schema;
   gchar **keys;
   gint i;
 
-  keys = g_settings_list_keys (settings);
+  g_object_get (settings, "settings-schema", &schema, NULL);
+
+  keys = g_settings_schema_list_keys (schema);
   for (i = 0; keys[i]; i++)
     {
       g_settings_reset (settings, keys[i]);
     }
 
+  g_settings_schema_unref (schema);
   g_strfreev (keys);
 }
 
@@ -682,6 +686,7 @@ int
 main (int argc, char **argv)
 {
   void (* function) (void);
+  gboolean need_settings;
 
 #ifdef G_OS_WIN32
   gchar *tmp;
@@ -728,6 +733,8 @@ main (int argc, char **argv)
       argc -= 2;
     }
 
+  need_settings = TRUE;
+
   if (strcmp (argv[1], "help") == 0)
     return gsettings_help (TRUE, argv[2]);
 
@@ -741,7 +748,10 @@ main (int argc, char **argv)
     function = gsettings_list_relocatable_schemas;
 
   else if (argc == 3 && strcmp (argv[1], "list-keys") == 0)
-    function = gsettings_list_keys;
+    {
+      need_settings = FALSE;
+      function = gsettings_list_keys;
+    }
 
   else if (argc == 3 && strcmp (argv[1], "list-children") == 0)
     function = gsettings_list_children;
@@ -750,7 +760,10 @@ main (int argc, char **argv)
     function = gsettings_list_recursively;
 
   else if (argc == 4 && strcmp (argv[1], "range") == 0)
-    function = gsettings_range;
+    {
+      need_settings = FALSE;
+      function = gsettings_range;
+    }
 
   else if (argc == 4 && strcmp (argv[1], "get") == 0)
     function = gsettings_get;
@@ -786,19 +799,45 @@ main (int argc, char **argv)
       parts = g_strsplit (argv[2], ":", 2);
 
       global_schema = g_settings_schema_source_lookup (global_schema_source, parts[0], TRUE);
-      if (parts[1])
-        {
-          if (!check_relocatable_schema (global_schema, parts[0]) || !check_path (parts[1]))
-            return 1;
 
-          global_settings = g_settings_new_full (global_schema, NULL, parts[1]);
+      if (need_settings)
+        {
+          if (parts[1])
+            {
+              if (!check_relocatable_schema (global_schema, parts[0]) || !check_path (parts[1]))
+                return 1;
+
+              global_settings = g_settings_new_full (global_schema, NULL, parts[1]);
+            }
+          else
+            {
+              if (!check_schema (global_schema, parts[0]))
+                return 1;
+
+              global_settings = g_settings_new_full (global_schema, NULL, NULL);
+            }
         }
       else
         {
-          if (!check_schema (global_schema, parts[0]))
-            return 1;
-
-          global_settings = g_settings_new_full (global_schema, NULL, NULL);
+          /* If the user has given a path then we enforce that we have a
+           * relocatable schema, but if they didn't give a path then it
+           * doesn't matter what type of schema we have (since it's
+           * reasonable to ask for introspection information on a
+           * relocatable schema without having to give the path).
+           */
+          if (parts[1])
+            {
+              if (!check_relocatable_schema (global_schema, parts[0]) || !check_path (parts[1]))
+                return 1;
+            }
+          else
+            {
+              if (global_schema == NULL)
+                {
+                  g_printerr (_("No such schema '%s'\n"), parts[0]);
+                  return 1;
+                }
+            }
         }
 
       g_strfreev (parts);


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