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,
246 enumerate (settings);
248 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 gsettings_range (GSettings *settings,
273 GVariant *range, *detail;
276 range = g_settings_get_range (settings, key);
277 g_variant_get (range, "(&sv)", &type, &detail);
279 if (strcmp (type, "type") == 0)
280 g_print ("type %s\n", g_variant_get_type_string (detail) + 1);
282 else if (strcmp (type, "range") == 0)
287 g_variant_get (detail, "(**)", &min, &max);
288 smin = g_variant_print (min, FALSE);
289 smax = g_variant_print (max, FALSE);
291 g_print ("range %s %s %s\n",
292 g_variant_get_type_string (min), smin, smax);
293 g_variant_unref (min);
294 g_variant_unref (max);
299 else if (strcmp (type, "enum") == 0 || strcmp (type, "flags") == 0)
304 g_print ("%s\n", type);
306 g_variant_iter_init (&iter, detail);
307 while (g_variant_iter_loop (&iter, "*", &item))
311 printed = g_variant_print (item, FALSE);
312 g_print ("%s\n", printed);
317 g_variant_unref (detail);
318 g_variant_unref (range);
322 gsettings_get (GSettings *settings,
329 value = g_settings_get_value (settings, key);
330 printed = g_variant_print (value, TRUE);
331 g_print ("%s\n", printed);
332 g_variant_unref (value);
337 gsettings_reset (GSettings *settings,
341 g_settings_reset (settings, key);
346 gsettings_writable (GSettings *settings,
351 g_settings_is_writable (settings, key) ?
356 value_changed (GSettings *settings,
363 value = g_settings_get_value (settings, key);
364 printed = g_variant_print (value, TRUE);
365 g_print ("%s: %s\n", key, printed);
366 g_variant_unref (value);
371 gsettings_monitor (GSettings *settings,
379 name = g_strdup_printf ("changed::%s", key);
380 g_signal_connect (settings, name, G_CALLBACK (value_changed), NULL);
383 g_signal_connect (settings, "changed", G_CALLBACK (value_changed), NULL);
385 g_main_loop_run (g_main_loop_new (NULL, FALSE));
389 gsettings_set (GSettings *settings,
393 const GVariantType *type;
394 GError *error = NULL;
397 gchar *freeme = NULL;
399 existing = g_settings_get_value (settings, key);
400 type = g_variant_get_type (existing);
402 new = g_variant_parse (type, value, NULL, NULL, &error);
404 /* A common error is to specify a string with single quotes
405 * (or use completion for that), and forget that the shell
406 * will eat one level of quoting, resulting in 'unknown keyword'
407 * error from the gvariant parser.
408 * To handle this case, try to parse again with an extra level
411 if (new == NULL && strstr (error->message, "unknown keyword"))
413 value = freeme = g_strdup_printf ("\"%s\"", value);
414 new = g_variant_parse (type, value, NULL, NULL, NULL);
416 g_clear_error (&error);
421 g_printerr ("%s\n", error->message);
425 if (!g_settings_range_check (settings, key, new))
427 g_printerr (_("The provided value is outside of the valid range\n"));
428 g_variant_unref (new);
432 g_settings_set_value (settings, key, new);
433 g_variant_unref (existing);
434 g_variant_unref (new);
442 gsettings_help (gboolean requested,
443 const gchar *command)
445 const gchar *description;
446 const gchar *synopsis;
449 string = g_string_new (NULL);
454 else if (strcmp (command, "list-schemas") == 0)
456 description = _("List the installed (non-relocatable) schemas");
460 else if (strcmp (command, "list-relocatable-schemas") == 0)
462 description = _("List the installed relocatable schemas");
466 else if (strcmp (command, "list-keys") == 0)
468 description = _("Lists the keys in SCHEMA");
469 synopsis = N_("SCHEMA[:PATH]");
472 else if (strcmp (command, "list-children") == 0)
474 description = _("Lists the children of SCHEMA");
475 synopsis = N_("SCHEMA[:PATH]");
478 else if (strcmp (command, "list-recursively") == 0)
480 description = _("List keys and values, recursively");
481 synopsis = N_("SCHEMA[:PATH]");
484 else if (strcmp (command, "get") == 0)
486 description = _("Gets the value of KEY");
487 synopsis = N_("SCHEMA[:PATH] KEY");
490 else if (strcmp (command, "range") == 0)
492 description = _("Queries the range of valid values for KEY");
493 synopsis = N_("SCHEMA[:PATH] KEY");
496 else if (strcmp (command, "set") == 0)
498 description = _("Sets the value of KEY to VALUE");
499 synopsis = N_("SCHEMA[:PATH] KEY VALUE");
502 else if (strcmp (command, "reset") == 0)
504 description = _("Resets KEY to its default value");
505 synopsis = N_("SCHEMA[:PATH] KEY");
508 else if (strcmp (command, "writable") == 0)
510 description = _("Checks if KEY is writable");
511 synopsis = N_("SCHEMA[:PATH] KEY");
514 else if (strcmp (command, "monitor") == 0)
516 description = _("Monitors KEY for changes.\n"
517 "If no KEY is specified, monitor all keys in SCHEMA.\n"
518 "Use ^C to stop monitoring.\n");
519 synopsis = N_("SCHEMA[:PATH] [KEY]");
523 g_string_printf (string, _("Unknown command %s\n\n"), command);
530 g_string_append (string,
532 " gsettings COMMAND [ARGS...]\n"
535 " help Show this information\n"
536 " list-schemas List installed schemas\n"
537 " list-relocatable-schemas List relocatable schemas\n"
538 " list-keys List keys in a schema\n"
539 " list-children List children of a schema\n"
540 " list-recursively List keys and values, recursively\n"
541 " range Queries the range of a key\n"
542 " get Get the value of a key\n"
543 " set Set the value of a key\n"
544 " reset Reset the value of a key\n"
545 " writable Check if a key is writable\n"
546 " monitor Watch for changes\n"
548 "Use 'gsettings help COMMAND' to get detailed help.\n\n"));
552 g_string_append_printf (string, _("Usage:\n gsettings %s %s\n\n%s\n\n"),
553 command, _(synopsis), description);
557 g_string_append (string, _("Arguments:\n"));
559 if (strstr (synopsis, "SCHEMA"))
560 g_string_append (string,
561 _(" SCHEMA The name of the schema\n"
562 " PATH The path, for relocatable schemas\n"));
564 if (strstr (synopsis, "[KEY]"))
565 g_string_append (string,
566 _(" KEY The (optional) key within the schema\n"));
568 else if (strstr (synopsis, "KEY"))
569 g_string_append (string,
570 _(" KEY The key within the schema\n"));
572 if (strstr (synopsis, "VALUE"))
573 g_string_append (string,
574 _(" VALUE The value to set\n"));
576 g_string_append (string, "\n");
581 g_print ("%s", string->str);
583 g_printerr ("%s", string->str);
585 g_string_free (string, TRUE);
587 return requested ? 0 : 1;
592 main (int argc, char **argv)
594 void (* function) (GSettings *, const gchar *, const gchar *);
598 setlocale (LC_ALL, "");
601 return gsettings_help (FALSE, NULL);
603 else if (strcmp (argv[1], "help") == 0)
604 return gsettings_help (TRUE, argv[2]);
606 else if (argc == 2 && strcmp (argv[1], "list-schemas") == 0)
607 function = gsettings_list_schemas;
609 else if (argc == 2 && strcmp (argv[1], "list-relocatable-schemas") == 0)
610 function = gsettings_list_relocatable_schemas;
612 else if (argc == 3 && strcmp (argv[1], "list-keys") == 0)
613 function = gsettings_list_keys;
615 else if (argc == 3 && strcmp (argv[1], "list-children") == 0)
616 function = gsettings_list_children;
618 else if (argc == 3 && strcmp (argv[1], "list-recursively") == 0)
619 function = gsettings_list_recursively;
621 else if (argc == 4 && strcmp (argv[1], "range") == 0)
622 function = gsettings_range;
624 else if (argc == 4 && strcmp (argv[1], "get") == 0)
625 function = gsettings_get;
627 else if (argc == 5 && strcmp (argv[1], "set") == 0)
628 function = gsettings_set;
630 else if (argc == 4 && strcmp (argv[1], "reset") == 0)
631 function = gsettings_reset;
633 else if (argc == 4 && strcmp (argv[1], "writable") == 0)
634 function = gsettings_writable;
636 else if ((argc == 3 || argc == 4) && strcmp (argv[1], "monitor") == 0)
637 function = gsettings_monitor;
640 return gsettings_help (FALSE, argv[1]);
648 if (argv[2][0] == '\0')
650 g_printerr (_("Empty schema name given"));
654 parts = g_strsplit (argv[2], ":", 2);
658 if (!check_relocatable_schema (parts[0]) || !check_path (parts[1]))
661 settings = g_settings_new_with_path (parts[0], parts[1]);
665 if (!check_schema (parts[0]))
668 settings = g_settings_new (parts[0]);
678 if (!check_key (settings, argv[3]))
686 (* function) (settings, key, argc > 4 ? argv[4] : NULL);
688 if (settings != NULL)
689 g_object_unref (settings);