Add g_settings_sync() and use it
[platform/upstream/glib.git] / gio / gsettings-tool.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 2010 Red Hat, Inc.
3  *
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.
8  *
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.
13  *
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.
18  *
19  * Author: Matthias Clasen
20  */
21
22 #include "config.h"
23
24 #include <locale.h>
25 #include <gi18n.h>
26 #include <gio.h>
27
28 static gint
29 usage (gint      *argc,
30        gchar    **argv[],
31        gboolean   use_stdout)
32 {
33   GOptionContext *context;
34   gchar *s;
35
36   g_set_prgname (g_path_get_basename ((*argv)[0]));
37
38   context = g_option_context_new (_("COMMAND"));
39   g_option_context_set_help_enabled (context, FALSE);
40   s = g_strdup_printf (
41     _("Commands:\n"
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"
47       "\n"
48       "Use '%s COMMAND --help' to get help for individual commands.\n"),
49       g_get_prgname ());
50   g_option_context_set_description (context, s);
51   g_free (s);
52   s = g_option_context_get_help (context, FALSE, NULL);
53   if (use_stdout)
54     g_print ("%s", s);
55   else
56     g_printerr ("%s", s);
57   g_free (s);
58   g_option_context_free (context);
59
60   return use_stdout ? 0 : 1;
61 }
62
63 static void
64 remove_arg (gint num, gint *argc, gchar **argv[])
65 {
66   gint n;
67
68   g_assert (num <= (*argc));
69
70   for (n = num; (*argv)[n] != NULL; n++)
71     (*argv)[n] = (*argv)[n+1];
72   (*argv)[n] = NULL;
73   (*argc) = (*argc) - 1;
74 }
75
76 static void
77 modify_argv0_for_command (gint         *argc,
78                           gchar       **argv[],
79                           const gchar  *command)
80 {
81   gchar *s;
82
83   g_assert (g_strcmp0 ((*argv)[1], command) == 0);
84   remove_arg (1, argc, argv);
85
86   s = g_strdup_printf ("%s %s", (*argv)[0], command);
87   (*argv)[0] = s;
88 }
89
90
91 static gint
92 handle_get (gint   *argc,
93             gchar **argv[])
94 {
95   gchar *schema;
96   gchar *path;
97   gchar *key;
98   GSettings *settings;
99   GVariant *v;
100   GOptionContext *context;
101   GOptionEntry entries[] = {
102     { "path", 'p', 0, G_OPTION_ARG_STRING, &path, N_("Specify the path for the schema"), N_("PATH") },
103     { NULL }
104   };
105   GError *error;
106   gint ret = 1;
107
108   modify_argv0_for_command (argc, argv, "get");
109
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,
114     _("Arguments:\n"
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);
118
119   path = NULL;
120
121   error = NULL;
122   if (!g_option_context_parse (context, argc, argv, NULL) || *argc != 3)
123     {
124       gchar *s;
125       s = g_option_context_get_help (context, FALSE, NULL);
126       g_printerr ("%s", s);
127       g_free (s);
128
129       goto out;
130     }
131
132   schema = (*argv)[1];
133   key = (*argv)[2];
134
135   settings = g_settings_new_with_path (schema, path);
136
137   v = g_settings_get_value (settings, key);
138   g_print ("%s\n", g_variant_print (v, FALSE));
139   g_variant_unref (v);
140   ret = 0;
141
142  out:
143   g_option_context_free (context);
144
145   return ret;
146 }
147
148 static gint
149 handle_set (gint   *argc,
150             gchar **argv[])
151 {
152   gchar *schema;
153   gchar *path;
154   gchar *key;
155   gchar *value;
156   GSettings *settings;
157   GVariant *v;
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") },
162     { NULL }
163   };
164   GError *error;
165   gint ret = 1;
166
167   modify_argv0_for_command (argc, argv, "set");
168
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,
173     _("Arguments:\n"
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);
178
179   path = NULL;
180
181   error = NULL;
182   if (!g_option_context_parse (context, argc, argv, NULL) || *argc != 4)
183     {
184       gchar *s;
185       s = g_option_context_get_help (context, FALSE, NULL);
186       g_printerr ("%s", s);
187       g_free (s);
188
189       goto out;
190     }
191
192   schema = (*argv)[1];
193   key = (*argv)[2];
194   value = (*argv)[3];
195
196   settings = g_settings_new_with_path (schema, path);
197
198   v = g_settings_get_value (settings, key);
199   type = g_variant_get_type (v);
200   g_variant_unref (v);
201
202   error = NULL;
203   v = g_variant_parse (type, value, NULL, NULL, &error);
204   if (v == NULL)
205     {
206       g_printerr ("%s\n", error->message);
207       goto out;
208     }
209
210   if (!g_settings_set_value (settings, key, v))
211     {
212       g_printerr (_("Key %s is not writable\n"), key);
213       goto out;
214     }
215
216   g_settings_sync (NULL);
217   ret = 0;
218
219  out:
220   g_option_context_free (context);
221
222   return ret;
223 }
224
225 static gint
226 handle_writable (gint   *argc,
227                  gchar **argv[])
228 {
229   gchar *schema;
230   gchar *path;
231   gchar *key;
232   GSettings *settings;
233   GOptionContext *context;
234   GOptionEntry entries[] = {
235     { "path", 'p', 0, G_OPTION_ARG_STRING, &path, N_("Specify the path for the schema"), N_("PATH") },
236     { NULL }
237   };
238   GError *error;
239   gint ret = 1;
240
241   modify_argv0_for_command (argc, argv, "writable");
242
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,
247     _("Arguments:\n"
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);
251
252   path = NULL;
253
254   error = NULL;
255   if (!g_option_context_parse (context, argc, argv, NULL) || *argc != 3)
256     {
257       gchar *s;
258       s = g_option_context_get_help (context, FALSE, NULL);
259       g_printerr ("%s", s);
260       g_free (s);
261
262       goto out;
263     }
264
265   schema = (*argv)[1];
266   key = (*argv)[2];
267
268   settings = g_settings_new_with_path (schema, path);
269
270   if (g_settings_is_writable (settings, key))
271     g_print ("true\n");
272   else
273     g_print ("false\n");
274   ret = 0;
275
276  out:
277   g_option_context_free (context);
278
279   return ret;
280 }
281
282 static void
283 key_changed (GSettings   *settings,
284              const gchar *key)
285 {
286   GVariant *v;
287   gchar *value;
288
289   v = g_settings_get_value (settings, key);
290   value = g_variant_print (v, FALSE);
291   g_print ("%s\n", value);
292   g_free (value);
293   g_variant_unref (v);
294 }
295
296 static gint
297 handle_monitor (gint   *argc,
298                 gchar **argv[])
299 {
300   gchar *schema;
301   gchar *path;
302   gchar *key;
303   GSettings *settings;
304   gchar *detailed_signal;
305   GMainLoop *loop;
306   GOptionContext *context;
307   GOptionEntry entries[] = {
308     { "path", 'p', 0, G_OPTION_ARG_STRING, &path, N_("Specify the path for the schema"), N_("PATH") },
309     { NULL }
310   };
311   GError *error;
312   gint ret = 1;
313
314   modify_argv0_for_command (argc, argv, "monitor");
315
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."));
321
322   g_option_context_set_description (context,
323     _("Arguments:\n"
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);
327
328   path = NULL;
329
330   error = NULL;
331   if (!g_option_context_parse (context, argc, argv, NULL) || *argc != 3)
332     {
333       gchar *s;
334       s = g_option_context_get_help (context, FALSE, NULL);
335       g_printerr ("%s", s);
336       g_free (s);
337
338       goto out;
339     }
340
341   schema = (*argv)[1];
342   key = (*argv)[2];
343
344   settings = g_settings_new_with_path (schema, path);
345
346   detailed_signal = g_strdup_printf ("changed::%s", key);
347   g_signal_connect (settings, detailed_signal,
348                     G_CALLBACK (key_changed), NULL);
349
350   loop = g_main_loop_new (NULL, FALSE);
351   g_main_loop_run (loop);
352   g_main_loop_unref (loop);
353
354  out:
355   g_option_context_free (context);
356
357   return ret;
358 }
359 int
360 main (int argc, char *argv[])
361 {
362   gboolean ret = 1;
363
364   setlocale (LC_ALL, "");
365
366   g_type_init ();
367
368   if (argc < 2)
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);
380   else
381     {
382       g_printerr (_("Unknown command '%s'\n"), argv[1]);
383       ret = usage (&argc, &argv, FALSE);
384     }
385
386   return ret;
387 }