1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 2010 Red Hat, Inc.
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 License, 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: Matthias Clasen
33 GOptionContext *context;
36 g_set_prgname (g_path_get_basename ((*argv)[0]));
38 context = g_option_context_new (_("COMMAND"));
39 g_option_context_set_help_enabled (context, FALSE);
42 " help Show this information\n"
43 " get Get the value of a key\n"
44 " set Set the value of a key\n"
45 " monitor Monitor a key for changes\n"
46 " writable Check if a key is writable\n"
48 "Use '%s COMMAND --help' to get help for individual commands.\n"),
50 g_option_context_set_description (context, s);
52 s = g_option_context_get_help (context, FALSE, NULL);
58 g_option_context_free (context);
60 return use_stdout ? 0 : 1;
64 remove_arg (gint num, gint *argc, gchar **argv[])
68 g_assert (num <= (*argc));
70 for (n = num; (*argv)[n] != NULL; n++)
71 (*argv)[n] = (*argv)[n+1];
73 (*argc) = (*argc) - 1;
77 modify_argv0_for_command (gint *argc,
83 g_assert (g_strcmp0 ((*argv)[1], command) == 0);
84 remove_arg (1, argc, argv);
86 s = g_strdup_printf ("%s %s", (*argv)[0], command);
92 handle_get (gint *argc,
100 GOptionContext *context;
101 GOptionEntry entries[] = {
102 { "path", 'p', 0, G_OPTION_ARG_STRING, &path, N_("Specify the path for the schema"), N_("PATH") },
108 modify_argv0_for_command (argc, argv, "get");
110 context = g_option_context_new (_("SCHEMA KEY"));
111 g_option_context_set_help_enabled (context, FALSE);
112 g_option_context_set_summary (context, _("Get the value of KEY"));
113 g_option_context_set_description (context,
115 " SCHEMA The id of the schema\n"
116 " KEY The name of the key\n"));
117 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
122 if (!g_option_context_parse (context, argc, argv, NULL) || *argc != 3)
125 s = g_option_context_get_help (context, FALSE, NULL);
126 g_printerr ("%s", s);
136 settings = g_settings_new_with_path (schema, path);
138 settings = g_settings_new (schema);
140 v = g_settings_get_value (settings, key);
141 g_print ("%s\n", g_variant_print (v, FALSE));
146 g_option_context_free (context);
152 handle_set (gint *argc,
161 const GVariantType *type;
162 GOptionContext *context;
163 GOptionEntry entries[] = {
164 { "path", 'p', 0, G_OPTION_ARG_STRING, &path, N_("Specify the path for the schema"), N_("PATH") },
170 modify_argv0_for_command (argc, argv, "set");
172 context = g_option_context_new (_("SCHEMA KEY VALUE"));
173 g_option_context_set_help_enabled (context, FALSE);
174 g_option_context_set_summary (context, _("Set the value of KEY"));
175 g_option_context_set_description (context,
177 " SCHEMA The id of the schema\n"
178 " KEY The name of the key\n"
179 " VALUE The value to set key to, as a serialized GVariant\n"));
180 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
185 if (!g_option_context_parse (context, argc, argv, NULL) || *argc != 4)
188 s = g_option_context_get_help (context, FALSE, NULL);
189 g_printerr ("%s", s);
200 settings = g_settings_new_with_path (schema, path);
202 settings = g_settings_new (schema);
204 v = g_settings_get_value (settings, key);
205 type = g_variant_get_type (v);
209 v = g_variant_parse (type, value, NULL, NULL, &error);
212 g_printerr ("%s\n", error->message);
216 if (!g_settings_set_value (settings, key, v))
218 g_printerr (_("Key %s is not writable\n"), key);
226 g_option_context_free (context);
232 handle_writable (gint *argc,
239 GOptionContext *context;
240 GOptionEntry entries[] = {
241 { "path", 'p', 0, G_OPTION_ARG_STRING, &path, N_("Specify the path for the schema"), N_("PATH") },
247 modify_argv0_for_command (argc, argv, "writable");
249 context = g_option_context_new (_("SCHEMA KEY"));
250 g_option_context_set_help_enabled (context, FALSE);
251 g_option_context_set_summary (context, _("Find out whether KEY is writable"));
252 g_option_context_set_description (context,
254 " SCHEMA The id of the schema\n"
255 " KEY The name of the key\n"));
256 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
261 if (!g_option_context_parse (context, argc, argv, NULL) || *argc != 3)
264 s = g_option_context_get_help (context, FALSE, NULL);
265 g_printerr ("%s", s);
274 settings = g_settings_new_with_path (schema, path);
276 if (g_settings_is_writable (settings, key))
283 g_option_context_free (context);
289 key_changed (GSettings *settings,
295 v = g_settings_get_value (settings, key);
296 value = g_variant_print (v, FALSE);
297 g_print ("%s\n", value);
303 handle_monitor (gint *argc,
310 gchar *detailed_signal;
312 GOptionContext *context;
313 GOptionEntry entries[] = {
314 { "path", 'p', 0, G_OPTION_ARG_STRING, &path, N_("Specify the path for the schema"), N_("PATH") },
320 modify_argv0_for_command (argc, argv, "monitor");
322 context = g_option_context_new (_("SCHEMA KEY"));
323 g_option_context_set_help_enabled (context, FALSE);
324 g_option_context_set_summary (context,
325 _("Monitor KEY for changes and print the changed values.\n"
326 "Monitoring will continue until the process is terminated."));
328 g_option_context_set_description (context,
330 " SCHEMA The id of the schema\n"
331 " KEY The name of the key\n"));
332 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
337 if (!g_option_context_parse (context, argc, argv, NULL) || *argc != 3)
340 s = g_option_context_get_help (context, FALSE, NULL);
341 g_printerr ("%s", s);
350 settings = g_settings_new_with_path (schema, path);
352 detailed_signal = g_strdup_printf ("changed::%s", key);
353 g_signal_connect (settings, detailed_signal,
354 G_CALLBACK (key_changed), NULL);
356 loop = g_main_loop_new (NULL, FALSE);
357 g_main_loop_run (loop);
358 g_main_loop_unref (loop);
361 g_option_context_free (context);
366 main (int argc, char *argv[])
370 setlocale (LC_ALL, "");
375 ret = usage (&argc, &argv, FALSE);
376 else if (g_strcmp0 (argv[1], "help") == 0)
377 ret = usage (&argc, &argv, TRUE);
378 else if (g_strcmp0 (argv[1], "get") == 0)
379 ret = handle_get (&argc, &argv);
380 else if (g_strcmp0 (argv[1], "set") == 0)
381 ret = handle_set (&argc, &argv);
382 else if (g_strcmp0 (argv[1], "monitor") == 0)
383 ret = handle_monitor (&argc, &argv);
384 else if (g_strcmp0 (argv[1], "writable") == 0)
385 ret = handle_writable (&argc, &argv);
388 g_printerr (_("Unknown command '%s'\n"), argv[1]);
389 ret = usage (&argc, &argv, FALSE);