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