gsettings-tool: Add --version into general usage
[platform/upstream/glib.git] / gio / gsettings-tool.c
index 993fa59..fd6d910 100644 (file)
 #include <string.h>
 #include <stdlib.h>
 
+#ifdef G_OS_WIN32
+#include "glib/glib-private.h"
+#endif
+
 static gboolean
 contained (const gchar * const *items,
            const gchar         *item)
@@ -145,6 +149,15 @@ output_list (const gchar * const *list)
 }
 
 static void
+gsettings_print_version (GSettings   *settings,
+                         const gchar *key,
+                         const gchar *value)
+{
+  g_print ("%d.%d.%d\n", glib_major_version, glib_minor_version,
+           glib_micro_version);
+}
+
+static void
 gsettings_list_schemas (GSettings   *settings,
                         const gchar *key,
                         const gchar *value)
@@ -218,7 +231,7 @@ enumerate (GSettings *settings)
   gchar *schema;
   gint i;
 
-  g_object_get (settings, "schema", &schema, NULL);
+  g_object_get (settings, "schema-id", &schema, NULL);
 
   keys = g_settings_list_keys (settings);
   for (i = 0; keys[i]; i++)
@@ -252,15 +265,10 @@ gsettings_list_recursively (GSettings   *settings,
       for (i = 0; children[i]; i++)
         {
           GSettings *child;
-          GSettingsSchema *schema;
 
           child = g_settings_get_child (settings, children[i]);
-          g_object_get (child, "settings-schema", &schema, NULL);
-
-          enumerate (child);
-
+          gsettings_list_recursively (child, NULL, NULL);
           g_object_unref (child);
-          g_settings_schema_unref (schema);
         }
 
       g_strfreev (children);
@@ -275,7 +283,7 @@ gsettings_list_recursively (GSettings   *settings,
       for (i = 0; schemas[i]; i++)
         {
           settings = g_settings_new (schemas[i]);
-          enumerate (settings);
+          gsettings_list_recursively (settings, NULL, NULL);
           g_object_unref (settings);
         }
     }
@@ -457,7 +465,6 @@ gsettings_set (GSettings   *settings,
 
   existing = g_settings_get_value (settings, key);
   type = g_variant_get_type (existing);
-  g_variant_unref (existing);
 
   new = g_variant_parse (type, value, NULL, NULL, &error);
 
@@ -490,6 +497,9 @@ gsettings_set (GSettings   *settings,
       new = g_variant_new_string (value);
     }
 
+  /* we're done with 'type' now, so we can free 'existing' */
+  g_variant_unref (existing);
+
   if (new == NULL)
     {
       g_printerr ("%s\n", error->message);
@@ -503,7 +513,11 @@ gsettings_set (GSettings   *settings,
       exit (1);
     }
 
-  g_settings_set_value (settings, key, new);
+  if (!g_settings_set_value (settings, key, new))
+    {
+      g_printerr (_("The key is not writable\n"));
+      exit (1);
+    }
 
   g_settings_sync ();
 
@@ -529,6 +543,12 @@ gsettings_help (gboolean     requested,
       synopsis = "[COMMAND]";
     }
 
+  else if (strcmp (command, "--version") == 0)
+    {
+      description = _("Print version information and exit");
+      synopsis = "";
+    }
+
   else if (strcmp (command, "list-schemas") == 0)
     {
       description = _("List the installed (non-relocatable) schemas");
@@ -614,6 +634,7 @@ gsettings_help (gboolean     requested,
     {
       g_string_append (string,
       _("Usage:\n"
+        "  gsettings --version\n"
         "  gsettings [--schemadir SCHEMADIR] COMMAND [ARGS...]\n"
         "\n"
         "Commands:\n"
@@ -688,7 +709,6 @@ main (int argc, char **argv)
   const gchar *key;
 
 #ifdef G_OS_WIN32
-  extern gchar *_glib_get_locale_dir (void);
   gchar *tmp;
 #endif
 
@@ -722,7 +742,7 @@ main (int argc, char **argv)
 
       if (schema_source == NULL)
         {
-          g_printerr ("Could not load schemas from %s: %s\n", argv[2], error->message);
+          g_printerr (_("Could not load schemas from %s: %s\n"), argv[2], error->message);
           g_clear_error (&error);
 
           return 1;
@@ -736,6 +756,9 @@ main (int argc, char **argv)
   if (strcmp (argv[1], "help") == 0)
     return gsettings_help (TRUE, argv[2]);
 
+  else if (argc == 2 && strcmp (argv[1], "--version") == 0)
+    function = gsettings_print_version;
+
   else if (argc == 2 && strcmp (argv[1], "list-schemas") == 0)
     function = gsettings_list_schemas;
 
@@ -775,8 +798,6 @@ main (int argc, char **argv)
   else
     return gsettings_help (FALSE, argv[1]);
 
-  g_type_init ();
-
   if (argc > 2)
     {
       gchar **parts;