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,
160 GVariant *v, *default_v;
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 default_v = g_settings_get_value (settings, key);
205 type = g_variant_get_type (default_v);
208 v = g_variant_parse (type, value, NULL, NULL, &error);
209 g_variant_unref (default_v);
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);
275 settings = g_settings_new_with_path (schema, path);
277 settings = g_settings_new (schema);
279 if (g_settings_is_writable (settings, key))
286 g_option_context_free (context);
292 key_changed (GSettings *settings,
298 v = g_settings_get_value (settings, key);
299 value = g_variant_print (v, FALSE);
300 g_print ("%s\n", value);
306 handle_monitor (gint *argc,
313 gchar *detailed_signal;
315 GOptionContext *context;
316 GOptionEntry entries[] = {
317 { "path", 'p', 0, G_OPTION_ARG_STRING, &path, N_("Specify the path for the schema"), N_("PATH") },
323 modify_argv0_for_command (argc, argv, "monitor");
325 context = g_option_context_new (_("SCHEMA KEY"));
326 g_option_context_set_help_enabled (context, FALSE);
327 g_option_context_set_summary (context,
328 _("Monitor KEY for changes and print the changed values.\n"
329 "Monitoring will continue until the process is terminated."));
331 g_option_context_set_description (context,
333 " SCHEMA The id of the schema\n"
334 " KEY The name of the key\n"));
335 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
340 if (!g_option_context_parse (context, argc, argv, NULL) || *argc != 3)
343 s = g_option_context_get_help (context, FALSE, NULL);
344 g_printerr ("%s", s);
354 settings = g_settings_new_with_path (schema, path);
356 settings = g_settings_new (schema);
358 detailed_signal = g_strdup_printf ("changed::%s", key);
359 g_signal_connect (settings, detailed_signal,
360 G_CALLBACK (key_changed), NULL);
362 loop = g_main_loop_new (NULL, FALSE);
363 g_main_loop_run (loop);
364 g_main_loop_unref (loop);
367 g_option_context_free (context);
372 main (int argc, char *argv[])
376 setlocale (LC_ALL, "");
381 ret = usage (&argc, &argv, FALSE);
382 else if (g_strcmp0 (argv[1], "help") == 0)
383 ret = usage (&argc, &argv, TRUE);
384 else if (g_strcmp0 (argv[1], "get") == 0)
385 ret = handle_get (&argc, &argv);
386 else if (g_strcmp0 (argv[1], "set") == 0)
387 ret = handle_set (&argc, &argv);
388 else if (g_strcmp0 (argv[1], "monitor") == 0)
389 ret = handle_monitor (&argc, &argv);
390 else if (g_strcmp0 (argv[1], "writable") == 0)
391 ret = handle_writable (&argc, &argv);
394 g_printerr (_("Unknown command '%s'\n"), argv[1]);
395 ret = usage (&argc, &argv, FALSE);