Update Friulian translation
[glib.git] / gio / gsettings-tool.c
blobd74b6dd9e73d1af3923a2cf7dab906e60eab637d
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_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_schema_list_keys (global_schema);
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 GSettingsSchema *schema;
205 gint i;
207 g_object_get (settings, "settings-schema", &schema, NULL);
209 keys = g_settings_schema_list_keys (schema);
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", g_settings_schema_get_id (schema), keys[i], printed);
218 g_variant_unref (value);
219 g_free (printed);
222 g_settings_schema_unref (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_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);
283 static void
284 gsettings_range (void)
286 GVariant *range, *detail;
287 const gchar *type;
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)
297 GVariant *min, *max;
298 gchar *smin, *smax;
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);
308 g_free (smin);
309 g_free (smax);
312 else if (strcmp (type, "enum") == 0 || strcmp (type, "flags") == 0)
314 GVariantIter iter;
315 GVariant *item;
317 g_print ("%s\n", type);
319 g_variant_iter_init (&iter, detail);
320 while (g_variant_iter_loop (&iter, "*", &item))
322 gchar *printed;
324 printed = g_variant_print (item, FALSE);
325 g_print ("%s\n", printed);
326 g_free (printed);
330 g_variant_unref (detail);
331 g_variant_unref (range);
334 static void
335 gsettings_get (void)
337 GVariant *value;
338 gchar *printed;
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);
344 g_free (printed);
347 static void
348 gsettings_reset (void)
350 g_settings_reset (global_settings, global_key);
351 g_settings_sync ();
354 static void
355 reset_all_keys (GSettings *settings)
357 GSettingsSchema *schema;
358 gchar **keys;
359 gint i;
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);
370 g_strfreev (keys);
373 static void
374 gsettings_reset_recursively (void)
376 gchar **children;
377 gint i;
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++)
385 GSettings *child;
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);
396 g_settings_sync ();
399 static void
400 gsettings_writable (void)
402 g_print ("%s\n",
403 g_settings_is_writable (global_settings, global_key) ?
404 "true" : "false");
407 static void
408 value_changed (GSettings *settings,
409 const gchar *key,
410 gpointer user_data)
412 GVariant *value;
413 gchar *printed;
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);
419 g_free (printed);
422 static void
423 gsettings_monitor (void)
425 if (global_key)
427 gchar *name;
429 name = g_strdup_printf ("changed::%s", global_key);
430 g_signal_connect (global_settings, name, G_CALLBACK (value_changed), NULL);
432 else
433 g_signal_connect (global_settings, "changed", G_CALLBACK (value_changed), NULL);
435 for (;;)
436 g_main_context_iteration (NULL, TRUE);
439 static void
440 gsettings_set (void)
442 const GVariantType *type;
443 GError *error = NULL;
444 GVariant *new;
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.
458 * Consider:
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().
472 if (new == NULL &&
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);
480 if (new == NULL)
482 gchar *context;
484 context = g_variant_parse_error_print_context (error, global_value);
485 g_printerr ("%s", context);
486 exit (1);
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);
493 exit (1);
496 if (!g_settings_set_value (global_settings, global_key, new))
498 g_printerr (_("The key is not writable\n"));
499 exit (1);
502 g_settings_sync ();
504 g_free (freeme);
507 static int
508 gsettings_help (gboolean requested,
509 const gchar *command)
511 const gchar *description;
512 const gchar *synopsis;
513 GString *string;
515 string = g_string_new (NULL);
517 if (command == 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");
529 synopsis = "";
532 else if (strcmp (command, "list-schemas") == 0)
534 description = _("List the installed (non-relocatable) schemas");
535 synopsis = "";
538 else if (strcmp (command, "list-relocatable-schemas") == 0)
540 description = _("List the installed relocatable schemas");
541 synopsis = "";
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]");
612 else
614 g_string_printf (string, _("Unknown command %s\n\n"), command);
615 requested = FALSE;
616 command = NULL;
619 if (command == NULL)
621 g_string_append (string,
622 _("Usage:\n"
623 " gsettings --version\n"
624 " gsettings [--schemadir SCHEMADIR] COMMAND [ARGS…]\n"
625 "\n"
626 "Commands:\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"
641 "\n"
642 "Use “gsettings help COMMAND” to get detailed help.\n\n"));
644 else
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");
678 if (requested)
679 g_print ("%s", string->str);
680 else
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;
695 #ifdef G_OS_WIN32
696 gchar *tmp;
697 #endif
699 setlocale (LC_ALL, "");
700 textdomain (GETTEXT_PACKAGE);
702 #ifdef G_OS_WIN32
703 tmp = _glib_get_locale_dir ();
704 bindtextdomain (GETTEXT_PACKAGE, tmp);
705 g_free (tmp);
706 #else
707 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
708 #endif
710 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
711 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
712 #endif
714 if (argc < 2)
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);
731 return 1;
734 /* shift remaining arguments (not correct wrt argv[0], but doesn't matter) */
735 argv = argv + 2;
736 argc -= 2;
738 else if (global_schema_source == NULL)
740 g_printerr (_("No schemas installed\n"));
741 return 1;
743 else
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;
802 else
803 return gsettings_help (FALSE, argv[1]);
805 if (argc > 2)
807 gchar **parts;
809 if (argv[2][0] == '\0')
811 g_printerr (_("Empty schema name given\n"));
812 return 1;
815 parts = g_strsplit (argv[2], ":", 2);
817 global_schema = g_settings_schema_source_lookup (global_schema_source, parts[0], TRUE);
819 if (need_settings)
821 if (parts[1])
823 if (!check_relocatable_schema (global_schema, parts[0]) || !check_path (parts[1]))
824 return 1;
826 global_settings = g_settings_new_full (global_schema, NULL, parts[1]);
828 else
830 if (!check_schema (global_schema, parts[0]))
831 return 1;
833 global_settings = g_settings_new_full (global_schema, NULL, NULL);
836 else
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).
844 if (parts[1])
846 if (!check_relocatable_schema (global_schema, parts[0]) || !check_path (parts[1]))
847 return 1;
849 else
851 if (global_schema == NULL)
853 g_printerr (_("No such schema “%s”\n"), parts[0]);
854 return 1;
859 g_strfreev (parts);
862 if (argc > 3)
864 if (!g_settings_schema_has_key (global_schema, argv[3]))
866 g_printerr (_("No such key “%s”\n"), argv[3]);
867 return 1;
870 global_key = argv[3];
871 global_schema_key = g_settings_schema_get_key (global_schema, global_key);
874 if (argc > 4)
875 global_value = argv[4];
877 (* function) ();
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);
885 return 0;