win32: make g_cond_wait_until() wait at least until end_time before returning with...
[glib.git] / gio / gsettings-tool.c
blobb7dd9d8fb9284e4ec04beefc50390b58e6307308
1 /*
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>
20 #include "config.h"
22 #include <gio/gio.h>
23 #include <gi18n.h>
24 #include <locale.h>
25 #include <string.h>
26 #include <stdlib.h>
28 #ifdef G_OS_WIN32
29 #include "glib/glib-private.h"
30 #endif
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;
39 static gboolean
40 is_relocatable_schema (GSettingsSchema *schema)
42 return g_settings_schema_get_path (schema) == NULL;
45 static gboolean
46 check_relocatable_schema (GSettingsSchema *schema,
47 const gchar *schema_id)
49 if (schema == NULL)
51 g_printerr (_("No such schema “%s”\n"), schema_id);
52 return FALSE;
55 if (!is_relocatable_schema (schema))
57 g_printerr (_("Schema “%s” is not relocatable "
58 "(path must not be specified)\n"),
59 schema_id);
60 return FALSE;
63 return TRUE;
66 static gboolean
67 check_schema (GSettingsSchema *schema,
68 const gchar *schema_id)
70 if (schema == NULL)
72 g_printerr (_("No such schema “%s”\n"), schema_id);
73 return FALSE;
76 if (is_relocatable_schema (schema))
78 g_printerr (_("Schema “%s” is relocatable "
79 "(path must be specified)\n"),
80 schema_id);
81 return FALSE;
84 return TRUE;
87 static gboolean
88 check_path (const gchar *path)
90 if (path[0] == '\0')
92 g_printerr (_("Empty path given.\n"));
93 return FALSE;
96 if (path[0] != '/')
98 g_printerr (_("Path must begin with a slash (/)\n"));
99 return FALSE;
102 if (!g_str_has_suffix (path, "/"))
104 g_printerr (_("Path must end with a slash (/)\n"));
105 return FALSE;
108 if (strstr (path, "//"))
110 g_printerr (_("Path must not contain two adjacent slashes (//)\n"));
111 return FALSE;
114 return TRUE;
117 static void
118 output_list (gchar **list)
120 gint i;
122 for (i = 0; list[i]; i++)
123 g_print ("%s\n", list[i]);
126 static void
127 gsettings_print_version (void)
129 g_print ("%d.%d.%d\n", glib_major_version, glib_minor_version,
130 glib_micro_version);
133 static void
134 gsettings_list_schemas (void)
136 gchar **schemas;
138 g_settings_schema_source_list_schemas (global_schema_source, TRUE, &schemas, NULL);
139 output_list (schemas);
140 g_strfreev (schemas);
143 static void
144 gsettings_list_schemas_with_paths (void)
146 gchar **schemas;
147 gsize i;
149 g_settings_schema_source_list_schemas (global_schema_source, TRUE, &schemas, NULL);
151 for (i = 0; schemas[i] != NULL; i++)
153 GSettingsSchema *schema;
154 gchar *schema_name;
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);
172 static void
173 gsettings_list_relocatable_schemas (void)
175 gchar **schemas;
177 g_settings_schema_source_list_schemas (global_schema_source, TRUE, NULL, &schemas);
178 output_list (schemas);
179 g_strfreev (schemas);
182 static void
183 gsettings_list_keys (void)
185 gchar **keys;
187 keys = g_settings_schema_list_keys (global_schema);
188 output_list (keys);
189 g_strfreev (keys);
192 static void
193 gsettings_list_children (void)
195 gchar **children;
196 gint max = 0;
197 gint i;
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++)
206 GSettings *child;
207 GSettingsSchema *schema;
208 gchar *path;
210 child = g_settings_get_child (global_settings, children[i]);
211 g_object_get (child,
212 "settings-schema", &schema,
213 "path", &path,
214 NULL);
216 if (g_settings_schema_get_path (schema) != NULL)
217 g_print ("%-*s %s\n", max, children[i], g_settings_schema_get_id (schema));
218 else
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);
223 g_free (path);
226 g_strfreev (children);
229 static void
230 enumerate (GSettings *settings)
232 gchar **keys;
233 GSettingsSchema *schema;
234 gint i;
236 g_object_get (settings, "settings-schema", &schema, NULL);
238 keys = g_settings_schema_list_keys (schema);
239 for (i = 0; keys[i]; i++)
241 GVariant *value;
242 gchar *printed;
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);
248 g_free (printed);
251 g_settings_schema_unref (schema);
252 g_strfreev (keys);
255 static void
256 list_recursively (GSettings *settings)
258 gchar **children;
259 gint i;
261 enumerate (settings);
262 children = g_settings_list_children (settings);
263 for (i = 0; children[i]; i++)
265 gboolean will_see_elsewhere = FALSE;
266 GSettings *child;
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);
293 static void
294 gsettings_list_recursively (void)
296 if (global_settings)
298 list_recursively (global_settings);
300 else
302 gchar **schemas;
303 gint i;
305 g_settings_schema_source_list_schemas (global_schema_source, TRUE, &schemas, NULL);
307 for (i = 0; schemas[i]; i++)
309 GSettings *settings;
311 settings = g_settings_new (schemas[i]);
312 list_recursively (settings);
313 g_object_unref (settings);
316 g_strfreev (schemas);
320 static void
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);
330 static void
331 gsettings_range (void)
333 GVariant *range, *detail;
334 const gchar *type;
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)
344 GVariant *min, *max;
345 gchar *smin, *smax;
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);
355 g_free (smin);
356 g_free (smax);
359 else if (strcmp (type, "enum") == 0 || strcmp (type, "flags") == 0)
361 GVariantIter iter;
362 GVariant *item;
364 g_print ("%s\n", type);
366 g_variant_iter_init (&iter, detail);
367 while (g_variant_iter_loop (&iter, "*", &item))
369 gchar *printed;
371 printed = g_variant_print (item, FALSE);
372 g_print ("%s\n", printed);
373 g_free (printed);
377 g_variant_unref (detail);
378 g_variant_unref (range);
381 static void
382 gsettings_get (void)
384 GVariant *value;
385 gchar *printed;
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);
391 g_free (printed);
394 static void
395 gsettings_reset (void)
397 g_settings_reset (global_settings, global_key);
398 g_settings_sync ();
401 static void
402 reset_all_keys (GSettings *settings)
404 GSettingsSchema *schema;
405 gchar **keys;
406 gint i;
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);
417 g_strfreev (keys);
420 static void
421 gsettings_reset_recursively (void)
423 gchar **children;
424 gint i;
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++)
432 GSettings *child;
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);
443 g_settings_sync ();
446 static void
447 gsettings_writable (void)
449 g_print ("%s\n",
450 g_settings_is_writable (global_settings, global_key) ?
451 "true" : "false");
454 static void
455 value_changed (GSettings *settings,
456 const gchar *key,
457 gpointer user_data)
459 GVariant *value;
460 gchar *printed;
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);
466 g_free (printed);
469 static void
470 gsettings_monitor (void)
472 if (global_key)
474 gchar *name;
476 name = g_strdup_printf ("changed::%s", global_key);
477 g_signal_connect (global_settings, name, G_CALLBACK (value_changed), NULL);
479 else
480 g_signal_connect (global_settings, "changed", G_CALLBACK (value_changed), NULL);
482 for (;;)
483 g_main_context_iteration (NULL, TRUE);
486 static void
487 gsettings_set (void)
489 const GVariantType *type;
490 GError *error = NULL;
491 GVariant *new;
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.
505 * Consider:
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().
519 if (new == NULL &&
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);
527 if (new == NULL)
529 gchar *context;
531 context = g_variant_parse_error_print_context (error, global_value);
532 g_printerr ("%s", context);
533 exit (1);
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);
540 exit (1);
543 if (!g_settings_set_value (global_settings, global_key, new))
545 g_printerr (_("The key is not writable\n"));
546 exit (1);
549 g_settings_sync ();
551 g_free (freeme);
554 static int
555 gsettings_help (gboolean requested,
556 const gchar *command)
558 const gchar *description;
559 const gchar *synopsis;
560 GString *string;
562 string = g_string_new (NULL);
564 if (command == 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");
576 synopsis = "";
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");
588 synopsis = "";
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]");
659 else
661 g_string_printf (string, _("Unknown command %s\n\n"), command);
662 requested = FALSE;
663 command = NULL;
666 if (command == NULL)
668 g_string_append (string,
669 _("Usage:\n"
670 " gsettings --version\n"
671 " gsettings [--schemadir SCHEMADIR] COMMAND [ARGS…]\n"
672 "\n"
673 "Commands:\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"
688 "\n"
689 "Use “gsettings help COMMAND” to get detailed help.\n\n"));
691 else
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");
725 if (requested)
726 g_print ("%s", string->str);
727 else
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;
742 #ifdef G_OS_WIN32
743 gchar *tmp;
744 #endif
746 setlocale (LC_ALL, "");
747 textdomain (GETTEXT_PACKAGE);
749 #ifdef G_OS_WIN32
750 tmp = _glib_get_locale_dir ();
751 bindtextdomain (GETTEXT_PACKAGE, tmp);
752 g_free (tmp);
753 #else
754 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
755 #endif
757 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
758 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
759 #endif
761 if (argc < 2)
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);
778 return 1;
781 /* shift remaining arguments (not correct wrt argv[0], but doesn't matter) */
782 argv = argv + 2;
783 argc -= 2;
785 else if (global_schema_source == NULL)
787 g_printerr (_("No schemas installed\n"));
788 return 1;
790 else
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;
857 else
858 return gsettings_help (FALSE, argv[1]);
860 if (argc > 2 && !skip_third_arg_test)
862 gchar **parts;
864 if (argv[2][0] == '\0')
866 g_printerr (_("Empty schema name given\n"));
867 return 1;
870 parts = g_strsplit (argv[2], ":", 2);
872 global_schema = g_settings_schema_source_lookup (global_schema_source, parts[0], TRUE);
874 if (need_settings)
876 if (parts[1])
878 if (!check_relocatable_schema (global_schema, parts[0]) || !check_path (parts[1]))
879 return 1;
881 global_settings = g_settings_new_full (global_schema, NULL, parts[1]);
883 else
885 if (!check_schema (global_schema, parts[0]))
886 return 1;
888 global_settings = g_settings_new_full (global_schema, NULL, NULL);
891 else
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).
899 if (parts[1])
901 if (!check_relocatable_schema (global_schema, parts[0]) || !check_path (parts[1]))
902 return 1;
904 else
906 if (global_schema == NULL)
908 g_printerr (_("No such schema “%s”\n"), parts[0]);
909 return 1;
914 g_strfreev (parts);
917 if (argc > 3)
919 if (!g_settings_schema_has_key (global_schema, argv[3]))
921 g_printerr (_("No such key “%s”\n"), argv[3]);
922 return 1;
925 global_key = argv[3];
926 global_schema_key = g_settings_schema_get_key (global_schema, global_key);
929 if (argc > 4)
930 global_value = argv[4];
932 (* function) ();
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);
940 return 0;