Add some more cases to the app-id unit tests
[glib.git] / gio / gsettings-tool.c
blob544af848e26077b8123410c47b3669adce49908a
1 /*
2 * Copyright © 2010 Codethink Limited
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.
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.
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/>.
17 * Author: Ryan Lortie <desrt@desrt.ca>
20 #include "config.h"
22 #include <gio/gio.h>
23 #include <gi18n.h>
24 #include <locale.h>
25 #include <string.h>
26 #include <stdlib.h>
28 #ifdef G_OS_WIN32
29 #include "glib/glib-private.h"
30 #endif
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;
39 static gboolean
40 is_relocatable_schema (GSettingsSchema *schema)
42 return g_settings_schema_get_path (schema) == NULL;
45 static gboolean
46 check_relocatable_schema (GSettingsSchema *schema,
47 const gchar *schema_id)
49 if (schema == NULL)
51 g_printerr (_("No such schema “%s”\n"), schema_id);
52 return FALSE;
55 if (!is_relocatable_schema (schema))
57 g_printerr (_("Schema “%s” is not relocatable "
58 "(path must not be specified)\n"),
59 schema_id);
60 return FALSE;
63 return TRUE;
66 static gboolean
67 check_schema (GSettingsSchema *schema,
68 const gchar *schema_id)
70 if (schema == NULL)
72 g_printerr (_("No such schema “%s”\n"), schema_id);
73 return FALSE;
76 if (is_relocatable_schema (schema))
78 g_printerr (_("Schema “%s” is relocatable "
79 "(path must be specified)\n"),
80 schema_id);
81 return FALSE;
84 return TRUE;
87 static gboolean
88 check_path (const gchar *path)
90 if (path[0] == '\0')
92 g_printerr (_("Empty path given.\n"));
93 return FALSE;
96 if (path[0] != '/')
98 g_printerr (_("Path must begin with a slash (/)\n"));
99 return FALSE;
102 if (!g_str_has_suffix (path, "/"))
104 g_printerr (_("Path must end with a slash (/)\n"));
105 return FALSE;
108 if (strstr (path, "//"))
110 g_printerr (_("Path must not contain two adjacent slashes (//)\n"));
111 return FALSE;
114 return TRUE;
117 static void
118 output_list (gchar **list)
120 gint i;
122 for (i = 0; list[i]; i++)
123 g_print ("%s\n", list[i]);
126 static void
127 gsettings_print_version (void)
129 g_print ("%d.%d.%d\n", glib_major_version, glib_minor_version,
130 glib_micro_version);
133 static void
134 gsettings_list_schemas (void)
136 gchar **schemas;
138 g_settings_schema_source_list_schemas (global_schema_source, TRUE, &schemas, NULL);
139 output_list (schemas);
140 g_strfreev (schemas);
143 static void
144 gsettings_list_relocatable_schemas (void)
146 gchar **schemas;
148 g_settings_schema_source_list_schemas (global_schema_source, TRUE, NULL, &schemas);
149 output_list (schemas);
150 g_strfreev (schemas);
153 static void
154 gsettings_list_keys (void)
156 gchar **keys;
158 keys = g_settings_schema_list_keys (global_schema);
159 output_list (keys);
160 g_strfreev (keys);
163 static void
164 gsettings_list_children (void)
166 gchar **children;
167 gint max = 0;
168 gint i;
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]);
175 for (i = 0; children[i]; i++)
177 GSettings *child;
178 GSettingsSchema *schema;
179 gchar *path;
181 child = g_settings_get_child (global_settings, children[i]);
182 g_object_get (child,
183 "settings-schema", &schema,
184 "path", &path,
185 NULL);
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);
192 g_object_unref (child);
193 g_settings_schema_unref (schema);
194 g_free (path);
197 g_strfreev (children);
200 static void
201 enumerate (GSettings *settings)
203 gchar **keys;
204 GSettingsSchema *schema;
205 gint i;
207 g_object_get (settings, "settings-schema", &schema, NULL);
209 keys = g_settings_schema_list_keys (schema);
210 for (i = 0; keys[i]; i++)
212 GVariant *value;
213 gchar *printed;
215 value = g_settings_get_value (settings, keys[i]);
216 printed = g_variant_print (value, TRUE);
217 g_print ("%s %s %s\n", g_settings_schema_get_id (schema), keys[i], printed);
218 g_variant_unref (value);
219 g_free (printed);
222 g_settings_schema_unref (schema);
223 g_strfreev (keys);
226 static void
227 list_recursively (GSettings *settings)
229 gchar **children;
230 gint i;
232 enumerate (settings);
233 children = g_settings_list_children (settings);
234 for (i = 0; children[i]; i++)
236 GSettings *child;
238 child = g_settings_get_child (settings, children[i]);
239 list_recursively (child);
240 g_object_unref (child);
243 g_strfreev (children);
246 static void
247 gsettings_list_recursively (void)
249 if (global_settings)
251 list_recursively (global_settings);
253 else
255 gchar **schemas;
256 gint i;
258 g_settings_schema_source_list_schemas (global_schema_source, TRUE, &schemas, NULL);
260 for (i = 0; schemas[i]; i++)
262 GSettings *settings;
264 settings = g_settings_new (schemas[i]);
265 list_recursively (settings);
266 g_object_unref (settings);
269 g_strfreev (schemas);
273 static void
274 gsettings_description (void)
276 const gchar *description;
277 description = g_settings_schema_key_get_description (global_schema_key);
278 g_print ("%s\n", description);
281 static void
282 gsettings_range (void)
284 GVariant *range, *detail;
285 const gchar *type;
287 range = g_settings_schema_key_get_range (global_schema_key);
288 g_variant_get (range, "(&sv)", &type, &detail);
290 if (strcmp (type, "type") == 0)
291 g_print ("type %s\n", g_variant_get_type_string (detail) + 1);
293 else if (strcmp (type, "range") == 0)
295 GVariant *min, *max;
296 gchar *smin, *smax;
298 g_variant_get (detail, "(**)", &min, &max);
299 smin = g_variant_print (min, FALSE);
300 smax = g_variant_print (max, FALSE);
302 g_print ("range %s %s %s\n",
303 g_variant_get_type_string (min), smin, smax);
304 g_variant_unref (min);
305 g_variant_unref (max);
306 g_free (smin);
307 g_free (smax);
310 else if (strcmp (type, "enum") == 0 || strcmp (type, "flags") == 0)
312 GVariantIter iter;
313 GVariant *item;
315 g_print ("%s\n", type);
317 g_variant_iter_init (&iter, detail);
318 while (g_variant_iter_loop (&iter, "*", &item))
320 gchar *printed;
322 printed = g_variant_print (item, FALSE);
323 g_print ("%s\n", printed);
324 g_free (printed);
328 g_variant_unref (detail);
329 g_variant_unref (range);
332 static void
333 gsettings_get (void)
335 GVariant *value;
336 gchar *printed;
338 value = g_settings_get_value (global_settings, global_key);
339 printed = g_variant_print (value, TRUE);
340 g_print ("%s\n", printed);
341 g_variant_unref (value);
342 g_free (printed);
345 static void
346 gsettings_reset (void)
348 g_settings_reset (global_settings, global_key);
349 g_settings_sync ();
352 static void
353 reset_all_keys (GSettings *settings)
355 GSettingsSchema *schema;
356 gchar **keys;
357 gint i;
359 g_object_get (settings, "settings-schema", &schema, NULL);
361 keys = g_settings_schema_list_keys (schema);
362 for (i = 0; keys[i]; i++)
364 g_settings_reset (settings, keys[i]);
367 g_settings_schema_unref (schema);
368 g_strfreev (keys);
371 static void
372 gsettings_reset_recursively (void)
374 gchar **children;
375 gint i;
377 g_settings_delay (global_settings);
379 reset_all_keys (global_settings);
380 children = g_settings_list_children (global_settings);
381 for (i = 0; children[i]; i++)
383 GSettings *child;
384 child = g_settings_get_child (global_settings, children[i]);
386 reset_all_keys (child);
388 g_object_unref (child);
391 g_strfreev (children);
393 g_settings_apply (global_settings);
394 g_settings_sync ();
397 static void
398 gsettings_writable (void)
400 g_print ("%s\n",
401 g_settings_is_writable (global_settings, global_key) ?
402 "true" : "false");
405 static void
406 value_changed (GSettings *settings,
407 const gchar *key,
408 gpointer user_data)
410 GVariant *value;
411 gchar *printed;
413 value = g_settings_get_value (settings, key);
414 printed = g_variant_print (value, TRUE);
415 g_print ("%s: %s\n", key, printed);
416 g_variant_unref (value);
417 g_free (printed);
420 static void
421 gsettings_monitor (void)
423 if (global_key)
425 gchar *name;
427 name = g_strdup_printf ("changed::%s", global_key);
428 g_signal_connect (global_settings, name, G_CALLBACK (value_changed), NULL);
430 else
431 g_signal_connect (global_settings, "changed", G_CALLBACK (value_changed), NULL);
433 for (;;)
434 g_main_context_iteration (NULL, TRUE);
437 static void
438 gsettings_set (void)
440 const GVariantType *type;
441 GError *error = NULL;
442 GVariant *new;
443 gchar *freeme = NULL;
445 type = g_settings_schema_key_get_value_type (global_schema_key);
447 new = g_variant_parse (type, global_value, NULL, NULL, &error);
449 /* If that didn't work and the type is string then we should assume
450 * that the user is just trying to set a string directly and forgot
451 * the quotes (or had them consumed by the shell).
453 * If the user started with a quote then we assume that some deeper
454 * problem is at play and we want the failure in that case.
456 * Consider:
458 * gsettings set x.y.z key "'i don't expect this to work'"
460 * Note that we should not just add quotes and try parsing again, but
461 * rather assume that the user is providing us with a bare string.
462 * Assume we added single quotes, then consider this case:
464 * gsettings set x.y.z key "i'd expect this to work"
466 * A similar example could be given for double quotes.
468 * Avoid that whole mess by just using g_variant_new_string().
470 if (new == NULL &&
471 g_variant_type_equal (type, G_VARIANT_TYPE_STRING) &&
472 global_value[0] != '\'' && global_value[0] != '"')
474 g_clear_error (&error);
475 new = g_variant_new_string (global_value);
478 if (new == NULL)
480 gchar *context;
482 context = g_variant_parse_error_print_context (error, global_value);
483 g_printerr ("%s", context);
484 exit (1);
487 if (!g_settings_schema_key_range_check (global_schema_key, new))
489 g_printerr (_("The provided value is outside of the valid range\n"));
490 g_variant_unref (new);
491 exit (1);
494 if (!g_settings_set_value (global_settings, global_key, new))
496 g_printerr (_("The key is not writable\n"));
497 exit (1);
500 g_settings_sync ();
502 g_free (freeme);
505 static int
506 gsettings_help (gboolean requested,
507 const gchar *command)
509 const gchar *description;
510 const gchar *synopsis;
511 GString *string;
513 string = g_string_new (NULL);
515 if (command == NULL)
518 else if (strcmp (command, "help") == 0)
520 description = _("Print help");
521 synopsis = "[COMMAND]";
524 else if (strcmp (command, "--version") == 0)
526 description = _("Print version information and exit");
527 synopsis = "";
530 else if (strcmp (command, "list-schemas") == 0)
532 description = _("List the installed (non-relocatable) schemas");
533 synopsis = "";
536 else if (strcmp (command, "list-relocatable-schemas") == 0)
538 description = _("List the installed relocatable schemas");
539 synopsis = "";
542 else if (strcmp (command, "list-keys") == 0)
544 description = _("List the keys in SCHEMA");
545 synopsis = N_("SCHEMA[:PATH]");
548 else if (strcmp (command, "list-children") == 0)
550 description = _("List the children of SCHEMA");
551 synopsis = N_("SCHEMA[:PATH]");
554 else if (strcmp (command, "list-recursively") == 0)
556 description = _("List keys and values, recursively\n"
557 "If no SCHEMA is given, list all keys\n");
558 synopsis = N_("[SCHEMA[:PATH]]");
561 else if (strcmp (command, "get") == 0)
563 description = _("Get the value of KEY");
564 synopsis = N_("SCHEMA[:PATH] KEY");
567 else if (strcmp (command, "range") == 0)
569 description = _("Query the range of valid values for KEY");
570 synopsis = N_("SCHEMA[:PATH] KEY");
573 else if (strcmp (command, "describe") == 0)
575 description = _("Query the description for KEY");
576 synopsis = N_("SCHEMA[:PATH] KEY");
579 else if (strcmp (command, "set") == 0)
581 description = _("Set the value of KEY to VALUE");
582 synopsis = N_("SCHEMA[:PATH] KEY VALUE");
585 else if (strcmp (command, "reset") == 0)
587 description = _("Reset KEY to its default value");
588 synopsis = N_("SCHEMA[:PATH] KEY");
591 else if (strcmp (command, "reset-recursively") == 0)
593 description = _("Reset all keys in SCHEMA to their defaults");
594 synopsis = N_("SCHEMA[:PATH]");
597 else if (strcmp (command, "writable") == 0)
599 description = _("Check if KEY is writable");
600 synopsis = N_("SCHEMA[:PATH] KEY");
603 else if (strcmp (command, "monitor") == 0)
605 description = _("Monitor KEY for changes.\n"
606 "If no KEY is specified, monitor all keys in SCHEMA.\n"
607 "Use ^C to stop monitoring.\n");
608 synopsis = N_("SCHEMA[:PATH] [KEY]");
610 else
612 g_string_printf (string, _("Unknown command %s\n\n"), command);
613 requested = FALSE;
614 command = NULL;
617 if (command == NULL)
619 g_string_append (string,
620 _("Usage:\n"
621 " gsettings --version\n"
622 " gsettings [--schemadir SCHEMADIR] COMMAND [ARGS…]\n"
623 "\n"
624 "Commands:\n"
625 " help Show this information\n"
626 " list-schemas List installed schemas\n"
627 " list-relocatable-schemas List relocatable schemas\n"
628 " list-keys List keys in a schema\n"
629 " list-children List children of a schema\n"
630 " list-recursively List keys and values, recursively\n"
631 " range Queries the range of a key\n"
632 " describe Queries the description of a key\n"
633 " get Get the value of a key\n"
634 " set Set the value of a key\n"
635 " reset Reset the value of a key\n"
636 " reset-recursively Reset all values in a given schema\n"
637 " writable Check if a key is writable\n"
638 " monitor Watch for changes\n"
639 "\n"
640 "Use “gsettings help COMMAND” to get detailed help.\n\n"));
642 else
644 g_string_append_printf (string, _("Usage:\n gsettings [--schemadir SCHEMADIR] %s %s\n\n%s\n\n"),
645 command, synopsis[0] ? _(synopsis) : "", description);
647 g_string_append (string, _("Arguments:\n"));
649 g_string_append (string,
650 _(" SCHEMADIR A directory to search for additional schemas\n"));
652 if (strstr (synopsis, "[COMMAND]"))
653 g_string_append (string,
654 _(" COMMAND The (optional) command to explain\n"));
656 else if (strstr (synopsis, "SCHEMA"))
657 g_string_append (string,
658 _(" SCHEMA The name of the schema\n"
659 " PATH The path, for relocatable schemas\n"));
661 if (strstr (synopsis, "[KEY]"))
662 g_string_append (string,
663 _(" KEY The (optional) key within the schema\n"));
665 else if (strstr (synopsis, "KEY"))
666 g_string_append (string,
667 _(" KEY The key within the schema\n"));
669 if (strstr (synopsis, "VALUE"))
670 g_string_append (string,
671 _(" VALUE The value to set\n"));
673 g_string_append (string, "\n");
676 if (requested)
677 g_print ("%s", string->str);
678 else
679 g_printerr ("%s\n", string->str);
681 g_string_free (string, TRUE);
683 return requested ? 0 : 1;
688 main (int argc, char **argv)
690 void (* function) (void);
691 gboolean need_settings;
693 #ifdef G_OS_WIN32
694 gchar *tmp;
695 #endif
697 setlocale (LC_ALL, "");
698 textdomain (GETTEXT_PACKAGE);
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
708 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
709 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
710 #endif
712 if (argc < 2)
713 return gsettings_help (FALSE, NULL);
715 global_schema_source = g_settings_schema_source_get_default ();
717 if (argc > 3 && g_str_equal (argv[1], "--schemadir"))
719 GSettingsSchemaSource *parent = global_schema_source;
720 GError *error = NULL;
722 global_schema_source = g_settings_schema_source_new_from_directory (argv[2], parent, FALSE, &error);
724 if (global_schema_source == NULL)
726 g_printerr (_("Could not load schemas from %s: %s\n"), argv[2], error->message);
727 g_clear_error (&error);
729 return 1;
732 /* shift remaining arguments (not correct wrt argv[0], but doesn't matter) */
733 argv = argv + 2;
734 argc -= 2;
736 else if (global_schema_source == NULL)
738 g_printerr (_("No schemas installed\n"));
739 return 1;
741 else
742 g_settings_schema_source_ref (global_schema_source);
744 need_settings = TRUE;
746 if (strcmp (argv[1], "help") == 0)
747 return gsettings_help (TRUE, argv[2]);
749 else if (argc == 2 && strcmp (argv[1], "--version") == 0)
750 function = gsettings_print_version;
752 else if (argc == 2 && strcmp (argv[1], "list-schemas") == 0)
753 function = gsettings_list_schemas;
755 else if (argc == 2 && strcmp (argv[1], "list-relocatable-schemas") == 0)
756 function = gsettings_list_relocatable_schemas;
758 else if (argc == 3 && strcmp (argv[1], "list-keys") == 0)
760 need_settings = FALSE;
761 function = gsettings_list_keys;
764 else if (argc == 3 && strcmp (argv[1], "list-children") == 0)
765 function = gsettings_list_children;
767 else if ((argc == 2 || argc == 3) && strcmp (argv[1], "list-recursively") == 0)
768 function = gsettings_list_recursively;
770 else if (argc == 4 && strcmp (argv[1], "describe") == 0)
772 need_settings = FALSE;
773 function = gsettings_description;
776 else if (argc == 4 && strcmp (argv[1], "range") == 0)
778 need_settings = FALSE;
779 function = gsettings_range;
782 else if (argc == 4 && strcmp (argv[1], "get") == 0)
783 function = gsettings_get;
785 else if (argc == 5 && strcmp (argv[1], "set") == 0)
786 function = gsettings_set;
788 else if (argc == 4 && strcmp (argv[1], "reset") == 0)
789 function = gsettings_reset;
791 else if (argc == 3 && strcmp (argv[1], "reset-recursively") == 0)
792 function = gsettings_reset_recursively;
794 else if (argc == 4 && strcmp (argv[1], "writable") == 0)
795 function = gsettings_writable;
797 else if ((argc == 3 || argc == 4) && strcmp (argv[1], "monitor") == 0)
798 function = gsettings_monitor;
800 else
801 return gsettings_help (FALSE, argv[1]);
803 if (argc > 2)
805 gchar **parts;
807 if (argv[2][0] == '\0')
809 g_printerr (_("Empty schema name given\n"));
810 return 1;
813 parts = g_strsplit (argv[2], ":", 2);
815 global_schema = g_settings_schema_source_lookup (global_schema_source, parts[0], TRUE);
817 if (need_settings)
819 if (parts[1])
821 if (!check_relocatable_schema (global_schema, parts[0]) || !check_path (parts[1]))
822 return 1;
824 global_settings = g_settings_new_full (global_schema, NULL, parts[1]);
826 else
828 if (!check_schema (global_schema, parts[0]))
829 return 1;
831 global_settings = g_settings_new_full (global_schema, NULL, NULL);
834 else
836 /* If the user has given a path then we enforce that we have a
837 * relocatable schema, but if they didn't give a path then it
838 * doesn't matter what type of schema we have (since it's
839 * reasonable to ask for introspection information on a
840 * relocatable schema without having to give the path).
842 if (parts[1])
844 if (!check_relocatable_schema (global_schema, parts[0]) || !check_path (parts[1]))
845 return 1;
847 else
849 if (global_schema == NULL)
851 g_printerr (_("No such schema “%s”\n"), parts[0]);
852 return 1;
857 g_strfreev (parts);
860 if (argc > 3)
862 if (!g_settings_schema_has_key (global_schema, argv[3]))
864 g_printerr (_("No such key “%s”\n"), argv[3]);
865 return 1;
868 global_key = argv[3];
869 global_schema_key = g_settings_schema_get_key (global_schema, global_key);
872 if (argc > 4)
873 global_value = argv[4];
875 (* function) ();
878 g_clear_pointer (&global_schema_source, g_settings_schema_source_unref);
879 g_clear_pointer (&global_schema_key, g_settings_schema_key_unref);
880 g_clear_pointer (&global_schema, g_settings_schema_unref);
881 g_clear_object (&global_settings);
883 return 0;