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 g_settings_set_value (settings, key, new);
524 gsettings_help (gboolean requested,
525 const gchar *command)
527 const gchar *description;
528 const gchar *synopsis;
531 string = g_string_new (NULL);
536 else if (strcmp (command, "help") == 0)
538 description = _("Print help");
539 synopsis = "[COMMAND]";
542 else if (strcmp (command, "--version") == 0)
544 description = _("Print version information and exit");
548 else if (strcmp (command, "list-schemas") == 0)
550 description = _("List the installed (non-relocatable) schemas");
554 else if (strcmp (command, "list-relocatable-schemas") == 0)
556 description = _("List the installed relocatable schemas");
560 else if (strcmp (command, "list-keys") == 0)
562 description = _("List the keys in SCHEMA");
563 synopsis = N_("SCHEMA[:PATH]");
566 else if (strcmp (command, "list-children") == 0)
568 description = _("List the children of SCHEMA");
569 synopsis = N_("SCHEMA[:PATH]");
572 else if (strcmp (command, "list-recursively") == 0)
574 description = _("List keys and values, recursively\n"
575 "If no SCHEMA is given, list all keys\n");
576 synopsis = N_("[SCHEMA[:PATH]]");
579 else if (strcmp (command, "get") == 0)
581 description = _("Get the value of KEY");
582 synopsis = N_("SCHEMA[:PATH] KEY");
585 else if (strcmp (command, "range") == 0)
587 description = _("Query the range of valid values for KEY");
588 synopsis = N_("SCHEMA[:PATH] KEY");
591 else if (strcmp (command, "set") == 0)
593 description = _("Set the value of KEY to VALUE");
594 synopsis = N_("SCHEMA[:PATH] KEY VALUE");
597 else if (strcmp (command, "reset") == 0)
599 description = _("Reset KEY to its default value");
600 synopsis = N_("SCHEMA[:PATH] KEY");
603 else if (strcmp (command, "reset-recursively") == 0)
605 description = _("Reset all keys in SCHEMA to their defaults");
606 synopsis = N_("SCHEMA[:PATH]");
609 else if (strcmp (command, "writable") == 0)
611 description = _("Check if KEY is writable");
612 synopsis = N_("SCHEMA[:PATH] KEY");
615 else if (strcmp (command, "monitor") == 0)
617 description = _("Monitor KEY for changes.\n"
618 "If no KEY is specified, monitor all keys in SCHEMA.\n"
619 "Use ^C to stop monitoring.\n");
620 synopsis = N_("SCHEMA[:PATH] [KEY]");
624 g_string_printf (string, _("Unknown command %s\n\n"), command);
631 g_string_append (string,
633 " gsettings [--schemadir SCHEMADIR] COMMAND [ARGS...]\n"
636 " help Show this information\n"
637 " list-schemas List installed schemas\n"
638 " list-relocatable-schemas List relocatable schemas\n"
639 " list-keys List keys in a schema\n"
640 " list-children List children of a schema\n"
641 " list-recursively List keys and values, recursively\n"
642 " range Queries the range of a key\n"
643 " get Get the value of a key\n"
644 " set Set the value of a key\n"
645 " reset Reset the value of a key\n"
646 " reset-recursively Reset all values in a given schema\n"
647 " writable Check if a key is writable\n"
648 " monitor Watch for changes\n"
650 "Use 'gsettings help COMMAND' to get detailed help.\n\n"));
654 g_string_append_printf (string, _("Usage:\n gsettings [--schemadir SCHEMADIR] %s %s\n\n%s\n\n"),
655 command, synopsis[0] ? _(synopsis) : "", description);
657 g_string_append (string, _("Arguments:\n"));
659 g_string_append (string,
660 _(" SCHEMADIR A directory to search for additional schemas\n"));
662 if (strstr (synopsis, "[COMMAND]"))
663 g_string_append (string,
664 _(" COMMAND The (optional) command to explain\n"));
666 else if (strstr (synopsis, "SCHEMA"))
667 g_string_append (string,
668 _(" SCHEMA The name of the schema\n"
669 " PATH The path, for relocatable schemas\n"));
671 if (strstr (synopsis, "[KEY]"))
672 g_string_append (string,
673 _(" KEY The (optional) key within the schema\n"));
675 else if (strstr (synopsis, "KEY"))
676 g_string_append (string,
677 _(" KEY The key within the schema\n"));
679 if (strstr (synopsis, "VALUE"))
680 g_string_append (string,
681 _(" VALUE The value to set\n"));
683 g_string_append (string, "\n");
687 g_print ("%s", string->str);
689 g_printerr ("%s\n", string->str);
691 g_string_free (string, TRUE);
693 return requested ? 0 : 1;
698 main (int argc, char **argv)
700 void (* function) (GSettings *, const gchar *, const gchar *);
701 GSettingsSchemaSource *schema_source;
702 GSettingsSchema *schema;
710 setlocale (LC_ALL, "");
711 textdomain (GETTEXT_PACKAGE);
714 tmp = _glib_get_locale_dir ();
715 bindtextdomain (GETTEXT_PACKAGE, tmp);
718 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
721 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
722 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
726 return gsettings_help (FALSE, NULL);
728 schema_source = g_settings_schema_source_ref (g_settings_schema_source_get_default ());
730 if (argc > 3 && g_str_equal (argv[1], "--schemadir"))
732 GSettingsSchemaSource *parent = schema_source;
733 GError *error = NULL;
735 schema_source = g_settings_schema_source_new_from_directory (argv[2], parent, FALSE, &error);
736 g_settings_schema_source_unref (parent);
738 if (schema_source == NULL)
740 g_printerr (_("Could not load schemas from %s: %s\n"), argv[2], error->message);
741 g_clear_error (&error);
746 /* shift remaining arguments (not correct wrt argv[0], but doesn't matter) */
751 if (strcmp (argv[1], "help") == 0)
752 return gsettings_help (TRUE, argv[2]);
754 else if (argc == 2 && strcmp (argv[1], "--version") == 0)
755 function = gsettings_print_version;
757 else if (argc == 2 && strcmp (argv[1], "list-schemas") == 0)
758 function = gsettings_list_schemas;
760 else if (argc == 2 && strcmp (argv[1], "list-relocatable-schemas") == 0)
761 function = gsettings_list_relocatable_schemas;
763 else if (argc == 3 && strcmp (argv[1], "list-keys") == 0)
764 function = gsettings_list_keys;
766 else if (argc == 3 && strcmp (argv[1], "list-children") == 0)
767 function = gsettings_list_children;
769 else if ((argc == 2 || argc == 3) && strcmp (argv[1], "list-recursively") == 0)
770 function = gsettings_list_recursively;
772 else if (argc == 4 && strcmp (argv[1], "range") == 0)
773 function = gsettings_range;
775 else if (argc == 4 && strcmp (argv[1], "get") == 0)
776 function = gsettings_get;
778 else if (argc == 5 && strcmp (argv[1], "set") == 0)
779 function = gsettings_set;
781 else if (argc == 4 && strcmp (argv[1], "reset") == 0)
782 function = gsettings_reset;
784 else if (argc == 3 && strcmp (argv[1], "reset-recursively") == 0)
785 function = gsettings_reset_recursively;
787 else if (argc == 4 && strcmp (argv[1], "writable") == 0)
788 function = gsettings_writable;
790 else if ((argc == 3 || argc == 4) && strcmp (argv[1], "monitor") == 0)
791 function = gsettings_monitor;
794 return gsettings_help (FALSE, argv[1]);
800 if (argv[2][0] == '\0')
802 g_printerr (_("Empty schema name given\n"));
806 parts = g_strsplit (argv[2], ":", 2);
808 schema = g_settings_schema_source_lookup (schema_source, parts[0], TRUE);
811 if (!check_relocatable_schema (schema, parts[0]) || !check_path (parts[1]))
814 settings = g_settings_new_full (schema, NULL, parts[1]);
818 if (!check_schema (schema, parts[0]))
821 settings = g_settings_new_full (schema, NULL, NULL);
834 if (!check_key (settings, argv[3]))
842 (* function) (settings, key, argc > 4 ? argv[4] : NULL);
844 if (settings != NULL)
845 g_object_unref (settings);
847 g_settings_schema_unref (schema);
849 g_settings_schema_source_unref (schema_source);