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>
29 #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 output_list (gchar **list)
122 for (i = 0; list[i]; i++)
123 g_print ("%s\n", list[i]);
127 gsettings_print_version (void)
129 g_print ("%d.%d.%d\n", glib_major_version, glib_minor_version,
134 gsettings_list_schemas (void)
138 g_settings_schema_source_list_schemas (global_schema_source, TRUE, &schemas, NULL);
139 output_list (schemas);
140 g_strfreev (schemas);
144 gsettings_list_relocatable_schemas (void)
148 g_settings_schema_source_list_schemas (global_schema_source, TRUE, NULL, &schemas);
149 output_list (schemas);
150 g_strfreev (schemas);
154 gsettings_list_keys (void)
158 keys = g_settings_list_keys (global_settings);
164 gsettings_list_children (void)
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++)
178 GSettingsSchema *schema;
181 child = g_settings_get_child (global_settings, children[i]);
183 "settings-schema", &schema,
187 if (g_settings_schema_get_path (schema) != NULL)
188 g_print ("%-*s %s\n", max, children[i], g_settings_schema_get_id (schema));
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);
197 g_strfreev (children);
201 enumerate (GSettings *settings)
207 g_object_get (settings, "schema-id", &schema, NULL);
209 keys = g_settings_list_keys (settings);
210 for (i = 0; keys[i]; i++)
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);
227 list_recursively (GSettings *settings)
232 enumerate (settings);
233 children = g_settings_list_children (settings);
234 for (i = 0; children[i]; i++)
238 child = g_settings_get_child (settings, children[i]);
239 list_recursively (child);
240 g_object_unref (child);
243 g_strfreev (children);
247 gsettings_list_recursively (void)
251 list_recursively (global_settings);
258 g_settings_schema_source_list_schemas (global_schema_source, TRUE, &schemas, NULL);
260 for (i = 0; schemas[i]; i++)
264 settings = g_settings_new (schemas[i]);
265 list_recursively (settings);
266 g_object_unref (settings);
269 g_strfreev (schemas);
274 gsettings_range (void)
276 GVariant *range, *detail;
279 range = g_settings_schema_key_get_range (global_schema_key);
280 g_variant_get (range, "(&sv)", &type, &detail);
282 if (strcmp (type, "type") == 0)
283 g_print ("type %s\n", g_variant_get_type_string (detail) + 1);
285 else if (strcmp (type, "range") == 0)
290 g_variant_get (detail, "(**)", &min, &max);
291 smin = g_variant_print (min, FALSE);
292 smax = g_variant_print (max, FALSE);
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);
302 else if (strcmp (type, "enum") == 0 || strcmp (type, "flags") == 0)
307 g_print ("%s\n", type);
309 g_variant_iter_init (&iter, detail);
310 while (g_variant_iter_loop (&iter, "*", &item))
314 printed = g_variant_print (item, FALSE);
315 g_print ("%s\n", printed);
320 g_variant_unref (detail);
321 g_variant_unref (range);
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);
338 gsettings_reset (void)
340 g_settings_reset (global_settings, global_key);
345 reset_all_keys (GSettings *settings)
350 keys = g_settings_list_keys (settings);
351 for (i = 0; keys[i]; i++)
353 g_settings_reset (settings, keys[i]);
360 gsettings_reset_recursively (void)
365 g_settings_delay (global_settings);
367 reset_all_keys (global_settings);
368 children = g_settings_list_children (global_settings);
369 for (i = 0; children[i]; i++)
372 child = g_settings_get_child (global_settings, children[i]);
374 reset_all_keys (child);
376 g_object_unref (child);
379 g_strfreev (children);
381 g_settings_apply (global_settings);
386 gsettings_writable (void)
389 g_settings_is_writable (global_settings, global_key) ?
394 value_changed (GSettings *settings,
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);
409 gsettings_monitor (void)
415 name = g_strdup_printf ("changed::%s", global_key);
416 g_signal_connect (global_settings, name, G_CALLBACK (value_changed), NULL);
419 g_signal_connect (global_settings, "changed", G_CALLBACK (value_changed), NULL);
422 g_main_context_iteration (NULL, TRUE);
428 const GVariantType *type;
429 GError *error = NULL;
431 gchar *freeme = NULL;
433 type = g_settings_schema_key_get_value_type (global_schema_key);
435 new = g_variant_parse (type, global_value, NULL, NULL, &error);
437 /* If that didn't work and the type is string then we should assume
438 * that the user is just trying to set a string directly and forgot
439 * the quotes (or had them consumed by the shell).
441 * If the user started with a quote then we assume that some deeper
442 * problem is at play and we want the failure in that case.
446 * gsettings set x.y.z key "'i don't expect this to work'"
448 * Note that we should not just add quotes and try parsing again, but
449 * rather assume that the user is providing us with a bare string.
450 * Assume we added single quotes, then consider this case:
452 * gsettings set x.y.z key "i'd expect this to work"
454 * A similar example could be given for double quotes.
456 * Avoid that whole mess by just using g_variant_new_string().
459 g_variant_type_equal (type, G_VARIANT_TYPE_STRING) &&
460 global_value[0] != '\'' && global_value[0] != '"')
462 g_clear_error (&error);
463 new = g_variant_new_string (global_value);
470 context = g_variant_parse_error_print_context (error, global_value);
471 g_printerr ("%s", context);
475 if (!g_settings_schema_key_range_check (global_schema_key, new))
477 g_printerr (_("The provided value is outside of the valid range\n"));
478 g_variant_unref (new);
482 if (!g_settings_set_value (global_settings, global_key, new))
484 g_printerr (_("The key is not writable\n"));
494 gsettings_help (gboolean requested,
495 const gchar *command)
497 const gchar *description;
498 const gchar *synopsis;
501 string = g_string_new (NULL);
506 else if (strcmp (command, "help") == 0)
508 description = _("Print help");
509 synopsis = "[COMMAND]";
512 else if (strcmp (command, "--version") == 0)
514 description = _("Print version information and exit");
518 else if (strcmp (command, "list-schemas") == 0)
520 description = _("List the installed (non-relocatable) schemas");
524 else if (strcmp (command, "list-relocatable-schemas") == 0)
526 description = _("List the installed relocatable schemas");
530 else if (strcmp (command, "list-keys") == 0)
532 description = _("List the keys in SCHEMA");
533 synopsis = N_("SCHEMA[:PATH]");
536 else if (strcmp (command, "list-children") == 0)
538 description = _("List the children of SCHEMA");
539 synopsis = N_("SCHEMA[:PATH]");
542 else if (strcmp (command, "list-recursively") == 0)
544 description = _("List keys and values, recursively\n"
545 "If no SCHEMA is given, list all keys\n");
546 synopsis = N_("[SCHEMA[:PATH]]");
549 else if (strcmp (command, "get") == 0)
551 description = _("Get the value of KEY");
552 synopsis = N_("SCHEMA[:PATH] KEY");
555 else if (strcmp (command, "range") == 0)
557 description = _("Query the range of valid values for KEY");
558 synopsis = N_("SCHEMA[:PATH] KEY");
561 else if (strcmp (command, "set") == 0)
563 description = _("Set the value of KEY to VALUE");
564 synopsis = N_("SCHEMA[:PATH] KEY VALUE");
567 else if (strcmp (command, "reset") == 0)
569 description = _("Reset KEY to its default value");
570 synopsis = N_("SCHEMA[:PATH] KEY");
573 else if (strcmp (command, "reset-recursively") == 0)
575 description = _("Reset all keys in SCHEMA to their defaults");
576 synopsis = N_("SCHEMA[:PATH]");
579 else if (strcmp (command, "writable") == 0)
581 description = _("Check if KEY is writable");
582 synopsis = N_("SCHEMA[:PATH] KEY");
585 else if (strcmp (command, "monitor") == 0)
587 description = _("Monitor KEY for changes.\n"
588 "If no KEY is specified, monitor all keys in SCHEMA.\n"
589 "Use ^C to stop monitoring.\n");
590 synopsis = N_("SCHEMA[:PATH] [KEY]");
594 g_string_printf (string, _("Unknown command %s\n\n"), command);
601 g_string_append (string,
603 " gsettings --version\n"
604 " gsettings [--schemadir SCHEMADIR] COMMAND [ARGS...]\n"
607 " help Show this information\n"
608 " list-schemas List installed schemas\n"
609 " list-relocatable-schemas List relocatable schemas\n"
610 " list-keys List keys in a schema\n"
611 " list-children List children of a schema\n"
612 " list-recursively List keys and values, recursively\n"
613 " range Queries the range of a key\n"
614 " get Get the value of a key\n"
615 " set Set the value of a key\n"
616 " reset Reset the value of a key\n"
617 " reset-recursively Reset all values in a given schema\n"
618 " writable Check if a key is writable\n"
619 " monitor Watch for changes\n"
621 "Use 'gsettings help COMMAND' to get detailed help.\n\n"));
625 g_string_append_printf (string, _("Usage:\n gsettings [--schemadir SCHEMADIR] %s %s\n\n%s\n\n"),
626 command, synopsis[0] ? _(synopsis) : "", description);
628 g_string_append (string, _("Arguments:\n"));
630 g_string_append (string,
631 _(" SCHEMADIR A directory to search for additional schemas\n"));
633 if (strstr (synopsis, "[COMMAND]"))
634 g_string_append (string,
635 _(" COMMAND The (optional) command to explain\n"));
637 else if (strstr (synopsis, "SCHEMA"))
638 g_string_append (string,
639 _(" SCHEMA The name of the schema\n"
640 " PATH The path, for relocatable schemas\n"));
642 if (strstr (synopsis, "[KEY]"))
643 g_string_append (string,
644 _(" KEY The (optional) key within the schema\n"));
646 else if (strstr (synopsis, "KEY"))
647 g_string_append (string,
648 _(" KEY The key within the schema\n"));
650 if (strstr (synopsis, "VALUE"))
651 g_string_append (string,
652 _(" VALUE The value to set\n"));
654 g_string_append (string, "\n");
658 g_print ("%s", string->str);
660 g_printerr ("%s\n", string->str);
662 g_string_free (string, TRUE);
664 return requested ? 0 : 1;
669 main (int argc, char **argv)
671 void (* function) (void);
677 setlocale (LC_ALL, "");
678 textdomain (GETTEXT_PACKAGE);
681 tmp = _glib_get_locale_dir ();
682 bindtextdomain (GETTEXT_PACKAGE, tmp);
685 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
688 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
689 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
693 return gsettings_help (FALSE, NULL);
695 global_schema_source = g_settings_schema_source_ref (g_settings_schema_source_get_default ());
697 if (argc > 3 && g_str_equal (argv[1], "--schemadir"))
699 GSettingsSchemaSource *parent = global_schema_source;
700 GError *error = NULL;
702 global_schema_source = g_settings_schema_source_new_from_directory (argv[2], parent, FALSE, &error);
703 g_settings_schema_source_unref (parent);
705 if (global_schema_source == NULL)
707 g_printerr (_("Could not load schemas from %s: %s\n"), argv[2], error->message);
708 g_clear_error (&error);
713 /* shift remaining arguments (not correct wrt argv[0], but doesn't matter) */
718 if (strcmp (argv[1], "help") == 0)
719 return gsettings_help (TRUE, argv[2]);
721 else if (argc == 2 && strcmp (argv[1], "--version") == 0)
722 function = gsettings_print_version;
724 else if (argc == 2 && strcmp (argv[1], "list-schemas") == 0)
725 function = gsettings_list_schemas;
727 else if (argc == 2 && strcmp (argv[1], "list-relocatable-schemas") == 0)
728 function = gsettings_list_relocatable_schemas;
730 else if (argc == 3 && strcmp (argv[1], "list-keys") == 0)
731 function = gsettings_list_keys;
733 else if (argc == 3 && strcmp (argv[1], "list-children") == 0)
734 function = gsettings_list_children;
736 else if ((argc == 2 || argc == 3) && strcmp (argv[1], "list-recursively") == 0)
737 function = gsettings_list_recursively;
739 else if (argc == 4 && strcmp (argv[1], "range") == 0)
740 function = gsettings_range;
742 else if (argc == 4 && strcmp (argv[1], "get") == 0)
743 function = gsettings_get;
745 else if (argc == 5 && strcmp (argv[1], "set") == 0)
746 function = gsettings_set;
748 else if (argc == 4 && strcmp (argv[1], "reset") == 0)
749 function = gsettings_reset;
751 else if (argc == 3 && strcmp (argv[1], "reset-recursively") == 0)
752 function = gsettings_reset_recursively;
754 else if (argc == 4 && strcmp (argv[1], "writable") == 0)
755 function = gsettings_writable;
757 else if ((argc == 3 || argc == 4) && strcmp (argv[1], "monitor") == 0)
758 function = gsettings_monitor;
761 return gsettings_help (FALSE, argv[1]);
767 if (argv[2][0] == '\0')
769 g_printerr (_("Empty schema name given\n"));
773 parts = g_strsplit (argv[2], ":", 2);
775 global_schema = g_settings_schema_source_lookup (global_schema_source, parts[0], TRUE);
778 if (!check_relocatable_schema (global_schema, parts[0]) || !check_path (parts[1]))
781 global_settings = g_settings_new_full (global_schema, NULL, parts[1]);
785 if (!check_schema (global_schema, parts[0]))
788 global_settings = g_settings_new_full (global_schema, NULL, NULL);
796 if (!g_settings_schema_has_key (global_schema, argv[3]))
798 g_printerr (_("No such key '%s'\n"), argv[3]);
802 global_key = argv[3];
803 global_schema_key = g_settings_schema_get_key (global_schema, global_key);
807 global_value = argv[4];
812 g_clear_pointer (&global_schema_source, g_settings_schema_source_unref);
813 g_clear_pointer (&global_schema_key, g_settings_schema_key_unref);
814 g_clear_pointer (&global_schema, g_settings_schema_unref);
815 g_clear_object (&global_settings);