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"
34 static GSettingsSchemaSource *global_schema_source;
35 static GSettings *global_settings;
36 static GSettingsSchema *global_schema;
37 static GSettingsSchemaKey *global_schema_key;
38 const gchar *global_key;
39 const gchar *global_value;
42 is_relocatable_schema (GSettingsSchema *schema)
44 return g_settings_schema_get_path (schema) == NULL;
48 check_relocatable_schema (GSettingsSchema *schema,
49 const gchar *schema_id)
53 g_printerr (_("No such schema '%s'\n"), schema_id);
57 if (!is_relocatable_schema (schema))
59 g_printerr (_("Schema '%s' is not relocatable "
60 "(path must not be specified)\n"),
69 check_schema (GSettingsSchema *schema,
70 const gchar *schema_id)
74 g_printerr (_("No such schema '%s'\n"), schema_id);
78 if (is_relocatable_schema (schema))
80 g_printerr (_("Schema '%s' is relocatable "
81 "(path must be specified)\n"),
90 check_path (const gchar *path)
94 g_printerr (_("Empty path given.\n"));
100 g_printerr (_("Path must begin with a slash (/)\n"));
104 if (!g_str_has_suffix (path, "/"))
106 g_printerr (_("Path must end with a slash (/)\n"));
110 if (strstr (path, "//"))
112 g_printerr (_("Path must not contain two adjacent slashes (//)\n"));
120 output_list (gchar **list)
124 for (i = 0; list[i]; i++)
125 g_print ("%s\n", list[i]);
129 gsettings_print_version (void)
131 g_print ("%d.%d.%d\n", glib_major_version, glib_minor_version,
136 gsettings_list_schemas (void)
140 g_settings_schema_source_list_schemas (global_schema_source, TRUE, &schemas, NULL);
141 output_list (schemas);
142 g_strfreev (schemas);
146 gsettings_list_relocatable_schemas (void)
150 g_settings_schema_source_list_schemas (global_schema_source, TRUE, NULL, &schemas);
151 output_list (schemas);
152 g_strfreev (schemas);
156 gsettings_list_keys (void)
160 keys = g_settings_list_keys (global_settings);
166 gsettings_list_children (void)
172 children = g_settings_list_children (global_settings);
173 for (i = 0; children[i]; i++)
174 if (strlen (children[i]) > max)
175 max = strlen (children[i]);
177 for (i = 0; children[i]; i++)
180 GSettingsSchema *schema;
183 child = g_settings_get_child (global_settings, children[i]);
185 "settings-schema", &schema,
189 if (g_settings_schema_get_path (schema) != NULL)
190 g_print ("%-*s %s\n", max, children[i], g_settings_schema_get_id (schema));
192 g_print ("%-*s %s:%s\n", max, children[i], g_settings_schema_get_id (schema), path);
194 g_object_unref (child);
195 g_settings_schema_unref (schema);
199 g_strfreev (children);
203 enumerate (GSettings *settings)
209 g_object_get (settings, "schema-id", &schema, NULL);
211 keys = g_settings_list_keys (settings);
212 for (i = 0; keys[i]; i++)
217 value = g_settings_get_value (settings, keys[i]);
218 printed = g_variant_print (value, TRUE);
219 g_print ("%s %s %s\n", schema, keys[i], printed);
220 g_variant_unref (value);
229 list_recursively (GSettings *settings)
234 enumerate (settings);
235 children = g_settings_list_children (settings);
236 for (i = 0; children[i]; i++)
240 child = g_settings_get_child (settings, children[i]);
241 list_recursively (child);
242 g_object_unref (child);
245 g_strfreev (children);
249 gsettings_list_recursively (void)
253 list_recursively (global_settings);
260 g_settings_schema_source_list_schemas (global_schema_source, TRUE, &schemas, NULL);
262 for (i = 0; schemas[i]; i++)
266 settings = g_settings_new (schemas[i]);
267 list_recursively (settings);
268 g_object_unref (settings);
271 g_strfreev (schemas);
276 gsettings_range (void)
278 GVariant *range, *detail;
281 range = g_settings_schema_key_get_range (global_schema_key);
282 g_variant_get (range, "(&sv)", &type, &detail);
284 if (strcmp (type, "type") == 0)
285 g_print ("type %s\n", g_variant_get_type_string (detail) + 1);
287 else if (strcmp (type, "range") == 0)
292 g_variant_get (detail, "(**)", &min, &max);
293 smin = g_variant_print (min, FALSE);
294 smax = g_variant_print (max, FALSE);
296 g_print ("range %s %s %s\n",
297 g_variant_get_type_string (min), smin, smax);
298 g_variant_unref (min);
299 g_variant_unref (max);
304 else if (strcmp (type, "enum") == 0 || strcmp (type, "flags") == 0)
309 g_print ("%s\n", type);
311 g_variant_iter_init (&iter, detail);
312 while (g_variant_iter_loop (&iter, "*", &item))
316 printed = g_variant_print (item, FALSE);
317 g_print ("%s\n", printed);
322 g_variant_unref (detail);
323 g_variant_unref (range);
332 value = g_settings_get_value (global_settings, global_key);
333 printed = g_variant_print (value, TRUE);
334 g_print ("%s\n", printed);
335 g_variant_unref (value);
340 gsettings_reset (void)
342 g_settings_reset (global_settings, global_key);
347 reset_all_keys (GSettings *settings)
352 keys = g_settings_list_keys (settings);
353 for (i = 0; keys[i]; i++)
355 g_settings_reset (settings, keys[i]);
362 gsettings_reset_recursively (void)
367 g_settings_delay (global_settings);
369 reset_all_keys (global_settings);
370 children = g_settings_list_children (global_settings);
371 for (i = 0; children[i]; i++)
374 child = g_settings_get_child (global_settings, children[i]);
376 reset_all_keys (child);
378 g_object_unref (child);
381 g_strfreev (children);
383 g_settings_apply (global_settings);
388 gsettings_writable (void)
391 g_settings_is_writable (global_settings, global_key) ?
396 value_changed (GSettings *settings,
403 value = g_settings_get_value (settings, key);
404 printed = g_variant_print (value, TRUE);
405 g_print ("%s: %s\n", key, printed);
406 g_variant_unref (value);
411 gsettings_monitor (void)
417 name = g_strdup_printf ("changed::%s", global_key);
418 g_signal_connect (global_settings, name, G_CALLBACK (value_changed), NULL);
421 g_signal_connect (global_settings, "changed", G_CALLBACK (value_changed), NULL);
423 g_main_loop_run (g_main_loop_new (NULL, FALSE));
429 const GVariantType *type;
430 GError *error = NULL;
432 gchar *freeme = NULL;
434 type = g_settings_schema_key_get_value_type (global_schema_key);
436 new = g_variant_parse (type, global_value, NULL, NULL, &error);
438 /* If that didn't work and the type is string then we should assume
439 * that the user is just trying to set a string directly and forgot
440 * the quotes (or had them consumed by the shell).
442 * If the user started with a quote then we assume that some deeper
443 * problem is at play and we want the failure in that case.
447 * gsettings set x.y.z key "'i don't expect this to work'"
449 * Note that we should not just add quotes and try parsing again, but
450 * rather assume that the user is providing us with a bare string.
451 * Assume we added single quotes, then consider this case:
453 * gsettings set x.y.z key "i'd expect this to work"
455 * A similar example could be given for double quotes.
457 * Avoid that whole mess by just using g_variant_new_string().
460 g_variant_type_equal (type, G_VARIANT_TYPE_STRING) &&
461 global_value[0] != '\'' && global_value[0] != '"')
463 g_clear_error (&error);
464 new = g_variant_new_string (global_value);
469 g_printerr ("%s\n", error->message);
473 if (!g_settings_schema_key_range_check (global_schema_key, new))
475 g_printerr (_("The provided value is outside of the valid range\n"));
476 g_variant_unref (new);
480 if (!g_settings_set_value (global_settings, global_key, new))
482 g_printerr (_("The key is not writable\n"));
492 gsettings_help (gboolean requested,
493 const gchar *command)
495 const gchar *description;
496 const gchar *synopsis;
499 string = g_string_new (NULL);
504 else if (strcmp (command, "help") == 0)
506 description = _("Print help");
507 synopsis = "[COMMAND]";
510 else if (strcmp (command, "--version") == 0)
512 description = _("Print version information and exit");
516 else if (strcmp (command, "list-schemas") == 0)
518 description = _("List the installed (non-relocatable) schemas");
522 else if (strcmp (command, "list-relocatable-schemas") == 0)
524 description = _("List the installed relocatable schemas");
528 else if (strcmp (command, "list-keys") == 0)
530 description = _("List the keys in SCHEMA");
531 synopsis = N_("SCHEMA[:PATH]");
534 else if (strcmp (command, "list-children") == 0)
536 description = _("List the children of SCHEMA");
537 synopsis = N_("SCHEMA[:PATH]");
540 else if (strcmp (command, "list-recursively") == 0)
542 description = _("List keys and values, recursively\n"
543 "If no SCHEMA is given, list all keys\n");
544 synopsis = N_("[SCHEMA[:PATH]]");
547 else if (strcmp (command, "get") == 0)
549 description = _("Get the value of KEY");
550 synopsis = N_("SCHEMA[:PATH] KEY");
553 else if (strcmp (command, "range") == 0)
555 description = _("Query the range of valid values for KEY");
556 synopsis = N_("SCHEMA[:PATH] KEY");
559 else if (strcmp (command, "set") == 0)
561 description = _("Set the value of KEY to VALUE");
562 synopsis = N_("SCHEMA[:PATH] KEY VALUE");
565 else if (strcmp (command, "reset") == 0)
567 description = _("Reset KEY to its default value");
568 synopsis = N_("SCHEMA[:PATH] KEY");
571 else if (strcmp (command, "reset-recursively") == 0)
573 description = _("Reset all keys in SCHEMA to their defaults");
574 synopsis = N_("SCHEMA[:PATH]");
577 else if (strcmp (command, "writable") == 0)
579 description = _("Check if KEY is writable");
580 synopsis = N_("SCHEMA[:PATH] KEY");
583 else if (strcmp (command, "monitor") == 0)
585 description = _("Monitor KEY for changes.\n"
586 "If no KEY is specified, monitor all keys in SCHEMA.\n"
587 "Use ^C to stop monitoring.\n");
588 synopsis = N_("SCHEMA[:PATH] [KEY]");
592 g_string_printf (string, _("Unknown command %s\n\n"), command);
599 g_string_append (string,
601 " gsettings --version\n"
602 " gsettings [--schemadir SCHEMADIR] COMMAND [ARGS...]\n"
605 " help Show this information\n"
606 " list-schemas List installed schemas\n"
607 " list-relocatable-schemas List relocatable schemas\n"
608 " list-keys List keys in a schema\n"
609 " list-children List children of a schema\n"
610 " list-recursively List keys and values, recursively\n"
611 " range Queries the range of a key\n"
612 " get Get the value of a key\n"
613 " set Set the value of a key\n"
614 " reset Reset the value of a key\n"
615 " reset-recursively Reset all values in a given schema\n"
616 " writable Check if a key is writable\n"
617 " monitor Watch for changes\n"
619 "Use 'gsettings help COMMAND' to get detailed help.\n\n"));
623 g_string_append_printf (string, _("Usage:\n gsettings [--schemadir SCHEMADIR] %s %s\n\n%s\n\n"),
624 command, synopsis[0] ? _(synopsis) : "", description);
626 g_string_append (string, _("Arguments:\n"));
628 g_string_append (string,
629 _(" SCHEMADIR A directory to search for additional schemas\n"));
631 if (strstr (synopsis, "[COMMAND]"))
632 g_string_append (string,
633 _(" COMMAND The (optional) command to explain\n"));
635 else if (strstr (synopsis, "SCHEMA"))
636 g_string_append (string,
637 _(" SCHEMA The name of the schema\n"
638 " PATH The path, for relocatable schemas\n"));
640 if (strstr (synopsis, "[KEY]"))
641 g_string_append (string,
642 _(" KEY The (optional) key within the schema\n"));
644 else if (strstr (synopsis, "KEY"))
645 g_string_append (string,
646 _(" KEY The key within the schema\n"));
648 if (strstr (synopsis, "VALUE"))
649 g_string_append (string,
650 _(" VALUE The value to set\n"));
652 g_string_append (string, "\n");
656 g_print ("%s", string->str);
658 g_printerr ("%s\n", string->str);
660 g_string_free (string, TRUE);
662 return requested ? 0 : 1;
667 main (int argc, char **argv)
669 void (* function) (void);
675 setlocale (LC_ALL, "");
676 textdomain (GETTEXT_PACKAGE);
679 tmp = _glib_get_locale_dir ();
680 bindtextdomain (GETTEXT_PACKAGE, tmp);
683 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
686 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
687 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
691 return gsettings_help (FALSE, NULL);
693 global_schema_source = g_settings_schema_source_ref (g_settings_schema_source_get_default ());
695 if (argc > 3 && g_str_equal (argv[1], "--schemadir"))
697 GSettingsSchemaSource *parent = global_schema_source;
698 GError *error = NULL;
700 global_schema_source = g_settings_schema_source_new_from_directory (argv[2], parent, FALSE, &error);
701 g_settings_schema_source_unref (parent);
703 if (global_schema_source == NULL)
705 g_printerr (_("Could not load schemas from %s: %s\n"), argv[2], error->message);
706 g_clear_error (&error);
711 /* shift remaining arguments (not correct wrt argv[0], but doesn't matter) */
716 if (strcmp (argv[1], "help") == 0)
717 return gsettings_help (TRUE, argv[2]);
719 else if (argc == 2 && strcmp (argv[1], "--version") == 0)
720 function = gsettings_print_version;
722 else if (argc == 2 && strcmp (argv[1], "list-schemas") == 0)
723 function = gsettings_list_schemas;
725 else if (argc == 2 && strcmp (argv[1], "list-relocatable-schemas") == 0)
726 function = gsettings_list_relocatable_schemas;
728 else if (argc == 3 && strcmp (argv[1], "list-keys") == 0)
729 function = gsettings_list_keys;
731 else if (argc == 3 && strcmp (argv[1], "list-children") == 0)
732 function = gsettings_list_children;
734 else if ((argc == 2 || argc == 3) && strcmp (argv[1], "list-recursively") == 0)
735 function = gsettings_list_recursively;
737 else if (argc == 4 && strcmp (argv[1], "range") == 0)
738 function = gsettings_range;
740 else if (argc == 4 && strcmp (argv[1], "get") == 0)
741 function = gsettings_get;
743 else if (argc == 5 && strcmp (argv[1], "set") == 0)
744 function = gsettings_set;
746 else if (argc == 4 && strcmp (argv[1], "reset") == 0)
747 function = gsettings_reset;
749 else if (argc == 3 && strcmp (argv[1], "reset-recursively") == 0)
750 function = gsettings_reset_recursively;
752 else if (argc == 4 && strcmp (argv[1], "writable") == 0)
753 function = gsettings_writable;
755 else if ((argc == 3 || argc == 4) && strcmp (argv[1], "monitor") == 0)
756 function = gsettings_monitor;
759 return gsettings_help (FALSE, argv[1]);
765 if (argv[2][0] == '\0')
767 g_printerr (_("Empty schema name given\n"));
771 parts = g_strsplit (argv[2], ":", 2);
773 global_schema = g_settings_schema_source_lookup (global_schema_source, parts[0], TRUE);
776 if (!check_relocatable_schema (global_schema, parts[0]) || !check_path (parts[1]))
779 global_settings = g_settings_new_full (global_schema, NULL, parts[1]);
783 if (!check_schema (global_schema, parts[0]))
786 global_settings = g_settings_new_full (global_schema, NULL, NULL);
794 if (!g_settings_schema_has_key (global_schema, argv[3]))
796 g_printerr (_("No such key '%s'\n"), argv[3]);
800 global_key = argv[3];
801 global_schema_key = g_settings_schema_get_key (global_schema, global_key);
805 global_value = argv[4];
810 g_clear_pointer (&global_schema_source, g_settings_schema_source_unref);
811 g_clear_pointer (&global_schema_key, g_settings_schema_key_unref);
812 g_clear_pointer (&global_schema, g_settings_schema_unref);
813 g_clear_object (&global_settings);