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 gsettings_range (GSettings *settings,
217 GVariant *range, *detail;
220 range = g_settings_get_range (settings, key);
221 g_variant_get (range, "(&sv)", &type, &detail);
223 if (strcmp (type, "type") == 0)
224 g_print ("type %s\n", g_variant_get_type_string (detail) + 1);
226 else if (strcmp (type, "range") == 0)
231 g_variant_get (detail, "(**)", &min, &max);
232 smin = g_variant_print (min, FALSE);
233 smax = g_variant_print (max, FALSE);
235 g_print ("range %s %s %s\n",
236 g_variant_get_type_string (min), smin, smax);
237 g_variant_unref (min);
238 g_variant_unref (max);
243 else if (strcmp (type, "enum") == 0 || strcmp (type, "flags") == 0)
248 g_print ("%s\n", type);
250 g_variant_iter_init (&iter, detail);
251 while (g_variant_iter_loop (&iter, "*", &item))
255 printed = g_variant_print (item, FALSE);
256 g_print ("%s\n", printed);
261 g_variant_unref (detail);
262 g_variant_unref (range);
266 gsettings_get (GSettings *settings,
273 value = g_settings_get_value (settings, key);
274 printed = g_variant_print (value, TRUE);
275 g_print ("%s\n", printed);
276 g_variant_unref (value);
281 gsettings_reset (GSettings *settings,
285 g_settings_reset (settings, key);
290 gsettings_writable (GSettings *settings,
295 g_settings_is_writable (settings, key) ?
300 value_changed (GSettings *settings,
307 value = g_settings_get_value (settings, key);
308 printed = g_variant_print (value, TRUE);
309 g_print ("%s: %s\n", key, printed);
310 g_variant_unref (value);
315 gsettings_monitor (GSettings *settings,
323 name = g_strdup_printf ("changed::%s", key);
324 g_signal_connect (settings, name, G_CALLBACK (value_changed), NULL);
327 g_signal_connect (settings, "changed", G_CALLBACK (value_changed), NULL);
329 g_main_loop_run (g_main_loop_new (NULL, FALSE));
333 gsettings_set (GSettings *settings,
337 const GVariantType *type;
338 GError *error = NULL;
342 existing = g_settings_get_value (settings, key);
343 type = g_variant_get_type (existing);
345 new = g_variant_parse (type, value, NULL, NULL, &error);
349 g_printerr ("%s\n", error->message);
353 if (!g_settings_range_check (settings, key, new))
355 g_printerr (_("The provided value is outside of the valid range\n"));
356 g_variant_unref (new);
360 g_settings_set_value (settings, key, new);
361 g_variant_unref (existing);
362 g_variant_unref (new);
368 gsettings_help (gboolean requested,
369 const gchar *command)
371 const gchar *description;
372 const gchar *synopsis;
375 string = g_string_new (NULL);
380 else if (strcmp (command, "list-schemas") == 0)
382 description = _("List the installed (non-relocatable) schemas");
386 else if (strcmp (command, "list-relocatable-schemas") == 0)
388 description = _("List the installed relocatable schemas");
392 else if (strcmp (command, "list-keys") == 0)
394 description = _("Lists the keys in SCHEMA");
395 synopsis = N_("SCHEMA[:PATH]");
398 else if (strcmp (command, "list-children") == 0)
400 description = _("Lists the children of SCHEMA");
401 synopsis = N_("SCHEMA[:PATH]");
404 else if (strcmp (command, "get") == 0)
406 description = _("Gets the value of KEY");
407 synopsis = N_("SCHEMA[:PATH] KEY");
410 else if (strcmp (command, "range") == 0)
412 description = _("Queries the range of valid values for KEY");
413 synopsis = N_("SCHEMA[:PATH] KEY");
416 else if (strcmp (command, "set") == 0)
418 description = _("Sets the value of KEY to VALUE");
419 synopsis = N_("SCHEMA[:PATH] KEY VALUE");
422 else if (strcmp (command, "reset") == 0)
424 description = _("Resets KEY to its default value");
425 synopsis = N_("SCHEMA[:PATH] KEY");
428 else if (strcmp (command, "writable") == 0)
430 description = _("Checks if KEY is writable");
431 synopsis = N_("SCHEMA[:PATH] KEY");
434 else if (strcmp (command, "monitor") == 0)
436 description = _("Monitors KEY for changes.\n"
437 "If no KEY is specified, monitor all keys in SCHEMA.\n"
438 "Use ^C to stop monitoring.\n");
439 synopsis = N_("SCHEMA[:PATH] [KEY]");
443 g_string_printf (string, _("Unknown command %s\n\n"), command);
450 g_string_append (string,
452 " gsettings COMMAND [ARGS...]\n"
455 " help Show this information\n"
456 " list-schemas List installed schemas\n"
457 " list-relocatable-schemas List relocatable schemas\n"
458 " list-keys List keys in a schema\n"
459 " list-children List children of a schema\n"
460 " range Queries the range of a key\n"
461 " get Get the value of a key\n"
462 " set Set the value of a key\n"
463 " reset Reset the value of a key\n"
464 " writable Check if a key is writable\n"
465 " monitor Watch for changes\n"
467 "Use 'gsettings help COMMAND' to get detailed help.\n\n"));
471 g_string_append_printf (string, _("Usage:\n gsettings %s %s\n\n%s\n\n"),
472 command, _(synopsis), description);
476 g_string_append (string, _("Arguments:\n"));
478 if (strstr (synopsis, "SCHEMA"))
479 g_string_append (string,
480 _(" SCHEMA The name of the schema\n"
481 " PATH The path, for relocatable schemas\n"));
483 if (strstr (synopsis, "[KEY]"))
484 g_string_append (string,
485 _(" KEY The (optional) key within the schema\n"));
487 else if (strstr (synopsis, "KEY"))
488 g_string_append (string,
489 _(" KEY The key within the schema\n"));
491 if (strstr (synopsis, "VALUE"))
492 g_string_append (string,
493 _(" VALUE The value to set\n"));
495 g_string_append (string, "\n");
500 g_print ("%s", string->str);
502 g_printerr ("%s", string->str);
504 g_string_free (string, TRUE);
506 return requested ? 0 : 1;
511 main (int argc, char **argv)
513 void (* function) (GSettings *, const gchar *, const gchar *);
517 setlocale (LC_ALL, "");
520 return gsettings_help (FALSE, NULL);
522 else if (strcmp (argv[1], "help") == 0)
523 return gsettings_help (TRUE, argv[2]);
525 else if (argc == 2 && strcmp (argv[1], "list-schemas") == 0)
526 function = gsettings_list_schemas;
528 else if (argc == 2 && strcmp (argv[1], "list-relocatable-schemas") == 0)
529 function = gsettings_list_relocatable_schemas;
531 else if (argc == 3 && strcmp (argv[1], "list-keys") == 0)
532 function = gsettings_list_keys;
534 else if (argc == 3 && strcmp (argv[1], "list-children") == 0)
535 function = gsettings_list_children;
537 else if (argc == 4 && strcmp (argv[1], "range") == 0)
538 function = gsettings_range;
540 else if (argc == 4 && strcmp (argv[1], "get") == 0)
541 function = gsettings_get;
543 else if (argc == 5 && strcmp (argv[1], "set") == 0)
544 function = gsettings_set;
546 else if (argc == 4 && strcmp (argv[1], "reset") == 0)
547 function = gsettings_reset;
549 else if (argc == 4 && strcmp (argv[1], "writable") == 0)
550 function = gsettings_writable;
552 else if ((argc == 3 || argc == 4) && strcmp (argv[1], "monitor") == 0)
553 function = gsettings_monitor;
556 return gsettings_help (FALSE, argv[1]);
564 if (argv[2][0] == '\0')
566 g_printerr (_("Empty schema name given"));
570 parts = g_strsplit (argv[2], ":", 2);
574 if (!check_relocatable_schema (parts[0]) || !check_path (parts[1]))
577 settings = g_settings_new_with_path (parts[0], parts[1]);
581 if (!check_schema (parts[0]))
584 settings = g_settings_new (parts[0]);
594 if (!check_key (settings, argv[3]))
602 (* function) (settings, key, argc > 4 ? argv[4] : NULL);
604 if (settings != NULL)
605 g_object_unref (settings);