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 contained (const gchar * const *items,
35 if (strcmp (*items++, item) == 0)
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 check_key (GSettings *settings,
126 keys = g_settings_list_keys (settings);
127 good = contained ((const gchar **) keys, key);
133 g_printerr (_("No such key '%s'\n"), key);
139 output_list (const gchar * const *list)
143 for (i = 0; list[i]; i++)
144 g_print ("%s\n", list[i]);
148 gsettings_list_schemas (GSettings *settings,
152 output_list (g_settings_list_schemas ());
156 gsettings_list_relocatable_schemas (GSettings *settings,
160 output_list (g_settings_list_relocatable_schemas ());
164 gsettings_list_keys (GSettings *settings,
170 keys = g_settings_list_keys (settings);
171 output_list ((const gchar **) keys);
176 gsettings_list_children (GSettings *settings,
184 children = g_settings_list_children (settings);
185 for (i = 0; children[i]; i++)
186 if (strlen (children[i]) > max)
187 max = strlen (children[i]);
189 for (i = 0; children[i]; i++)
192 GSettingsSchema *schema;
195 child = g_settings_get_child (settings, children[i]);
197 "settings-schema", &schema,
201 if (g_settings_schema_get_path (schema) != NULL)
202 g_print ("%-*s %s\n", max, children[i], g_settings_schema_get_id (schema));
204 g_print ("%-*s %s:%s\n", max, children[i], g_settings_schema_get_id (schema), path);
206 g_object_unref (child);
207 g_settings_schema_unref (schema);
211 g_strfreev (children);
215 enumerate (GSettings *settings)
221 g_object_get (settings, "schema-id", &schema, NULL);
223 keys = g_settings_list_keys (settings);
224 for (i = 0; keys[i]; i++)
229 value = g_settings_get_value (settings, keys[i]);
230 printed = g_variant_print (value, TRUE);
231 g_print ("%s %s %s\n", schema, keys[i], printed);
232 g_variant_unref (value);
241 gsettings_list_recursively (GSettings *settings,
250 enumerate (settings);
251 children = g_settings_list_children (settings);
252 for (i = 0; children[i]; i++)
256 child = g_settings_get_child (settings, children[i]);
257 gsettings_list_recursively (child, NULL, NULL);
258 g_object_unref (child);
261 g_strfreev (children);
265 const gchar * const *schemas;
268 schemas = g_settings_list_schemas ();
270 for (i = 0; schemas[i]; i++)
272 settings = g_settings_new (schemas[i]);
273 gsettings_list_recursively (settings, NULL, NULL);
274 g_object_unref (settings);
280 gsettings_range (GSettings *settings,
284 GVariant *range, *detail;
287 range = g_settings_get_range (settings, key);
288 g_variant_get (range, "(&sv)", &type, &detail);
290 if (strcmp (type, "type") == 0)
291 g_print ("type %s\n", g_variant_get_type_string (detail) + 1);
293 else if (strcmp (type, "range") == 0)
298 g_variant_get (detail, "(**)", &min, &max);
299 smin = g_variant_print (min, FALSE);
300 smax = g_variant_print (max, FALSE);
302 g_print ("range %s %s %s\n",
303 g_variant_get_type_string (min), smin, smax);
304 g_variant_unref (min);
305 g_variant_unref (max);
310 else if (strcmp (type, "enum") == 0 || strcmp (type, "flags") == 0)
315 g_print ("%s\n", type);
317 g_variant_iter_init (&iter, detail);
318 while (g_variant_iter_loop (&iter, "*", &item))
322 printed = g_variant_print (item, FALSE);
323 g_print ("%s\n", printed);
328 g_variant_unref (detail);
329 g_variant_unref (range);
333 gsettings_get (GSettings *settings,
340 value = g_settings_get_value (settings, key);
341 printed = g_variant_print (value, TRUE);
342 g_print ("%s\n", printed);
343 g_variant_unref (value);
348 gsettings_reset (GSettings *settings,
352 g_settings_reset (settings, key);
357 reset_all_keys (GSettings *settings)
362 keys = g_settings_list_keys (settings);
363 for (i = 0; keys[i]; i++)
365 g_settings_reset (settings, keys[i]);
372 gsettings_reset_recursively (GSettings *settings,
379 g_settings_delay (settings);
381 reset_all_keys (settings);
382 children = g_settings_list_children (settings);
383 for (i = 0; children[i]; i++)
386 child = g_settings_get_child (settings, children[i]);
388 reset_all_keys (child);
390 g_object_unref (child);
393 g_strfreev (children);
395 g_settings_apply (settings);
400 gsettings_writable (GSettings *settings,
405 g_settings_is_writable (settings, key) ?
410 value_changed (GSettings *settings,
417 value = g_settings_get_value (settings, key);
418 printed = g_variant_print (value, TRUE);
419 g_print ("%s: %s\n", key, printed);
420 g_variant_unref (value);
425 gsettings_monitor (GSettings *settings,
433 name = g_strdup_printf ("changed::%s", key);
434 g_signal_connect (settings, name, G_CALLBACK (value_changed), NULL);
437 g_signal_connect (settings, "changed", G_CALLBACK (value_changed), NULL);
439 g_main_loop_run (g_main_loop_new (NULL, FALSE));
443 gsettings_set (GSettings *settings,
447 const GVariantType *type;
448 GError *error = NULL;
451 gchar *freeme = NULL;
453 existing = g_settings_get_value (settings, key);
454 type = g_variant_get_type (existing);
456 new = g_variant_parse (type, value, NULL, NULL, &error);
458 /* If that didn't work and the type is string then we should assume
459 * that the user is just trying to set a string directly and forgot
460 * the quotes (or had them consumed by the shell).
462 * If the user started with a quote then we assume that some deeper
463 * problem is at play and we want the failure in that case.
467 * gsettings set x.y.z key "'i don't expect this to work'"
469 * Note that we should not just add quotes and try parsing again, but
470 * rather assume that the user is providing us with a bare string.
471 * Assume we added single quotes, then consider this case:
473 * gsettings set x.y.z key "i'd expect this to work"
475 * A similar example could be given for double quotes.
477 * Avoid that whole mess by just using g_variant_new_string().
480 g_variant_type_equal (type, G_VARIANT_TYPE_STRING) &&
481 value[0] != '\'' && value[0] != '"')
483 g_clear_error (&error);
484 new = g_variant_new_string (value);
487 /* we're done with 'type' now, so we can free 'existing' */
488 g_variant_unref (existing);
492 g_printerr ("%s\n", error->message);
496 if (!g_settings_range_check (settings, key, new))
498 g_printerr (_("The provided value is outside of the valid range\n"));
499 g_variant_unref (new);
503 g_settings_set_value (settings, key, new);
511 gsettings_help (gboolean requested,
512 const gchar *command)
514 const gchar *description;
515 const gchar *synopsis;
518 string = g_string_new (NULL);
523 else if (strcmp (command, "help") == 0)
525 description = _("Print help");
526 synopsis = "[COMMAND]";
529 else if (strcmp (command, "list-schemas") == 0)
531 description = _("List the installed (non-relocatable) schemas");
535 else if (strcmp (command, "list-relocatable-schemas") == 0)
537 description = _("List the installed relocatable schemas");
541 else if (strcmp (command, "list-keys") == 0)
543 description = _("List the keys in SCHEMA");
544 synopsis = N_("SCHEMA[:PATH]");
547 else if (strcmp (command, "list-children") == 0)
549 description = _("List the children of SCHEMA");
550 synopsis = N_("SCHEMA[:PATH]");
553 else if (strcmp (command, "list-recursively") == 0)
555 description = _("List keys and values, recursively\n"
556 "If no SCHEMA is given, list all keys\n");
557 synopsis = N_("[SCHEMA[:PATH]]");
560 else if (strcmp (command, "get") == 0)
562 description = _("Get the value of KEY");
563 synopsis = N_("SCHEMA[:PATH] KEY");
566 else if (strcmp (command, "range") == 0)
568 description = _("Query the range of valid values for KEY");
569 synopsis = N_("SCHEMA[:PATH] KEY");
572 else if (strcmp (command, "set") == 0)
574 description = _("Set the value of KEY to VALUE");
575 synopsis = N_("SCHEMA[:PATH] KEY VALUE");
578 else if (strcmp (command, "reset") == 0)
580 description = _("Reset KEY to its default value");
581 synopsis = N_("SCHEMA[:PATH] KEY");
584 else if (strcmp (command, "reset-recursively") == 0)
586 description = _("Reset all keys in SCHEMA to their defaults");
587 synopsis = N_("SCHEMA[:PATH]");
590 else if (strcmp (command, "writable") == 0)
592 description = _("Check if KEY is writable");
593 synopsis = N_("SCHEMA[:PATH] KEY");
596 else if (strcmp (command, "monitor") == 0)
598 description = _("Monitor KEY for changes.\n"
599 "If no KEY is specified, monitor all keys in SCHEMA.\n"
600 "Use ^C to stop monitoring.\n");
601 synopsis = N_("SCHEMA[:PATH] [KEY]");
605 g_string_printf (string, _("Unknown command %s\n\n"), command);
612 g_string_append (string,
614 " gsettings [--schemadir SCHEMADIR] COMMAND [ARGS...]\n"
617 " help Show this information\n"
618 " list-schemas List installed schemas\n"
619 " list-relocatable-schemas List relocatable schemas\n"
620 " list-keys List keys in a schema\n"
621 " list-children List children of a schema\n"
622 " list-recursively List keys and values, recursively\n"
623 " range Queries the range of a key\n"
624 " get Get the value of a key\n"
625 " set Set the value of a key\n"
626 " reset Reset the value of a key\n"
627 " reset-recursively Reset all values in a given schema\n"
628 " writable Check if a key is writable\n"
629 " monitor Watch for changes\n"
631 "Use 'gsettings help COMMAND' to get detailed help.\n\n"));
635 g_string_append_printf (string, _("Usage:\n gsettings [--schemadir SCHEMADIR] %s %s\n\n%s\n\n"),
636 command, synopsis[0] ? _(synopsis) : "", description);
638 g_string_append (string, _("Arguments:\n"));
640 g_string_append (string,
641 _(" SCHEMADIR A directory to search for additional schemas\n"));
643 if (strstr (synopsis, "[COMMAND]"))
644 g_string_append (string,
645 _(" COMMAND The (optional) command to explain\n"));
647 else if (strstr (synopsis, "SCHEMA"))
648 g_string_append (string,
649 _(" SCHEMA The name of the schema\n"
650 " PATH The path, for relocatable schemas\n"));
652 if (strstr (synopsis, "[KEY]"))
653 g_string_append (string,
654 _(" KEY The (optional) key within the schema\n"));
656 else if (strstr (synopsis, "KEY"))
657 g_string_append (string,
658 _(" KEY The key within the schema\n"));
660 if (strstr (synopsis, "VALUE"))
661 g_string_append (string,
662 _(" VALUE The value to set\n"));
664 g_string_append (string, "\n");
668 g_print ("%s", string->str);
670 g_printerr ("%s\n", string->str);
672 g_string_free (string, TRUE);
674 return requested ? 0 : 1;
679 main (int argc, char **argv)
681 void (* function) (GSettings *, const gchar *, const gchar *);
682 GSettingsSchemaSource *schema_source;
683 GSettingsSchema *schema;
688 extern gchar *_glib_get_locale_dir (void);
692 setlocale (LC_ALL, "");
693 textdomain (GETTEXT_PACKAGE);
696 tmp = _glib_get_locale_dir ();
697 bindtextdomain (GETTEXT_PACKAGE, tmp);
700 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
703 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
704 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
708 return gsettings_help (FALSE, NULL);
710 schema_source = g_settings_schema_source_ref (g_settings_schema_source_get_default ());
712 if (argc > 3 && g_str_equal (argv[1], "--schemadir"))
714 GSettingsSchemaSource *parent = schema_source;
715 GError *error = NULL;
717 schema_source = g_settings_schema_source_new_from_directory (argv[2], parent, FALSE, &error);
718 g_settings_schema_source_unref (parent);
720 if (schema_source == NULL)
722 g_printerr ("Could not load schemas from %s: %s\n", argv[2], error->message);
723 g_clear_error (&error);
728 /* shift remaining arguments (not correct wrt argv[0], but doesn't matter) */
733 if (strcmp (argv[1], "help") == 0)
734 return gsettings_help (TRUE, argv[2]);
736 else if (argc == 2 && strcmp (argv[1], "list-schemas") == 0)
737 function = gsettings_list_schemas;
739 else if (argc == 2 && strcmp (argv[1], "list-relocatable-schemas") == 0)
740 function = gsettings_list_relocatable_schemas;
742 else if (argc == 3 && strcmp (argv[1], "list-keys") == 0)
743 function = gsettings_list_keys;
745 else if (argc == 3 && strcmp (argv[1], "list-children") == 0)
746 function = gsettings_list_children;
748 else if ((argc == 2 || argc == 3) && strcmp (argv[1], "list-recursively") == 0)
749 function = gsettings_list_recursively;
751 else if (argc == 4 && strcmp (argv[1], "range") == 0)
752 function = gsettings_range;
754 else if (argc == 4 && strcmp (argv[1], "get") == 0)
755 function = gsettings_get;
757 else if (argc == 5 && strcmp (argv[1], "set") == 0)
758 function = gsettings_set;
760 else if (argc == 4 && strcmp (argv[1], "reset") == 0)
761 function = gsettings_reset;
763 else if (argc == 3 && strcmp (argv[1], "reset-recursively") == 0)
764 function = gsettings_reset_recursively;
766 else if (argc == 4 && strcmp (argv[1], "writable") == 0)
767 function = gsettings_writable;
769 else if ((argc == 3 || argc == 4) && strcmp (argv[1], "monitor") == 0)
770 function = gsettings_monitor;
773 return gsettings_help (FALSE, argv[1]);
781 if (argv[2][0] == '\0')
783 g_printerr (_("Empty schema name given\n"));
787 parts = g_strsplit (argv[2], ":", 2);
789 schema = g_settings_schema_source_lookup (schema_source, parts[0], TRUE);
792 if (!check_relocatable_schema (schema, parts[0]) || !check_path (parts[1]))
795 settings = g_settings_new_full (schema, NULL, parts[1]);
799 if (!check_schema (schema, parts[0]))
802 settings = g_settings_new_full (schema, NULL, NULL);
815 if (!check_key (settings, argv[3]))
823 (* function) (settings, key, argc > 4 ? argv[4] : NULL);
825 if (settings != NULL)
826 g_object_unref (settings);
828 g_settings_schema_unref (schema);
830 g_settings_schema_source_unref (schema_source);