[glib] Add a command to list keys and values recursively



commit 6298e88538fb5799432774574713af259701c735
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Oct 30 00:00:06 2010 -0400

    Add a command to list keys and values recursively
    
    This is similar to gconftool-2 -R, which is very handy
    for collecting information for bug reports, etc. It is now
    possible to say gsettings list-recursively org.foo.bar, and
    this will produce a list of schemas, keys and values for
    org.foo.bar and all its child and grandchild schemata,
    recursively.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=632571

 gio/gsettings-bash-completion.sh |    6 ++--
 gio/gsettings-tool.c             |   66 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 69 insertions(+), 3 deletions(-)
---
diff --git a/gio/gsettings-bash-completion.sh b/gio/gsettings-bash-completion.sh
index b19fe82..e2b8da4 100644
--- a/gio/gsettings-bash-completion.sh
+++ b/gio/gsettings-bash-completion.sh
@@ -9,15 +9,15 @@ __gsettings() {
 
   case "${COMP_CWORD}" in
     1)
-      choices=$'help \nlist-schemas\nlist-relocatable-schemas\nlist-keys \nlist-children \nget \nrange \nset \nreset \nwritable \nmonitor'
+      choices=$'help \nlist-schemas\nlist-relocatable-schemas\nlist-keys \nlist-children \nlist-recursively \nget \nrange \nset \nreset \nwritable \nmonitor'
       ;;
 
     2)
       case "${COMP_WORDS[1]}" in
         help)
-          choices=$'list-schemas\nlist-relocatable-schemas\nlist-keys\nlist-children\nget\nrange\nset\nreset\nwritable\nmonitor'
+          choices=$'list-schemas\nlist-relocatable-schemas\nlist-keys\nlist-children\nlist-recursively\nget\nrange\nset\nreset\nwritable\nmonitor'
           ;;
-        list-keys|list-children)
+        list-keys|list-children|list-recursively)
           choices="$(gsettings list-schemas)"$'\n'"$(gsettings list-relocatable-schemas | sed -e 's.$.:/.')"
           ;;
 
diff --git a/gio/gsettings-tool.c b/gio/gsettings-tool.c
index ca58a4b..5042d1c 100644
--- a/gio/gsettings-tool.c
+++ b/gio/gsettings-tool.c
@@ -210,6 +210,62 @@ gsettings_list_children (GSettings   *settings,
 }
 
 static void
+enumerate (GSettings *settings)
+{
+  gchar **keys;
+  gchar *schema;
+  gint i;
+
+  g_object_get (settings, "schema", &schema, NULL);
+
+  keys = g_settings_list_keys (settings);
+  for (i = 0; keys[i]; i++)
+    {
+      GVariant *value;
+      gchar *printed;
+
+      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_variant_unref (value);
+      g_free (printed);
+    }
+
+  g_free (schema);
+  g_strfreev (keys);
+}
+
+static void
+gsettings_list_recursively (GSettings   *settings,
+                            const gchar *key,
+                            const gchar *value)
+{
+  gchar **children;
+  gint i;
+
+  enumerate (settings);
+
+  children = g_settings_list_children (settings);
+
+  for (i = 0; children[i]; i++)
+    {
+      GSettings *child;
+      gchar *schema;
+
+      child = g_settings_get_child (settings, children[i]);
+      g_object_get (child, "schema", &schema, NULL);
+
+      if (is_schema (schema))
+        enumerate (child);
+
+      g_object_unref (child);
+      g_free (schema);
+    }
+
+  g_strfreev (children);
+}
+
+static void
 gsettings_range (GSettings   *settings,
                  const gchar *key,
                  const gchar *value)
@@ -401,6 +457,12 @@ gsettings_help (gboolean     requested,
       synopsis = N_("SCHEMA[:PATH]");
     }
 
+  else if (strcmp (command, "list-recursively") == 0)
+    {
+      description = _("List keys and values, recursively");
+      synopsis = N_("SCHEMA[:PATH]");
+    }
+
   else if (strcmp (command, "get") == 0)
     {
       description = _("Gets the value of KEY");
@@ -457,6 +519,7 @@ gsettings_help (gboolean     requested,
         "  list-relocatable-schemas  List relocatable schemas\n"
         "  list-keys                 List keys in a schema\n"
         "  list-children             List children of a schema\n"
+        "  list-recursively          List keys and values, recursively\n"
         "  range                     Queries the range of a key\n"
         "  get                       Get the value of a key\n"
         "  set                       Set the value of a key\n"
@@ -534,6 +597,9 @@ main (int argc, char **argv)
   else if (argc == 3 && strcmp (argv[1], "list-children") == 0)
     function = gsettings_list_children;
 
+  else if (argc == 3 && strcmp (argv[1], "list-recursively") == 0)
+    function = gsettings_list_recursively;
+
   else if (argc == 4 && strcmp (argv[1], "range") == 0)
     function = gsettings_range;
 



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