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>
31 contained (const gchar * const *items,
35 if (strcmp (*items++, item) == 0)
42 is_schema (const gchar *schema)
44 return contained (g_settings_list_schemas (), schema);
48 is_relocatable_schema (const gchar *schema)
50 return contained (g_settings_list_relocatable_schemas (), schema);
54 check_relocatable_schema (const gchar *schema)
56 if (is_relocatable_schema (schema))
59 if (is_schema (schema))
60 g_printerr (_("Schema '%s' is not relocatable "
61 "(path must not be specified)\n"),
65 g_printerr (_("No such schema '%s'\n"), schema);
71 check_schema (const gchar *schema)
73 if (is_schema (schema))
76 if (is_relocatable_schema (schema))
77 g_printerr (_("Schema '%s' is relocatable "
78 "(path must be specified)\n"),
82 g_printerr (_("No such schema '%s'\n"), schema);
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 check_key (GSettings *settings,
124 keys = g_settings_list_keys (settings);
125 good = contained ((const gchar **) keys, key);
131 g_printerr (_("No such key '%s'\n"), key);
137 output_list (const gchar * const *list)
141 for (i = 0; list[i]; i++)
142 g_print ("%s\n", list[i]);
146 gsettings_list_schemas (GSettings *settings,
150 output_list (g_settings_list_schemas ());
154 gsettings_list_relocatable_schemas (GSettings *settings,
158 output_list (g_settings_list_relocatable_schemas ());
162 gsettings_list_keys (GSettings *settings,
168 keys = g_settings_list_keys (settings);
169 output_list ((const gchar **) keys);
174 gsettings_list_children (GSettings *settings,
182 children = g_settings_list_children (settings);
183 for (i = 0; children[i]; i++)
184 if (strlen (children[i]) > max)
185 max = strlen (children[i]);
187 for (i = 0; children[i]; i++)
193 child = g_settings_get_child (settings, children[i]);
199 if (is_schema (schema))
200 g_print ("%-*s %s\n", max, children[i], schema);
202 g_print ("%-*s %s:%s\n", max, children[i], schema, path);
204 g_object_unref (child);
209 g_strfreev (children);
213 enumerate (GSettings *settings)
219 g_object_get (settings, "schema", &schema, NULL);
221 keys = g_settings_list_keys (settings);
222 for (i = 0; keys[i]; i++)
227 value = g_settings_get_value (settings, keys[i]);
228 printed = g_variant_print (value, TRUE);
229 g_print ("%s %s %s\n", schema, keys[i], printed);
230 g_variant_unref (value);
239 gsettings_list_recursively (GSettings *settings,
248 enumerate (settings);
249 children = g_settings_list_children (settings);
250 for (i = 0; children[i]; i++)
255 child = g_settings_get_child (settings, children[i]);
256 g_object_get (child, "schema", &schema, NULL);
258 if (is_schema (schema))
261 g_object_unref (child);
265 g_strfreev (children);
269 const gchar * const *schemas;
272 schemas = g_settings_list_schemas ();
274 for (i = 0; schemas[i]; i++)
276 settings = g_settings_new (schemas[i]);
277 enumerate (settings);
278 g_object_unref (settings);
284 gsettings_range (GSettings *settings,
288 GVariant *range, *detail;
291 range = g_settings_get_range (settings, key);
292 g_variant_get (range, "(&sv)", &type, &detail);
294 if (strcmp (type, "type") == 0)
295 g_print ("type %s\n", g_variant_get_type_string (detail) + 1);
297 else if (strcmp (type, "range") == 0)
302 g_variant_get (detail, "(**)", &min, &max);
303 smin = g_variant_print (min, FALSE);
304 smax = g_variant_print (max, FALSE);
306 g_print ("range %s %s %s\n",
307 g_variant_get_type_string (min), smin, smax);
308 g_variant_unref (min);
309 g_variant_unref (max);
314 else if (strcmp (type, "enum") == 0 || strcmp (type, "flags") == 0)
319 g_print ("%s\n", type);
321 g_variant_iter_init (&iter, detail);
322 while (g_variant_iter_loop (&iter, "*", &item))
326 printed = g_variant_print (item, FALSE);
327 g_print ("%s\n", printed);
332 g_variant_unref (detail);
333 g_variant_unref (range);
337 gsettings_get (GSettings *settings,
344 value = g_settings_get_value (settings, key);
345 printed = g_variant_print (value, TRUE);
346 g_print ("%s\n", printed);
347 g_variant_unref (value);
352 gsettings_reset (GSettings *settings,
356 g_settings_reset (settings, key);
361 gsettings_writable (GSettings *settings,
366 g_settings_is_writable (settings, key) ?
371 value_changed (GSettings *settings,
378 value = g_settings_get_value (settings, key);
379 printed = g_variant_print (value, TRUE);
380 g_print ("%s: %s\n", key, printed);
381 g_variant_unref (value);
386 gsettings_monitor (GSettings *settings,
394 name = g_strdup_printf ("changed::%s", key);
395 g_signal_connect (settings, name, G_CALLBACK (value_changed), NULL);
398 g_signal_connect (settings, "changed", G_CALLBACK (value_changed), NULL);
400 g_main_loop_run (g_main_loop_new (NULL, FALSE));
404 gsettings_set (GSettings *settings,
408 const GVariantType *type;
409 GError *error = NULL;
413 gchar *freeme = NULL;
415 existing = g_settings_get_value (settings, key);
416 type = g_variant_get_type (existing);
418 new = g_variant_parse (type, value, NULL, NULL, &error);
420 /* A common error is to specify a string with single quotes
421 * (or use completion for that), and forget that the shell
422 * will eat one level of quoting, resulting in 'unknown keyword'
423 * error from the gvariant parser.
424 * To handle this case, try to parse again with an extra level
428 g_error_matches (error, G_VARIANT_PARSE_ERROR,
429 G_VARIANT_PARSE_ERROR_UNKNOWN_KEYWORD))
431 value = freeme = g_strdup_printf ("\"%s\"", value);
432 new = g_variant_parse (type, value, NULL, NULL, NULL);
434 g_clear_error (&error);
439 g_printerr ("%s\n", error->message);
443 if (!g_settings_range_check (settings, key, new))
445 g_printerr (_("The provided value is outside of the valid range\n"));
449 g_settings_set_value (settings, key, new);
452 stored = g_settings_get_value (settings, key);
453 if (g_variant_equal (stored, existing))
455 g_printerr (_("Failed to set value\n"));
459 g_variant_unref (stored);
460 g_variant_unref (existing);
461 g_variant_unref (new);
467 gsettings_help (gboolean requested,
468 const gchar *command)
470 const gchar *description;
471 const gchar *synopsis;
474 string = g_string_new (NULL);
479 else if (strcmp (command, "help") == 0)
481 description = _("Print help");
482 synopsis = "[COMMAND]";
485 else if (strcmp (command, "list-schemas") == 0)
487 description = _("List the installed (non-relocatable) schemas");
491 else if (strcmp (command, "list-relocatable-schemas") == 0)
493 description = _("List the installed relocatable schemas");
497 else if (strcmp (command, "list-keys") == 0)
499 description = _("List the keys in SCHEMA");
500 synopsis = N_("SCHEMA[:PATH]");
503 else if (strcmp (command, "list-children") == 0)
505 description = _("List the children of SCHEMA");
506 synopsis = N_("SCHEMA[:PATH]");
509 else if (strcmp (command, "list-recursively") == 0)
511 description = _("List keys and values, recursively\n"
512 "If no SCHEMA is given, list all keys\n");
513 synopsis = N_("[SCHEMA[:PATH]]");
516 else if (strcmp (command, "get") == 0)
518 description = _("Get the value of KEY");
519 synopsis = N_("SCHEMA[:PATH] KEY");
522 else if (strcmp (command, "range") == 0)
524 description = _("Query the range of valid values for KEY");
525 synopsis = N_("SCHEMA[:PATH] KEY");
528 else if (strcmp (command, "set") == 0)
530 description = _("Set the value of KEY to VALUE");
531 synopsis = N_("SCHEMA[:PATH] KEY VALUE");
534 else if (strcmp (command, "reset") == 0)
536 description = _("Reset KEY to its default value");
537 synopsis = N_("SCHEMA[:PATH] KEY");
540 else if (strcmp (command, "writable") == 0)
542 description = _("Check if KEY is writable");
543 synopsis = N_("SCHEMA[:PATH] KEY");
546 else if (strcmp (command, "monitor") == 0)
548 description = _("Monitor KEY for changes.\n"
549 "If no KEY is specified, monitor all keys in SCHEMA.\n"
550 "Use ^C to stop monitoring.\n");
551 synopsis = N_("SCHEMA[:PATH] [KEY]");
555 g_string_printf (string, _("Unknown command %s\n\n"), command);
562 g_string_append (string,
564 " gsettings COMMAND [ARGS...]\n"
567 " help Show this information\n"
568 " list-schemas List installed schemas\n"
569 " list-relocatable-schemas List relocatable schemas\n"
570 " list-keys List keys in a schema\n"
571 " list-children List children of a schema\n"
572 " list-recursively List keys and values, recursively\n"
573 " range Queries the range of a key\n"
574 " get Get the value of a key\n"
575 " set Set the value of a key\n"
576 " reset Reset the value of a key\n"
577 " writable Check if a key is writable\n"
578 " monitor Watch for changes\n"
580 "Use 'gsettings help COMMAND' to get detailed help.\n\n"));
584 g_string_append_printf (string, _("Usage:\n gsettings %s %s\n\n%s\n\n"),
585 command, synopsis[0] ? _(synopsis) : "", description);
589 g_string_append (string, _("Arguments:\n"));
591 if (strstr (synopsis, "[COMMAND]"))
592 g_string_append (string,
593 _(" COMMAND The (optional) command to explain\n"));
595 else if (strstr (synopsis, "SCHEMA"))
596 g_string_append (string,
597 _(" SCHEMA The name of the schema\n"
598 " PATH The path, for relocatable schemas\n"));
600 if (strstr (synopsis, "[KEY]"))
601 g_string_append (string,
602 _(" KEY The (optional) key within the schema\n"));
604 else if (strstr (synopsis, "KEY"))
605 g_string_append (string,
606 _(" KEY The key within the schema\n"));
608 if (strstr (synopsis, "VALUE"))
609 g_string_append (string,
610 _(" VALUE The value to set\n"));
612 g_string_append (string, "\n");
617 g_print ("%s", string->str);
619 g_printerr ("%s\n", string->str);
621 g_string_free (string, TRUE);
623 return requested ? 0 : 1;
628 main (int argc, char **argv)
630 void (* function) (GSettings *, const gchar *, const gchar *);
634 setlocale (LC_ALL, "");
635 textdomain (GETTEXT_PACKAGE);
638 gchar *tmp = _glib_get_locale_dir ();
639 bindtextdomain (GETTEXT_PACKAGE, tmp);
642 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
645 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
646 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
650 return gsettings_help (FALSE, NULL);
652 else if (strcmp (argv[1], "help") == 0)
653 return gsettings_help (TRUE, argv[2]);
655 else if (argc == 2 && strcmp (argv[1], "list-schemas") == 0)
656 function = gsettings_list_schemas;
658 else if (argc == 2 && strcmp (argv[1], "list-relocatable-schemas") == 0)
659 function = gsettings_list_relocatable_schemas;
661 else if (argc == 3 && strcmp (argv[1], "list-keys") == 0)
662 function = gsettings_list_keys;
664 else if (argc == 3 && strcmp (argv[1], "list-children") == 0)
665 function = gsettings_list_children;
667 else if ((argc == 2 || argc == 3) && strcmp (argv[1], "list-recursively") == 0)
668 function = gsettings_list_recursively;
670 else if (argc == 4 && strcmp (argv[1], "range") == 0)
671 function = gsettings_range;
673 else if (argc == 4 && strcmp (argv[1], "get") == 0)
674 function = gsettings_get;
676 else if (argc == 5 && strcmp (argv[1], "set") == 0)
677 function = gsettings_set;
679 else if (argc == 4 && strcmp (argv[1], "reset") == 0)
680 function = gsettings_reset;
682 else if (argc == 4 && strcmp (argv[1], "writable") == 0)
683 function = gsettings_writable;
685 else if ((argc == 3 || argc == 4) && strcmp (argv[1], "monitor") == 0)
686 function = gsettings_monitor;
689 return gsettings_help (FALSE, argv[1]);
697 if (argv[2][0] == '\0')
699 g_printerr (_("Empty schema name given\n"));
703 parts = g_strsplit (argv[2], ":", 2);
707 if (!check_relocatable_schema (parts[0]) || !check_path (parts[1]))
710 settings = g_settings_new_with_path (parts[0], parts[1]);
714 if (!check_schema (parts[0]))
717 settings = g_settings_new (parts[0]);
727 if (!check_key (settings, argv[3]))
735 (* function) (settings, key, argc > 4 ? argv[4] : NULL);
737 if (settings != NULL)
738 g_object_unref (settings);