Merge remote branch 'gvdb/master'
[platform/upstream/glib.git] / gio / gsettings-tool.c
1 /*
2  * Copyright © 2010 Codethink Limited
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 licence, 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: Ryan Lortie <desrt@desrt.ca>
20  */
21
22 #include <gio/gio.h>
23 #include <string.h>
24 #include <stdlib.h>
25
26 static gboolean
27 contained (const gchar * const *items,
28            const gchar         *item)
29 {
30   while (*items)
31     if (strcmp (*items++, item) == 0)
32       return TRUE;
33
34   return FALSE;
35 }
36
37 static gboolean
38 is_schema (const gchar *schema)
39 {
40   return contained (g_settings_list_schemas (), schema);
41 }
42
43 static gboolean
44 is_relocatable_schema (const gchar *schema)
45 {
46   return contained (g_settings_list_relocatable_schemas (), schema);
47 }
48
49 static gboolean
50 check_relocatable_schema (const gchar *schema)
51 {
52   if (is_relocatable_schema (schema))
53     return TRUE;
54
55   if (is_schema (schema))
56     g_printerr ("Schema '%s' is not relocatable "
57                 "(path must not be specified)\n",
58                 schema);
59
60   else
61     g_printerr ("No such schema '%s'\n", schema);
62
63   return FALSE;
64 }
65
66 static gboolean
67 check_schema (const gchar *schema)
68 {
69   if (is_schema (schema))
70     return TRUE;
71
72   if (is_relocatable_schema (schema))
73     g_printerr ("Schema '%s' is relocatable "
74                 "(path must be specified)\n",
75                 schema);
76
77   else
78     g_printerr ("No such schema '%s'\n", schema);
79
80   return FALSE;
81 }
82
83 static gboolean
84 check_path (const gchar *path)
85 {
86   if (path[0] == '\0')
87     {
88       g_printerr ("Empty path given.\n");
89       return FALSE;
90     }
91
92   if (path[0] != '/')
93     {
94       g_printerr ("Path must begin with a slash (/)\n");
95       return FALSE;
96     }
97
98   if (!g_str_has_suffix (path, "/"))
99     {
100       g_printerr ("Path must end with a slash (/)\n");
101       return FALSE;
102     }
103
104   if (strstr (path, "//"))
105     {
106       g_printerr ("Path must not contain two adjacent slashes (//)\n");
107       return FALSE;
108     }
109
110   return TRUE;
111 }
112
113 static gboolean
114 check_key (GSettings   *settings,
115            const gchar *key)
116 {
117   gboolean good;
118   gchar **keys;
119
120   keys = g_settings_list_keys (settings);
121   good = contained ((const gchar **) keys, key);
122   g_strfreev (keys);
123
124   if (good)
125     return TRUE;
126
127   g_printerr ("No such key '%s'\n", key);
128
129   return FALSE;
130 }
131
132 static void
133 output_list (const gchar * const *list)
134 {
135   gint i;
136
137   for (i = 0; list[i]; i++)
138     g_print ("%s\n", list[i]);
139 }
140
141 static void
142 gsettings_list_schemas (GSettings   *settings,
143                         const gchar *key,
144                         const gchar *value)
145 {
146   output_list (g_settings_list_schemas ());
147 }
148
149 static void
150 gsettings_list_relocatable_schemas (GSettings   *settings,
151                                     const gchar *key,
152                                     const gchar *value)
153 {
154   output_list (g_settings_list_relocatable_schemas ());
155 }
156
157 static void
158 gsettings_list_keys (GSettings   *settings,
159                      const gchar *key,
160                      const gchar *value)
161 {
162   gchar **keys;
163
164   keys = g_settings_list_keys (settings);
165   output_list ((const gchar **) keys);
166   g_strfreev (keys);
167 }
168
169 static void
170 gsettings_list_children (GSettings   *settings,
171                          const gchar *key,
172                          const gchar *value)
173 {
174   gchar **children;
175   gint max = 0;
176   gint i;
177
178   children = g_settings_list_children (settings);
179   for (i = 0; children[i]; i++)
180     if (strlen (children[i]) > max)
181       max = strlen (children[i]);
182
183   for (i = 0; children[i]; i++)
184     {
185       GSettings *child;
186       gchar *schema;
187       gchar *path;
188
189       child = g_settings_get_child (settings, children[i]);
190       g_object_get (child,
191                     "schema", &schema,
192                     "path", &path,
193                     NULL);
194
195       if (is_schema (schema))
196         g_print ("%-*s   %s\n", max, children[i], schema);
197       else
198         g_print ("%-*s   %s:%s\n", max, children[i], schema, path);
199
200       g_object_unref (child);
201       g_free (schema);
202       g_free (path);
203     }
204
205   g_strfreev (children);
206 }
207
208 static void
209 gsettings_get (GSettings   *settings,
210                const gchar *key,
211                const gchar *value_)
212 {
213   GVariant *value;
214   gchar *printed;
215
216   value = g_settings_get_value (settings, key);
217   printed = g_variant_print (value, TRUE);
218   g_print ("%s\n", printed);
219   g_variant_unref (value);
220   g_free (printed);
221 }
222
223 static void
224 gsettings_reset (GSettings   *settings,
225                  const gchar *key,
226                  const gchar *value)
227 {
228   g_settings_reset (settings, key);
229   g_settings_sync ();
230 }
231
232 static void
233 gsettings_writable (GSettings   *settings,
234                     const gchar *key,
235                     const gchar *value)
236 {
237   g_print ("%s\n",
238            g_settings_is_writable (settings, key) ?
239            "true" : "false");
240 }
241
242 static void
243 value_changed (GSettings   *settings,
244                const gchar *key,
245                gpointer     user_data)
246 {
247   GVariant *value;
248   gchar *printed;
249
250   value = g_settings_get_value (settings, key);
251   printed = g_variant_print (value, TRUE);
252   g_print ("%s: %s\n", key, printed);
253   g_variant_unref (value);
254   g_free (printed);
255 }
256
257 static void
258 gsettings_monitor (GSettings   *settings,
259                    const gchar *key,
260                    const gchar *value)
261 {
262   if (key)
263     {
264       gchar *name;
265
266       name = g_strdup_printf ("changed::%s", key);
267       g_signal_connect (settings, name, G_CALLBACK (value_changed), NULL);
268     }
269   else
270     g_signal_connect (settings, "changed", G_CALLBACK (value_changed), NULL);
271
272   g_main_loop_run (g_main_loop_new (NULL, FALSE));
273 }
274
275 static void
276 gsettings_set (GSettings   *settings,
277                const gchar *key,
278                const gchar *value)
279 {
280   const GVariantType *type;
281   GError *error = NULL;
282   GVariant *existing;
283   GVariant *new;
284
285   existing = g_settings_get_value (settings, key);
286   type = g_variant_get_type (existing);
287
288   new = g_variant_parse (type, value, NULL, NULL, &error);
289
290   if (new == NULL)
291     {
292       g_printerr ("%s\n", error->message);
293       exit (1);
294     }
295
296   g_settings_set_value (settings, key, new);
297   g_variant_unref (existing);
298   g_variant_unref (new);
299
300   g_settings_sync ();
301 }
302
303 static int
304 gsettings_help (gboolean     requested,
305                 const gchar *command)
306 {
307   const gchar *description;
308   const gchar *synopsis;
309   GString *string;
310
311   string = g_string_new (NULL);
312
313   if (command == NULL)
314     ;
315
316   else if (strcmp (command, "list-schemas") == 0)
317     {
318       description = "List the installed (non-relocatable) schemas";
319       synopsis = "";
320     }
321
322   else if (strcmp (command, "list-relocatable-schemas") == 0)
323     {
324       description = "List the installed relocatable schemas";
325       synopsis = "";
326     }
327
328   else if (strcmp (command, "list-keys") == 0)
329     {
330       description = "Lists the keys in SCHEMA";
331       synopsis = "SCHEMA[:PATH]";
332     }
333
334   else if (strcmp (command, "list-children") == 0)
335     {
336       description = "Lists the children of SCHEMA";
337       synopsis = "SCHEMA[:PATH]";
338     }
339
340   else if (strcmp (command, "get") == 0)
341     {
342       description = "Gets the value of KEY";
343       synopsis = "SCHEMA[:PATH] KEY";
344     }
345
346   else if (strcmp (command, "set") == 0)
347     {
348       description = "Sets the value of KEY to VALUE";
349       synopsis = "SCHEMA[:PATH] KEY VALUE";
350     }
351
352   else if (strcmp (command, "reset") == 0)
353     {
354       description = "Resets KEY to its default value";
355       synopsis = "SCHEMA[:PATH] KEY";
356     }
357
358   else if (strcmp (command, "writable") == 0)
359     {
360       description = "Checks if KEY is writable";
361       synopsis = "SCHEMA[:PATH] KEY";
362     }
363
364   else if (strcmp (command, "monitor") == 0)
365     {
366       description = "Monitors KEY for changes.\n"
367                     "If no KEY is specified, monitor all keys in SCHEMA.\n"
368                     "Use ^C to stop monitoring.\n";
369       synopsis = "SCHEMA[:PATH] [KEY]";
370     }
371   else
372     {
373       g_string_printf (string, "Unknown command %s\n\n", command);
374       requested = FALSE;
375       command = NULL;
376     }
377
378   if (command == NULL)
379     {
380       g_string_append (string,
381         "Usage:\n"
382         "  gsettings COMMAND [ARGS...]\n"
383         "\n"
384         "Commands:\n"
385         "  help                      Show this information\n"
386         "  list-schemas              List installed schemas\n"
387         "  list-relocatable-schemas  List relocatable schemas\n"
388         "  list-keys                 List keys in a schema\n"
389         "  list-children             List children of a schema\n"
390         "  get                       Get the value of a key\n"
391         "  set                       Set the value of a key\n"
392         "  reset                     Reset the value of a key\n"
393         "  writable                  Check if a key is writable\n"
394         "  monitor                   Watch for changes\n"
395         "\n"
396         "Use 'gsettings help COMMAND' to get detailed help.\n\n");
397     }
398   else
399     {
400       g_string_append_printf (string, "Usage:\n  gsettings %s %s\n\n%s\n\n",
401                               command, synopsis, description);
402
403       if (synopsis[0])
404         {
405           g_string_append (string, "Arguments:\n");
406
407           if (strstr (synopsis, "SCHEMA"))
408             g_string_append (string,
409                              "  SCHEMA    The name of the schema\n"
410                              "  PATH      The path, for relocatable schemas\n");
411
412           if (strstr (synopsis, "[KEY]"))
413             g_string_append (string,
414                              "  KEY       The (optional) key within the schema\n");
415
416           else if (strstr (synopsis, "KEY"))
417             g_string_append (string,
418                              "  KEY       The key within the schema\n");
419
420           if (strstr (synopsis, "VALUE"))
421             g_string_append (string,
422                              "  VALUE     The value to set\n");
423
424           g_string_append (string, "\n");
425         }
426     }
427
428   if (requested)
429     g_print ("%s", string->str);
430   else
431     g_printerr ("%s", string->str);
432
433   g_string_free (string, TRUE);
434
435   return requested ? 0 : 1;
436 }
437
438
439 int
440 main (int argc, char **argv)
441 {
442   void (* function) (GSettings *, const gchar *, const gchar *);
443   GSettings *settings;
444   const gchar *key;
445
446   if (argc < 2)
447     return gsettings_help (FALSE, NULL);
448
449   else if (strcmp (argv[1], "help") == 0)
450     return gsettings_help (TRUE, argv[2]);
451
452   else if (argc == 2 && strcmp (argv[1], "list-schemas") == 0)
453     function = gsettings_list_schemas;
454
455   else if (argc == 2 && strcmp (argv[1], "list-relocatable-schemas") == 0)
456     function = gsettings_list_relocatable_schemas;
457
458   else if (argc == 3 && strcmp (argv[1], "list-keys") == 0)
459     function = gsettings_list_keys;
460
461   else if (argc == 3 && strcmp (argv[1], "list-children") == 0)
462     function = gsettings_list_children;
463
464   else if (argc == 4 && strcmp (argv[1], "get") == 0)
465     function = gsettings_get;
466
467   else if (argc == 5 && strcmp (argv[1], "set") == 0)
468     function = gsettings_set;
469
470   else if (argc == 4 && strcmp (argv[1], "reset") == 0)
471     function = gsettings_reset;
472
473   else if (argc == 4 && strcmp (argv[1], "writable") == 0)
474     function = gsettings_writable;
475
476   else if ((argc == 3 || argc == 4) && strcmp (argv[1], "monitor") == 0)
477     function = gsettings_monitor;
478
479   else
480     return gsettings_help (FALSE, argv[1]);
481
482   g_type_init ();
483
484   if (argc > 2)
485     {
486       gchar **parts;
487
488       if (argv[2][0] == '\0')
489         {
490           g_printerr ("Empty schema name given");
491           return 1;
492         }
493
494       parts = g_strsplit (argv[2], ":", 2);
495
496       if (parts[1])
497         {
498           if (!check_relocatable_schema (parts[0]) || !check_path (parts[1]))
499             return 1;
500
501           settings = g_settings_new_with_path (parts[0], parts[1]);
502         }
503       else
504         {
505           if (!check_schema (parts[0]))
506             return 1;
507
508           settings = g_settings_new (parts[0]);
509         }
510
511       g_strfreev (parts);
512     }
513   else
514     settings = NULL;
515
516   if (argc > 3)
517     {
518       if (!check_key (settings, argv[3]))
519         return 1;
520
521       key = argv[3];
522     }
523   else
524     key = NULL;
525
526   (* function) (settings, key, argc > 4 ? argv[4] : NULL);
527
528   if (settings != NULL)
529     g_object_unref (settings);
530
531   return 0;
532 }