Tizen 2.1 base
[platform/upstream/glib2.0.git] / gio / gsettings-tool.c
1 /*
2  * Copyright © 2010 Codethink Limited
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the licence, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  *
19  * Author: Ryan Lortie <desrt@desrt.ca>
20  */
21
22 #include "config.h"
23
24 #include <gio/gio.h>
25 #include <gi18n.h>
26 #include <locale.h>
27 #include <string.h>
28 #include <stdlib.h>
29
30 static gboolean
31 contained (const gchar * const *items,
32            const gchar         *item)
33 {
34   while (*items)
35     if (strcmp (*items++, item) == 0)
36       return TRUE;
37
38   return FALSE;
39 }
40
41 static gboolean
42 is_relocatable_schema (GSettingsSchema *schema)
43 {
44   return g_settings_schema_get_path (schema) == NULL;
45 }
46
47 static gboolean
48 check_relocatable_schema (GSettingsSchema *schema,
49                           const gchar     *schema_id)
50 {
51   if (schema == NULL)
52     {
53       g_printerr (_("No such schema '%s'\n"), schema_id);
54       return FALSE;
55     }
56
57   if (!is_relocatable_schema (schema))
58     {
59       g_printerr (_("Schema '%s' is not relocatable "
60                     "(path must not be specified)\n"),
61                   schema_id);
62       return FALSE;
63     }
64
65   return TRUE;
66 }
67
68 static gboolean
69 check_schema (GSettingsSchema *schema,
70               const gchar *schema_id)
71 {
72   if (schema == NULL)
73     {
74       g_printerr (_("No such schema '%s'\n"), schema_id);
75       return FALSE;
76     }
77
78   if (is_relocatable_schema (schema))
79     {
80       g_printerr (_("Schema '%s' is relocatable "
81                     "(path must be specified)\n"),
82                   schema_id);
83       return FALSE;
84     }
85
86   return TRUE;
87 }
88
89 static gboolean
90 check_path (const gchar *path)
91 {
92   if (path[0] == '\0')
93     {
94       g_printerr (_("Empty path given.\n"));
95       return FALSE;
96     }
97
98   if (path[0] != '/')
99     {
100       g_printerr (_("Path must begin with a slash (/)\n"));
101       return FALSE;
102     }
103
104   if (!g_str_has_suffix (path, "/"))
105     {
106       g_printerr (_("Path must end with a slash (/)\n"));
107       return FALSE;
108     }
109
110   if (strstr (path, "//"))
111     {
112       g_printerr (_("Path must not contain two adjacent slashes (//)\n"));
113       return FALSE;
114     }
115
116   return TRUE;
117 }
118
119 static gboolean
120 check_key (GSettings   *settings,
121            const gchar *key)
122 {
123   gboolean good;
124   gchar **keys;
125
126   keys = g_settings_list_keys (settings);
127   good = contained ((const gchar **) keys, key);
128   g_strfreev (keys);
129
130   if (good)
131     return TRUE;
132
133   g_printerr (_("No such key '%s'\n"), key);
134
135   return FALSE;
136 }
137
138 static void
139 output_list (const gchar * const *list)
140 {
141   gint i;
142
143   for (i = 0; list[i]; i++)
144     g_print ("%s\n", list[i]);
145 }
146
147 static void
148 gsettings_list_schemas (GSettings   *settings,
149                         const gchar *key,
150                         const gchar *value)
151 {
152   output_list (g_settings_list_schemas ());
153 }
154
155 static void
156 gsettings_list_relocatable_schemas (GSettings   *settings,
157                                     const gchar *key,
158                                     const gchar *value)
159 {
160   output_list (g_settings_list_relocatable_schemas ());
161 }
162
163 static void
164 gsettings_list_keys (GSettings   *settings,
165                      const gchar *key,
166                      const gchar *value)
167 {
168   gchar **keys;
169
170   keys = g_settings_list_keys (settings);
171   output_list ((const gchar **) keys);
172   g_strfreev (keys);
173 }
174
175 static void
176 gsettings_list_children (GSettings   *settings,
177                          const gchar *key,
178                          const gchar *value)
179 {
180   gchar **children;
181   gint max = 0;
182   gint i;
183
184   children = g_settings_list_children (settings);
185   for (i = 0; children[i]; i++)
186     if (strlen (children[i]) > max)
187       max = strlen (children[i]);
188
189   for (i = 0; children[i]; i++)
190     {
191       GSettings *child;
192       GSettingsSchema *schema;
193       gchar *path;
194
195       child = g_settings_get_child (settings, children[i]);
196       g_object_get (child,
197                     "settings-schema", &schema,
198                     "path", &path,
199                     NULL);
200
201       if (g_settings_schema_get_path (schema) != NULL)
202         g_print ("%-*s   %s\n", max, children[i], g_settings_schema_get_id (schema));
203       else
204         g_print ("%-*s   %s:%s\n", max, children[i], g_settings_schema_get_id (schema), path);
205
206       g_object_unref (child);
207       g_settings_schema_unref (schema);
208       g_free (path);
209     }
210
211   g_strfreev (children);
212 }
213
214 static void
215 enumerate (GSettings *settings)
216 {
217   gchar **keys;
218   gchar *schema;
219   gint i;
220
221   g_object_get (settings, "schema", &schema, NULL);
222
223   keys = g_settings_list_keys (settings);
224   for (i = 0; keys[i]; i++)
225     {
226       GVariant *value;
227       gchar *printed;
228
229       value = g_settings_get_value (settings, keys[i]);
230       printed = g_variant_print (value, TRUE);
231       g_print ("%s %s %s\n", schema, keys[i], printed);
232       g_variant_unref (value);
233       g_free (printed);
234     }
235
236   g_free (schema);
237   g_strfreev (keys);
238 }
239
240 static void
241 gsettings_list_recursively (GSettings   *settings,
242                             const gchar *key,
243                             const gchar *value)
244 {
245   if (settings)
246     {
247       gchar **children;
248       gint i;
249
250       enumerate (settings);
251       children = g_settings_list_children (settings);
252       for (i = 0; children[i]; i++)
253         {
254           GSettings *child;
255           GSettingsSchema *schema;
256
257           child = g_settings_get_child (settings, children[i]);
258           g_object_get (child, "settings-schema", &schema, NULL);
259
260           enumerate (child);
261
262           g_object_unref (child);
263           g_settings_schema_unref (schema);
264         }
265
266       g_strfreev (children);
267     }
268   else
269     {
270       const gchar * const *schemas;
271       gint i;
272
273       schemas = g_settings_list_schemas ();
274
275       for (i = 0; schemas[i]; i++)
276         {
277           settings = g_settings_new (schemas[i]);
278           enumerate (settings);
279           g_object_unref (settings);
280         }
281     }
282 }
283
284 static void
285 gsettings_range (GSettings   *settings,
286                  const gchar *key,
287                  const gchar *value)
288 {
289   GVariant *range, *detail;
290   const gchar *type;
291
292   range = g_settings_get_range (settings, key);
293   g_variant_get (range, "(&sv)", &type, &detail);
294
295   if (strcmp (type, "type") == 0)
296     g_print ("type %s\n", g_variant_get_type_string (detail) + 1);
297
298   else if (strcmp (type, "range") == 0)
299     {
300       GVariant *min, *max;
301       gchar *smin, *smax;
302
303       g_variant_get (detail, "(**)", &min, &max);
304       smin = g_variant_print (min, FALSE);
305       smax = g_variant_print (max, FALSE);
306
307       g_print ("range %s %s %s\n",
308                g_variant_get_type_string (min), smin, smax);
309       g_variant_unref (min);
310       g_variant_unref (max);
311       g_free (smin);
312       g_free (smax);
313     }
314
315   else if (strcmp (type, "enum") == 0 || strcmp (type, "flags") == 0)
316     {
317       GVariantIter iter;
318       GVariant *item;
319
320       g_print ("%s\n", type);
321
322       g_variant_iter_init (&iter, detail);
323       while (g_variant_iter_loop (&iter, "*", &item))
324         {
325           gchar *printed;
326
327           printed = g_variant_print (item, FALSE);
328           g_print ("%s\n", printed);
329           g_free (printed);
330         }
331     }
332
333   g_variant_unref (detail);
334   g_variant_unref (range);
335 }
336
337 static void
338 gsettings_get (GSettings   *settings,
339                const gchar *key,
340                const gchar *value_)
341 {
342   GVariant *value;
343   gchar *printed;
344
345   value = g_settings_get_value (settings, key);
346   printed = g_variant_print (value, TRUE);
347   g_print ("%s\n", printed);
348   g_variant_unref (value);
349   g_free (printed);
350 }
351
352 static void
353 gsettings_reset (GSettings   *settings,
354                  const gchar *key,
355                  const gchar *value)
356 {
357   g_settings_reset (settings, key);
358   g_settings_sync ();
359 }
360
361 static void
362 reset_all_keys (GSettings   *settings)
363 {
364   gchar **keys;
365   gint i;
366
367   keys = g_settings_list_keys (settings);
368   for (i = 0; keys[i]; i++)
369     {
370       g_settings_reset (settings, keys[i]);
371     }
372
373   g_strfreev (keys);
374 }
375
376 static void
377 gsettings_reset_recursively (GSettings   *settings,
378                              const gchar *key,
379                              const gchar *value)
380 {
381   gchar **children;
382   gint i;
383
384   g_settings_delay (settings);
385
386   reset_all_keys (settings);
387   children = g_settings_list_children (settings);
388   for (i = 0; children[i]; i++)
389     {
390       GSettings *child;
391       child = g_settings_get_child (settings, children[i]);
392
393       reset_all_keys (child);
394
395       g_object_unref (child);
396     }
397
398   g_strfreev (children);
399
400   g_settings_apply (settings);
401   g_settings_sync ();
402 }
403
404 static void
405 gsettings_writable (GSettings   *settings,
406                     const gchar *key,
407                     const gchar *value)
408 {
409   g_print ("%s\n",
410            g_settings_is_writable (settings, key) ?
411            "true" : "false");
412 }
413
414 static void
415 value_changed (GSettings   *settings,
416                const gchar *key,
417                gpointer     user_data)
418 {
419   GVariant *value;
420   gchar *printed;
421
422   value = g_settings_get_value (settings, key);
423   printed = g_variant_print (value, TRUE);
424   g_print ("%s: %s\n", key, printed);
425   g_variant_unref (value);
426   g_free (printed);
427 }
428
429 static void
430 gsettings_monitor (GSettings   *settings,
431                    const gchar *key,
432                    const gchar *value)
433 {
434   if (key)
435     {
436       gchar *name;
437
438       name = g_strdup_printf ("changed::%s", key);
439       g_signal_connect (settings, name, G_CALLBACK (value_changed), NULL);
440     }
441   else
442     g_signal_connect (settings, "changed", G_CALLBACK (value_changed), NULL);
443
444   g_main_loop_run (g_main_loop_new (NULL, FALSE));
445 }
446
447 static void
448 gsettings_set (GSettings   *settings,
449                const gchar *key,
450                const gchar *value)
451 {
452   const GVariantType *type;
453   GError *error = NULL;
454   GVariant *existing;
455   GVariant *new;
456   gchar *freeme = NULL;
457
458   existing = g_settings_get_value (settings, key);
459   type = g_variant_get_type (existing);
460
461   new = g_variant_parse (type, value, NULL, NULL, &error);
462
463   /* If that didn't work and the type is string then we should assume
464    * that the user is just trying to set a string directly and forgot
465    * the quotes (or had them consumed by the shell).
466    *
467    * If the user started with a quote then we assume that some deeper
468    * problem is at play and we want the failure in that case.
469    *
470    * Consider:
471    *
472    *   gsettings set x.y.z key "'i don't expect this to work'"
473    *
474    * Note that we should not just add quotes and try parsing again, but
475    * rather assume that the user is providing us with a bare string.
476    * Assume we added single quotes, then consider this case:
477    *
478    *   gsettings set x.y.z key "i'd expect this to work"
479    *
480    * A similar example could be given for double quotes.
481    *
482    * Avoid that whole mess by just using g_variant_new_string().
483    */
484   if (new == NULL &&
485       g_variant_type_equal (type, G_VARIANT_TYPE_STRING) &&
486       value[0] != '\'' && value[0] != '"')
487     {
488       g_clear_error (&error);
489       new = g_variant_new_string (value);
490     }
491
492   /* we're done with 'type' now, so we can free 'existing' */
493   g_variant_unref (existing);
494
495   if (new == NULL)
496     {
497       g_printerr ("%s\n", error->message);
498       exit (1);
499     }
500
501   if (!g_settings_range_check (settings, key, new))
502     {
503       g_printerr (_("The provided value is outside of the valid range\n"));
504       g_variant_unref (new);
505       exit (1);
506     }
507
508   g_settings_set_value (settings, key, new);
509
510   g_settings_sync ();
511
512   g_free (freeme);
513 }
514
515 static int
516 gsettings_help (gboolean     requested,
517                 const gchar *command)
518 {
519   const gchar *description;
520   const gchar *synopsis;
521   GString *string;
522
523   string = g_string_new (NULL);
524
525   if (command == NULL)
526     ;
527
528   else if (strcmp (command, "help") == 0)
529     {
530       description = _("Print help");
531       synopsis = "[COMMAND]";
532     }
533
534   else if (strcmp (command, "list-schemas") == 0)
535     {
536       description = _("List the installed (non-relocatable) schemas");
537       synopsis = "";
538     }
539
540   else if (strcmp (command, "list-relocatable-schemas") == 0)
541     {
542       description = _("List the installed relocatable schemas");
543       synopsis = "";
544     }
545
546   else if (strcmp (command, "list-keys") == 0)
547     {
548       description = _("List the keys in SCHEMA");
549       synopsis = N_("SCHEMA[:PATH]");
550     }
551
552   else if (strcmp (command, "list-children") == 0)
553     {
554       description = _("List the children of SCHEMA");
555       synopsis = N_("SCHEMA[:PATH]");
556     }
557
558   else if (strcmp (command, "list-recursively") == 0)
559     {
560       description = _("List keys and values, recursively\n"
561                       "If no SCHEMA is given, list all keys\n");
562       synopsis = N_("[SCHEMA[:PATH]]");
563     }
564
565   else if (strcmp (command, "get") == 0)
566     {
567       description = _("Get the value of KEY");
568       synopsis = N_("SCHEMA[:PATH] KEY");
569     }
570
571   else if (strcmp (command, "range") == 0)
572     {
573       description = _("Query the range of valid values for KEY");
574       synopsis = N_("SCHEMA[:PATH] KEY");
575     }
576
577   else if (strcmp (command, "set") == 0)
578     {
579       description = _("Set the value of KEY to VALUE");
580       synopsis = N_("SCHEMA[:PATH] KEY VALUE");
581     }
582
583   else if (strcmp (command, "reset") == 0)
584     {
585       description = _("Reset KEY to its default value");
586       synopsis = N_("SCHEMA[:PATH] KEY");
587     }
588
589   else if (strcmp (command, "reset-recursively") == 0)
590     {
591       description = _("Reset all keys in SCHEMA to their defaults");
592       synopsis = N_("SCHEMA[:PATH]");
593     }
594
595   else if (strcmp (command, "writable") == 0)
596     {
597       description = _("Check if KEY is writable");
598       synopsis = N_("SCHEMA[:PATH] KEY");
599     }
600
601   else if (strcmp (command, "monitor") == 0)
602     {
603       description = _("Monitor KEY for changes.\n"
604                     "If no KEY is specified, monitor all keys in SCHEMA.\n"
605                     "Use ^C to stop monitoring.\n");
606       synopsis = N_("SCHEMA[:PATH] [KEY]");
607     }
608   else
609     {
610       g_string_printf (string, _("Unknown command %s\n\n"), command);
611       requested = FALSE;
612       command = NULL;
613     }
614
615   if (command == NULL)
616     {
617       g_string_append (string,
618       _("Usage:\n"
619         "  gsettings [--schemadir SCHEMADIR] COMMAND [ARGS...]\n"
620         "\n"
621         "Commands:\n"
622         "  help                      Show this information\n"
623         "  list-schemas              List installed schemas\n"
624         "  list-relocatable-schemas  List relocatable schemas\n"
625         "  list-keys                 List keys in a schema\n"
626         "  list-children             List children of a schema\n"
627         "  list-recursively          List keys and values, recursively\n"
628         "  range                     Queries the range of a key\n"
629         "  get                       Get the value of a key\n"
630         "  set                       Set the value of a key\n"
631         "  reset                     Reset the value of a key\n"
632         "  reset-recursively         Reset all values in a given schema\n"
633         "  writable                  Check if a key is writable\n"
634         "  monitor                   Watch for changes\n"
635         "\n"
636         "Use 'gsettings help COMMAND' to get detailed help.\n\n"));
637     }
638   else
639     {
640       g_string_append_printf (string, _("Usage:\n  gsettings [--schemadir SCHEMADIR] %s %s\n\n%s\n\n"),
641                               command, synopsis[0] ? _(synopsis) : "", description);
642
643       g_string_append (string, _("Arguments:\n"));
644
645       g_string_append (string,
646                        _("  SCHEMADIR A directory to search for additional schemas\n"));
647
648       if (strstr (synopsis, "[COMMAND]"))
649         g_string_append (string,
650                        _("  COMMAND   The (optional) command to explain\n"));
651
652       else if (strstr (synopsis, "SCHEMA"))
653         g_string_append (string,
654                        _("  SCHEMA    The name of the schema\n"
655                          "  PATH      The path, for relocatable schemas\n"));
656
657       if (strstr (synopsis, "[KEY]"))
658         g_string_append (string,
659                        _("  KEY       The (optional) key within the schema\n"));
660
661       else if (strstr (synopsis, "KEY"))
662         g_string_append (string,
663                        _("  KEY       The key within the schema\n"));
664
665       if (strstr (synopsis, "VALUE"))
666         g_string_append (string,
667                        _("  VALUE     The value to set\n"));
668
669       g_string_append (string, "\n");
670     }
671
672   if (requested)
673     g_print ("%s", string->str);
674   else
675     g_printerr ("%s\n", string->str);
676
677   g_string_free (string, TRUE);
678
679   return requested ? 0 : 1;
680 }
681
682
683 int
684 main (int argc, char **argv)
685 {
686   void (* function) (GSettings *, const gchar *, const gchar *);
687   GSettingsSchemaSource *schema_source;
688   GSettingsSchema *schema;
689   GSettings *settings;
690   const gchar *key;
691
692 #ifdef G_OS_WIN32
693   extern gchar *_glib_get_locale_dir (void);
694   gchar *tmp;
695 #endif
696
697   setlocale (LC_ALL, "");
698   textdomain (GETTEXT_PACKAGE);
699
700 #ifdef G_OS_WIN32
701   tmp = _glib_get_locale_dir ();
702   bindtextdomain (GETTEXT_PACKAGE, tmp);
703   g_free (tmp);
704 #else
705   bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
706 #endif
707
708 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
709   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
710 #endif
711
712   if (argc < 2)
713     return gsettings_help (FALSE, NULL);
714
715   schema_source = g_settings_schema_source_ref (g_settings_schema_source_get_default ());
716
717   if (argc > 3 && g_str_equal (argv[1], "--schemadir"))
718     {
719       GSettingsSchemaSource *parent = schema_source;
720       GError *error = NULL;
721
722       schema_source = g_settings_schema_source_new_from_directory (argv[2], parent, FALSE, &error);
723       g_settings_schema_source_unref (parent);
724
725       if (schema_source == NULL)
726         {
727           g_printerr ("Could not load schemas from %s: %s\n", argv[2], error->message);
728           g_clear_error (&error);
729
730           return 1;
731         }
732
733       /* shift remaining arguments (not correct wrt argv[0], but doesn't matter) */
734       argv = argv + 2;
735       argc -= 2;
736     }
737
738   if (strcmp (argv[1], "help") == 0)
739     return gsettings_help (TRUE, argv[2]);
740
741   else if (argc == 2 && strcmp (argv[1], "list-schemas") == 0)
742     function = gsettings_list_schemas;
743
744   else if (argc == 2 && strcmp (argv[1], "list-relocatable-schemas") == 0)
745     function = gsettings_list_relocatable_schemas;
746
747   else if (argc == 3 && strcmp (argv[1], "list-keys") == 0)
748     function = gsettings_list_keys;
749
750   else if (argc == 3 && strcmp (argv[1], "list-children") == 0)
751     function = gsettings_list_children;
752
753   else if ((argc == 2 || argc == 3) && strcmp (argv[1], "list-recursively") == 0)
754     function = gsettings_list_recursively;
755
756   else if (argc == 4 && strcmp (argv[1], "range") == 0)
757     function = gsettings_range;
758
759   else if (argc == 4 && strcmp (argv[1], "get") == 0)
760     function = gsettings_get;
761
762   else if (argc == 5 && strcmp (argv[1], "set") == 0)
763     function = gsettings_set;
764
765   else if (argc == 4 && strcmp (argv[1], "reset") == 0)
766     function = gsettings_reset;
767
768   else if (argc == 3 && strcmp (argv[1], "reset-recursively") == 0)
769     function = gsettings_reset_recursively;
770
771   else if (argc == 4 && strcmp (argv[1], "writable") == 0)
772     function = gsettings_writable;
773
774   else if ((argc == 3 || argc == 4) && strcmp (argv[1], "monitor") == 0)
775     function = gsettings_monitor;
776
777   else
778     return gsettings_help (FALSE, argv[1]);
779
780   g_type_init ();
781
782   if (argc > 2)
783     {
784       gchar **parts;
785
786       if (argv[2][0] == '\0')
787         {
788           g_printerr (_("Empty schema name given\n"));
789           return 1;
790         }
791
792       parts = g_strsplit (argv[2], ":", 2);
793
794       schema = g_settings_schema_source_lookup (schema_source, parts[0], TRUE);
795       if (parts[1])
796         {
797           if (!check_relocatable_schema (schema, parts[0]) || !check_path (parts[1]))
798             return 1;
799
800           settings = g_settings_new_full (schema, NULL, parts[1]);
801         }
802       else
803         {
804           if (!check_schema (schema, parts[0]))
805             return 1;
806
807           settings = g_settings_new_full (schema, NULL, NULL);
808         }
809
810       g_strfreev (parts);
811     }
812   else
813     {
814       settings = NULL;
815       schema = NULL;
816     }
817
818   if (argc > 3)
819     {
820       if (!check_key (settings, argv[3]))
821         return 1;
822
823       key = argv[3];
824     }
825   else
826     key = NULL;
827
828   (* function) (settings, key, argc > 4 ? argv[4] : NULL);
829
830   if (settings != NULL)
831     g_object_unref (settings);
832   if (schema != NULL)
833     g_settings_schema_unref (schema);
834
835   g_settings_schema_source_unref (schema_source);
836
837   return 0;
838 }