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>
27 contained (const gchar * const *items,
31 if (strcmp (*items++, item) == 0)
38 is_schema (const gchar *schema)
40 return contained (g_settings_list_schemas (), schema);
44 is_relocatable_schema (const gchar *schema)
46 return contained (g_settings_list_relocatable_schemas (), schema);
50 check_relocatable_schema (const gchar *schema)
52 if (is_relocatable_schema (schema))
55 if (is_schema (schema))
56 g_printerr ("Schema '%s' is not relocatable "
57 "(path must not be specified)\n",
61 g_printerr ("No such schema '%s'\n", schema);
67 check_schema (const gchar *schema)
69 if (is_schema (schema))
72 if (is_relocatable_schema (schema))
73 g_printerr ("Schema '%s' is relocatable "
74 "(path must be specified)\n",
78 g_printerr ("No such schema '%s'\n", schema);
84 check_path (const gchar *path)
88 g_printerr ("Empty path given.\n");
94 g_printerr ("Path must begin with a slash (/)\n");
98 if (!g_str_has_suffix (path, "/"))
100 g_printerr ("Path must end with a slash (/)\n");
104 if (strstr (path, "//"))
106 g_printerr ("Path must not contain two adjacent slashes (//)\n");
114 check_key (GSettings *settings,
120 keys = g_settings_list_keys (settings);
121 good = contained ((const gchar **) keys, key);
127 g_printerr ("No such key '%s'\n", key);
133 output_list (const gchar * const *list)
137 for (i = 0; list[i]; i++)
138 g_print ("%s\n", list[i]);
142 gsettings_list_schemas (GSettings *settings,
146 output_list (g_settings_list_schemas ());
150 gsettings_list_relocatable_schemas (GSettings *settings,
154 output_list (g_settings_list_relocatable_schemas ());
158 gsettings_list_keys (GSettings *settings,
164 keys = g_settings_list_keys (settings);
165 output_list ((const gchar **) keys);
170 gsettings_list_children (GSettings *settings,
178 children = g_settings_list_children (settings);
179 for (i = 0; children[i]; i++)
180 if (strlen (children[i]) > max)
181 max = strlen (children[i]);
183 for (i = 0; children[i]; i++)
189 child = g_settings_get_child (settings, children[i]);
195 if (is_schema (schema))
196 g_print ("%-*s %s\n", max, children[i], schema);
198 g_print ("%-*s %s:%s\n", max, children[i], schema, path);
200 g_object_unref (child);
205 g_strfreev (children);
209 gsettings_range (GSettings *settings,
213 GVariant *range, *detail;
216 range = g_settings_get_range (settings, key);
217 g_variant_get (range, "(&sv)", &type, &detail);
219 if (strcmp (type, "type") == 0)
220 g_print ("type %s\n", g_variant_get_type_string (detail) + 1);
222 else if (strcmp (type, "range") == 0)
227 g_variant_get (detail, "(**)", &min, &max);
228 smin = g_variant_print (min, FALSE);
229 smax = g_variant_print (max, FALSE);
231 g_print ("range %s %s %s\n",
232 g_variant_get_type_string (min), smin, smax);
233 g_variant_unref (min);
234 g_variant_unref (max);
239 else if (strcmp (type, "enum") == 0 || strcmp (type, "flags") == 0)
244 g_print ("%s\n", type);
246 g_variant_iter_init (&iter, detail);
247 while (g_variant_iter_loop (&iter, "*", &item))
251 printed = g_variant_print (item, FALSE);
252 g_print ("%s\n", printed);
257 g_variant_unref (detail);
258 g_variant_unref (range);
262 gsettings_get (GSettings *settings,
269 value = g_settings_get_value (settings, key);
270 printed = g_variant_print (value, TRUE);
271 g_print ("%s\n", printed);
272 g_variant_unref (value);
277 gsettings_reset (GSettings *settings,
281 g_settings_reset (settings, key);
286 gsettings_writable (GSettings *settings,
291 g_settings_is_writable (settings, key) ?
296 value_changed (GSettings *settings,
303 value = g_settings_get_value (settings, key);
304 printed = g_variant_print (value, TRUE);
305 g_print ("%s: %s\n", key, printed);
306 g_variant_unref (value);
311 gsettings_monitor (GSettings *settings,
319 name = g_strdup_printf ("changed::%s", key);
320 g_signal_connect (settings, name, G_CALLBACK (value_changed), NULL);
323 g_signal_connect (settings, "changed", G_CALLBACK (value_changed), NULL);
325 g_main_loop_run (g_main_loop_new (NULL, FALSE));
329 gsettings_set (GSettings *settings,
333 const GVariantType *type;
334 GError *error = NULL;
338 existing = g_settings_get_value (settings, key);
339 type = g_variant_get_type (existing);
341 new = g_variant_parse (type, value, NULL, NULL, &error);
345 g_printerr ("%s\n", error->message);
349 if (!g_settings_range_check (settings, key, new))
351 g_printerr ("The provided value is outside of the valid range\n");
352 g_variant_unref (new);
356 g_settings_set_value (settings, key, new);
357 g_variant_unref (existing);
358 g_variant_unref (new);
364 gsettings_help (gboolean requested,
365 const gchar *command)
367 const gchar *description;
368 const gchar *synopsis;
371 string = g_string_new (NULL);
376 else if (strcmp (command, "list-schemas") == 0)
378 description = "List the installed (non-relocatable) schemas";
382 else if (strcmp (command, "list-relocatable-schemas") == 0)
384 description = "List the installed relocatable schemas";
388 else if (strcmp (command, "list-keys") == 0)
390 description = "Lists the keys in SCHEMA";
391 synopsis = "SCHEMA[:PATH]";
394 else if (strcmp (command, "list-children") == 0)
396 description = "Lists the children of SCHEMA";
397 synopsis = "SCHEMA[:PATH]";
400 else if (strcmp (command, "get") == 0)
402 description = "Gets the value of KEY";
403 synopsis = "SCHEMA[:PATH] KEY";
406 else if (strcmp (command, "range") == 0)
408 description = "Queries the range of valid values for KEY";
409 synopsis = "SCHEMA[:PATH] KEY";
412 else if (strcmp (command, "set") == 0)
414 description = "Sets the value of KEY to VALUE";
415 synopsis = "SCHEMA[:PATH] KEY VALUE";
418 else if (strcmp (command, "reset") == 0)
420 description = "Resets KEY to its default value";
421 synopsis = "SCHEMA[:PATH] KEY";
424 else if (strcmp (command, "writable") == 0)
426 description = "Checks if KEY is writable";
427 synopsis = "SCHEMA[:PATH] KEY";
430 else if (strcmp (command, "monitor") == 0)
432 description = "Monitors KEY for changes.\n"
433 "If no KEY is specified, monitor all keys in SCHEMA.\n"
434 "Use ^C to stop monitoring.\n";
435 synopsis = "SCHEMA[:PATH] [KEY]";
439 g_string_printf (string, "Unknown command %s\n\n", command);
446 g_string_append (string,
448 " gsettings COMMAND [ARGS...]\n"
451 " help Show this information\n"
452 " list-schemas List installed schemas\n"
453 " list-relocatable-schemas List relocatable schemas\n"
454 " list-keys List keys in a schema\n"
455 " list-children List children of a schema\n"
456 " range Queries the range of a key\n"
457 " get Get the value of a key\n"
458 " set Set the value of a key\n"
459 " reset Reset the value of a key\n"
460 " writable Check if a key is writable\n"
461 " monitor Watch for changes\n"
463 "Use 'gsettings help COMMAND' to get detailed help.\n\n");
467 g_string_append_printf (string, "Usage:\n gsettings %s %s\n\n%s\n\n",
468 command, synopsis, description);
472 g_string_append (string, "Arguments:\n");
474 if (strstr (synopsis, "SCHEMA"))
475 g_string_append (string,
476 " SCHEMA The name of the schema\n"
477 " PATH The path, for relocatable schemas\n");
479 if (strstr (synopsis, "[KEY]"))
480 g_string_append (string,
481 " KEY The (optional) key within the schema\n");
483 else if (strstr (synopsis, "KEY"))
484 g_string_append (string,
485 " KEY The key within the schema\n");
487 if (strstr (synopsis, "VALUE"))
488 g_string_append (string,
489 " VALUE The value to set\n");
491 g_string_append (string, "\n");
496 g_print ("%s", string->str);
498 g_printerr ("%s", string->str);
500 g_string_free (string, TRUE);
502 return requested ? 0 : 1;
507 main (int argc, char **argv)
509 void (* function) (GSettings *, const gchar *, const gchar *);
514 return gsettings_help (FALSE, NULL);
516 else if (strcmp (argv[1], "help") == 0)
517 return gsettings_help (TRUE, argv[2]);
519 else if (argc == 2 && strcmp (argv[1], "list-schemas") == 0)
520 function = gsettings_list_schemas;
522 else if (argc == 2 && strcmp (argv[1], "list-relocatable-schemas") == 0)
523 function = gsettings_list_relocatable_schemas;
525 else if (argc == 3 && strcmp (argv[1], "list-keys") == 0)
526 function = gsettings_list_keys;
528 else if (argc == 3 && strcmp (argv[1], "list-children") == 0)
529 function = gsettings_list_children;
531 else if (argc == 4 && strcmp (argv[1], "range") == 0)
532 function = gsettings_range;
534 else if (argc == 4 && strcmp (argv[1], "get") == 0)
535 function = gsettings_get;
537 else if (argc == 5 && strcmp (argv[1], "set") == 0)
538 function = gsettings_set;
540 else if (argc == 4 && strcmp (argv[1], "reset") == 0)
541 function = gsettings_reset;
543 else if (argc == 4 && strcmp (argv[1], "writable") == 0)
544 function = gsettings_writable;
546 else if ((argc == 3 || argc == 4) && strcmp (argv[1], "monitor") == 0)
547 function = gsettings_monitor;
550 return gsettings_help (FALSE, argv[1]);
558 if (argv[2][0] == '\0')
560 g_printerr ("Empty schema name given");
564 parts = g_strsplit (argv[2], ":", 2);
568 if (!check_relocatable_schema (parts[0]) || !check_path (parts[1]))
571 settings = g_settings_new_with_path (parts[0], parts[1]);
575 if (!check_schema (parts[0]))
578 settings = g_settings_new (parts[0]);
588 if (!check_key (settings, argv[3]))
596 (* function) (settings, key, argc > 4 ? argv[4] : NULL);
598 if (settings != NULL)
599 g_object_unref (settings);