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_schemas_with_paths (void)
149 g_settings_schema_source_list_schemas (global_schema_source
, TRUE
, &schemas
, NULL
);
151 for (i
= 0; schemas
[i
] != NULL
; i
++)
153 GSettingsSchema
*schema
;
155 const gchar
*schema_path
;
157 schema_name
= g_steal_pointer (&schemas
[i
]);
159 schema
= g_settings_schema_source_lookup (global_schema_source
, schema_name
, TRUE
);
160 schema_path
= g_settings_schema_get_path (schema
);
162 schemas
[i
] = g_strconcat (schema_name
, " ", schema_path
, NULL
);
164 g_settings_schema_unref (schema
);
165 g_free (schema_name
);
168 output_list (schemas
);
169 g_strfreev (schemas
);
173 gsettings_list_relocatable_schemas (void)
177 g_settings_schema_source_list_schemas (global_schema_source
, TRUE
, NULL
, &schemas
);
178 output_list (schemas
);
179 g_strfreev (schemas
);
183 gsettings_list_keys (void)
187 keys
= g_settings_schema_list_keys (global_schema
);
193 gsettings_list_children (void)
199 children
= g_settings_list_children (global_settings
);
200 for (i
= 0; children
[i
]; i
++)
201 if (strlen (children
[i
]) > max
)
202 max
= strlen (children
[i
]);
204 for (i
= 0; children
[i
]; i
++)
207 GSettingsSchema
*schema
;
210 child
= g_settings_get_child (global_settings
, children
[i
]);
212 "settings-schema", &schema
,
216 if (g_settings_schema_get_path (schema
) != NULL
)
217 g_print ("%-*s %s\n", max
, children
[i
], g_settings_schema_get_id (schema
));
219 g_print ("%-*s %s:%s\n", max
, children
[i
], g_settings_schema_get_id (schema
), path
);
221 g_object_unref (child
);
222 g_settings_schema_unref (schema
);
226 g_strfreev (children
);
230 enumerate (GSettings
*settings
)
233 GSettingsSchema
*schema
;
236 g_object_get (settings
, "settings-schema", &schema
, NULL
);
238 keys
= g_settings_schema_list_keys (schema
);
239 for (i
= 0; keys
[i
]; i
++)
244 value
= g_settings_get_value (settings
, keys
[i
]);
245 printed
= g_variant_print (value
, TRUE
);
246 g_print ("%s %s %s\n", g_settings_schema_get_id (schema
), keys
[i
], printed
);
247 g_variant_unref (value
);
251 g_settings_schema_unref (schema
);
256 list_recursively (GSettings
*settings
)
261 enumerate (settings
);
262 children
= g_settings_list_children (settings
);
263 for (i
= 0; children
[i
]; i
++)
265 gboolean will_see_elsewhere
= FALSE
;
268 child
= g_settings_get_child (settings
, children
[i
]);
270 if (global_settings
== NULL
)
272 /* we're listing all non-relocatable settings objects from the
273 * top-level, so if this one is non-relocatable, don't recurse,
274 * because we will pick it up later on.
277 GSettingsSchema
*child_schema
;
279 g_object_get (child
, "settings-schema", &child_schema
, NULL
);
280 will_see_elsewhere
= !is_relocatable_schema (child_schema
);
281 g_settings_schema_unref (child_schema
);
284 if (!will_see_elsewhere
)
285 list_recursively (child
);
287 g_object_unref (child
);
290 g_strfreev (children
);
294 gsettings_list_recursively (void)
298 list_recursively (global_settings
);
305 g_settings_schema_source_list_schemas (global_schema_source
, TRUE
, &schemas
, NULL
);
307 for (i
= 0; schemas
[i
]; i
++)
311 settings
= g_settings_new (schemas
[i
]);
312 list_recursively (settings
);
313 g_object_unref (settings
);
316 g_strfreev (schemas
);
321 gsettings_description (void)
323 const gchar
*description
;
324 description
= g_settings_schema_key_get_description (global_schema_key
);
325 if (description
== NULL
)
326 description
= g_settings_schema_key_get_summary (global_schema_key
);
327 g_print ("%s\n", description
);
331 gsettings_range (void)
333 GVariant
*range
, *detail
;
336 range
= g_settings_schema_key_get_range (global_schema_key
);
337 g_variant_get (range
, "(&sv)", &type
, &detail
);
339 if (strcmp (type
, "type") == 0)
340 g_print ("type %s\n", g_variant_get_type_string (detail
) + 1);
342 else if (strcmp (type
, "range") == 0)
347 g_variant_get (detail
, "(**)", &min
, &max
);
348 smin
= g_variant_print (min
, FALSE
);
349 smax
= g_variant_print (max
, FALSE
);
351 g_print ("range %s %s %s\n",
352 g_variant_get_type_string (min
), smin
, smax
);
353 g_variant_unref (min
);
354 g_variant_unref (max
);
359 else if (strcmp (type
, "enum") == 0 || strcmp (type
, "flags") == 0)
364 g_print ("%s\n", type
);
366 g_variant_iter_init (&iter
, detail
);
367 while (g_variant_iter_loop (&iter
, "*", &item
))
371 printed
= g_variant_print (item
, FALSE
);
372 g_print ("%s\n", printed
);
377 g_variant_unref (detail
);
378 g_variant_unref (range
);
387 value
= g_settings_get_value (global_settings
, global_key
);
388 printed
= g_variant_print (value
, TRUE
);
389 g_print ("%s\n", printed
);
390 g_variant_unref (value
);
395 gsettings_reset (void)
397 g_settings_reset (global_settings
, global_key
);
402 reset_all_keys (GSettings
*settings
)
404 GSettingsSchema
*schema
;
408 g_object_get (settings
, "settings-schema", &schema
, NULL
);
410 keys
= g_settings_schema_list_keys (schema
);
411 for (i
= 0; keys
[i
]; i
++)
413 g_settings_reset (settings
, keys
[i
]);
416 g_settings_schema_unref (schema
);
421 gsettings_reset_recursively (void)
426 g_settings_delay (global_settings
);
428 reset_all_keys (global_settings
);
429 children
= g_settings_list_children (global_settings
);
430 for (i
= 0; children
[i
]; i
++)
433 child
= g_settings_get_child (global_settings
, children
[i
]);
435 reset_all_keys (child
);
437 g_object_unref (child
);
440 g_strfreev (children
);
442 g_settings_apply (global_settings
);
447 gsettings_writable (void)
450 g_settings_is_writable (global_settings
, global_key
) ?
455 value_changed (GSettings
*settings
,
462 value
= g_settings_get_value (settings
, key
);
463 printed
= g_variant_print (value
, TRUE
);
464 g_print ("%s: %s\n", key
, printed
);
465 g_variant_unref (value
);
470 gsettings_monitor (void)
476 name
= g_strdup_printf ("changed::%s", global_key
);
477 g_signal_connect (global_settings
, name
, G_CALLBACK (value_changed
), NULL
);
480 g_signal_connect (global_settings
, "changed", G_CALLBACK (value_changed
), NULL
);
483 g_main_context_iteration (NULL
, TRUE
);
489 const GVariantType
*type
;
490 GError
*error
= NULL
;
492 gchar
*freeme
= NULL
;
494 type
= g_settings_schema_key_get_value_type (global_schema_key
);
496 new = g_variant_parse (type
, global_value
, NULL
, NULL
, &error
);
498 /* If that didn't work and the type is string then we should assume
499 * that the user is just trying to set a string directly and forgot
500 * the quotes (or had them consumed by the shell).
502 * If the user started with a quote then we assume that some deeper
503 * problem is at play and we want the failure in that case.
507 * gsettings set x.y.z key "'i don't expect this to work'"
509 * Note that we should not just add quotes and try parsing again, but
510 * rather assume that the user is providing us with a bare string.
511 * Assume we added single quotes, then consider this case:
513 * gsettings set x.y.z key "i'd expect this to work"
515 * A similar example could be given for double quotes.
517 * Avoid that whole mess by just using g_variant_new_string().
520 g_variant_type_equal (type
, G_VARIANT_TYPE_STRING
) &&
521 global_value
[0] != '\'' && global_value
[0] != '"')
523 g_clear_error (&error
);
524 new = g_variant_new_string (global_value
);
531 context
= g_variant_parse_error_print_context (error
, global_value
);
532 g_printerr ("%s", context
);
536 if (!g_settings_schema_key_range_check (global_schema_key
, new))
538 g_printerr (_("The provided value is outside of the valid range\n"));
539 g_variant_unref (new);
543 if (!g_settings_set_value (global_settings
, global_key
, new))
545 g_printerr (_("The key is not writable\n"));
555 gsettings_help (gboolean requested
,
556 const gchar
*command
)
558 const gchar
*description
;
559 const gchar
*synopsis
;
562 string
= g_string_new (NULL
);
567 else if (strcmp (command
, "help") == 0)
569 description
= _("Print help");
570 synopsis
= "[COMMAND]";
573 else if (strcmp (command
, "--version") == 0)
575 description
= _("Print version information and exit");
579 else if (strcmp (command
, "list-schemas") == 0)
581 description
= _("List the installed (non-relocatable) schemas");
582 synopsis
= "[--print-paths]";
585 else if (strcmp (command
, "list-relocatable-schemas") == 0)
587 description
= _("List the installed relocatable schemas");
591 else if (strcmp (command
, "list-keys") == 0)
593 description
= _("List the keys in SCHEMA");
594 synopsis
= N_("SCHEMA[:PATH]");
597 else if (strcmp (command
, "list-children") == 0)
599 description
= _("List the children of SCHEMA");
600 synopsis
= N_("SCHEMA[:PATH]");
603 else if (strcmp (command
, "list-recursively") == 0)
605 description
= _("List keys and values, recursively\n"
606 "If no SCHEMA is given, list all keys\n");
607 synopsis
= N_("[SCHEMA[:PATH]]");
610 else if (strcmp (command
, "get") == 0)
612 description
= _("Get the value of KEY");
613 synopsis
= N_("SCHEMA[:PATH] KEY");
616 else if (strcmp (command
, "range") == 0)
618 description
= _("Query the range of valid values for KEY");
619 synopsis
= N_("SCHEMA[:PATH] KEY");
622 else if (strcmp (command
, "describe") == 0)
624 description
= _("Query the description for KEY");
625 synopsis
= N_("SCHEMA[:PATH] KEY");
628 else if (strcmp (command
, "set") == 0)
630 description
= _("Set the value of KEY to VALUE");
631 synopsis
= N_("SCHEMA[:PATH] KEY VALUE");
634 else if (strcmp (command
, "reset") == 0)
636 description
= _("Reset KEY to its default value");
637 synopsis
= N_("SCHEMA[:PATH] KEY");
640 else if (strcmp (command
, "reset-recursively") == 0)
642 description
= _("Reset all keys in SCHEMA to their defaults");
643 synopsis
= N_("SCHEMA[:PATH]");
646 else if (strcmp (command
, "writable") == 0)
648 description
= _("Check if KEY is writable");
649 synopsis
= N_("SCHEMA[:PATH] KEY");
652 else if (strcmp (command
, "monitor") == 0)
654 description
= _("Monitor KEY for changes.\n"
655 "If no KEY is specified, monitor all keys in SCHEMA.\n"
656 "Use ^C to stop monitoring.\n");
657 synopsis
= N_("SCHEMA[:PATH] [KEY]");
661 g_string_printf (string
, _("Unknown command %s\n\n"), command
);
668 g_string_append (string
,
670 " gsettings --version\n"
671 " gsettings [--schemadir SCHEMADIR] COMMAND [ARGS…]\n"
674 " help Show this information\n"
675 " list-schemas List installed schemas\n"
676 " list-relocatable-schemas List relocatable schemas\n"
677 " list-keys List keys in a schema\n"
678 " list-children List children of a schema\n"
679 " list-recursively List keys and values, recursively\n"
680 " range Queries the range of a key\n"
681 " describe Queries the description of a key\n"
682 " get Get the value of a key\n"
683 " set Set the value of a key\n"
684 " reset Reset the value of a key\n"
685 " reset-recursively Reset all values in a given schema\n"
686 " writable Check if a key is writable\n"
687 " monitor Watch for changes\n"
689 "Use “gsettings help COMMAND” to get detailed help.\n\n"));
693 g_string_append_printf (string
, _("Usage:\n gsettings [--schemadir SCHEMADIR] %s %s\n\n%s\n\n"),
694 command
, synopsis
[0] ? _(synopsis
) : "", description
);
696 g_string_append (string
, _("Arguments:\n"));
698 g_string_append (string
,
699 _(" SCHEMADIR A directory to search for additional schemas\n"));
701 if (strstr (synopsis
, "[COMMAND]"))
702 g_string_append (string
,
703 _(" COMMAND The (optional) command to explain\n"));
705 else if (strstr (synopsis
, "SCHEMA"))
706 g_string_append (string
,
707 _(" SCHEMA The name of the schema\n"
708 " PATH The path, for relocatable schemas\n"));
710 if (strstr (synopsis
, "[KEY]"))
711 g_string_append (string
,
712 _(" KEY The (optional) key within the schema\n"));
714 else if (strstr (synopsis
, "KEY"))
715 g_string_append (string
,
716 _(" KEY The key within the schema\n"));
718 if (strstr (synopsis
, "VALUE"))
719 g_string_append (string
,
720 _(" VALUE The value to set\n"));
722 g_string_append (string
, "\n");
726 g_print ("%s", string
->str
);
728 g_printerr ("%s\n", string
->str
);
730 g_string_free (string
, TRUE
);
732 return requested
? 0 : 1;
737 main (int argc
, char **argv
)
739 void (* function
) (void);
740 gboolean need_settings
, skip_third_arg_test
;
746 setlocale (LC_ALL
, "");
747 textdomain (GETTEXT_PACKAGE
);
750 tmp
= _glib_get_locale_dir ();
751 bindtextdomain (GETTEXT_PACKAGE
, tmp
);
754 bindtextdomain (GETTEXT_PACKAGE
, GLIB_LOCALE_DIR
);
757 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
758 bind_textdomain_codeset (GETTEXT_PACKAGE
, "UTF-8");
762 return gsettings_help (FALSE
, NULL
);
764 global_schema_source
= g_settings_schema_source_get_default ();
766 if (argc
> 3 && g_str_equal (argv
[1], "--schemadir"))
768 GSettingsSchemaSource
*parent
= global_schema_source
;
769 GError
*error
= NULL
;
771 global_schema_source
= g_settings_schema_source_new_from_directory (argv
[2], parent
, FALSE
, &error
);
773 if (global_schema_source
== NULL
)
775 g_printerr (_("Could not load schemas from %s: %s\n"), argv
[2], error
->message
);
776 g_clear_error (&error
);
781 /* shift remaining arguments (not correct wrt argv[0], but doesn't matter) */
785 else if (global_schema_source
== NULL
)
787 g_printerr (_("No schemas installed\n"));
791 g_settings_schema_source_ref (global_schema_source
);
793 need_settings
= TRUE
;
794 skip_third_arg_test
= FALSE
;
796 if (strcmp (argv
[1], "help") == 0)
797 return gsettings_help (TRUE
, argv
[2]);
799 else if (argc
== 2 && strcmp (argv
[1], "--version") == 0)
800 function
= gsettings_print_version
;
802 else if (argc
== 2 && strcmp (argv
[1], "list-schemas") == 0)
803 function
= gsettings_list_schemas
;
805 else if (argc
== 3 && strcmp (argv
[1], "list-schemas") == 0
806 && strcmp (argv
[2], "--print-paths") == 0)
808 skip_third_arg_test
= TRUE
;
809 function
= gsettings_list_schemas_with_paths
;
812 else if (argc
== 2 && strcmp (argv
[1], "list-relocatable-schemas") == 0)
813 function
= gsettings_list_relocatable_schemas
;
815 else if (argc
== 3 && strcmp (argv
[1], "list-keys") == 0)
817 need_settings
= FALSE
;
818 function
= gsettings_list_keys
;
821 else if (argc
== 3 && strcmp (argv
[1], "list-children") == 0)
822 function
= gsettings_list_children
;
824 else if ((argc
== 2 || argc
== 3) && strcmp (argv
[1], "list-recursively") == 0)
825 function
= gsettings_list_recursively
;
827 else if (argc
== 4 && strcmp (argv
[1], "describe") == 0)
829 need_settings
= FALSE
;
830 function
= gsettings_description
;
833 else if (argc
== 4 && strcmp (argv
[1], "range") == 0)
835 need_settings
= FALSE
;
836 function
= gsettings_range
;
839 else if (argc
== 4 && strcmp (argv
[1], "get") == 0)
840 function
= gsettings_get
;
842 else if (argc
== 5 && strcmp (argv
[1], "set") == 0)
843 function
= gsettings_set
;
845 else if (argc
== 4 && strcmp (argv
[1], "reset") == 0)
846 function
= gsettings_reset
;
848 else if (argc
== 3 && strcmp (argv
[1], "reset-recursively") == 0)
849 function
= gsettings_reset_recursively
;
851 else if (argc
== 4 && strcmp (argv
[1], "writable") == 0)
852 function
= gsettings_writable
;
854 else if ((argc
== 3 || argc
== 4) && strcmp (argv
[1], "monitor") == 0)
855 function
= gsettings_monitor
;
858 return gsettings_help (FALSE
, argv
[1]);
860 if (argc
> 2 && !skip_third_arg_test
)
864 if (argv
[2][0] == '\0')
866 g_printerr (_("Empty schema name given\n"));
870 parts
= g_strsplit (argv
[2], ":", 2);
872 global_schema
= g_settings_schema_source_lookup (global_schema_source
, parts
[0], TRUE
);
878 if (!check_relocatable_schema (global_schema
, parts
[0]) || !check_path (parts
[1]))
881 global_settings
= g_settings_new_full (global_schema
, NULL
, parts
[1]);
885 if (!check_schema (global_schema
, parts
[0]))
888 global_settings
= g_settings_new_full (global_schema
, NULL
, NULL
);
893 /* If the user has given a path then we enforce that we have a
894 * relocatable schema, but if they didn't give a path then it
895 * doesn't matter what type of schema we have (since it's
896 * reasonable to ask for introspection information on a
897 * relocatable schema without having to give the path).
901 if (!check_relocatable_schema (global_schema
, parts
[0]) || !check_path (parts
[1]))
906 if (global_schema
== NULL
)
908 g_printerr (_("No such schema “%s”\n"), parts
[0]);
919 if (!g_settings_schema_has_key (global_schema
, argv
[3]))
921 g_printerr (_("No such key “%s”\n"), argv
[3]);
925 global_key
= argv
[3];
926 global_schema_key
= g_settings_schema_get_key (global_schema
, global_key
);
930 global_value
= argv
[4];
935 g_clear_pointer (&global_schema_source
, g_settings_schema_source_unref
);
936 g_clear_pointer (&global_schema_key
, g_settings_schema_key_unref
);
937 g_clear_pointer (&global_schema
, g_settings_schema_unref
);
938 g_clear_object (&global_settings
);