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_get (GSettings *settings,
216 value = g_settings_get_value (settings, key);
217 printed = g_variant_print (value, TRUE);
218 g_print ("%s\n", printed);
219 g_variant_unref (value);
224 gsettings_reset (GSettings *settings,
228 g_settings_reset (settings, key);
233 gsettings_writable (GSettings *settings,
238 g_settings_is_writable (settings, key) ?
243 value_changed (GSettings *settings,
250 value = g_settings_get_value (settings, key);
251 printed = g_variant_print (value, TRUE);
252 g_print ("%s: %s\n", key, printed);
253 g_variant_unref (value);
258 gsettings_monitor (GSettings *settings,
266 name = g_strdup_printf ("changed::%s", key);
267 g_signal_connect (settings, name, G_CALLBACK (value_changed), NULL);
270 g_signal_connect (settings, "changed", G_CALLBACK (value_changed), NULL);
272 g_main_loop_run (g_main_loop_new (NULL, FALSE));
276 gsettings_set (GSettings *settings,
280 const GVariantType *type;
281 GError *error = NULL;
285 existing = g_settings_get_value (settings, key);
286 type = g_variant_get_type (existing);
288 new = g_variant_parse (type, value, NULL, NULL, &error);
292 g_printerr ("%s\n", error->message);
296 if (!g_settings_range_check (settings, key, new))
298 g_printerr ("The provided value is outside of the valid range\n");
299 g_variant_unref (new);
303 g_settings_set_value (settings, key, new);
304 g_variant_unref (existing);
305 g_variant_unref (new);
311 gsettings_help (gboolean requested,
312 const gchar *command)
314 const gchar *description;
315 const gchar *synopsis;
318 string = g_string_new (NULL);
323 else if (strcmp (command, "list-schemas") == 0)
325 description = "List the installed (non-relocatable) schemas";
329 else if (strcmp (command, "list-relocatable-schemas") == 0)
331 description = "List the installed relocatable schemas";
335 else if (strcmp (command, "list-keys") == 0)
337 description = "Lists the keys in SCHEMA";
338 synopsis = "SCHEMA[:PATH]";
341 else if (strcmp (command, "list-children") == 0)
343 description = "Lists the children of SCHEMA";
344 synopsis = "SCHEMA[:PATH]";
347 else if (strcmp (command, "get") == 0)
349 description = "Gets the value of KEY";
350 synopsis = "SCHEMA[:PATH] KEY";
353 else if (strcmp (command, "set") == 0)
355 description = "Sets the value of KEY to VALUE";
356 synopsis = "SCHEMA[:PATH] KEY VALUE";
359 else if (strcmp (command, "reset") == 0)
361 description = "Resets KEY to its default value";
362 synopsis = "SCHEMA[:PATH] KEY";
365 else if (strcmp (command, "writable") == 0)
367 description = "Checks if KEY is writable";
368 synopsis = "SCHEMA[:PATH] KEY";
371 else if (strcmp (command, "monitor") == 0)
373 description = "Monitors KEY for changes.\n"
374 "If no KEY is specified, monitor all keys in SCHEMA.\n"
375 "Use ^C to stop monitoring.\n";
376 synopsis = "SCHEMA[:PATH] [KEY]";
380 g_string_printf (string, "Unknown command %s\n\n", command);
387 g_string_append (string,
389 " gsettings COMMAND [ARGS...]\n"
392 " help Show this information\n"
393 " list-schemas List installed schemas\n"
394 " list-relocatable-schemas List relocatable schemas\n"
395 " list-keys List keys in a schema\n"
396 " list-children List children of a schema\n"
397 " get Get the value of a key\n"
398 " set Set the value of a key\n"
399 " reset Reset the value of a key\n"
400 " writable Check if a key is writable\n"
401 " monitor Watch for changes\n"
403 "Use 'gsettings help COMMAND' to get detailed help.\n\n");
407 g_string_append_printf (string, "Usage:\n gsettings %s %s\n\n%s\n\n",
408 command, synopsis, description);
412 g_string_append (string, "Arguments:\n");
414 if (strstr (synopsis, "SCHEMA"))
415 g_string_append (string,
416 " SCHEMA The name of the schema\n"
417 " PATH The path, for relocatable schemas\n");
419 if (strstr (synopsis, "[KEY]"))
420 g_string_append (string,
421 " KEY The (optional) key within the schema\n");
423 else if (strstr (synopsis, "KEY"))
424 g_string_append (string,
425 " KEY The key within the schema\n");
427 if (strstr (synopsis, "VALUE"))
428 g_string_append (string,
429 " VALUE The value to set\n");
431 g_string_append (string, "\n");
436 g_print ("%s", string->str);
438 g_printerr ("%s", string->str);
440 g_string_free (string, TRUE);
442 return requested ? 0 : 1;
447 main (int argc, char **argv)
449 void (* function) (GSettings *, const gchar *, const gchar *);
454 return gsettings_help (FALSE, NULL);
456 else if (strcmp (argv[1], "help") == 0)
457 return gsettings_help (TRUE, argv[2]);
459 else if (argc == 2 && strcmp (argv[1], "list-schemas") == 0)
460 function = gsettings_list_schemas;
462 else if (argc == 2 && strcmp (argv[1], "list-relocatable-schemas") == 0)
463 function = gsettings_list_relocatable_schemas;
465 else if (argc == 3 && strcmp (argv[1], "list-keys") == 0)
466 function = gsettings_list_keys;
468 else if (argc == 3 && strcmp (argv[1], "list-children") == 0)
469 function = gsettings_list_children;
471 else if (argc == 4 && strcmp (argv[1], "get") == 0)
472 function = gsettings_get;
474 else if (argc == 5 && strcmp (argv[1], "set") == 0)
475 function = gsettings_set;
477 else if (argc == 4 && strcmp (argv[1], "reset") == 0)
478 function = gsettings_reset;
480 else if (argc == 4 && strcmp (argv[1], "writable") == 0)
481 function = gsettings_writable;
483 else if ((argc == 3 || argc == 4) && strcmp (argv[1], "monitor") == 0)
484 function = gsettings_monitor;
487 return gsettings_help (FALSE, argv[1]);
495 if (argv[2][0] == '\0')
497 g_printerr ("Empty schema name given");
501 parts = g_strsplit (argv[2], ":", 2);
505 if (!check_relocatable_schema (parts[0]) || !check_path (parts[1]))
508 settings = g_settings_new_with_path (parts[0], parts[1]);
512 if (!check_schema (parts[0]))
515 settings = g_settings_new (parts[0]);
525 if (!check_key (settings, argv[3]))
533 (* function) (settings, key, argc > 4 ? argv[4] : NULL);
535 if (settings != NULL)
536 g_object_unref (settings);