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);
135 settings = g_settings_new_with_path (schema, path);
137 v = g_settings_get_value (settings, key);
138 g_print ("%s\n", g_variant_print (v, FALSE));
143 g_option_context_free (context);
149 handle_set (gint *argc,
158 const GVariantType *type;
159 GOptionContext *context;
160 GOptionEntry entries[] = {
161 { "path", 'p', 0, G_OPTION_ARG_STRING, &path, N_("Specify the path for the schema"), N_("PATH") },
167 modify_argv0_for_command (argc, argv, "set");
169 context = g_option_context_new (_("SCHEMA KEY VALUE"));
170 g_option_context_set_help_enabled (context, FALSE);
171 g_option_context_set_summary (context, _("Set the value of KEY"));
172 g_option_context_set_description (context,
174 " SCHEMA The id of the schema\n"
175 " KEY The name of the key\n"
176 " VALUE The value to set key to, as a serialized GVariant\n"));
177 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
182 if (!g_option_context_parse (context, argc, argv, NULL) || *argc != 4)
185 s = g_option_context_get_help (context, FALSE, NULL);
186 g_printerr ("%s", s);
196 settings = g_settings_new_with_path (schema, path);
198 v = g_settings_get_value (settings, key);
199 type = g_variant_get_type (v);
203 v = g_variant_parse (type, value, NULL, NULL, &error);
206 g_printerr ("%s\n", error->message);
210 if (!g_settings_set_value (settings, key, v))
212 g_printerr (_("Key %s is not writable\n"), key);
220 g_option_context_free (context);
226 handle_writable (gint *argc,
233 GOptionContext *context;
234 GOptionEntry entries[] = {
235 { "path", 'p', 0, G_OPTION_ARG_STRING, &path, N_("Specify the path for the schema"), N_("PATH") },
241 modify_argv0_for_command (argc, argv, "writable");
243 context = g_option_context_new (_("SCHEMA KEY"));
244 g_option_context_set_help_enabled (context, FALSE);
245 g_option_context_set_summary (context, _("Find out whether KEY is writable"));
246 g_option_context_set_description (context,
248 " SCHEMA The id of the schema\n"
249 " KEY The name of the key\n"));
250 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
255 if (!g_option_context_parse (context, argc, argv, NULL) || *argc != 3)
258 s = g_option_context_get_help (context, FALSE, NULL);
259 g_printerr ("%s", s);
268 settings = g_settings_new_with_path (schema, path);
270 if (g_settings_is_writable (settings, key))
277 g_option_context_free (context);
283 key_changed (GSettings *settings,
289 v = g_settings_get_value (settings, key);
290 value = g_variant_print (v, FALSE);
291 g_print ("%s\n", value);
297 handle_monitor (gint *argc,
304 gchar *detailed_signal;
306 GOptionContext *context;
307 GOptionEntry entries[] = {
308 { "path", 'p', 0, G_OPTION_ARG_STRING, &path, N_("Specify the path for the schema"), N_("PATH") },
314 modify_argv0_for_command (argc, argv, "monitor");
316 context = g_option_context_new (_("SCHEMA KEY"));
317 g_option_context_set_help_enabled (context, FALSE);
318 g_option_context_set_summary (context,
319 _("Monitor KEY for changes and print the changed values.\n"
320 "Monitoring will continue until the process is terminated."));
322 g_option_context_set_description (context,
324 " SCHEMA The id of the schema\n"
325 " KEY The name of the key\n"));
326 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
331 if (!g_option_context_parse (context, argc, argv, NULL) || *argc != 3)
334 s = g_option_context_get_help (context, FALSE, NULL);
335 g_printerr ("%s", s);
344 settings = g_settings_new_with_path (schema, path);
346 detailed_signal = g_strdup_printf ("changed::%s", key);
347 g_signal_connect (settings, detailed_signal,
348 G_CALLBACK (key_changed), NULL);
350 loop = g_main_loop_new (NULL, FALSE);
351 g_main_loop_run (loop);
352 g_main_loop_unref (loop);
355 g_option_context_free (context);
360 main (int argc, char *argv[])
364 setlocale (LC_ALL, "");
369 ret = usage (&argc, &argv, FALSE);
370 else if (g_strcmp0 (argv[1], "help") == 0)
371 ret = usage (&argc, &argv, TRUE);
372 else if (g_strcmp0 (argv[1], "get") == 0)
373 ret = handle_get (&argc, &argv);
374 else if (g_strcmp0 (argv[1], "set") == 0)
375 ret = handle_set (&argc, &argv);
376 else if (g_strcmp0 (argv[1], "monitor") == 0)
377 ret = handle_monitor (&argc, &argv);
378 else if (g_strcmp0 (argv[1], "writable") == 0)
379 ret = handle_writable (&argc, &argv);
382 g_printerr (_("Unknown command '%s'\n"), argv[1]);
383 ret = usage (&argc, &argv, FALSE);