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 g_settings_set_value (settings, key, new);
297 g_variant_unref (existing);
298 g_variant_unref (new);
304 gsettings_help (gboolean requested,
305 const gchar *command)
307 const gchar *description;
308 const gchar *synopsis;
311 string = g_string_new (NULL);
316 else if (strcmp (command, "list-schemas") == 0)
318 description = "List the installed (non-relocatable) schemas";
322 else if (strcmp (command, "list-relocatable-schemas") == 0)
324 description = "List the installed relocatable schemas";
328 else if (strcmp (command, "list-keys") == 0)
330 description = "Lists the keys in SCHEMA";
331 synopsis = "SCHEMA[:PATH]";
334 else if (strcmp (command, "list-children") == 0)
336 description = "Lists the children of SCHEMA";
337 synopsis = "SCHEMA[:PATH]";
340 else if (strcmp (command, "get") == 0)
342 description = "Gets the value of KEY";
343 synopsis = "SCHEMA[:PATH] KEY";
346 else if (strcmp (command, "set") == 0)
348 description = "Sets the value of KEY to VALUE";
349 synopsis = "SCHEMA[:PATH] KEY VALUE";
352 else if (strcmp (command, "reset") == 0)
354 description = "Resets KEY to its default value";
355 synopsis = "SCHEMA[:PATH] KEY";
358 else if (strcmp (command, "writable") == 0)
360 description = "Checks if KEY is writable";
361 synopsis = "SCHEMA[:PATH] KEY";
364 else if (strcmp (command, "monitor") == 0)
366 description = "Monitors KEY for changes.\n"
367 "If no KEY is specified, monitor all keys in SCHEMA.\n"
368 "Use ^C to stop monitoring.\n";
369 synopsis = "SCHEMA[:PATH] [KEY]";
373 g_string_printf (string, "Unknown command %s\n\n", command);
380 g_string_append (string,
382 " gsettings COMMAND [ARGS...]\n"
385 " help Show this information\n"
386 " list-schemas List installed schemas\n"
387 " list-relocatable-schemas List relocatable schemas\n"
388 " list-keys List keys in a schema\n"
389 " list-children List children of a schema\n"
390 " get Get the value of a key\n"
391 " set Set the value of a key\n"
392 " reset Reset the value of a key\n"
393 " writable Check if a key is writable\n"
394 " monitor Watch for changes\n"
396 "Use 'gsettings help COMMAND' to get detailed help.\n\n");
400 g_string_append_printf (string, "Usage:\n gsettings %s %s\n\n%s\n\n",
401 command, synopsis, description);
405 g_string_append (string, "Arguments:\n");
407 if (strstr (synopsis, "SCHEMA"))
408 g_string_append (string,
409 " SCHEMA The name of the schema\n"
410 " PATH The path, for relocatable schemas\n");
412 if (strstr (synopsis, "[KEY]"))
413 g_string_append (string,
414 " KEY The (optional) key within the schema\n");
416 else if (strstr (synopsis, "KEY"))
417 g_string_append (string,
418 " KEY The key within the schema\n");
420 if (strstr (synopsis, "VALUE"))
421 g_string_append (string,
422 " VALUE The value to set\n");
424 g_string_append (string, "\n");
429 g_print ("%s", string->str);
431 g_printerr ("%s", string->str);
433 g_string_free (string, TRUE);
435 return requested ? 0 : 1;
440 main (int argc, char **argv)
442 void (* function) (GSettings *, const gchar *, const gchar *);
447 return gsettings_help (FALSE, NULL);
449 else if (strcmp (argv[1], "help") == 0)
450 return gsettings_help (TRUE, argv[2]);
452 else if (argc == 2 && strcmp (argv[1], "list-schemas") == 0)
453 function = gsettings_list_schemas;
455 else if (argc == 2 && strcmp (argv[1], "list-relocatable-schemas") == 0)
456 function = gsettings_list_relocatable_schemas;
458 else if (argc == 3 && strcmp (argv[1], "list-keys") == 0)
459 function = gsettings_list_keys;
461 else if (argc == 3 && strcmp (argv[1], "list-children") == 0)
462 function = gsettings_list_children;
464 else if (argc == 4 && strcmp (argv[1], "get") == 0)
465 function = gsettings_get;
467 else if (argc == 5 && strcmp (argv[1], "set") == 0)
468 function = gsettings_set;
470 else if (argc == 4 && strcmp (argv[1], "reset") == 0)
471 function = gsettings_reset;
473 else if (argc == 4 && strcmp (argv[1], "writable") == 0)
474 function = gsettings_writable;
476 else if ((argc == 3 || argc == 4) && strcmp (argv[1], "monitor") == 0)
477 function = gsettings_monitor;
480 return gsettings_help (FALSE, argv[1]);
488 if (argv[2][0] == '\0')
490 g_printerr ("Empty schema name given");
494 parts = g_strsplit (argv[2], ":", 2);
498 if (!check_relocatable_schema (parts[0]) || !check_path (parts[1]))
501 settings = g_settings_new_with_path (parts[0], parts[1]);
505 if (!check_schema (parts[0]))
508 settings = g_settings_new (parts[0]);
518 if (!check_key (settings, argv[3]))
526 (* function) (settings, key, argc > 4 ? argv[4] : NULL);
528 if (settings != NULL)
529 g_object_unref (settings);