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