GSeekable: document seek-past-end semantics
[glib.git] / gio / gsettings-tool.c
blob7d9c250941fdc551ed142c93710753de89d1099e
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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Author: Ryan Lortie <desrt@desrt.ca>
22 #include "config.h"
24 #include <gio/gio.h>
25 #include <gi18n.h>
26 #include <locale.h>
27 #include <string.h>
28 #include <stdlib.h>
30 #ifdef G_OS_WIN32
31 #include "glib/glib-private.h"
32 #endif
34 static gboolean
35 contained (const gchar * const *items,
36 const gchar *item)
38 while (*items)
39 if (strcmp (*items++, item) == 0)
40 return TRUE;
42 return FALSE;
45 static gboolean
46 is_relocatable_schema (GSettingsSchema *schema)
48 return g_settings_schema_get_path (schema) == NULL;
51 static gboolean
52 check_relocatable_schema (GSettingsSchema *schema,
53 const gchar *schema_id)
55 if (schema == NULL)
57 g_printerr (_("No such schema '%s'\n"), schema_id);
58 return FALSE;
61 if (!is_relocatable_schema (schema))
63 g_printerr (_("Schema '%s' is not relocatable "
64 "(path must not be specified)\n"),
65 schema_id);
66 return FALSE;
69 return TRUE;
72 static gboolean
73 check_schema (GSettingsSchema *schema,
74 const gchar *schema_id)
76 if (schema == NULL)
78 g_printerr (_("No such schema '%s'\n"), schema_id);
79 return FALSE;
82 if (is_relocatable_schema (schema))
84 g_printerr (_("Schema '%s' is relocatable "
85 "(path must be specified)\n"),
86 schema_id);
87 return FALSE;
90 return TRUE;
93 static gboolean
94 check_path (const gchar *path)
96 if (path[0] == '\0')
98 g_printerr (_("Empty path given.\n"));
99 return FALSE;
102 if (path[0] != '/')
104 g_printerr (_("Path must begin with a slash (/)\n"));
105 return FALSE;
108 if (!g_str_has_suffix (path, "/"))
110 g_printerr (_("Path must end with a slash (/)\n"));
111 return FALSE;
114 if (strstr (path, "//"))
116 g_printerr (_("Path must not contain two adjacent slashes (//)\n"));
117 return FALSE;
120 return TRUE;
123 static gboolean
124 check_key (GSettings *settings,
125 const gchar *key)
127 gboolean good;
128 gchar **keys;
130 keys = g_settings_list_keys (settings);
131 good = contained ((const gchar **) keys, key);
132 g_strfreev (keys);
134 if (good)
135 return TRUE;
137 g_printerr (_("No such key '%s'\n"), key);
139 return FALSE;
142 static void
143 output_list (const gchar * const *list)
145 gint i;
147 for (i = 0; list[i]; i++)
148 g_print ("%s\n", list[i]);
151 static void
152 gsettings_print_version (GSettings *settings,
153 const gchar *key,
154 const gchar *value)
156 g_print ("%d.%d.%d\n", glib_major_version, glib_minor_version,
157 glib_micro_version);
160 static void
161 gsettings_list_schemas (GSettings *settings,
162 const gchar *key,
163 const gchar *value)
165 output_list (g_settings_list_schemas ());
168 static void
169 gsettings_list_relocatable_schemas (GSettings *settings,
170 const gchar *key,
171 const gchar *value)
173 output_list (g_settings_list_relocatable_schemas ());
176 static void
177 gsettings_list_keys (GSettings *settings,
178 const gchar *key,
179 const gchar *value)
181 gchar **keys;
183 keys = g_settings_list_keys (settings);
184 output_list ((const gchar **) keys);
185 g_strfreev (keys);
188 static void
189 gsettings_list_children (GSettings *settings,
190 const gchar *key,
191 const gchar *value)
193 gchar **children;
194 gint max = 0;
195 gint i;
197 children = g_settings_list_children (settings);
198 for (i = 0; children[i]; i++)
199 if (strlen (children[i]) > max)
200 max = strlen (children[i]);
202 for (i = 0; children[i]; i++)
204 GSettings *child;
205 GSettingsSchema *schema;
206 gchar *path;
208 child = g_settings_get_child (settings, children[i]);
209 g_object_get (child,
210 "settings-schema", &schema,
211 "path", &path,
212 NULL);
214 if (g_settings_schema_get_path (schema) != NULL)
215 g_print ("%-*s %s\n", max, children[i], g_settings_schema_get_id (schema));
216 else
217 g_print ("%-*s %s:%s\n", max, children[i], g_settings_schema_get_id (schema), path);
219 g_object_unref (child);
220 g_settings_schema_unref (schema);
221 g_free (path);
224 g_strfreev (children);
227 static void
228 enumerate (GSettings *settings)
230 gchar **keys;
231 gchar *schema;
232 gint i;
234 g_object_get (settings, "schema-id", &schema, NULL);
236 keys = g_settings_list_keys (settings);
237 for (i = 0; keys[i]; i++)
239 GVariant *value;
240 gchar *printed;
242 value = g_settings_get_value (settings, keys[i]);
243 printed = g_variant_print (value, TRUE);
244 g_print ("%s %s %s\n", schema, keys[i], printed);
245 g_variant_unref (value);
246 g_free (printed);
249 g_free (schema);
250 g_strfreev (keys);
253 static void
254 gsettings_list_recursively (GSettings *settings,
255 const gchar *key,
256 const gchar *value)
258 if (settings)
260 gchar **children;
261 gint i;
263 enumerate (settings);
264 children = g_settings_list_children (settings);
265 for (i = 0; children[i]; i++)
267 GSettings *child;
269 child = g_settings_get_child (settings, children[i]);
270 gsettings_list_recursively (child, NULL, NULL);
271 g_object_unref (child);
274 g_strfreev (children);
276 else
278 const gchar * const *schemas;
279 gint i;
281 schemas = g_settings_list_schemas ();
283 for (i = 0; schemas[i]; i++)
285 settings = g_settings_new (schemas[i]);
286 gsettings_list_recursively (settings, NULL, NULL);
287 g_object_unref (settings);
292 static void
293 gsettings_range (GSettings *settings,
294 const gchar *key,
295 const gchar *value)
297 GVariant *range, *detail;
298 const gchar *type;
300 range = g_settings_get_range (settings, key);
301 g_variant_get (range, "(&sv)", &type, &detail);
303 if (strcmp (type, "type") == 0)
304 g_print ("type %s\n", g_variant_get_type_string (detail) + 1);
306 else if (strcmp (type, "range") == 0)
308 GVariant *min, *max;
309 gchar *smin, *smax;
311 g_variant_get (detail, "(**)", &min, &max);
312 smin = g_variant_print (min, FALSE);
313 smax = g_variant_print (max, FALSE);
315 g_print ("range %s %s %s\n",
316 g_variant_get_type_string (min), smin, smax);
317 g_variant_unref (min);
318 g_variant_unref (max);
319 g_free (smin);
320 g_free (smax);
323 else if (strcmp (type, "enum") == 0 || strcmp (type, "flags") == 0)
325 GVariantIter iter;
326 GVariant *item;
328 g_print ("%s\n", type);
330 g_variant_iter_init (&iter, detail);
331 while (g_variant_iter_loop (&iter, "*", &item))
333 gchar *printed;
335 printed = g_variant_print (item, FALSE);
336 g_print ("%s\n", printed);
337 g_free (printed);
341 g_variant_unref (detail);
342 g_variant_unref (range);
345 static void
346 gsettings_get (GSettings *settings,
347 const gchar *key,
348 const gchar *value_)
350 GVariant *value;
351 gchar *printed;
353 value = g_settings_get_value (settings, key);
354 printed = g_variant_print (value, TRUE);
355 g_print ("%s\n", printed);
356 g_variant_unref (value);
357 g_free (printed);
360 static void
361 gsettings_reset (GSettings *settings,
362 const gchar *key,
363 const gchar *value)
365 g_settings_reset (settings, key);
366 g_settings_sync ();
369 static void
370 reset_all_keys (GSettings *settings)
372 gchar **keys;
373 gint i;
375 keys = g_settings_list_keys (settings);
376 for (i = 0; keys[i]; i++)
378 g_settings_reset (settings, keys[i]);
381 g_strfreev (keys);
384 static void
385 gsettings_reset_recursively (GSettings *settings,
386 const gchar *key,
387 const gchar *value)
389 gchar **children;
390 gint i;
392 g_settings_delay (settings);
394 reset_all_keys (settings);
395 children = g_settings_list_children (settings);
396 for (i = 0; children[i]; i++)
398 GSettings *child;
399 child = g_settings_get_child (settings, children[i]);
401 reset_all_keys (child);
403 g_object_unref (child);
406 g_strfreev (children);
408 g_settings_apply (settings);
409 g_settings_sync ();
412 static void
413 gsettings_writable (GSettings *settings,
414 const gchar *key,
415 const gchar *value)
417 g_print ("%s\n",
418 g_settings_is_writable (settings, key) ?
419 "true" : "false");
422 static void
423 value_changed (GSettings *settings,
424 const gchar *key,
425 gpointer user_data)
427 GVariant *value;
428 gchar *printed;
430 value = g_settings_get_value (settings, key);
431 printed = g_variant_print (value, TRUE);
432 g_print ("%s: %s\n", key, printed);
433 g_variant_unref (value);
434 g_free (printed);
437 static void
438 gsettings_monitor (GSettings *settings,
439 const gchar *key,
440 const gchar *value)
442 if (key)
444 gchar *name;
446 name = g_strdup_printf ("changed::%s", key);
447 g_signal_connect (settings, name, G_CALLBACK (value_changed), NULL);
449 else
450 g_signal_connect (settings, "changed", G_CALLBACK (value_changed), NULL);
452 g_main_loop_run (g_main_loop_new (NULL, FALSE));
455 static void
456 gsettings_set (GSettings *settings,
457 const gchar *key,
458 const gchar *value)
460 const GVariantType *type;
461 GError *error = NULL;
462 GVariant *existing;
463 GVariant *new;
464 gchar *freeme = NULL;
466 existing = g_settings_get_value (settings, key);
467 type = g_variant_get_type (existing);
469 new = g_variant_parse (type, value, NULL, NULL, &error);
471 /* If that didn't work and the type is string then we should assume
472 * that the user is just trying to set a string directly and forgot
473 * the quotes (or had them consumed by the shell).
475 * If the user started with a quote then we assume that some deeper
476 * problem is at play and we want the failure in that case.
478 * Consider:
480 * gsettings set x.y.z key "'i don't expect this to work'"
482 * Note that we should not just add quotes and try parsing again, but
483 * rather assume that the user is providing us with a bare string.
484 * Assume we added single quotes, then consider this case:
486 * gsettings set x.y.z key "i'd expect this to work"
488 * A similar example could be given for double quotes.
490 * Avoid that whole mess by just using g_variant_new_string().
492 if (new == NULL &&
493 g_variant_type_equal (type, G_VARIANT_TYPE_STRING) &&
494 value[0] != '\'' && value[0] != '"')
496 g_clear_error (&error);
497 new = g_variant_new_string (value);
500 /* we're done with 'type' now, so we can free 'existing' */
501 g_variant_unref (existing);
503 if (new == NULL)
505 g_printerr ("%s\n", error->message);
506 exit (1);
509 if (!g_settings_range_check (settings, key, new))
511 g_printerr (_("The provided value is outside of the valid range\n"));
512 g_variant_unref (new);
513 exit (1);
516 if (!g_settings_set_value (settings, key, new))
518 g_printerr (_("The key is not writable\n"));
519 exit (1);
522 g_settings_sync ();
524 g_free (freeme);
527 static int
528 gsettings_help (gboolean requested,
529 const gchar *command)
531 const gchar *description;
532 const gchar *synopsis;
533 GString *string;
535 string = g_string_new (NULL);
537 if (command == NULL)
540 else if (strcmp (command, "help") == 0)
542 description = _("Print help");
543 synopsis = "[COMMAND]";
546 else if (strcmp (command, "--version") == 0)
548 description = _("Print version information and exit");
549 synopsis = "";
552 else if (strcmp (command, "list-schemas") == 0)
554 description = _("List the installed (non-relocatable) schemas");
555 synopsis = "";
558 else if (strcmp (command, "list-relocatable-schemas") == 0)
560 description = _("List the installed relocatable schemas");
561 synopsis = "";
564 else if (strcmp (command, "list-keys") == 0)
566 description = _("List the keys in SCHEMA");
567 synopsis = N_("SCHEMA[:PATH]");
570 else if (strcmp (command, "list-children") == 0)
572 description = _("List the children of SCHEMA");
573 synopsis = N_("SCHEMA[:PATH]");
576 else if (strcmp (command, "list-recursively") == 0)
578 description = _("List keys and values, recursively\n"
579 "If no SCHEMA is given, list all keys\n");
580 synopsis = N_("[SCHEMA[:PATH]]");
583 else if (strcmp (command, "get") == 0)
585 description = _("Get the value of KEY");
586 synopsis = N_("SCHEMA[:PATH] KEY");
589 else if (strcmp (command, "range") == 0)
591 description = _("Query the range of valid values for KEY");
592 synopsis = N_("SCHEMA[:PATH] KEY");
595 else if (strcmp (command, "set") == 0)
597 description = _("Set the value of KEY to VALUE");
598 synopsis = N_("SCHEMA[:PATH] KEY VALUE");
601 else if (strcmp (command, "reset") == 0)
603 description = _("Reset KEY to its default value");
604 synopsis = N_("SCHEMA[:PATH] KEY");
607 else if (strcmp (command, "reset-recursively") == 0)
609 description = _("Reset all keys in SCHEMA to their defaults");
610 synopsis = N_("SCHEMA[:PATH]");
613 else if (strcmp (command, "writable") == 0)
615 description = _("Check if KEY is writable");
616 synopsis = N_("SCHEMA[:PATH] KEY");
619 else if (strcmp (command, "monitor") == 0)
621 description = _("Monitor KEY for changes.\n"
622 "If no KEY is specified, monitor all keys in SCHEMA.\n"
623 "Use ^C to stop monitoring.\n");
624 synopsis = N_("SCHEMA[:PATH] [KEY]");
626 else
628 g_string_printf (string, _("Unknown command %s\n\n"), command);
629 requested = FALSE;
630 command = NULL;
633 if (command == NULL)
635 g_string_append (string,
636 _("Usage:\n"
637 " gsettings [--schemadir SCHEMADIR] COMMAND [ARGS...]\n"
638 "\n"
639 "Commands:\n"
640 " help Show this information\n"
641 " list-schemas List installed schemas\n"
642 " list-relocatable-schemas List relocatable schemas\n"
643 " list-keys List keys in a schema\n"
644 " list-children List children of a schema\n"
645 " list-recursively List keys and values, recursively\n"
646 " range Queries the range of a key\n"
647 " get Get the value of a key\n"
648 " set Set the value of a key\n"
649 " reset Reset the value of a key\n"
650 " reset-recursively Reset all values in a given schema\n"
651 " writable Check if a key is writable\n"
652 " monitor Watch for changes\n"
653 "\n"
654 "Use 'gsettings help COMMAND' to get detailed help.\n\n"));
656 else
658 g_string_append_printf (string, _("Usage:\n gsettings [--schemadir SCHEMADIR] %s %s\n\n%s\n\n"),
659 command, synopsis[0] ? _(synopsis) : "", description);
661 g_string_append (string, _("Arguments:\n"));
663 g_string_append (string,
664 _(" SCHEMADIR A directory to search for additional schemas\n"));
666 if (strstr (synopsis, "[COMMAND]"))
667 g_string_append (string,
668 _(" COMMAND The (optional) command to explain\n"));
670 else if (strstr (synopsis, "SCHEMA"))
671 g_string_append (string,
672 _(" SCHEMA The name of the schema\n"
673 " PATH The path, for relocatable schemas\n"));
675 if (strstr (synopsis, "[KEY]"))
676 g_string_append (string,
677 _(" KEY The (optional) key within the schema\n"));
679 else if (strstr (synopsis, "KEY"))
680 g_string_append (string,
681 _(" KEY The key within the schema\n"));
683 if (strstr (synopsis, "VALUE"))
684 g_string_append (string,
685 _(" VALUE The value to set\n"));
687 g_string_append (string, "\n");
690 if (requested)
691 g_print ("%s", string->str);
692 else
693 g_printerr ("%s\n", string->str);
695 g_string_free (string, TRUE);
697 return requested ? 0 : 1;
702 main (int argc, char **argv)
704 void (* function) (GSettings *, const gchar *, const gchar *);
705 GSettingsSchemaSource *schema_source;
706 GSettingsSchema *schema;
707 GSettings *settings;
708 const gchar *key;
710 #ifdef G_OS_WIN32
711 gchar *tmp;
712 #endif
714 setlocale (LC_ALL, "");
715 textdomain (GETTEXT_PACKAGE);
717 #ifdef G_OS_WIN32
718 tmp = _glib_get_locale_dir ();
719 bindtextdomain (GETTEXT_PACKAGE, tmp);
720 g_free (tmp);
721 #else
722 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
723 #endif
725 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
726 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
727 #endif
729 if (argc < 2)
730 return gsettings_help (FALSE, NULL);
732 schema_source = g_settings_schema_source_ref (g_settings_schema_source_get_default ());
734 if (argc > 3 && g_str_equal (argv[1], "--schemadir"))
736 GSettingsSchemaSource *parent = schema_source;
737 GError *error = NULL;
739 schema_source = g_settings_schema_source_new_from_directory (argv[2], parent, FALSE, &error);
740 g_settings_schema_source_unref (parent);
742 if (schema_source == NULL)
744 g_printerr (_("Could not load schemas from %s: %s\n"), argv[2], error->message);
745 g_clear_error (&error);
747 return 1;
750 /* shift remaining arguments (not correct wrt argv[0], but doesn't matter) */
751 argv = argv + 2;
752 argc -= 2;
755 if (strcmp (argv[1], "help") == 0)
756 return gsettings_help (TRUE, argv[2]);
758 else if (argc == 2 && strcmp (argv[1], "--version") == 0)
759 function = gsettings_print_version;
761 else if (argc == 2 && strcmp (argv[1], "list-schemas") == 0)
762 function = gsettings_list_schemas;
764 else if (argc == 2 && strcmp (argv[1], "list-relocatable-schemas") == 0)
765 function = gsettings_list_relocatable_schemas;
767 else if (argc == 3 && strcmp (argv[1], "list-keys") == 0)
768 function = gsettings_list_keys;
770 else if (argc == 3 && strcmp (argv[1], "list-children") == 0)
771 function = gsettings_list_children;
773 else if ((argc == 2 || argc == 3) && strcmp (argv[1], "list-recursively") == 0)
774 function = gsettings_list_recursively;
776 else if (argc == 4 && strcmp (argv[1], "range") == 0)
777 function = gsettings_range;
779 else if (argc == 4 && strcmp (argv[1], "get") == 0)
780 function = gsettings_get;
782 else if (argc == 5 && strcmp (argv[1], "set") == 0)
783 function = gsettings_set;
785 else if (argc == 4 && strcmp (argv[1], "reset") == 0)
786 function = gsettings_reset;
788 else if (argc == 3 && strcmp (argv[1], "reset-recursively") == 0)
789 function = gsettings_reset_recursively;
791 else if (argc == 4 && strcmp (argv[1], "writable") == 0)
792 function = gsettings_writable;
794 else if ((argc == 3 || argc == 4) && strcmp (argv[1], "monitor") == 0)
795 function = gsettings_monitor;
797 else
798 return gsettings_help (FALSE, argv[1]);
800 if (argc > 2)
802 gchar **parts;
804 if (argv[2][0] == '\0')
806 g_printerr (_("Empty schema name given\n"));
807 return 1;
810 parts = g_strsplit (argv[2], ":", 2);
812 schema = g_settings_schema_source_lookup (schema_source, parts[0], TRUE);
813 if (parts[1])
815 if (!check_relocatable_schema (schema, parts[0]) || !check_path (parts[1]))
816 return 1;
818 settings = g_settings_new_full (schema, NULL, parts[1]);
820 else
822 if (!check_schema (schema, parts[0]))
823 return 1;
825 settings = g_settings_new_full (schema, NULL, NULL);
828 g_strfreev (parts);
830 else
832 settings = NULL;
833 schema = NULL;
836 if (argc > 3)
838 if (!check_key (settings, argv[3]))
839 return 1;
841 key = argv[3];
843 else
844 key = NULL;
846 (* function) (settings, key, argc > 4 ? argv[4] : NULL);
848 if (settings != NULL)
849 g_object_unref (settings);
850 if (schema != NULL)
851 g_settings_schema_unref (schema);
853 g_settings_schema_source_unref (schema_source);
855 return 0;