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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Author: Ryan Lortie <desrt@desrt.ca>
31 #include "glib/glib-private.h"
35 contained (const gchar * const *items,
39 if (strcmp (*items++, item) == 0)
46 is_relocatable_schema (GSettingsSchema *schema)
48 return g_settings_schema_get_path (schema) == NULL;
52 check_relocatable_schema (GSettingsSchema *schema,
53 const gchar *schema_id)
57 g_printerr (_("No such schema '%s'\n"), schema_id);
61 if (!is_relocatable_schema (schema))
63 g_printerr (_("Schema '%s' is not relocatable "
64 "(path must not be specified)\n"),
73 check_schema (GSettingsSchema *schema,
74 const gchar *schema_id)
78 g_printerr (_("No such schema '%s'\n"), schema_id);
82 if (is_relocatable_schema (schema))
84 g_printerr (_("Schema '%s' is relocatable "
85 "(path must be specified)\n"),
94 check_path (const gchar *path)
98 g_printerr (_("Empty path given.\n"));
104 g_printerr (_("Path must begin with a slash (/)\n"));
108 if (!g_str_has_suffix (path, "/"))
110 g_printerr (_("Path must end with a slash (/)\n"));
114 if (strstr (path, "//"))
116 g_printerr (_("Path must not contain two adjacent slashes (//)\n"));
124 check_key (GSettings *settings,
130 keys = g_settings_list_keys (settings);
131 good = contained ((const gchar **) keys, key);
137 g_printerr (_("No such key '%s'\n"), key);
143 output_list (const gchar * const *list)
147 for (i = 0; list[i]; i++)
148 g_print ("%s\n", list[i]);
152 gsettings_print_version (GSettings *settings,
156 g_print ("%d.%d.%d\n", glib_major_version, glib_minor_version,
161 gsettings_list_schemas (GSettings *settings,
165 output_list (g_settings_list_schemas ());
169 gsettings_list_relocatable_schemas (GSettings *settings,
173 output_list (g_settings_list_relocatable_schemas ());
177 gsettings_list_keys (GSettings *settings,
183 keys = g_settings_list_keys (settings);
184 output_list ((const gchar **) keys);
189 gsettings_list_children (GSettings *settings,
197 children = g_settings_list_children (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 (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)
234 g_object_get (settings, "schema-id", &schema, NULL);
236 keys = g_settings_list_keys (settings);
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", schema, keys[i], printed);
245 g_variant_unref (value);
254 gsettings_list_recursively (GSettings *settings,
263 enumerate (settings);
264 children = g_settings_list_children (settings);
265 for (i = 0; children[i]; i++)
269 child = g_settings_get_child (settings, children[i]);
270 gsettings_list_recursively (child, NULL, NULL);
271 g_object_unref (child);
274 g_strfreev (children);
278 const gchar * const *schemas;
281 schemas = g_settings_list_schemas ();
283 for (i = 0; schemas[i]; i++)
285 settings = g_settings_new (schemas[i]);
286 gsettings_list_recursively (settings, NULL, NULL);
287 g_object_unref (settings);
293 gsettings_range (GSettings *settings,
297 GVariant *range, *detail;
300 range = g_settings_get_range (settings, key);
301 g_variant_get (range, "(&sv)", &type, &detail);
303 if (strcmp (type, "type") == 0)
304 g_print ("type %s\n", g_variant_get_type_string (detail) + 1);
306 else if (strcmp (type, "range") == 0)
311 g_variant_get (detail, "(**)", &min, &max);
312 smin = g_variant_print (min, FALSE);
313 smax = g_variant_print (max, FALSE);
315 g_print ("range %s %s %s\n",
316 g_variant_get_type_string (min), smin, smax);
317 g_variant_unref (min);
318 g_variant_unref (max);
323 else if (strcmp (type, "enum") == 0 || strcmp (type, "flags") == 0)
328 g_print ("%s\n", type);
330 g_variant_iter_init (&iter, detail);
331 while (g_variant_iter_loop (&iter, "*", &item))
335 printed = g_variant_print (item, FALSE);
336 g_print ("%s\n", printed);
341 g_variant_unref (detail);
342 g_variant_unref (range);
346 gsettings_get (GSettings *settings,
353 value = g_settings_get_value (settings, key);
354 printed = g_variant_print (value, TRUE);
355 g_print ("%s\n", printed);
356 g_variant_unref (value);
361 gsettings_reset (GSettings *settings,
365 g_settings_reset (settings, key);
370 reset_all_keys (GSettings *settings)
375 keys = g_settings_list_keys (settings);
376 for (i = 0; keys[i]; i++)
378 g_settings_reset (settings, keys[i]);
385 gsettings_reset_recursively (GSettings *settings,
392 g_settings_delay (settings);
394 reset_all_keys (settings);
395 children = g_settings_list_children (settings);
396 for (i = 0; children[i]; i++)
399 child = g_settings_get_child (settings, children[i]);
401 reset_all_keys (child);
403 g_object_unref (child);
406 g_strfreev (children);
408 g_settings_apply (settings);
413 gsettings_writable (GSettings *settings,
418 g_settings_is_writable (settings, key) ?
423 value_changed (GSettings *settings,
430 value = g_settings_get_value (settings, key);
431 printed = g_variant_print (value, TRUE);
432 g_print ("%s: %s\n", key, printed);
433 g_variant_unref (value);
438 gsettings_monitor (GSettings *settings,
446 name = g_strdup_printf ("changed::%s", key);
447 g_signal_connect (settings, name, G_CALLBACK (value_changed), NULL);
450 g_signal_connect (settings, "changed", G_CALLBACK (value_changed), NULL);
452 g_main_loop_run (g_main_loop_new (NULL, FALSE));
456 gsettings_set (GSettings *settings,
460 const GVariantType *type;
461 GError *error = NULL;
464 gchar *freeme = NULL;
466 existing = g_settings_get_value (settings, key);
467 type = g_variant_get_type (existing);
469 new = g_variant_parse (type, value, NULL, NULL, &error);
471 /* If that didn't work and the type is string then we should assume
472 * that the user is just trying to set a string directly and forgot
473 * the quotes (or had them consumed by the shell).
475 * If the user started with a quote then we assume that some deeper
476 * problem is at play and we want the failure in that case.
480 * gsettings set x.y.z key "'i don't expect this to work'"
482 * Note that we should not just add quotes and try parsing again, but
483 * rather assume that the user is providing us with a bare string.
484 * Assume we added single quotes, then consider this case:
486 * gsettings set x.y.z key "i'd expect this to work"
488 * A similar example could be given for double quotes.
490 * Avoid that whole mess by just using g_variant_new_string().
493 g_variant_type_equal (type, G_VARIANT_TYPE_STRING) &&
494 value[0] != '\'' && value[0] != '"')
496 g_clear_error (&error);
497 new = g_variant_new_string (value);
500 /* we're done with 'type' now, so we can free 'existing' */
501 g_variant_unref (existing);
505 g_printerr ("%s\n", error->message);
509 if (!g_settings_range_check (settings, key, new))
511 g_printerr (_("The provided value is outside of the valid range\n"));
512 g_variant_unref (new);
516 if (!g_settings_set_value (settings, key, new))
518 g_printerr (_("The key is not writable\n"));
528 gsettings_help (gboolean requested,
529 const gchar *command)
531 const gchar *description;
532 const gchar *synopsis;
535 string = g_string_new (NULL);
540 else if (strcmp (command, "help") == 0)
542 description = _("Print help");
543 synopsis = "[COMMAND]";
546 else if (strcmp (command, "--version") == 0)
548 description = _("Print version information and exit");
552 else if (strcmp (command, "list-schemas") == 0)
554 description = _("List the installed (non-relocatable) schemas");
558 else if (strcmp (command, "list-relocatable-schemas") == 0)
560 description = _("List the installed relocatable schemas");
564 else if (strcmp (command, "list-keys") == 0)
566 description = _("List the keys in SCHEMA");
567 synopsis = N_("SCHEMA[:PATH]");
570 else if (strcmp (command, "list-children") == 0)
572 description = _("List the children of SCHEMA");
573 synopsis = N_("SCHEMA[:PATH]");
576 else if (strcmp (command, "list-recursively") == 0)
578 description = _("List keys and values, recursively\n"
579 "If no SCHEMA is given, list all keys\n");
580 synopsis = N_("[SCHEMA[:PATH]]");
583 else if (strcmp (command, "get") == 0)
585 description = _("Get the value of KEY");
586 synopsis = N_("SCHEMA[:PATH] KEY");
589 else if (strcmp (command, "range") == 0)
591 description = _("Query the range of valid values for KEY");
592 synopsis = N_("SCHEMA[:PATH] KEY");
595 else if (strcmp (command, "set") == 0)
597 description = _("Set the value of KEY to VALUE");
598 synopsis = N_("SCHEMA[:PATH] KEY VALUE");
601 else if (strcmp (command, "reset") == 0)
603 description = _("Reset KEY to its default value");
604 synopsis = N_("SCHEMA[:PATH] KEY");
607 else if (strcmp (command, "reset-recursively") == 0)
609 description = _("Reset all keys in SCHEMA to their defaults");
610 synopsis = N_("SCHEMA[:PATH]");
613 else if (strcmp (command, "writable") == 0)
615 description = _("Check if KEY is writable");
616 synopsis = N_("SCHEMA[:PATH] KEY");
619 else if (strcmp (command, "monitor") == 0)
621 description = _("Monitor KEY for changes.\n"
622 "If no KEY is specified, monitor all keys in SCHEMA.\n"
623 "Use ^C to stop monitoring.\n");
624 synopsis = N_("SCHEMA[:PATH] [KEY]");
628 g_string_printf (string, _("Unknown command %s\n\n"), command);
635 g_string_append (string,
637 " gsettings --version\n"
638 " gsettings [--schemadir SCHEMADIR] COMMAND [ARGS...]\n"
641 " help Show this information\n"
642 " list-schemas List installed schemas\n"
643 " list-relocatable-schemas List relocatable schemas\n"
644 " list-keys List keys in a schema\n"
645 " list-children List children of a schema\n"
646 " list-recursively List keys and values, recursively\n"
647 " range Queries the range of a key\n"
648 " get Get the value of a key\n"
649 " set Set the value of a key\n"
650 " reset Reset the value of a key\n"
651 " reset-recursively Reset all values in a given schema\n"
652 " writable Check if a key is writable\n"
653 " monitor Watch for changes\n"
655 "Use 'gsettings help COMMAND' to get detailed help.\n\n"));
659 g_string_append_printf (string, _("Usage:\n gsettings [--schemadir SCHEMADIR] %s %s\n\n%s\n\n"),
660 command, synopsis[0] ? _(synopsis) : "", description);
662 g_string_append (string, _("Arguments:\n"));
664 g_string_append (string,
665 _(" SCHEMADIR A directory to search for additional schemas\n"));
667 if (strstr (synopsis, "[COMMAND]"))
668 g_string_append (string,
669 _(" COMMAND The (optional) command to explain\n"));
671 else if (strstr (synopsis, "SCHEMA"))
672 g_string_append (string,
673 _(" SCHEMA The name of the schema\n"
674 " PATH The path, for relocatable schemas\n"));
676 if (strstr (synopsis, "[KEY]"))
677 g_string_append (string,
678 _(" KEY The (optional) key within the schema\n"));
680 else if (strstr (synopsis, "KEY"))
681 g_string_append (string,
682 _(" KEY The key within the schema\n"));
684 if (strstr (synopsis, "VALUE"))
685 g_string_append (string,
686 _(" VALUE The value to set\n"));
688 g_string_append (string, "\n");
692 g_print ("%s", string->str);
694 g_printerr ("%s\n", string->str);
696 g_string_free (string, TRUE);
698 return requested ? 0 : 1;
703 main (int argc, char **argv)
705 void (* function) (GSettings *, const gchar *, const gchar *);
706 GSettingsSchemaSource *schema_source;
707 GSettingsSchema *schema;
715 setlocale (LC_ALL, "");
716 textdomain (GETTEXT_PACKAGE);
719 tmp = _glib_get_locale_dir ();
720 bindtextdomain (GETTEXT_PACKAGE, tmp);
723 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
726 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
727 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
731 return gsettings_help (FALSE, NULL);
733 schema_source = g_settings_schema_source_ref (g_settings_schema_source_get_default ());
735 if (argc > 3 && g_str_equal (argv[1], "--schemadir"))
737 GSettingsSchemaSource *parent = schema_source;
738 GError *error = NULL;
740 schema_source = g_settings_schema_source_new_from_directory (argv[2], parent, FALSE, &error);
741 g_settings_schema_source_unref (parent);
743 if (schema_source == NULL)
745 g_printerr (_("Could not load schemas from %s: %s\n"), argv[2], error->message);
746 g_clear_error (&error);
751 /* shift remaining arguments (not correct wrt argv[0], but doesn't matter) */
756 if (strcmp (argv[1], "help") == 0)
757 return gsettings_help (TRUE, argv[2]);
759 else if (argc == 2 && strcmp (argv[1], "--version") == 0)
760 function = gsettings_print_version;
762 else if (argc == 2 && strcmp (argv[1], "list-schemas") == 0)
763 function = gsettings_list_schemas;
765 else if (argc == 2 && strcmp (argv[1], "list-relocatable-schemas") == 0)
766 function = gsettings_list_relocatable_schemas;
768 else if (argc == 3 && strcmp (argv[1], "list-keys") == 0)
769 function = gsettings_list_keys;
771 else if (argc == 3 && strcmp (argv[1], "list-children") == 0)
772 function = gsettings_list_children;
774 else if ((argc == 2 || argc == 3) && strcmp (argv[1], "list-recursively") == 0)
775 function = gsettings_list_recursively;
777 else if (argc == 4 && strcmp (argv[1], "range") == 0)
778 function = gsettings_range;
780 else if (argc == 4 && strcmp (argv[1], "get") == 0)
781 function = gsettings_get;
783 else if (argc == 5 && strcmp (argv[1], "set") == 0)
784 function = gsettings_set;
786 else if (argc == 4 && strcmp (argv[1], "reset") == 0)
787 function = gsettings_reset;
789 else if (argc == 3 && strcmp (argv[1], "reset-recursively") == 0)
790 function = gsettings_reset_recursively;
792 else if (argc == 4 && strcmp (argv[1], "writable") == 0)
793 function = gsettings_writable;
795 else if ((argc == 3 || argc == 4) && strcmp (argv[1], "monitor") == 0)
796 function = gsettings_monitor;
799 return gsettings_help (FALSE, argv[1]);
805 if (argv[2][0] == '\0')
807 g_printerr (_("Empty schema name given\n"));
811 parts = g_strsplit (argv[2], ":", 2);
813 schema = g_settings_schema_source_lookup (schema_source, parts[0], TRUE);
816 if (!check_relocatable_schema (schema, parts[0]) || !check_path (parts[1]))
819 settings = g_settings_new_full (schema, NULL, parts[1]);
823 if (!check_schema (schema, parts[0]))
826 settings = g_settings_new_full (schema, NULL, NULL);
839 if (!check_key (settings, argv[3]))
847 (* function) (settings, key, argc > 4 ? argv[4] : NULL);
849 if (settings != NULL)
850 g_object_unref (settings);
852 g_settings_schema_unref (schema);
854 g_settings_schema_source_unref (schema_source);