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