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, see <http://www.gnu.org/licenses/>.
17 * Author: Ryan Lortie <desrt@desrt.ca>
29 #include "glib/glib-private.h"
32 static GSettingsSchemaSource
*global_schema_source
;
33 static GSettings
*global_settings
;
34 static GSettingsSchema
*global_schema
;
35 static GSettingsSchemaKey
*global_schema_key
;
36 const gchar
*global_key
;
37 const gchar
*global_value
;
40 is_relocatable_schema (GSettingsSchema
*schema
)
42 return g_settings_schema_get_path (schema
) == NULL
;
46 check_relocatable_schema (GSettingsSchema
*schema
,
47 const gchar
*schema_id
)
51 g_printerr (_("No such schema “%s”\n"), schema_id
);
55 if (!is_relocatable_schema (schema
))
57 g_printerr (_("Schema “%s” is not relocatable "
58 "(path must not be specified)\n"),
67 check_schema (GSettingsSchema
*schema
,
68 const gchar
*schema_id
)
72 g_printerr (_("No such schema “%s”\n"), schema_id
);
76 if (is_relocatable_schema (schema
))
78 g_printerr (_("Schema “%s” is relocatable "
79 "(path must be specified)\n"),
88 check_path (const gchar
*path
)
92 g_printerr (_("Empty path given.\n"));
98 g_printerr (_("Path must begin with a slash (/)\n"));
102 if (!g_str_has_suffix (path
, "/"))
104 g_printerr (_("Path must end with a slash (/)\n"));
108 if (strstr (path
, "//"))
110 g_printerr (_("Path must not contain two adjacent slashes (//)\n"));
118 output_list (gchar
**list
)
122 for (i
= 0; list
[i
]; i
++)
123 g_print ("%s\n", list
[i
]);
127 gsettings_print_version (void)
129 g_print ("%d.%d.%d\n", glib_major_version
, glib_minor_version
,
134 gsettings_list_schemas (void)
138 g_settings_schema_source_list_schemas (global_schema_source
, TRUE
, &schemas
, NULL
);
139 output_list (schemas
);
140 g_strfreev (schemas
);
144 gsettings_list_relocatable_schemas (void)
148 g_settings_schema_source_list_schemas (global_schema_source
, TRUE
, NULL
, &schemas
);
149 output_list (schemas
);
150 g_strfreev (schemas
);
154 gsettings_list_keys (void)
158 keys
= g_settings_schema_list_keys (global_schema
);
164 gsettings_list_children (void)
170 children
= g_settings_list_children (global_settings
);
171 for (i
= 0; children
[i
]; i
++)
172 if (strlen (children
[i
]) > max
)
173 max
= strlen (children
[i
]);
175 for (i
= 0; children
[i
]; i
++)
178 GSettingsSchema
*schema
;
181 child
= g_settings_get_child (global_settings
, children
[i
]);
183 "settings-schema", &schema
,
187 if (g_settings_schema_get_path (schema
) != NULL
)
188 g_print ("%-*s %s\n", max
, children
[i
], g_settings_schema_get_id (schema
));
190 g_print ("%-*s %s:%s\n", max
, children
[i
], g_settings_schema_get_id (schema
), path
);
192 g_object_unref (child
);
193 g_settings_schema_unref (schema
);
197 g_strfreev (children
);
201 enumerate (GSettings
*settings
)
204 GSettingsSchema
*schema
;
207 g_object_get (settings
, "settings-schema", &schema
, NULL
);
209 keys
= g_settings_schema_list_keys (schema
);
210 for (i
= 0; keys
[i
]; i
++)
215 value
= g_settings_get_value (settings
, keys
[i
]);
216 printed
= g_variant_print (value
, TRUE
);
217 g_print ("%s %s %s\n", g_settings_schema_get_id (schema
), keys
[i
], printed
);
218 g_variant_unref (value
);
222 g_settings_schema_unref (schema
);
227 list_recursively (GSettings
*settings
)
232 enumerate (settings
);
233 children
= g_settings_list_children (settings
);
234 for (i
= 0; children
[i
]; i
++)
238 child
= g_settings_get_child (settings
, children
[i
]);
239 list_recursively (child
);
240 g_object_unref (child
);
243 g_strfreev (children
);
247 gsettings_list_recursively (void)
251 list_recursively (global_settings
);
258 g_settings_schema_source_list_schemas (global_schema_source
, TRUE
, &schemas
, NULL
);
260 for (i
= 0; schemas
[i
]; i
++)
264 settings
= g_settings_new (schemas
[i
]);
265 list_recursively (settings
);
266 g_object_unref (settings
);
269 g_strfreev (schemas
);
274 gsettings_description (void)
276 const gchar
*description
;
277 description
= g_settings_schema_key_get_description (global_schema_key
);
278 g_print ("%s\n", description
);
282 gsettings_range (void)
284 GVariant
*range
, *detail
;
287 range
= g_settings_schema_key_get_range (global_schema_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
);
338 value
= g_settings_get_value (global_settings
, global_key
);
339 printed
= g_variant_print (value
, TRUE
);
340 g_print ("%s\n", printed
);
341 g_variant_unref (value
);
346 gsettings_reset (void)
348 g_settings_reset (global_settings
, global_key
);
353 reset_all_keys (GSettings
*settings
)
355 GSettingsSchema
*schema
;
359 g_object_get (settings
, "settings-schema", &schema
, NULL
);
361 keys
= g_settings_schema_list_keys (schema
);
362 for (i
= 0; keys
[i
]; i
++)
364 g_settings_reset (settings
, keys
[i
]);
367 g_settings_schema_unref (schema
);
372 gsettings_reset_recursively (void)
377 g_settings_delay (global_settings
);
379 reset_all_keys (global_settings
);
380 children
= g_settings_list_children (global_settings
);
381 for (i
= 0; children
[i
]; i
++)
384 child
= g_settings_get_child (global_settings
, children
[i
]);
386 reset_all_keys (child
);
388 g_object_unref (child
);
391 g_strfreev (children
);
393 g_settings_apply (global_settings
);
398 gsettings_writable (void)
401 g_settings_is_writable (global_settings
, global_key
) ?
406 value_changed (GSettings
*settings
,
413 value
= g_settings_get_value (settings
, key
);
414 printed
= g_variant_print (value
, TRUE
);
415 g_print ("%s: %s\n", key
, printed
);
416 g_variant_unref (value
);
421 gsettings_monitor (void)
427 name
= g_strdup_printf ("changed::%s", global_key
);
428 g_signal_connect (global_settings
, name
, G_CALLBACK (value_changed
), NULL
);
431 g_signal_connect (global_settings
, "changed", G_CALLBACK (value_changed
), NULL
);
434 g_main_context_iteration (NULL
, TRUE
);
440 const GVariantType
*type
;
441 GError
*error
= NULL
;
443 gchar
*freeme
= NULL
;
445 type
= g_settings_schema_key_get_value_type (global_schema_key
);
447 new = g_variant_parse (type
, global_value
, NULL
, NULL
, &error
);
449 /* If that didn't work and the type is string then we should assume
450 * that the user is just trying to set a string directly and forgot
451 * the quotes (or had them consumed by the shell).
453 * If the user started with a quote then we assume that some deeper
454 * problem is at play and we want the failure in that case.
458 * gsettings set x.y.z key "'i don't expect this to work'"
460 * Note that we should not just add quotes and try parsing again, but
461 * rather assume that the user is providing us with a bare string.
462 * Assume we added single quotes, then consider this case:
464 * gsettings set x.y.z key "i'd expect this to work"
466 * A similar example could be given for double quotes.
468 * Avoid that whole mess by just using g_variant_new_string().
471 g_variant_type_equal (type
, G_VARIANT_TYPE_STRING
) &&
472 global_value
[0] != '\'' && global_value
[0] != '"')
474 g_clear_error (&error
);
475 new = g_variant_new_string (global_value
);
482 context
= g_variant_parse_error_print_context (error
, global_value
);
483 g_printerr ("%s", context
);
487 if (!g_settings_schema_key_range_check (global_schema_key
, new))
489 g_printerr (_("The provided value is outside of the valid range\n"));
490 g_variant_unref (new);
494 if (!g_settings_set_value (global_settings
, global_key
, new))
496 g_printerr (_("The key is not writable\n"));
506 gsettings_help (gboolean requested
,
507 const gchar
*command
)
509 const gchar
*description
;
510 const gchar
*synopsis
;
513 string
= g_string_new (NULL
);
518 else if (strcmp (command
, "help") == 0)
520 description
= _("Print help");
521 synopsis
= "[COMMAND]";
524 else if (strcmp (command
, "--version") == 0)
526 description
= _("Print version information and exit");
530 else if (strcmp (command
, "list-schemas") == 0)
532 description
= _("List the installed (non-relocatable) schemas");
536 else if (strcmp (command
, "list-relocatable-schemas") == 0)
538 description
= _("List the installed relocatable schemas");
542 else if (strcmp (command
, "list-keys") == 0)
544 description
= _("List the keys in SCHEMA");
545 synopsis
= N_("SCHEMA[:PATH]");
548 else if (strcmp (command
, "list-children") == 0)
550 description
= _("List the children of SCHEMA");
551 synopsis
= N_("SCHEMA[:PATH]");
554 else if (strcmp (command
, "list-recursively") == 0)
556 description
= _("List keys and values, recursively\n"
557 "If no SCHEMA is given, list all keys\n");
558 synopsis
= N_("[SCHEMA[:PATH]]");
561 else if (strcmp (command
, "get") == 0)
563 description
= _("Get the value of KEY");
564 synopsis
= N_("SCHEMA[:PATH] KEY");
567 else if (strcmp (command
, "range") == 0)
569 description
= _("Query the range of valid values for KEY");
570 synopsis
= N_("SCHEMA[:PATH] KEY");
573 else if (strcmp (command
, "describe") == 0)
575 description
= _("Query the description for KEY");
576 synopsis
= N_("SCHEMA[:PATH] KEY");
579 else if (strcmp (command
, "set") == 0)
581 description
= _("Set the value of KEY to VALUE");
582 synopsis
= N_("SCHEMA[:PATH] KEY VALUE");
585 else if (strcmp (command
, "reset") == 0)
587 description
= _("Reset KEY to its default value");
588 synopsis
= N_("SCHEMA[:PATH] KEY");
591 else if (strcmp (command
, "reset-recursively") == 0)
593 description
= _("Reset all keys in SCHEMA to their defaults");
594 synopsis
= N_("SCHEMA[:PATH]");
597 else if (strcmp (command
, "writable") == 0)
599 description
= _("Check if KEY is writable");
600 synopsis
= N_("SCHEMA[:PATH] KEY");
603 else if (strcmp (command
, "monitor") == 0)
605 description
= _("Monitor KEY for changes.\n"
606 "If no KEY is specified, monitor all keys in SCHEMA.\n"
607 "Use ^C to stop monitoring.\n");
608 synopsis
= N_("SCHEMA[:PATH] [KEY]");
612 g_string_printf (string
, _("Unknown command %s\n\n"), command
);
619 g_string_append (string
,
621 " gsettings --version\n"
622 " gsettings [--schemadir SCHEMADIR] COMMAND [ARGS…]\n"
625 " help Show this information\n"
626 " list-schemas List installed schemas\n"
627 " list-relocatable-schemas List relocatable schemas\n"
628 " list-keys List keys in a schema\n"
629 " list-children List children of a schema\n"
630 " list-recursively List keys and values, recursively\n"
631 " range Queries the range of a key\n"
632 " describe Queries the description of a key\n"
633 " get Get the value of a key\n"
634 " set Set the value of a key\n"
635 " reset Reset the value of a key\n"
636 " reset-recursively Reset all values in a given schema\n"
637 " writable Check if a key is writable\n"
638 " monitor Watch for changes\n"
640 "Use “gsettings help COMMAND” to get detailed help.\n\n"));
644 g_string_append_printf (string
, _("Usage:\n gsettings [--schemadir SCHEMADIR] %s %s\n\n%s\n\n"),
645 command
, synopsis
[0] ? _(synopsis
) : "", description
);
647 g_string_append (string
, _("Arguments:\n"));
649 g_string_append (string
,
650 _(" SCHEMADIR A directory to search for additional schemas\n"));
652 if (strstr (synopsis
, "[COMMAND]"))
653 g_string_append (string
,
654 _(" COMMAND The (optional) command to explain\n"));
656 else if (strstr (synopsis
, "SCHEMA"))
657 g_string_append (string
,
658 _(" SCHEMA The name of the schema\n"
659 " PATH The path, for relocatable schemas\n"));
661 if (strstr (synopsis
, "[KEY]"))
662 g_string_append (string
,
663 _(" KEY The (optional) key within the schema\n"));
665 else if (strstr (synopsis
, "KEY"))
666 g_string_append (string
,
667 _(" KEY The key within the schema\n"));
669 if (strstr (synopsis
, "VALUE"))
670 g_string_append (string
,
671 _(" VALUE The value to set\n"));
673 g_string_append (string
, "\n");
677 g_print ("%s", string
->str
);
679 g_printerr ("%s\n", string
->str
);
681 g_string_free (string
, TRUE
);
683 return requested
? 0 : 1;
688 main (int argc
, char **argv
)
690 void (* function
) (void);
691 gboolean need_settings
;
697 setlocale (LC_ALL
, "");
698 textdomain (GETTEXT_PACKAGE
);
701 tmp
= _glib_get_locale_dir ();
702 bindtextdomain (GETTEXT_PACKAGE
, tmp
);
705 bindtextdomain (GETTEXT_PACKAGE
, GLIB_LOCALE_DIR
);
708 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
709 bind_textdomain_codeset (GETTEXT_PACKAGE
, "UTF-8");
713 return gsettings_help (FALSE
, NULL
);
715 global_schema_source
= g_settings_schema_source_get_default ();
717 if (argc
> 3 && g_str_equal (argv
[1], "--schemadir"))
719 GSettingsSchemaSource
*parent
= global_schema_source
;
720 GError
*error
= NULL
;
722 global_schema_source
= g_settings_schema_source_new_from_directory (argv
[2], parent
, FALSE
, &error
);
724 if (global_schema_source
== NULL
)
726 g_printerr (_("Could not load schemas from %s: %s\n"), argv
[2], error
->message
);
727 g_clear_error (&error
);
732 /* shift remaining arguments (not correct wrt argv[0], but doesn't matter) */
736 else if (global_schema_source
== NULL
)
738 g_printerr (_("No schemas installed\n"));
742 g_settings_schema_source_ref (global_schema_source
);
744 need_settings
= TRUE
;
746 if (strcmp (argv
[1], "help") == 0)
747 return gsettings_help (TRUE
, argv
[2]);
749 else if (argc
== 2 && strcmp (argv
[1], "--version") == 0)
750 function
= gsettings_print_version
;
752 else if (argc
== 2 && strcmp (argv
[1], "list-schemas") == 0)
753 function
= gsettings_list_schemas
;
755 else if (argc
== 2 && strcmp (argv
[1], "list-relocatable-schemas") == 0)
756 function
= gsettings_list_relocatable_schemas
;
758 else if (argc
== 3 && strcmp (argv
[1], "list-keys") == 0)
760 need_settings
= FALSE
;
761 function
= gsettings_list_keys
;
764 else if (argc
== 3 && strcmp (argv
[1], "list-children") == 0)
765 function
= gsettings_list_children
;
767 else if ((argc
== 2 || argc
== 3) && strcmp (argv
[1], "list-recursively") == 0)
768 function
= gsettings_list_recursively
;
770 else if (argc
== 4 && strcmp (argv
[1], "describe") == 0)
772 need_settings
= FALSE
;
773 function
= gsettings_description
;
776 else if (argc
== 4 && strcmp (argv
[1], "range") == 0)
778 need_settings
= FALSE
;
779 function
= gsettings_range
;
782 else if (argc
== 4 && strcmp (argv
[1], "get") == 0)
783 function
= gsettings_get
;
785 else if (argc
== 5 && strcmp (argv
[1], "set") == 0)
786 function
= gsettings_set
;
788 else if (argc
== 4 && strcmp (argv
[1], "reset") == 0)
789 function
= gsettings_reset
;
791 else if (argc
== 3 && strcmp (argv
[1], "reset-recursively") == 0)
792 function
= gsettings_reset_recursively
;
794 else if (argc
== 4 && strcmp (argv
[1], "writable") == 0)
795 function
= gsettings_writable
;
797 else if ((argc
== 3 || argc
== 4) && strcmp (argv
[1], "monitor") == 0)
798 function
= gsettings_monitor
;
801 return gsettings_help (FALSE
, argv
[1]);
807 if (argv
[2][0] == '\0')
809 g_printerr (_("Empty schema name given\n"));
813 parts
= g_strsplit (argv
[2], ":", 2);
815 global_schema
= g_settings_schema_source_lookup (global_schema_source
, parts
[0], TRUE
);
821 if (!check_relocatable_schema (global_schema
, parts
[0]) || !check_path (parts
[1]))
824 global_settings
= g_settings_new_full (global_schema
, NULL
, parts
[1]);
828 if (!check_schema (global_schema
, parts
[0]))
831 global_settings
= g_settings_new_full (global_schema
, NULL
, NULL
);
836 /* If the user has given a path then we enforce that we have a
837 * relocatable schema, but if they didn't give a path then it
838 * doesn't matter what type of schema we have (since it's
839 * reasonable to ask for introspection information on a
840 * relocatable schema without having to give the path).
844 if (!check_relocatable_schema (global_schema
, parts
[0]) || !check_path (parts
[1]))
849 if (global_schema
== NULL
)
851 g_printerr (_("No such schema “%s”\n"), parts
[0]);
862 if (!g_settings_schema_has_key (global_schema
, argv
[3]))
864 g_printerr (_("No such key “%s”\n"), argv
[3]);
868 global_key
= argv
[3];
869 global_schema_key
= g_settings_schema_get_key (global_schema
, global_key
);
873 global_value
= argv
[4];
878 g_clear_pointer (&global_schema_source
, g_settings_schema_source_unref
);
879 g_clear_pointer (&global_schema_key
, g_settings_schema_key_unref
);
880 g_clear_pointer (&global_schema
, g_settings_schema_unref
);
881 g_clear_object (&global_settings
);