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);
218 /* XXX: workaround for now
220 * if we exit() so quickly, GDBus may not have had a chance to
221 * actually send the message (since we're using it async).
223 * GDBusConnection has no API to sync or wait for messages to be sent,
224 * so we send a meaningless message and wait for the reply to ensure
225 * that all messages that came before must have been sent.
228 GDBusConnection *session;
230 session = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
231 g_dbus_connection_call_sync (session, "org.gtk.DoesNotExist", "/",
232 "org.gtk.DoesNotExist", "Workaround",
233 g_variant_new ("()"), 0, -1, NULL, NULL);
237 g_option_context_free (context);
243 handle_writable (gint *argc,
250 GOptionContext *context;
251 GOptionEntry entries[] = {
252 { "path", 'p', 0, G_OPTION_ARG_STRING, &path, N_("Specify the path for the schema"), N_("PATH") },
258 modify_argv0_for_command (argc, argv, "writable");
260 context = g_option_context_new (_("SCHEMA KEY"));
261 g_option_context_set_help_enabled (context, FALSE);
262 g_option_context_set_summary (context, _("Find out whether KEY is writable"));
263 g_option_context_set_description (context,
265 " SCHEMA The id of the schema\n"
266 " KEY The name of the key\n"));
267 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
272 if (!g_option_context_parse (context, argc, argv, NULL) || *argc != 3)
275 s = g_option_context_get_help (context, FALSE, NULL);
276 g_printerr ("%s", s);
285 settings = g_settings_new_with_path (schema, path);
287 if (g_settings_is_writable (settings, key))
294 g_option_context_free (context);
300 key_changed (GSettings *settings,
306 v = g_settings_get_value (settings, key);
307 value = g_variant_print (v, FALSE);
308 g_print ("%s\n", value);
314 handle_monitor (gint *argc,
321 gchar *detailed_signal;
323 GOptionContext *context;
324 GOptionEntry entries[] = {
325 { "path", 'p', 0, G_OPTION_ARG_STRING, &path, N_("Specify the path for the schema"), N_("PATH") },
331 modify_argv0_for_command (argc, argv, "monitor");
333 context = g_option_context_new (_("SCHEMA KEY"));
334 g_option_context_set_help_enabled (context, FALSE);
335 g_option_context_set_summary (context,
336 _("Monitor KEY for changes and print the changed values.\n"
337 "Monitoring will continue until the process is terminated."));
339 g_option_context_set_description (context,
341 " SCHEMA The id of the schema\n"
342 " KEY The name of the key\n"));
343 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
348 if (!g_option_context_parse (context, argc, argv, NULL) || *argc != 3)
351 s = g_option_context_get_help (context, FALSE, NULL);
352 g_printerr ("%s", s);
361 settings = g_settings_new_with_path (schema, path);
363 detailed_signal = g_strdup_printf ("changed::%s", key);
364 g_signal_connect (settings, detailed_signal,
365 G_CALLBACK (key_changed), NULL);
367 loop = g_main_loop_new (NULL, FALSE);
368 g_main_loop_run (loop);
369 g_main_loop_unref (loop);
372 g_option_context_free (context);
377 main (int argc, char *argv[])
381 setlocale (LC_ALL, "");
386 ret = usage (&argc, &argv, FALSE);
387 else if (g_strcmp0 (argv[1], "help") == 0)
388 ret = usage (&argc, &argv, TRUE);
389 else if (g_strcmp0 (argv[1], "get") == 0)
390 ret = handle_get (&argc, &argv);
391 else if (g_strcmp0 (argv[1], "set") == 0)
392 ret = handle_set (&argc, &argv);
393 else if (g_strcmp0 (argv[1], "monitor") == 0)
394 ret = handle_monitor (&argc, &argv);
395 else if (g_strcmp0 (argv[1], "writable") == 0)
396 ret = handle_writable (&argc, &argv);
399 g_printerr (_("Unknown command '%s'\n"), argv[1]);
400 ret = usage (&argc, &argv, FALSE);