tests: only chmod autorun.exe on UNIX
[glib.git] / gio / gsettings-tool.c
blobcf3ef19137f1b8c4f969b7a80baa5a7e32fcea24
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 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>
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_relocatable_schemas (void)
146 gchar **schemas;
148 g_settings_schema_source_list_schemas (global_schema_source, TRUE, NULL, &schemas);
149 output_list (schemas);
150 g_strfreev (schemas);
153 static void
154 gsettings_list_keys (void)
156 gchar **keys;
158 keys = g_settings_list_keys (global_settings);
159 output_list (keys);
160 g_strfreev (keys);
163 static void
164 gsettings_list_children (void)
166 gchar **children;
167 gint max = 0;
168 gint i;
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++)
177 GSettings *child;
178 GSettingsSchema *schema;
179 gchar *path;
181 child = g_settings_get_child (global_settings, children[i]);
182 g_object_get (child,
183 "settings-schema", &schema,
184 "path", &path,
185 NULL);
187 if (g_settings_schema_get_path (schema) != NULL)
188 g_print ("%-*s %s\n", max, children[i], g_settings_schema_get_id (schema));
189 else
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);
194 g_free (path);
197 g_strfreev (children);
200 static void
201 enumerate (GSettings *settings)
203 gchar **keys;
204 gchar *schema;
205 gint i;
207 g_object_get (settings, "schema-id", &schema, NULL);
209 keys = g_settings_list_keys (settings);
210 for (i = 0; keys[i]; i++)
212 GVariant *value;
213 gchar *printed;
215 value = g_settings_get_value (settings, keys[i]);
216 printed = g_variant_print (value, TRUE);
217 g_print ("%s %s %s\n", schema, keys[i], printed);
218 g_variant_unref (value);
219 g_free (printed);
222 g_free (schema);
223 g_strfreev (keys);
226 static void
227 list_recursively (GSettings *settings)
229 gchar **children;
230 gint i;
232 enumerate (settings);
233 children = g_settings_list_children (settings);
234 for (i = 0; children[i]; i++)
236 GSettings *child;
238 child = g_settings_get_child (settings, children[i]);
239 list_recursively (child);
240 g_object_unref (child);
243 g_strfreev (children);
246 static void
247 gsettings_list_recursively (void)
249 if (global_settings)
251 list_recursively (global_settings);
253 else
255 gchar **schemas;
256 gint i;
258 g_settings_schema_source_list_schemas (global_schema_source, TRUE, &schemas, NULL);
260 for (i = 0; schemas[i]; i++)
262 GSettings *settings;
264 settings = g_settings_new (schemas[i]);
265 list_recursively (settings);
266 g_object_unref (settings);
269 g_strfreev (schemas);
273 static void
274 gsettings_range (void)
276 GVariant *range, *detail;
277 const gchar *type;
279 range = g_settings_schema_key_get_range (global_schema_key);
280 g_variant_get (range, "(&sv)", &type, &detail);
282 if (strcmp (type, "type") == 0)
283 g_print ("type %s\n", g_variant_get_type_string (detail) + 1);
285 else if (strcmp (type, "range") == 0)
287 GVariant *min, *max;
288 gchar *smin, *smax;
290 g_variant_get (detail, "(**)", &min, &max);
291 smin = g_variant_print (min, FALSE);
292 smax = g_variant_print (max, FALSE);
294 g_print ("range %s %s %s\n",
295 g_variant_get_type_string (min), smin, smax);
296 g_variant_unref (min);
297 g_variant_unref (max);
298 g_free (smin);
299 g_free (smax);
302 else if (strcmp (type, "enum") == 0 || strcmp (type, "flags") == 0)
304 GVariantIter iter;
305 GVariant *item;
307 g_print ("%s\n", type);
309 g_variant_iter_init (&iter, detail);
310 while (g_variant_iter_loop (&iter, "*", &item))
312 gchar *printed;
314 printed = g_variant_print (item, FALSE);
315 g_print ("%s\n", printed);
316 g_free (printed);
320 g_variant_unref (detail);
321 g_variant_unref (range);
324 static void
325 gsettings_get (void)
327 GVariant *value;
328 gchar *printed;
330 value = g_settings_get_value (global_settings, global_key);
331 printed = g_variant_print (value, TRUE);
332 g_print ("%s\n", printed);
333 g_variant_unref (value);
334 g_free (printed);
337 static void
338 gsettings_reset (void)
340 g_settings_reset (global_settings, global_key);
341 g_settings_sync ();
344 static void
345 reset_all_keys (GSettings *settings)
347 gchar **keys;
348 gint i;
350 keys = g_settings_list_keys (settings);
351 for (i = 0; keys[i]; i++)
353 g_settings_reset (settings, keys[i]);
356 g_strfreev (keys);
359 static void
360 gsettings_reset_recursively (void)
362 gchar **children;
363 gint i;
365 g_settings_delay (global_settings);
367 reset_all_keys (global_settings);
368 children = g_settings_list_children (global_settings);
369 for (i = 0; children[i]; i++)
371 GSettings *child;
372 child = g_settings_get_child (global_settings, children[i]);
374 reset_all_keys (child);
376 g_object_unref (child);
379 g_strfreev (children);
381 g_settings_apply (global_settings);
382 g_settings_sync ();
385 static void
386 gsettings_writable (void)
388 g_print ("%s\n",
389 g_settings_is_writable (global_settings, global_key) ?
390 "true" : "false");
393 static void
394 value_changed (GSettings *settings,
395 const gchar *key,
396 gpointer user_data)
398 GVariant *value;
399 gchar *printed;
401 value = g_settings_get_value (settings, key);
402 printed = g_variant_print (value, TRUE);
403 g_print ("%s: %s\n", key, printed);
404 g_variant_unref (value);
405 g_free (printed);
408 static void
409 gsettings_monitor (void)
411 gchar **keys;
413 if (global_key)
415 gchar *name;
417 name = g_strdup_printf ("changed::%s", global_key);
418 g_signal_connect (global_settings, name, G_CALLBACK (value_changed), NULL);
420 else
421 g_signal_connect (global_settings, "changed", G_CALLBACK (value_changed), NULL);
423 /* We have to read a value from GSettings before we start receiving
424 * signals...
426 * If the schema has zero keys then we won't be displaying any
427 * notifications anyway.
429 keys = g_settings_list_keys (global_settings);
430 if (keys[0])
431 g_variant_unref (g_settings_get_value (global_settings, keys[0]));
432 g_strfreev (keys);
434 for (;;)
435 g_main_context_iteration (NULL, TRUE);
438 static void
439 gsettings_set (void)
441 const GVariantType *type;
442 GError *error = NULL;
443 GVariant *new;
444 gchar *freeme = NULL;
446 type = g_settings_schema_key_get_value_type (global_schema_key);
448 new = g_variant_parse (type, global_value, NULL, NULL, &error);
450 /* If that didn't work and the type is string then we should assume
451 * that the user is just trying to set a string directly and forgot
452 * the quotes (or had them consumed by the shell).
454 * If the user started with a quote then we assume that some deeper
455 * problem is at play and we want the failure in that case.
457 * Consider:
459 * gsettings set x.y.z key "'i don't expect this to work'"
461 * Note that we should not just add quotes and try parsing again, but
462 * rather assume that the user is providing us with a bare string.
463 * Assume we added single quotes, then consider this case:
465 * gsettings set x.y.z key "i'd expect this to work"
467 * A similar example could be given for double quotes.
469 * Avoid that whole mess by just using g_variant_new_string().
471 if (new == NULL &&
472 g_variant_type_equal (type, G_VARIANT_TYPE_STRING) &&
473 global_value[0] != '\'' && global_value[0] != '"')
475 g_clear_error (&error);
476 new = g_variant_new_string (global_value);
479 if (new == NULL)
481 gchar *context;
483 context = g_variant_parse_error_print_context (error, global_value);
484 g_printerr ("%s", context);
485 exit (1);
488 if (!g_settings_schema_key_range_check (global_schema_key, new))
490 g_printerr (_("The provided value is outside of the valid range\n"));
491 g_variant_unref (new);
492 exit (1);
495 if (!g_settings_set_value (global_settings, global_key, new))
497 g_printerr (_("The key is not writable\n"));
498 exit (1);
501 g_settings_sync ();
503 g_free (freeme);
506 static int
507 gsettings_help (gboolean requested,
508 const gchar *command)
510 const gchar *description;
511 const gchar *synopsis;
512 GString *string;
514 string = g_string_new (NULL);
516 if (command == NULL)
519 else if (strcmp (command, "help") == 0)
521 description = _("Print help");
522 synopsis = "[COMMAND]";
525 else if (strcmp (command, "--version") == 0)
527 description = _("Print version information and exit");
528 synopsis = "";
531 else if (strcmp (command, "list-schemas") == 0)
533 description = _("List the installed (non-relocatable) schemas");
534 synopsis = "";
537 else if (strcmp (command, "list-relocatable-schemas") == 0)
539 description = _("List the installed relocatable schemas");
540 synopsis = "";
543 else if (strcmp (command, "list-keys") == 0)
545 description = _("List the keys in SCHEMA");
546 synopsis = N_("SCHEMA[:PATH]");
549 else if (strcmp (command, "list-children") == 0)
551 description = _("List the children of SCHEMA");
552 synopsis = N_("SCHEMA[:PATH]");
555 else if (strcmp (command, "list-recursively") == 0)
557 description = _("List keys and values, recursively\n"
558 "If no SCHEMA is given, list all keys\n");
559 synopsis = N_("[SCHEMA[:PATH]]");
562 else if (strcmp (command, "get") == 0)
564 description = _("Get the value of KEY");
565 synopsis = N_("SCHEMA[:PATH] KEY");
568 else if (strcmp (command, "range") == 0)
570 description = _("Query the range of valid values for KEY");
571 synopsis = N_("SCHEMA[:PATH] KEY");
574 else if (strcmp (command, "set") == 0)
576 description = _("Set the value of KEY to VALUE");
577 synopsis = N_("SCHEMA[:PATH] KEY VALUE");
580 else if (strcmp (command, "reset") == 0)
582 description = _("Reset KEY to its default value");
583 synopsis = N_("SCHEMA[:PATH] KEY");
586 else if (strcmp (command, "reset-recursively") == 0)
588 description = _("Reset all keys in SCHEMA to their defaults");
589 synopsis = N_("SCHEMA[:PATH]");
592 else if (strcmp (command, "writable") == 0)
594 description = _("Check if KEY is writable");
595 synopsis = N_("SCHEMA[:PATH] KEY");
598 else if (strcmp (command, "monitor") == 0)
600 description = _("Monitor KEY for changes.\n"
601 "If no KEY is specified, monitor all keys in SCHEMA.\n"
602 "Use ^C to stop monitoring.\n");
603 synopsis = N_("SCHEMA[:PATH] [KEY]");
605 else
607 g_string_printf (string, _("Unknown command %s\n\n"), command);
608 requested = FALSE;
609 command = NULL;
612 if (command == NULL)
614 g_string_append (string,
615 _("Usage:\n"
616 " gsettings --version\n"
617 " gsettings [--schemadir SCHEMADIR] COMMAND [ARGS...]\n"
618 "\n"
619 "Commands:\n"
620 " help Show this information\n"
621 " list-schemas List installed schemas\n"
622 " list-relocatable-schemas List relocatable schemas\n"
623 " list-keys List keys in a schema\n"
624 " list-children List children of a schema\n"
625 " list-recursively List keys and values, recursively\n"
626 " range Queries the range of a key\n"
627 " get Get the value of a key\n"
628 " set Set the value of a key\n"
629 " reset Reset the value of a key\n"
630 " reset-recursively Reset all values in a given schema\n"
631 " writable Check if a key is writable\n"
632 " monitor Watch for changes\n"
633 "\n"
634 "Use 'gsettings help COMMAND' to get detailed help.\n\n"));
636 else
638 g_string_append_printf (string, _("Usage:\n gsettings [--schemadir SCHEMADIR] %s %s\n\n%s\n\n"),
639 command, synopsis[0] ? _(synopsis) : "", description);
641 g_string_append (string, _("Arguments:\n"));
643 g_string_append (string,
644 _(" SCHEMADIR A directory to search for additional schemas\n"));
646 if (strstr (synopsis, "[COMMAND]"))
647 g_string_append (string,
648 _(" COMMAND The (optional) command to explain\n"));
650 else if (strstr (synopsis, "SCHEMA"))
651 g_string_append (string,
652 _(" SCHEMA The name of the schema\n"
653 " PATH The path, for relocatable schemas\n"));
655 if (strstr (synopsis, "[KEY]"))
656 g_string_append (string,
657 _(" KEY The (optional) key within the schema\n"));
659 else if (strstr (synopsis, "KEY"))
660 g_string_append (string,
661 _(" KEY The key within the schema\n"));
663 if (strstr (synopsis, "VALUE"))
664 g_string_append (string,
665 _(" VALUE The value to set\n"));
667 g_string_append (string, "\n");
670 if (requested)
671 g_print ("%s", string->str);
672 else
673 g_printerr ("%s\n", string->str);
675 g_string_free (string, TRUE);
677 return requested ? 0 : 1;
682 main (int argc, char **argv)
684 void (* function) (void);
686 #ifdef G_OS_WIN32
687 gchar *tmp;
688 #endif
690 setlocale (LC_ALL, "");
691 textdomain (GETTEXT_PACKAGE);
693 #ifdef G_OS_WIN32
694 tmp = _glib_get_locale_dir ();
695 bindtextdomain (GETTEXT_PACKAGE, tmp);
696 g_free (tmp);
697 #else
698 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
699 #endif
701 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
702 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
703 #endif
705 if (argc < 2)
706 return gsettings_help (FALSE, NULL);
708 global_schema_source = g_settings_schema_source_ref (g_settings_schema_source_get_default ());
710 if (argc > 3 && g_str_equal (argv[1], "--schemadir"))
712 GSettingsSchemaSource *parent = global_schema_source;
713 GError *error = NULL;
715 global_schema_source = g_settings_schema_source_new_from_directory (argv[2], parent, FALSE, &error);
716 g_settings_schema_source_unref (parent);
718 if (global_schema_source == NULL)
720 g_printerr (_("Could not load schemas from %s: %s\n"), argv[2], error->message);
721 g_clear_error (&error);
723 return 1;
726 /* shift remaining arguments (not correct wrt argv[0], but doesn't matter) */
727 argv = argv + 2;
728 argc -= 2;
731 if (strcmp (argv[1], "help") == 0)
732 return gsettings_help (TRUE, argv[2]);
734 else if (argc == 2 && strcmp (argv[1], "--version") == 0)
735 function = gsettings_print_version;
737 else if (argc == 2 && strcmp (argv[1], "list-schemas") == 0)
738 function = gsettings_list_schemas;
740 else if (argc == 2 && strcmp (argv[1], "list-relocatable-schemas") == 0)
741 function = gsettings_list_relocatable_schemas;
743 else if (argc == 3 && strcmp (argv[1], "list-keys") == 0)
744 function = gsettings_list_keys;
746 else if (argc == 3 && strcmp (argv[1], "list-children") == 0)
747 function = gsettings_list_children;
749 else if ((argc == 2 || argc == 3) && strcmp (argv[1], "list-recursively") == 0)
750 function = gsettings_list_recursively;
752 else if (argc == 4 && strcmp (argv[1], "range") == 0)
753 function = gsettings_range;
755 else if (argc == 4 && strcmp (argv[1], "get") == 0)
756 function = gsettings_get;
758 else if (argc == 5 && strcmp (argv[1], "set") == 0)
759 function = gsettings_set;
761 else if (argc == 4 && strcmp (argv[1], "reset") == 0)
762 function = gsettings_reset;
764 else if (argc == 3 && strcmp (argv[1], "reset-recursively") == 0)
765 function = gsettings_reset_recursively;
767 else if (argc == 4 && strcmp (argv[1], "writable") == 0)
768 function = gsettings_writable;
770 else if ((argc == 3 || argc == 4) && strcmp (argv[1], "monitor") == 0)
771 function = gsettings_monitor;
773 else
774 return gsettings_help (FALSE, argv[1]);
776 if (argc > 2)
778 gchar **parts;
780 if (argv[2][0] == '\0')
782 g_printerr (_("Empty schema name given\n"));
783 return 1;
786 parts = g_strsplit (argv[2], ":", 2);
788 global_schema = g_settings_schema_source_lookup (global_schema_source, parts[0], TRUE);
789 if (parts[1])
791 if (!check_relocatable_schema (global_schema, parts[0]) || !check_path (parts[1]))
792 return 1;
794 global_settings = g_settings_new_full (global_schema, NULL, parts[1]);
796 else
798 if (!check_schema (global_schema, parts[0]))
799 return 1;
801 global_settings = g_settings_new_full (global_schema, NULL, NULL);
804 g_strfreev (parts);
807 if (argc > 3)
809 if (!g_settings_schema_has_key (global_schema, argv[3]))
811 g_printerr (_("No such key '%s'\n"), argv[3]);
812 return 1;
815 global_key = argv[3];
816 global_schema_key = g_settings_schema_get_key (global_schema, global_key);
819 if (argc > 4)
820 global_value = argv[4];
822 (* function) ();
825 g_clear_pointer (&global_schema_source, g_settings_schema_source_unref);
826 g_clear_pointer (&global_schema_key, g_settings_schema_key_unref);
827 g_clear_pointer (&global_schema, g_settings_schema_unref);
828 g_clear_object (&global_settings);
830 return 0;