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.1 of the License, 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 if (description
== NULL
)
279 description
= g_settings_schema_key_get_summary (global_schema_key
);
280 g_print ("%s\n", description
);
284 gsettings_range (void)
286 GVariant
*range
, *detail
;
289 range
= g_settings_schema_key_get_range (global_schema_key
);
290 g_variant_get (range
, "(&sv)", &type
, &detail
);
292 if (strcmp (type
, "type") == 0)
293 g_print ("type %s\n", g_variant_get_type_string (detail
) + 1);
295 else if (strcmp (type
, "range") == 0)
300 g_variant_get (detail
, "(**)", &min
, &max
);
301 smin
= g_variant_print (min
, FALSE
);
302 smax
= g_variant_print (max
, FALSE
);
304 g_print ("range %s %s %s\n",
305 g_variant_get_type_string (min
), smin
, smax
);
306 g_variant_unref (min
);
307 g_variant_unref (max
);
312 else if (strcmp (type
, "enum") == 0 || strcmp (type
, "flags") == 0)
317 g_print ("%s\n", type
);
319 g_variant_iter_init (&iter
, detail
);
320 while (g_variant_iter_loop (&iter
, "*", &item
))
324 printed
= g_variant_print (item
, FALSE
);
325 g_print ("%s\n", printed
);
330 g_variant_unref (detail
);
331 g_variant_unref (range
);
340 value
= g_settings_get_value (global_settings
, global_key
);
341 printed
= g_variant_print (value
, TRUE
);
342 g_print ("%s\n", printed
);
343 g_variant_unref (value
);
348 gsettings_reset (void)
350 g_settings_reset (global_settings
, global_key
);
355 reset_all_keys (GSettings
*settings
)
357 GSettingsSchema
*schema
;
361 g_object_get (settings
, "settings-schema", &schema
, NULL
);
363 keys
= g_settings_schema_list_keys (schema
);
364 for (i
= 0; keys
[i
]; i
++)
366 g_settings_reset (settings
, keys
[i
]);
369 g_settings_schema_unref (schema
);
374 gsettings_reset_recursively (void)
379 g_settings_delay (global_settings
);
381 reset_all_keys (global_settings
);
382 children
= g_settings_list_children (global_settings
);
383 for (i
= 0; children
[i
]; i
++)
386 child
= g_settings_get_child (global_settings
, children
[i
]);
388 reset_all_keys (child
);
390 g_object_unref (child
);
393 g_strfreev (children
);
395 g_settings_apply (global_settings
);
400 gsettings_writable (void)
403 g_settings_is_writable (global_settings
, global_key
) ?
408 value_changed (GSettings
*settings
,
415 value
= g_settings_get_value (settings
, key
);
416 printed
= g_variant_print (value
, TRUE
);
417 g_print ("%s: %s\n", key
, printed
);
418 g_variant_unref (value
);
423 gsettings_monitor (void)
429 name
= g_strdup_printf ("changed::%s", global_key
);
430 g_signal_connect (global_settings
, name
, G_CALLBACK (value_changed
), NULL
);
433 g_signal_connect (global_settings
, "changed", G_CALLBACK (value_changed
), NULL
);
436 g_main_context_iteration (NULL
, TRUE
);
442 const GVariantType
*type
;
443 GError
*error
= NULL
;
445 gchar
*freeme
= NULL
;
447 type
= g_settings_schema_key_get_value_type (global_schema_key
);
449 new = g_variant_parse (type
, global_value
, NULL
, NULL
, &error
);
451 /* If that didn't work and the type is string then we should assume
452 * that the user is just trying to set a string directly and forgot
453 * the quotes (or had them consumed by the shell).
455 * If the user started with a quote then we assume that some deeper
456 * problem is at play and we want the failure in that case.
460 * gsettings set x.y.z key "'i don't expect this to work'"
462 * Note that we should not just add quotes and try parsing again, but
463 * rather assume that the user is providing us with a bare string.
464 * Assume we added single quotes, then consider this case:
466 * gsettings set x.y.z key "i'd expect this to work"
468 * A similar example could be given for double quotes.
470 * Avoid that whole mess by just using g_variant_new_string().
473 g_variant_type_equal (type
, G_VARIANT_TYPE_STRING
) &&
474 global_value
[0] != '\'' && global_value
[0] != '"')
476 g_clear_error (&error
);
477 new = g_variant_new_string (global_value
);
484 context
= g_variant_parse_error_print_context (error
, global_value
);
485 g_printerr ("%s", context
);
489 if (!g_settings_schema_key_range_check (global_schema_key
, new))
491 g_printerr (_("The provided value is outside of the valid range\n"));
492 g_variant_unref (new);
496 if (!g_settings_set_value (global_settings
, global_key
, new))
498 g_printerr (_("The key is not writable\n"));
508 gsettings_help (gboolean requested
,
509 const gchar
*command
)
511 const gchar
*description
;
512 const gchar
*synopsis
;
515 string
= g_string_new (NULL
);
520 else if (strcmp (command
, "help") == 0)
522 description
= _("Print help");
523 synopsis
= "[COMMAND]";
526 else if (strcmp (command
, "--version") == 0)
528 description
= _("Print version information and exit");
532 else if (strcmp (command
, "list-schemas") == 0)
534 description
= _("List the installed (non-relocatable) schemas");
538 else if (strcmp (command
, "list-relocatable-schemas") == 0)
540 description
= _("List the installed relocatable schemas");
544 else if (strcmp (command
, "list-keys") == 0)
546 description
= _("List the keys in SCHEMA");
547 synopsis
= N_("SCHEMA[:PATH]");
550 else if (strcmp (command
, "list-children") == 0)
552 description
= _("List the children of SCHEMA");
553 synopsis
= N_("SCHEMA[:PATH]");
556 else if (strcmp (command
, "list-recursively") == 0)
558 description
= _("List keys and values, recursively\n"
559 "If no SCHEMA is given, list all keys\n");
560 synopsis
= N_("[SCHEMA[:PATH]]");
563 else if (strcmp (command
, "get") == 0)
565 description
= _("Get the value of KEY");
566 synopsis
= N_("SCHEMA[:PATH] KEY");
569 else if (strcmp (command
, "range") == 0)
571 description
= _("Query the range of valid values for KEY");
572 synopsis
= N_("SCHEMA[:PATH] KEY");
575 else if (strcmp (command
, "describe") == 0)
577 description
= _("Query the description for KEY");
578 synopsis
= N_("SCHEMA[:PATH] KEY");
581 else if (strcmp (command
, "set") == 0)
583 description
= _("Set the value of KEY to VALUE");
584 synopsis
= N_("SCHEMA[:PATH] KEY VALUE");
587 else if (strcmp (command
, "reset") == 0)
589 description
= _("Reset KEY to its default value");
590 synopsis
= N_("SCHEMA[:PATH] KEY");
593 else if (strcmp (command
, "reset-recursively") == 0)
595 description
= _("Reset all keys in SCHEMA to their defaults");
596 synopsis
= N_("SCHEMA[:PATH]");
599 else if (strcmp (command
, "writable") == 0)
601 description
= _("Check if KEY is writable");
602 synopsis
= N_("SCHEMA[:PATH] KEY");
605 else if (strcmp (command
, "monitor") == 0)
607 description
= _("Monitor KEY for changes.\n"
608 "If no KEY is specified, monitor all keys in SCHEMA.\n"
609 "Use ^C to stop monitoring.\n");
610 synopsis
= N_("SCHEMA[:PATH] [KEY]");
614 g_string_printf (string
, _("Unknown command %s\n\n"), command
);
621 g_string_append (string
,
623 " gsettings --version\n"
624 " gsettings [--schemadir SCHEMADIR] COMMAND [ARGS…]\n"
627 " help Show this information\n"
628 " list-schemas List installed schemas\n"
629 " list-relocatable-schemas List relocatable schemas\n"
630 " list-keys List keys in a schema\n"
631 " list-children List children of a schema\n"
632 " list-recursively List keys and values, recursively\n"
633 " range Queries the range of a key\n"
634 " describe Queries the description of a key\n"
635 " get Get the value of a key\n"
636 " set Set the value of a key\n"
637 " reset Reset the value of a key\n"
638 " reset-recursively Reset all values in a given schema\n"
639 " writable Check if a key is writable\n"
640 " monitor Watch for changes\n"
642 "Use “gsettings help COMMAND” to get detailed help.\n\n"));
646 g_string_append_printf (string
, _("Usage:\n gsettings [--schemadir SCHEMADIR] %s %s\n\n%s\n\n"),
647 command
, synopsis
[0] ? _(synopsis
) : "", description
);
649 g_string_append (string
, _("Arguments:\n"));
651 g_string_append (string
,
652 _(" SCHEMADIR A directory to search for additional schemas\n"));
654 if (strstr (synopsis
, "[COMMAND]"))
655 g_string_append (string
,
656 _(" COMMAND The (optional) command to explain\n"));
658 else if (strstr (synopsis
, "SCHEMA"))
659 g_string_append (string
,
660 _(" SCHEMA The name of the schema\n"
661 " PATH The path, for relocatable schemas\n"));
663 if (strstr (synopsis
, "[KEY]"))
664 g_string_append (string
,
665 _(" KEY The (optional) key within the schema\n"));
667 else if (strstr (synopsis
, "KEY"))
668 g_string_append (string
,
669 _(" KEY The key within the schema\n"));
671 if (strstr (synopsis
, "VALUE"))
672 g_string_append (string
,
673 _(" VALUE The value to set\n"));
675 g_string_append (string
, "\n");
679 g_print ("%s", string
->str
);
681 g_printerr ("%s\n", string
->str
);
683 g_string_free (string
, TRUE
);
685 return requested
? 0 : 1;
690 main (int argc
, char **argv
)
692 void (* function
) (void);
693 gboolean need_settings
;
699 setlocale (LC_ALL
, "");
700 textdomain (GETTEXT_PACKAGE
);
703 tmp
= _glib_get_locale_dir ();
704 bindtextdomain (GETTEXT_PACKAGE
, tmp
);
707 bindtextdomain (GETTEXT_PACKAGE
, GLIB_LOCALE_DIR
);
710 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
711 bind_textdomain_codeset (GETTEXT_PACKAGE
, "UTF-8");
715 return gsettings_help (FALSE
, NULL
);
717 global_schema_source
= g_settings_schema_source_get_default ();
719 if (argc
> 3 && g_str_equal (argv
[1], "--schemadir"))
721 GSettingsSchemaSource
*parent
= global_schema_source
;
722 GError
*error
= NULL
;
724 global_schema_source
= g_settings_schema_source_new_from_directory (argv
[2], parent
, FALSE
, &error
);
726 if (global_schema_source
== NULL
)
728 g_printerr (_("Could not load schemas from %s: %s\n"), argv
[2], error
->message
);
729 g_clear_error (&error
);
734 /* shift remaining arguments (not correct wrt argv[0], but doesn't matter) */
738 else if (global_schema_source
== NULL
)
740 g_printerr (_("No schemas installed\n"));
744 g_settings_schema_source_ref (global_schema_source
);
746 need_settings
= TRUE
;
748 if (strcmp (argv
[1], "help") == 0)
749 return gsettings_help (TRUE
, argv
[2]);
751 else if (argc
== 2 && strcmp (argv
[1], "--version") == 0)
752 function
= gsettings_print_version
;
754 else if (argc
== 2 && strcmp (argv
[1], "list-schemas") == 0)
755 function
= gsettings_list_schemas
;
757 else if (argc
== 2 && strcmp (argv
[1], "list-relocatable-schemas") == 0)
758 function
= gsettings_list_relocatable_schemas
;
760 else if (argc
== 3 && strcmp (argv
[1], "list-keys") == 0)
762 need_settings
= FALSE
;
763 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], "describe") == 0)
774 need_settings
= FALSE
;
775 function
= gsettings_description
;
778 else if (argc
== 4 && strcmp (argv
[1], "range") == 0)
780 need_settings
= FALSE
;
781 function
= gsettings_range
;
784 else if (argc
== 4 && strcmp (argv
[1], "get") == 0)
785 function
= gsettings_get
;
787 else if (argc
== 5 && strcmp (argv
[1], "set") == 0)
788 function
= gsettings_set
;
790 else if (argc
== 4 && strcmp (argv
[1], "reset") == 0)
791 function
= gsettings_reset
;
793 else if (argc
== 3 && strcmp (argv
[1], "reset-recursively") == 0)
794 function
= gsettings_reset_recursively
;
796 else if (argc
== 4 && strcmp (argv
[1], "writable") == 0)
797 function
= gsettings_writable
;
799 else if ((argc
== 3 || argc
== 4) && strcmp (argv
[1], "monitor") == 0)
800 function
= gsettings_monitor
;
803 return gsettings_help (FALSE
, argv
[1]);
809 if (argv
[2][0] == '\0')
811 g_printerr (_("Empty schema name given\n"));
815 parts
= g_strsplit (argv
[2], ":", 2);
817 global_schema
= g_settings_schema_source_lookup (global_schema_source
, parts
[0], TRUE
);
823 if (!check_relocatable_schema (global_schema
, parts
[0]) || !check_path (parts
[1]))
826 global_settings
= g_settings_new_full (global_schema
, NULL
, parts
[1]);
830 if (!check_schema (global_schema
, parts
[0]))
833 global_settings
= g_settings_new_full (global_schema
, NULL
, NULL
);
838 /* If the user has given a path then we enforce that we have a
839 * relocatable schema, but if they didn't give a path then it
840 * doesn't matter what type of schema we have (since it's
841 * reasonable to ask for introspection information on a
842 * relocatable schema without having to give the path).
846 if (!check_relocatable_schema (global_schema
, parts
[0]) || !check_path (parts
[1]))
851 if (global_schema
== NULL
)
853 g_printerr (_("No such schema “%s”\n"), parts
[0]);
864 if (!g_settings_schema_has_key (global_schema
, argv
[3]))
866 g_printerr (_("No such key “%s”\n"), argv
[3]);
870 global_key
= argv
[3];
871 global_schema_key
= g_settings_schema_get_key (global_schema
, global_key
);
875 global_value
= argv
[4];
880 g_clear_pointer (&global_schema_source
, g_settings_schema_source_unref
);
881 g_clear_pointer (&global_schema_key
, g_settings_schema_key_unref
);
882 g_clear_pointer (&global_schema
, g_settings_schema_unref
);
883 g_clear_object (&global_settings
);