2 * Copyright © 2010 Codethink Limited
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Author: Ryan Lortie <desrt@desrt.ca>
30 #include "glib/glib-private.h"
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;
40 is_relocatable_schema (GSettingsSchema *schema)
42 return g_settings_schema_get_path (schema) == NULL;
46 check_relocatable_schema (GSettingsSchema *schema,
47 const gchar *schema_id)
51 g_printerr (_("No such schema “%s”\n"), schema_id);
55 if (!is_relocatable_schema (schema))
57 g_printerr (_("Schema “%s” is not relocatable "
58 "(path must not be specified)\n"),
67 check_schema (GSettingsSchema *schema,
68 const gchar *schema_id)
72 g_printerr (_("No such schema “%s”\n"), schema_id);
76 if (is_relocatable_schema (schema))
78 g_printerr (_("Schema “%s” is relocatable "
79 "(path must be specified)\n"),
88 check_path (const gchar *path)
92 g_printerr (_("Empty path given.\n"));
98 g_printerr (_("Path must begin with a slash (/)\n"));
102 if (!g_str_has_suffix (path, "/"))
104 g_printerr (_("Path must end with a slash (/)\n"));
108 if (strstr (path, "//"))
110 g_printerr (_("Path must not contain two adjacent slashes (//)\n"));
118 qsort_cmp (const void *a,
121 return g_strcmp0 (*(gchar* const*)a, *(gchar* const*)b);
125 output_list (gchar **list)
129 qsort (list, g_strv_length (list), sizeof (gchar*), qsort_cmp);
130 for (i = 0; list[i]; i++)
131 g_print ("%s\n", list[i]);
135 gsettings_print_version (void)
137 g_print ("%d.%d.%d\n", glib_major_version, glib_minor_version,
142 gsettings_list_schemas (void)
146 g_settings_schema_source_list_schemas (global_schema_source, TRUE, &schemas, NULL);
147 output_list (schemas);
148 g_strfreev (schemas);
152 gsettings_list_schemas_with_paths (void)
157 g_settings_schema_source_list_schemas (global_schema_source, TRUE, &schemas, NULL);
159 for (i = 0; schemas[i] != NULL; i++)
161 GSettingsSchema *schema;
163 const gchar *schema_path;
165 schema_name = g_steal_pointer (&schemas[i]);
167 schema = g_settings_schema_source_lookup (global_schema_source, schema_name, TRUE);
168 schema_path = g_settings_schema_get_path (schema);
170 schemas[i] = g_strconcat (schema_name, " ", schema_path, NULL);
172 g_settings_schema_unref (schema);
173 g_free (schema_name);
176 output_list (schemas);
177 g_strfreev (schemas);
181 gsettings_list_relocatable_schemas (void)
185 g_settings_schema_source_list_schemas (global_schema_source, TRUE, NULL, &schemas);
186 output_list (schemas);
187 g_strfreev (schemas);
191 gsettings_list_keys (void)
195 keys = g_settings_schema_list_keys (global_schema);
201 gsettings_list_children (void)
207 children = g_settings_list_children (global_settings);
208 qsort (children, g_strv_length (children), sizeof (gchar*), qsort_cmp);
209 for (i = 0; children[i]; i++)
211 gsize len = strlen (children[i]);
216 for (i = 0; children[i]; i++)
219 GSettingsSchema *schema;
222 child = g_settings_get_child (global_settings, children[i]);
224 "settings-schema", &schema,
228 if (g_settings_schema_get_path (schema) != NULL)
229 g_print ("%-*s %s\n", (int) MIN (max, G_MAXINT), children[i],
230 g_settings_schema_get_id (schema));
232 g_print ("%-*s %s:%s\n", (int) MIN (max, G_MAXINT), children[i],
233 g_settings_schema_get_id (schema), path);
235 g_object_unref (child);
236 g_settings_schema_unref (schema);
240 g_strfreev (children);
244 enumerate (GSettings *settings)
247 GSettingsSchema *schema;
250 g_object_get (settings, "settings-schema", &schema, NULL);
252 keys = g_settings_schema_list_keys (schema);
253 qsort (keys, g_strv_length (keys), sizeof (gchar*), qsort_cmp);
254 for (i = 0; keys[i]; i++)
259 value = g_settings_get_value (settings, keys[i]);
260 printed = g_variant_print (value, TRUE);
261 g_print ("%s %s %s\n", g_settings_schema_get_id (schema), keys[i], printed);
262 g_variant_unref (value);
266 g_settings_schema_unref (schema);
271 list_recursively (GSettings *settings)
276 enumerate (settings);
277 children = g_settings_list_children (settings);
278 qsort (children, g_strv_length (children), sizeof (gchar*), qsort_cmp);
279 for (i = 0; children[i]; i++)
281 gboolean will_see_elsewhere = FALSE;
284 child = g_settings_get_child (settings, children[i]);
286 if (global_settings == NULL)
288 /* we're listing all non-relocatable settings objects from the
289 * top-level, so if this one is non-relocatable, don't recurse,
290 * because we will pick it up later on.
293 GSettingsSchema *child_schema;
295 g_object_get (child, "settings-schema", &child_schema, NULL);
296 will_see_elsewhere = !is_relocatable_schema (child_schema);
297 g_settings_schema_unref (child_schema);
300 if (!will_see_elsewhere)
301 list_recursively (child);
303 g_object_unref (child);
306 g_strfreev (children);
310 gsettings_list_recursively (void)
314 list_recursively (global_settings);
321 g_settings_schema_source_list_schemas (global_schema_source, TRUE, &schemas, NULL);
322 qsort (schemas, g_strv_length (schemas), sizeof (gchar*), qsort_cmp);
324 for (i = 0; schemas[i]; i++)
328 settings = g_settings_new (schemas[i]);
329 list_recursively (settings);
330 g_object_unref (settings);
333 g_strfreev (schemas);
338 gsettings_description (void)
340 const gchar *description;
341 description = g_settings_schema_key_get_description (global_schema_key);
342 if (description == NULL)
343 description = g_settings_schema_key_get_summary (global_schema_key);
344 g_print ("%s\n", description);
348 gsettings_range (void)
350 GVariant *range, *detail;
353 range = g_settings_schema_key_get_range (global_schema_key);
354 g_variant_get (range, "(&sv)", &type, &detail);
356 if (strcmp (type, "type") == 0)
357 g_print ("type %s\n", g_variant_get_type_string (detail) + 1);
359 else if (strcmp (type, "range") == 0)
364 g_variant_get (detail, "(**)", &min, &max);
365 smin = g_variant_print (min, FALSE);
366 smax = g_variant_print (max, FALSE);
368 g_print ("range %s %s %s\n",
369 g_variant_get_type_string (min), smin, smax);
370 g_variant_unref (min);
371 g_variant_unref (max);
376 else if (strcmp (type, "enum") == 0 || strcmp (type, "flags") == 0)
381 g_print ("%s\n", type);
383 g_variant_iter_init (&iter, detail);
384 while (g_variant_iter_loop (&iter, "*", &item))
388 printed = g_variant_print (item, FALSE);
389 g_print ("%s\n", printed);
394 g_variant_unref (detail);
395 g_variant_unref (range);
404 value = g_settings_get_value (global_settings, global_key);
405 printed = g_variant_print (value, TRUE);
406 g_print ("%s\n", printed);
407 g_variant_unref (value);
412 gsettings_reset (void)
414 g_settings_reset (global_settings, global_key);
419 reset_all_keys (GSettings *settings)
421 GSettingsSchema *schema;
425 g_object_get (settings, "settings-schema", &schema, NULL);
427 keys = g_settings_schema_list_keys (schema);
428 for (i = 0; keys[i]; i++)
430 g_settings_reset (settings, keys[i]);
433 g_settings_schema_unref (schema);
438 gsettings_reset_recursively (void)
443 g_settings_delay (global_settings);
445 reset_all_keys (global_settings);
446 children = g_settings_list_children (global_settings);
447 for (i = 0; children[i]; i++)
450 child = g_settings_get_child (global_settings, children[i]);
452 reset_all_keys (child);
454 g_object_unref (child);
457 g_strfreev (children);
459 g_settings_apply (global_settings);
464 gsettings_writable (void)
467 g_settings_is_writable (global_settings, global_key) ?
472 value_changed (GSettings *settings,
479 value = g_settings_get_value (settings, key);
480 printed = g_variant_print (value, TRUE);
481 g_print ("%s: %s\n", key, printed);
482 g_variant_unref (value);
487 gsettings_monitor (void)
493 name = g_strdup_printf ("changed::%s", global_key);
494 g_signal_connect (global_settings, name, G_CALLBACK (value_changed), NULL);
497 g_signal_connect (global_settings, "changed", G_CALLBACK (value_changed), NULL);
500 g_main_context_iteration (NULL, TRUE);
506 const GVariantType *type;
507 GError *error = NULL;
509 gchar *freeme = NULL;
511 type = g_settings_schema_key_get_value_type (global_schema_key);
513 new = g_variant_parse (type, global_value, NULL, NULL, &error);
515 /* If that didn't work and the type is string then we should assume
516 * that the user is just trying to set a string directly and forgot
517 * the quotes (or had them consumed by the shell).
519 * If the user started with a quote then we assume that some deeper
520 * problem is at play and we want the failure in that case.
524 * gsettings set x.y.z key "'i don't expect this to work'"
526 * Note that we should not just add quotes and try parsing again, but
527 * rather assume that the user is providing us with a bare string.
528 * Assume we added single quotes, then consider this case:
530 * gsettings set x.y.z key "i'd expect this to work"
532 * A similar example could be given for double quotes.
534 * Avoid that whole mess by just using g_variant_new_string().
537 g_variant_type_equal (type, G_VARIANT_TYPE_STRING) &&
538 global_value[0] != '\'' && global_value[0] != '"')
540 g_clear_error (&error);
541 new = g_variant_new_string (global_value);
548 context = g_variant_parse_error_print_context (error, global_value);
549 g_printerr ("%s", context);
553 if (!g_settings_schema_key_range_check (global_schema_key, new))
555 g_printerr (_("The provided value is outside of the valid range\n"));
556 g_variant_unref (new);
560 if (!g_settings_set_value (global_settings, global_key, new))
562 g_printerr (_("The key is not writable\n"));
572 gsettings_help (gboolean requested,
573 const gchar *command)
575 const gchar *description = NULL;
576 const gchar *synopsis = NULL;
579 string = g_string_new (NULL);
584 else if (strcmp (command, "help") == 0)
586 description = _("Print help");
587 synopsis = "[COMMAND]";
590 else if (strcmp (command, "--version") == 0)
592 description = _("Print version information and exit");
596 else if (strcmp (command, "list-schemas") == 0)
598 description = _("List the installed (non-relocatable) schemas");
599 synopsis = "[--print-paths]";
602 else if (strcmp (command, "list-relocatable-schemas") == 0)
604 description = _("List the installed relocatable schemas");
608 else if (strcmp (command, "list-keys") == 0)
610 description = _("List the keys in SCHEMA");
611 synopsis = N_("SCHEMA[:PATH]");
614 else if (strcmp (command, "list-children") == 0)
616 description = _("List the children of SCHEMA");
617 synopsis = N_("SCHEMA[:PATH]");
620 else if (strcmp (command, "list-recursively") == 0)
622 description = _("List keys and values, recursively\n"
623 "If no SCHEMA is given, list all keys\n");
624 synopsis = N_("[SCHEMA[:PATH]]");
627 else if (strcmp (command, "get") == 0)
629 description = _("Get the value of KEY");
630 synopsis = N_("SCHEMA[:PATH] KEY");
633 else if (strcmp (command, "range") == 0)
635 description = _("Query the range of valid values for KEY");
636 synopsis = N_("SCHEMA[:PATH] KEY");
639 else if (strcmp (command, "describe") == 0)
641 description = _("Query the description for KEY");
642 synopsis = N_("SCHEMA[:PATH] KEY");
645 else if (strcmp (command, "set") == 0)
647 description = _("Set the value of KEY to VALUE");
648 synopsis = N_("SCHEMA[:PATH] KEY VALUE");
651 else if (strcmp (command, "reset") == 0)
653 description = _("Reset KEY to its default value");
654 synopsis = N_("SCHEMA[:PATH] KEY");
657 else if (strcmp (command, "reset-recursively") == 0)
659 description = _("Reset all keys in SCHEMA to their defaults");
660 synopsis = N_("SCHEMA[:PATH]");
663 else if (strcmp (command, "writable") == 0)
665 description = _("Check if KEY is writable");
666 synopsis = N_("SCHEMA[:PATH] KEY");
669 else if (strcmp (command, "monitor") == 0)
671 description = _("Monitor KEY for changes.\n"
672 "If no KEY is specified, monitor all keys in SCHEMA.\n"
673 "Use ^C to stop monitoring.\n");
674 synopsis = N_("SCHEMA[:PATH] [KEY]");
678 g_string_printf (string, _("Unknown command %s\n\n"), command);
685 g_string_append (string,
687 " gsettings --version\n"
688 " gsettings [--schemadir SCHEMADIR] COMMAND [ARGS…]\n"
691 " help Show this information\n"
692 " list-schemas List installed schemas\n"
693 " list-relocatable-schemas List relocatable schemas\n"
694 " list-keys List keys in a schema\n"
695 " list-children List children of a schema\n"
696 " list-recursively List keys and values, recursively\n"
697 " range Queries the range of a key\n"
698 " describe Queries the description of a key\n"
699 " get Get the value of a key\n"
700 " set Set the value of a key\n"
701 " reset Reset the value of a key\n"
702 " reset-recursively Reset all values in a given schema\n"
703 " writable Check if a key is writable\n"
704 " monitor Watch for changes\n"
706 "Use “gsettings help COMMAND” to get detailed help.\n\n"));
710 g_string_append_printf (string, _("Usage:\n gsettings [--schemadir SCHEMADIR] %s %s\n\n%s\n\n"),
711 command, synopsis[0] ? _(synopsis) : "", description);
713 g_string_append (string, _("Arguments:\n"));
715 g_string_append (string,
716 _(" SCHEMADIR A directory to search for additional schemas\n"));
718 if (strstr (synopsis, "[COMMAND]"))
719 g_string_append (string,
720 _(" COMMAND The (optional) command to explain\n"));
722 else if (strstr (synopsis, "SCHEMA"))
723 g_string_append (string,
724 _(" SCHEMA The name of the schema\n"
725 " PATH The path, for relocatable schemas\n"));
727 if (strstr (synopsis, "[KEY]"))
728 g_string_append (string,
729 _(" KEY The (optional) key within the schema\n"));
731 else if (strstr (synopsis, "KEY"))
732 g_string_append (string,
733 _(" KEY The key within the schema\n"));
735 if (strstr (synopsis, "VALUE"))
736 g_string_append (string,
737 _(" VALUE The value to set\n"));
739 g_string_append (string, "\n");
743 g_print ("%s", string->str);
745 g_printerr ("%s\n", string->str);
747 g_string_free (string, TRUE);
749 return requested ? 0 : 1;
754 main (int argc, char **argv)
756 void (* function) (void);
757 gboolean need_settings, skip_third_arg_test;
763 setlocale (LC_ALL, GLIB_DEFAULT_LOCALE);
764 textdomain (GETTEXT_PACKAGE);
767 tmp = _glib_get_locale_dir ();
768 bindtextdomain (GETTEXT_PACKAGE, tmp);
771 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
774 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
775 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
779 return gsettings_help (FALSE, NULL);
781 global_schema_source = g_settings_schema_source_get_default ();
783 if (argc > 3 && g_str_equal (argv[1], "--schemadir"))
785 GSettingsSchemaSource *parent = global_schema_source;
786 GError *error = NULL;
788 global_schema_source = g_settings_schema_source_new_from_directory (argv[2], parent, FALSE, &error);
790 if (global_schema_source == NULL)
792 g_printerr (_("Could not load schemas from %s: %s\n"), argv[2], error->message);
793 g_clear_error (&error);
798 /* shift remaining arguments (not correct wrt argv[0], but doesn't matter) */
802 else if (global_schema_source == NULL)
804 g_printerr (_("No schemas installed\n"));
808 g_settings_schema_source_ref (global_schema_source);
810 need_settings = TRUE;
811 skip_third_arg_test = FALSE;
813 if (strcmp (argv[1], "help") == 0)
814 return gsettings_help (TRUE, argv[2]);
816 else if (argc == 2 && strcmp (argv[1], "--version") == 0)
817 function = gsettings_print_version;
819 else if (argc == 2 && strcmp (argv[1], "list-schemas") == 0)
820 function = gsettings_list_schemas;
822 else if (argc == 3 && strcmp (argv[1], "list-schemas") == 0
823 && strcmp (argv[2], "--print-paths") == 0)
825 skip_third_arg_test = TRUE;
826 function = gsettings_list_schemas_with_paths;
829 else if (argc == 2 && strcmp (argv[1], "list-relocatable-schemas") == 0)
830 function = gsettings_list_relocatable_schemas;
832 else if (argc == 3 && strcmp (argv[1], "list-keys") == 0)
834 need_settings = FALSE;
835 function = gsettings_list_keys;
838 else if (argc == 3 && strcmp (argv[1], "list-children") == 0)
839 function = gsettings_list_children;
841 else if ((argc == 2 || argc == 3) && strcmp (argv[1], "list-recursively") == 0)
842 function = gsettings_list_recursively;
844 else if (argc == 4 && strcmp (argv[1], "describe") == 0)
846 need_settings = FALSE;
847 function = gsettings_description;
850 else if (argc == 4 && strcmp (argv[1], "range") == 0)
852 need_settings = FALSE;
853 function = gsettings_range;
856 else if (argc == 4 && strcmp (argv[1], "get") == 0)
857 function = gsettings_get;
859 else if (argc == 5 && strcmp (argv[1], "set") == 0)
860 function = gsettings_set;
862 else if (argc == 4 && strcmp (argv[1], "reset") == 0)
863 function = gsettings_reset;
865 else if (argc == 3 && strcmp (argv[1], "reset-recursively") == 0)
866 function = gsettings_reset_recursively;
868 else if (argc == 4 && strcmp (argv[1], "writable") == 0)
869 function = gsettings_writable;
871 else if ((argc == 3 || argc == 4) && strcmp (argv[1], "monitor") == 0)
872 function = gsettings_monitor;
875 return gsettings_help (FALSE, argv[1]);
877 if (argc > 2 && !skip_third_arg_test)
881 if (argv[2][0] == '\0')
883 g_printerr (_("Empty schema name given\n"));
887 parts = g_strsplit (argv[2], ":", 2);
889 global_schema = g_settings_schema_source_lookup (global_schema_source, parts[0], TRUE);
895 if (!check_relocatable_schema (global_schema, parts[0]) || !check_path (parts[1]))
898 global_settings = g_settings_new_full (global_schema, NULL, parts[1]);
902 if (!check_schema (global_schema, parts[0]))
905 global_settings = g_settings_new_full (global_schema, NULL, NULL);
910 /* If the user has given a path then we enforce that we have a
911 * relocatable schema, but if they didn't give a path then it
912 * doesn't matter what type of schema we have (since it's
913 * reasonable to ask for introspection information on a
914 * relocatable schema without having to give the path).
918 if (!check_relocatable_schema (global_schema, parts[0]) || !check_path (parts[1]))
923 if (global_schema == NULL)
925 g_printerr (_("No such schema “%s”\n"), parts[0]);
936 if (!g_settings_schema_has_key (global_schema, argv[3]))
938 g_printerr (_("No such key “%s”\n"), argv[3]);
942 global_key = argv[3];
943 global_schema_key = g_settings_schema_get_key (global_schema, global_key);
947 global_value = argv[4];
952 g_clear_pointer (&global_schema_source, g_settings_schema_source_unref);
953 g_clear_pointer (&global_schema_key, g_settings_schema_key_unref);
954 g_clear_pointer (&global_schema, g_settings_schema_unref);
955 g_clear_object (&global_settings);