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.1 of the License, 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>
28 #include "glib/glib-private.h"
30 static GSettingsSchemaSource *global_schema_source;
31 static GSettings *global_settings;
32 static GSettingsSchema *global_schema;
33 static GSettingsSchemaKey *global_schema_key;
34 const gchar *global_key;
35 const gchar *global_value;
38 is_relocatable_schema (GSettingsSchema *schema)
40 return g_settings_schema_get_path (schema) == NULL;
44 check_relocatable_schema (GSettingsSchema *schema,
45 const gchar *schema_id)
49 g_printerr (_("No such schema “%s”\n"), schema_id);
53 if (!is_relocatable_schema (schema))
55 g_printerr (_("Schema “%s” is not relocatable "
56 "(path must not be specified)\n"),
65 check_schema (GSettingsSchema *schema,
66 const gchar *schema_id)
70 g_printerr (_("No such schema “%s”\n"), schema_id);
74 if (is_relocatable_schema (schema))
76 g_printerr (_("Schema “%s” is relocatable "
77 "(path must be specified)\n"),
86 check_path (const gchar *path)
90 g_printerr (_("Empty path given.\n"));
96 g_printerr (_("Path must begin with a slash (/)\n"));
100 if (!g_str_has_suffix (path, "/"))
102 g_printerr (_("Path must end with a slash (/)\n"));
106 if (strstr (path, "//"))
108 g_printerr (_("Path must not contain two adjacent slashes (//)\n"));
116 output_list (gchar **list)
120 for (i = 0; list[i]; i++)
121 g_print ("%s\n", list[i]);
125 gsettings_print_version (void)
127 g_print ("%d.%d.%d\n", glib_major_version, glib_minor_version,
132 gsettings_list_schemas (void)
136 g_settings_schema_source_list_schemas (global_schema_source, TRUE, &schemas, NULL);
137 output_list (schemas);
138 g_strfreev (schemas);
142 gsettings_list_schemas_with_paths (void)
147 g_settings_schema_source_list_schemas (global_schema_source, TRUE, &schemas, NULL);
149 for (i = 0; schemas[i] != NULL; i++)
151 GSettingsSchema *schema;
153 const gchar *schema_path;
155 schema_name = g_steal_pointer (&schemas[i]);
157 schema = g_settings_schema_source_lookup (global_schema_source, schema_name, TRUE);
158 schema_path = g_settings_schema_get_path (schema);
160 schemas[i] = g_strconcat (schema_name, " ", schema_path, NULL);
162 g_settings_schema_unref (schema);
163 g_free (schema_name);
166 output_list (schemas);
167 g_strfreev (schemas);
171 gsettings_list_relocatable_schemas (void)
175 g_settings_schema_source_list_schemas (global_schema_source, TRUE, NULL, &schemas);
176 output_list (schemas);
177 g_strfreev (schemas);
181 gsettings_list_keys (void)
185 keys = g_settings_schema_list_keys (global_schema);
191 gsettings_list_children (void)
197 children = g_settings_list_children (global_settings);
198 for (i = 0; children[i]; i++)
199 if (strlen (children[i]) > max)
200 max = strlen (children[i]);
202 for (i = 0; children[i]; i++)
205 GSettingsSchema *schema;
208 child = g_settings_get_child (global_settings, children[i]);
210 "settings-schema", &schema,
214 if (g_settings_schema_get_path (schema) != NULL)
215 g_print ("%-*s %s\n", max, children[i], g_settings_schema_get_id (schema));
217 g_print ("%-*s %s:%s\n", max, children[i], g_settings_schema_get_id (schema), path);
219 g_object_unref (child);
220 g_settings_schema_unref (schema);
224 g_strfreev (children);
228 enumerate (GSettings *settings)
231 GSettingsSchema *schema;
234 g_object_get (settings, "settings-schema", &schema, NULL);
236 keys = g_settings_schema_list_keys (schema);
237 for (i = 0; keys[i]; i++)
242 value = g_settings_get_value (settings, keys[i]);
243 printed = g_variant_print (value, TRUE);
244 g_print ("%s %s %s\n", g_settings_schema_get_id (schema), keys[i], printed);
245 g_variant_unref (value);
249 g_settings_schema_unref (schema);
254 list_recursively (GSettings *settings)
259 enumerate (settings);
260 children = g_settings_list_children (settings);
261 for (i = 0; children[i]; i++)
263 gboolean will_see_elsewhere = FALSE;
266 child = g_settings_get_child (settings, children[i]);
268 if (global_settings == NULL)
270 /* we're listing all non-relocatable settings objects from the
271 * top-level, so if this one is non-relocatable, don't recurse,
272 * because we will pick it up later on.
275 GSettingsSchema *child_schema;
277 g_object_get (child, "settings-schema", &child_schema, NULL);
278 will_see_elsewhere = !is_relocatable_schema (child_schema);
279 g_settings_schema_unref (child_schema);
282 if (!will_see_elsewhere)
283 list_recursively (child);
285 g_object_unref (child);
288 g_strfreev (children);
292 gsettings_list_recursively (void)
296 list_recursively (global_settings);
303 g_settings_schema_source_list_schemas (global_schema_source, TRUE, &schemas, NULL);
305 for (i = 0; schemas[i]; i++)
309 settings = g_settings_new (schemas[i]);
310 list_recursively (settings);
311 g_object_unref (settings);
314 g_strfreev (schemas);
319 gsettings_description (void)
321 const gchar *description;
322 description = g_settings_schema_key_get_description (global_schema_key);
323 if (description == NULL)
324 description = g_settings_schema_key_get_summary (global_schema_key);
325 g_print ("%s\n", description);
329 gsettings_range (void)
331 GVariant *range, *detail;
334 range = g_settings_schema_key_get_range (global_schema_key);
335 g_variant_get (range, "(&sv)", &type, &detail);
337 if (strcmp (type, "type") == 0)
338 g_print ("type %s\n", g_variant_get_type_string (detail) + 1);
340 else if (strcmp (type, "range") == 0)
345 g_variant_get (detail, "(**)", &min, &max);
346 smin = g_variant_print (min, FALSE);
347 smax = g_variant_print (max, FALSE);
349 g_print ("range %s %s %s\n",
350 g_variant_get_type_string (min), smin, smax);
351 g_variant_unref (min);
352 g_variant_unref (max);
357 else if (strcmp (type, "enum") == 0 || strcmp (type, "flags") == 0)
362 g_print ("%s\n", type);
364 g_variant_iter_init (&iter, detail);
365 while (g_variant_iter_loop (&iter, "*", &item))
369 printed = g_variant_print (item, FALSE);
370 g_print ("%s\n", printed);
375 g_variant_unref (detail);
376 g_variant_unref (range);
385 value = g_settings_get_value (global_settings, global_key);
386 printed = g_variant_print (value, TRUE);
387 g_print ("%s\n", printed);
388 g_variant_unref (value);
393 gsettings_reset (void)
395 g_settings_reset (global_settings, global_key);
400 reset_all_keys (GSettings *settings)
402 GSettingsSchema *schema;
406 g_object_get (settings, "settings-schema", &schema, NULL);
408 keys = g_settings_schema_list_keys (schema);
409 for (i = 0; keys[i]; i++)
411 g_settings_reset (settings, keys[i]);
414 g_settings_schema_unref (schema);
419 gsettings_reset_recursively (void)
424 g_settings_delay (global_settings);
426 reset_all_keys (global_settings);
427 children = g_settings_list_children (global_settings);
428 for (i = 0; children[i]; i++)
431 child = g_settings_get_child (global_settings, children[i]);
433 reset_all_keys (child);
435 g_object_unref (child);
438 g_strfreev (children);
440 g_settings_apply (global_settings);
445 gsettings_writable (void)
448 g_settings_is_writable (global_settings, global_key) ?
453 value_changed (GSettings *settings,
460 value = g_settings_get_value (settings, key);
461 printed = g_variant_print (value, TRUE);
462 g_print ("%s: %s\n", key, printed);
463 g_variant_unref (value);
468 gsettings_monitor (void)
474 name = g_strdup_printf ("changed::%s", global_key);
475 g_signal_connect (global_settings, name, G_CALLBACK (value_changed), NULL);
478 g_signal_connect (global_settings, "changed", G_CALLBACK (value_changed), NULL);
481 g_main_context_iteration (NULL, TRUE);
487 const GVariantType *type;
488 GError *error = NULL;
490 gchar *freeme = NULL;
492 type = g_settings_schema_key_get_value_type (global_schema_key);
494 new = g_variant_parse (type, global_value, NULL, NULL, &error);
496 /* If that didn't work and the type is string then we should assume
497 * that the user is just trying to set a string directly and forgot
498 * the quotes (or had them consumed by the shell).
500 * If the user started with a quote then we assume that some deeper
501 * problem is at play and we want the failure in that case.
505 * gsettings set x.y.z key "'i don't expect this to work'"
507 * Note that we should not just add quotes and try parsing again, but
508 * rather assume that the user is providing us with a bare string.
509 * Assume we added single quotes, then consider this case:
511 * gsettings set x.y.z key "i'd expect this to work"
513 * A similar example could be given for double quotes.
515 * Avoid that whole mess by just using g_variant_new_string().
518 g_variant_type_equal (type, G_VARIANT_TYPE_STRING) &&
519 global_value[0] != '\'' && global_value[0] != '"')
521 g_clear_error (&error);
522 new = g_variant_new_string (global_value);
529 context = g_variant_parse_error_print_context (error, global_value);
530 g_printerr ("%s", context);
534 if (!g_settings_schema_key_range_check (global_schema_key, new))
536 g_printerr (_("The provided value is outside of the valid range\n"));
537 g_variant_unref (new);
541 if (!g_settings_set_value (global_settings, global_key, new))
543 g_printerr (_("The key is not writable\n"));
553 gsettings_help (gboolean requested,
554 const gchar *command)
556 const gchar *description;
557 const gchar *synopsis;
560 string = g_string_new (NULL);
565 else if (strcmp (command, "help") == 0)
567 description = _("Print help");
568 synopsis = "[COMMAND]";
571 else if (strcmp (command, "--version") == 0)
573 description = _("Print version information and exit");
577 else if (strcmp (command, "list-schemas") == 0)
579 description = _("List the installed (non-relocatable) schemas");
580 synopsis = "[--print-paths]";
583 else if (strcmp (command, "list-relocatable-schemas") == 0)
585 description = _("List the installed relocatable schemas");
589 else if (strcmp (command, "list-keys") == 0)
591 description = _("List the keys in SCHEMA");
592 synopsis = N_("SCHEMA[:PATH]");
595 else if (strcmp (command, "list-children") == 0)
597 description = _("List the children of SCHEMA");
598 synopsis = N_("SCHEMA[:PATH]");
601 else if (strcmp (command, "list-recursively") == 0)
603 description = _("List keys and values, recursively\n"
604 "If no SCHEMA is given, list all keys\n");
605 synopsis = N_("[SCHEMA[:PATH]]");
608 else if (strcmp (command, "get") == 0)
610 description = _("Get the value of KEY");
611 synopsis = N_("SCHEMA[:PATH] KEY");
614 else if (strcmp (command, "range") == 0)
616 description = _("Query the range of valid values for KEY");
617 synopsis = N_("SCHEMA[:PATH] KEY");
620 else if (strcmp (command, "describe") == 0)
622 description = _("Query the description for KEY");
623 synopsis = N_("SCHEMA[:PATH] KEY");
626 else if (strcmp (command, "set") == 0)
628 description = _("Set the value of KEY to VALUE");
629 synopsis = N_("SCHEMA[:PATH] KEY VALUE");
632 else if (strcmp (command, "reset") == 0)
634 description = _("Reset KEY to its default value");
635 synopsis = N_("SCHEMA[:PATH] KEY");
638 else if (strcmp (command, "reset-recursively") == 0)
640 description = _("Reset all keys in SCHEMA to their defaults");
641 synopsis = N_("SCHEMA[:PATH]");
644 else if (strcmp (command, "writable") == 0)
646 description = _("Check if KEY is writable");
647 synopsis = N_("SCHEMA[:PATH] KEY");
650 else if (strcmp (command, "monitor") == 0)
652 description = _("Monitor KEY for changes.\n"
653 "If no KEY is specified, monitor all keys in SCHEMA.\n"
654 "Use ^C to stop monitoring.\n");
655 synopsis = N_("SCHEMA[:PATH] [KEY]");
659 g_string_printf (string, _("Unknown command %s\n\n"), command);
666 g_string_append (string,
668 " gsettings --version\n"
669 " gsettings [--schemadir SCHEMADIR] COMMAND [ARGS…]\n"
672 " help Show this information\n"
673 " list-schemas List installed schemas\n"
674 " list-relocatable-schemas List relocatable schemas\n"
675 " list-keys List keys in a schema\n"
676 " list-children List children of a schema\n"
677 " list-recursively List keys and values, recursively\n"
678 " range Queries the range of a key\n"
679 " describe Queries the description of a key\n"
680 " get Get the value of a key\n"
681 " set Set the value of a key\n"
682 " reset Reset the value of a key\n"
683 " reset-recursively Reset all values in a given schema\n"
684 " writable Check if a key is writable\n"
685 " monitor Watch for changes\n"
687 "Use “gsettings help COMMAND” to get detailed help.\n\n"));
691 g_string_append_printf (string, _("Usage:\n gsettings [--schemadir SCHEMADIR] %s %s\n\n%s\n\n"),
692 command, synopsis[0] ? _(synopsis) : "", description);
694 g_string_append (string, _("Arguments:\n"));
696 g_string_append (string,
697 _(" SCHEMADIR A directory to search for additional schemas\n"));
699 if (strstr (synopsis, "[COMMAND]"))
700 g_string_append (string,
701 _(" COMMAND The (optional) command to explain\n"));
703 else if (strstr (synopsis, "SCHEMA"))
704 g_string_append (string,
705 _(" SCHEMA The name of the schema\n"
706 " PATH The path, for relocatable schemas\n"));
708 if (strstr (synopsis, "[KEY]"))
709 g_string_append (string,
710 _(" KEY The (optional) key within the schema\n"));
712 else if (strstr (synopsis, "KEY"))
713 g_string_append (string,
714 _(" KEY The key within the schema\n"));
716 if (strstr (synopsis, "VALUE"))
717 g_string_append (string,
718 _(" VALUE The value to set\n"));
720 g_string_append (string, "\n");
724 g_print ("%s", string->str);
726 g_printerr ("%s\n", string->str);
728 g_string_free (string, TRUE);
730 return requested ? 0 : 1;
735 main (int argc, char **argv)
737 void (* function) (void);
738 gboolean need_settings, skip_third_arg_test;
744 setlocale (LC_ALL, GLIB_DEFAULT_LOCALE);
745 textdomain (GETTEXT_PACKAGE);
748 tmp = _glib_get_locale_dir ();
749 bindtextdomain (GETTEXT_PACKAGE, tmp);
752 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
755 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
756 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
760 return gsettings_help (FALSE, NULL);
762 global_schema_source = g_settings_schema_source_get_default ();
764 if (argc > 3 && g_str_equal (argv[1], "--schemadir"))
766 GSettingsSchemaSource *parent = global_schema_source;
767 GError *error = NULL;
769 global_schema_source = g_settings_schema_source_new_from_directory (argv[2], parent, FALSE, &error);
771 if (global_schema_source == NULL)
773 g_printerr (_("Could not load schemas from %s: %s\n"), argv[2], error->message);
774 g_clear_error (&error);
779 /* shift remaining arguments (not correct wrt argv[0], but doesn't matter) */
783 else if (global_schema_source == NULL)
785 g_printerr (_("No schemas installed\n"));
789 g_settings_schema_source_ref (global_schema_source);
791 need_settings = TRUE;
792 skip_third_arg_test = FALSE;
794 if (strcmp (argv[1], "help") == 0)
795 return gsettings_help (TRUE, argv[2]);
797 else if (argc == 2 && strcmp (argv[1], "--version") == 0)
798 function = gsettings_print_version;
800 else if (argc == 2 && strcmp (argv[1], "list-schemas") == 0)
801 function = gsettings_list_schemas;
803 else if (argc == 3 && strcmp (argv[1], "list-schemas") == 0
804 && strcmp (argv[2], "--print-paths") == 0)
806 skip_third_arg_test = TRUE;
807 function = gsettings_list_schemas_with_paths;
810 else if (argc == 2 && strcmp (argv[1], "list-relocatable-schemas") == 0)
811 function = gsettings_list_relocatable_schemas;
813 else if (argc == 3 && strcmp (argv[1], "list-keys") == 0)
815 need_settings = FALSE;
816 function = gsettings_list_keys;
819 else if (argc == 3 && strcmp (argv[1], "list-children") == 0)
820 function = gsettings_list_children;
822 else if ((argc == 2 || argc == 3) && strcmp (argv[1], "list-recursively") == 0)
823 function = gsettings_list_recursively;
825 else if (argc == 4 && strcmp (argv[1], "describe") == 0)
827 need_settings = FALSE;
828 function = gsettings_description;
831 else if (argc == 4 && strcmp (argv[1], "range") == 0)
833 need_settings = FALSE;
834 function = gsettings_range;
837 else if (argc == 4 && strcmp (argv[1], "get") == 0)
838 function = gsettings_get;
840 else if (argc == 5 && strcmp (argv[1], "set") == 0)
841 function = gsettings_set;
843 else if (argc == 4 && strcmp (argv[1], "reset") == 0)
844 function = gsettings_reset;
846 else if (argc == 3 && strcmp (argv[1], "reset-recursively") == 0)
847 function = gsettings_reset_recursively;
849 else if (argc == 4 && strcmp (argv[1], "writable") == 0)
850 function = gsettings_writable;
852 else if ((argc == 3 || argc == 4) && strcmp (argv[1], "monitor") == 0)
853 function = gsettings_monitor;
856 return gsettings_help (FALSE, argv[1]);
858 if (argc > 2 && !skip_third_arg_test)
862 if (argv[2][0] == '\0')
864 g_printerr (_("Empty schema name given\n"));
868 parts = g_strsplit (argv[2], ":", 2);
870 global_schema = g_settings_schema_source_lookup (global_schema_source, parts[0], TRUE);
876 if (!check_relocatable_schema (global_schema, parts[0]) || !check_path (parts[1]))
879 global_settings = g_settings_new_full (global_schema, NULL, parts[1]);
883 if (!check_schema (global_schema, parts[0]))
886 global_settings = g_settings_new_full (global_schema, NULL, NULL);
891 /* If the user has given a path then we enforce that we have a
892 * relocatable schema, but if they didn't give a path then it
893 * doesn't matter what type of schema we have (since it's
894 * reasonable to ask for introspection information on a
895 * relocatable schema without having to give the path).
899 if (!check_relocatable_schema (global_schema, parts[0]) || !check_path (parts[1]))
904 if (global_schema == NULL)
906 g_printerr (_("No such schema “%s”\n"), parts[0]);
917 if (!g_settings_schema_has_key (global_schema, argv[3]))
919 g_printerr (_("No such key “%s”\n"), argv[3]);
923 global_key = argv[3];
924 global_schema_key = g_settings_schema_get_key (global_schema, global_key);
928 global_value = argv[4];
933 g_clear_pointer (&global_schema_source, g_settings_schema_source_unref);
934 g_clear_pointer (&global_schema_key, g_settings_schema_key_unref);
935 g_clear_pointer (&global_schema, g_settings_schema_unref);
936 g_clear_object (&global_settings);