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_list_schemas (GSettings *settings,
156 output_list (g_settings_list_schemas ());
160 gsettings_list_relocatable_schemas (GSettings *settings,
164 output_list (g_settings_list_relocatable_schemas ());
168 gsettings_list_keys (GSettings *settings,
174 keys = g_settings_list_keys (settings);
175 output_list ((const gchar **) keys);
180 gsettings_list_children (GSettings *settings,
188 children = g_settings_list_children (settings);
189 for (i = 0; children[i]; i++)
190 if (strlen (children[i]) > max)
191 max = strlen (children[i]);
193 for (i = 0; children[i]; i++)
196 GSettingsSchema *schema;
199 child = g_settings_get_child (settings, children[i]);
201 "settings-schema", &schema,
205 if (g_settings_schema_get_path (schema) != NULL)
206 g_print ("%-*s %s\n", max, children[i], g_settings_schema_get_id (schema));
208 g_print ("%-*s %s:%s\n", max, children[i], g_settings_schema_get_id (schema), path);
210 g_object_unref (child);
211 g_settings_schema_unref (schema);
215 g_strfreev (children);
219 enumerate (GSettings *settings)
225 g_object_get (settings, "schema-id", &schema, NULL);
227 keys = g_settings_list_keys (settings);
228 for (i = 0; keys[i]; i++)
233 value = g_settings_get_value (settings, keys[i]);
234 printed = g_variant_print (value, TRUE);
235 g_print ("%s %s %s\n", schema, keys[i], printed);
236 g_variant_unref (value);
245 gsettings_list_recursively (GSettings *settings,
254 enumerate (settings);
255 children = g_settings_list_children (settings);
256 for (i = 0; children[i]; i++)
260 child = g_settings_get_child (settings, children[i]);
261 gsettings_list_recursively (child, NULL, NULL);
262 g_object_unref (child);
265 g_strfreev (children);
269 const gchar * const *schemas;
272 schemas = g_settings_list_schemas ();
274 for (i = 0; schemas[i]; i++)
276 settings = g_settings_new (schemas[i]);
277 gsettings_list_recursively (settings, NULL, NULL);
278 g_object_unref (settings);
284 gsettings_range (GSettings *settings,
288 GVariant *range, *detail;
291 range = g_settings_get_range (settings, key);
292 g_variant_get (range, "(&sv)", &type, &detail);
294 if (strcmp (type, "type") == 0)
295 g_print ("type %s\n", g_variant_get_type_string (detail) + 1);
297 else if (strcmp (type, "range") == 0)
302 g_variant_get (detail, "(**)", &min, &max);
303 smin = g_variant_print (min, FALSE);
304 smax = g_variant_print (max, FALSE);
306 g_print ("range %s %s %s\n",
307 g_variant_get_type_string (min), smin, smax);
308 g_variant_unref (min);
309 g_variant_unref (max);
314 else if (strcmp (type, "enum") == 0 || strcmp (type, "flags") == 0)
319 g_print ("%s\n", type);
321 g_variant_iter_init (&iter, detail);
322 while (g_variant_iter_loop (&iter, "*", &item))
326 printed = g_variant_print (item, FALSE);
327 g_print ("%s\n", printed);
332 g_variant_unref (detail);
333 g_variant_unref (range);
337 gsettings_get (GSettings *settings,
344 value = g_settings_get_value (settings, key);
345 printed = g_variant_print (value, TRUE);
346 g_print ("%s\n", printed);
347 g_variant_unref (value);
352 gsettings_reset (GSettings *settings,
356 g_settings_reset (settings, key);
361 reset_all_keys (GSettings *settings)
366 keys = g_settings_list_keys (settings);
367 for (i = 0; keys[i]; i++)
369 g_settings_reset (settings, keys[i]);
376 gsettings_reset_recursively (GSettings *settings,
383 g_settings_delay (settings);
385 reset_all_keys (settings);
386 children = g_settings_list_children (settings);
387 for (i = 0; children[i]; i++)
390 child = g_settings_get_child (settings, children[i]);
392 reset_all_keys (child);
394 g_object_unref (child);
397 g_strfreev (children);
399 g_settings_apply (settings);
404 gsettings_writable (GSettings *settings,
409 g_settings_is_writable (settings, key) ?
414 value_changed (GSettings *settings,
421 value = g_settings_get_value (settings, key);
422 printed = g_variant_print (value, TRUE);
423 g_print ("%s: %s\n", key, printed);
424 g_variant_unref (value);
429 gsettings_monitor (GSettings *settings,
437 name = g_strdup_printf ("changed::%s", key);
438 g_signal_connect (settings, name, G_CALLBACK (value_changed), NULL);
441 g_signal_connect (settings, "changed", G_CALLBACK (value_changed), NULL);
443 g_main_loop_run (g_main_loop_new (NULL, FALSE));
447 gsettings_set (GSettings *settings,
451 const GVariantType *type;
452 GError *error = NULL;
455 gchar *freeme = NULL;
457 existing = g_settings_get_value (settings, key);
458 type = g_variant_get_type (existing);
460 new = g_variant_parse (type, value, NULL, NULL, &error);
462 /* If that didn't work and the type is string then we should assume
463 * that the user is just trying to set a string directly and forgot
464 * the quotes (or had them consumed by the shell).
466 * If the user started with a quote then we assume that some deeper
467 * problem is at play and we want the failure in that case.
471 * gsettings set x.y.z key "'i don't expect this to work'"
473 * Note that we should not just add quotes and try parsing again, but
474 * rather assume that the user is providing us with a bare string.
475 * Assume we added single quotes, then consider this case:
477 * gsettings set x.y.z key "i'd expect this to work"
479 * A similar example could be given for double quotes.
481 * Avoid that whole mess by just using g_variant_new_string().
484 g_variant_type_equal (type, G_VARIANT_TYPE_STRING) &&
485 value[0] != '\'' && value[0] != '"')
487 g_clear_error (&error);
488 new = g_variant_new_string (value);
491 /* we're done with 'type' now, so we can free 'existing' */
492 g_variant_unref (existing);
496 g_printerr ("%s\n", error->message);
500 if (!g_settings_range_check (settings, key, new))
502 g_printerr (_("The provided value is outside of the valid range\n"));
503 g_variant_unref (new);
507 g_settings_set_value (settings, key, new);
515 gsettings_help (gboolean requested,
516 const gchar *command)
518 const gchar *description;
519 const gchar *synopsis;
522 string = g_string_new (NULL);
527 else if (strcmp (command, "help") == 0)
529 description = _("Print help");
530 synopsis = "[COMMAND]";
533 else if (strcmp (command, "list-schemas") == 0)
535 description = _("List the installed (non-relocatable) schemas");
539 else if (strcmp (command, "list-relocatable-schemas") == 0)
541 description = _("List the installed relocatable schemas");
545 else if (strcmp (command, "list-keys") == 0)
547 description = _("List the keys in SCHEMA");
548 synopsis = N_("SCHEMA[:PATH]");
551 else if (strcmp (command, "list-children") == 0)
553 description = _("List the children of SCHEMA");
554 synopsis = N_("SCHEMA[:PATH]");
557 else if (strcmp (command, "list-recursively") == 0)
559 description = _("List keys and values, recursively\n"
560 "If no SCHEMA is given, list all keys\n");
561 synopsis = N_("[SCHEMA[:PATH]]");
564 else if (strcmp (command, "get") == 0)
566 description = _("Get the value of KEY");
567 synopsis = N_("SCHEMA[:PATH] KEY");
570 else if (strcmp (command, "range") == 0)
572 description = _("Query the range of valid values for KEY");
573 synopsis = N_("SCHEMA[:PATH] KEY");
576 else if (strcmp (command, "set") == 0)
578 description = _("Set the value of KEY to VALUE");
579 synopsis = N_("SCHEMA[:PATH] KEY VALUE");
582 else if (strcmp (command, "reset") == 0)
584 description = _("Reset KEY to its default value");
585 synopsis = N_("SCHEMA[:PATH] KEY");
588 else if (strcmp (command, "reset-recursively") == 0)
590 description = _("Reset all keys in SCHEMA to their defaults");
591 synopsis = N_("SCHEMA[:PATH]");
594 else if (strcmp (command, "writable") == 0)
596 description = _("Check if KEY is writable");
597 synopsis = N_("SCHEMA[:PATH] KEY");
600 else if (strcmp (command, "monitor") == 0)
602 description = _("Monitor KEY for changes.\n"
603 "If no KEY is specified, monitor all keys in SCHEMA.\n"
604 "Use ^C to stop monitoring.\n");
605 synopsis = N_("SCHEMA[:PATH] [KEY]");
609 g_string_printf (string, _("Unknown command %s\n\n"), command);
616 g_string_append (string,
618 " gsettings [--schemadir SCHEMADIR] COMMAND [ARGS...]\n"
621 " help Show this information\n"
622 " list-schemas List installed schemas\n"
623 " list-relocatable-schemas List relocatable schemas\n"
624 " list-keys List keys in a schema\n"
625 " list-children List children of a schema\n"
626 " list-recursively List keys and values, recursively\n"
627 " range Queries the range of a key\n"
628 " get Get the value of a key\n"
629 " set Set the value of a key\n"
630 " reset Reset the value of a key\n"
631 " reset-recursively Reset all values in a given schema\n"
632 " writable Check if a key is writable\n"
633 " monitor Watch for changes\n"
635 "Use 'gsettings help COMMAND' to get detailed help.\n\n"));
639 g_string_append_printf (string, _("Usage:\n gsettings [--schemadir SCHEMADIR] %s %s\n\n%s\n\n"),
640 command, synopsis[0] ? _(synopsis) : "", description);
642 g_string_append (string, _("Arguments:\n"));
644 g_string_append (string,
645 _(" SCHEMADIR A directory to search for additional schemas\n"));
647 if (strstr (synopsis, "[COMMAND]"))
648 g_string_append (string,
649 _(" COMMAND The (optional) command to explain\n"));
651 else if (strstr (synopsis, "SCHEMA"))
652 g_string_append (string,
653 _(" SCHEMA The name of the schema\n"
654 " PATH The path, for relocatable schemas\n"));
656 if (strstr (synopsis, "[KEY]"))
657 g_string_append (string,
658 _(" KEY The (optional) key within the schema\n"));
660 else if (strstr (synopsis, "KEY"))
661 g_string_append (string,
662 _(" KEY The key within the schema\n"));
664 if (strstr (synopsis, "VALUE"))
665 g_string_append (string,
666 _(" VALUE The value to set\n"));
668 g_string_append (string, "\n");
672 g_print ("%s", string->str);
674 g_printerr ("%s\n", string->str);
676 g_string_free (string, TRUE);
678 return requested ? 0 : 1;
683 main (int argc, char **argv)
685 void (* function) (GSettings *, const gchar *, const gchar *);
686 GSettingsSchemaSource *schema_source;
687 GSettingsSchema *schema;
695 setlocale (LC_ALL, "");
696 textdomain (GETTEXT_PACKAGE);
699 tmp = _glib_get_locale_dir ();
700 bindtextdomain (GETTEXT_PACKAGE, tmp);
703 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
706 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
707 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
711 return gsettings_help (FALSE, NULL);
713 schema_source = g_settings_schema_source_ref (g_settings_schema_source_get_default ());
715 if (argc > 3 && g_str_equal (argv[1], "--schemadir"))
717 GSettingsSchemaSource *parent = schema_source;
718 GError *error = NULL;
720 schema_source = g_settings_schema_source_new_from_directory (argv[2], parent, FALSE, &error);
721 g_settings_schema_source_unref (parent);
723 if (schema_source == NULL)
725 g_printerr ("Could not load schemas from %s: %s\n", argv[2], error->message);
726 g_clear_error (&error);
731 /* shift remaining arguments (not correct wrt argv[0], but doesn't matter) */
736 if (strcmp (argv[1], "help") == 0)
737 return gsettings_help (TRUE, argv[2]);
739 else if (argc == 2 && strcmp (argv[1], "list-schemas") == 0)
740 function = gsettings_list_schemas;
742 else if (argc == 2 && strcmp (argv[1], "list-relocatable-schemas") == 0)
743 function = gsettings_list_relocatable_schemas;
745 else if (argc == 3 && strcmp (argv[1], "list-keys") == 0)
746 function = gsettings_list_keys;
748 else if (argc == 3 && strcmp (argv[1], "list-children") == 0)
749 function = gsettings_list_children;
751 else if ((argc == 2 || argc == 3) && strcmp (argv[1], "list-recursively") == 0)
752 function = gsettings_list_recursively;
754 else if (argc == 4 && strcmp (argv[1], "range") == 0)
755 function = gsettings_range;
757 else if (argc == 4 && strcmp (argv[1], "get") == 0)
758 function = gsettings_get;
760 else if (argc == 5 && strcmp (argv[1], "set") == 0)
761 function = gsettings_set;
763 else if (argc == 4 && strcmp (argv[1], "reset") == 0)
764 function = gsettings_reset;
766 else if (argc == 3 && strcmp (argv[1], "reset-recursively") == 0)
767 function = gsettings_reset_recursively;
769 else if (argc == 4 && strcmp (argv[1], "writable") == 0)
770 function = gsettings_writable;
772 else if ((argc == 3 || argc == 4) && strcmp (argv[1], "monitor") == 0)
773 function = gsettings_monitor;
776 return gsettings_help (FALSE, argv[1]);
782 if (argv[2][0] == '\0')
784 g_printerr (_("Empty schema name given\n"));
788 parts = g_strsplit (argv[2], ":", 2);
790 schema = g_settings_schema_source_lookup (schema_source, parts[0], TRUE);
793 if (!check_relocatable_schema (schema, parts[0]) || !check_path (parts[1]))
796 settings = g_settings_new_full (schema, NULL, parts[1]);
800 if (!check_schema (schema, parts[0]))
803 settings = g_settings_new_full (schema, NULL, NULL);
816 if (!check_key (settings, argv[3]))
824 (* function) (settings, key, argc > 4 ? argv[4] : NULL);
826 if (settings != NULL)
827 g_object_unref (settings);
829 g_settings_schema_unref (schema);
831 g_settings_schema_source_unref (schema_source);