gsettings-tool: implement range-checking
[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   if (!g_settings_range_check (settings, key, new))
297     {
298       g_printerr ("The provided value is outside of the valid range\n");
299       g_variant_unref (new);
300       exit (1);
301     }
302
303   g_settings_set_value (settings, key, new);
304   g_variant_unref (existing);
305   g_variant_unref (new);
306
307   g_settings_sync ();
308 }
309
310 static int
311 gsettings_help (gboolean     requested,
312                 const gchar *command)
313 {
314   const gchar *description;
315   const gchar *synopsis;
316   GString *string;
317
318   string = g_string_new (NULL);
319
320   if (command == NULL)
321     ;
322
323   else if (strcmp (command, "list-schemas") == 0)
324     {
325       description = "List the installed (non-relocatable) schemas";
326       synopsis = "";
327     }
328
329   else if (strcmp (command, "list-relocatable-schemas") == 0)
330     {
331       description = "List the installed relocatable schemas";
332       synopsis = "";
333     }
334
335   else if (strcmp (command, "list-keys") == 0)
336     {
337       description = "Lists the keys in SCHEMA";
338       synopsis = "SCHEMA[:PATH]";
339     }
340
341   else if (strcmp (command, "list-children") == 0)
342     {
343       description = "Lists the children of SCHEMA";
344       synopsis = "SCHEMA[:PATH]";
345     }
346
347   else if (strcmp (command, "get") == 0)
348     {
349       description = "Gets the value of KEY";
350       synopsis = "SCHEMA[:PATH] KEY";
351     }
352
353   else if (strcmp (command, "set") == 0)
354     {
355       description = "Sets the value of KEY to VALUE";
356       synopsis = "SCHEMA[:PATH] KEY VALUE";
357     }
358
359   else if (strcmp (command, "reset") == 0)
360     {
361       description = "Resets KEY to its default value";
362       synopsis = "SCHEMA[:PATH] KEY";
363     }
364
365   else if (strcmp (command, "writable") == 0)
366     {
367       description = "Checks if KEY is writable";
368       synopsis = "SCHEMA[:PATH] KEY";
369     }
370
371   else if (strcmp (command, "monitor") == 0)
372     {
373       description = "Monitors KEY for changes.\n"
374                     "If no KEY is specified, monitor all keys in SCHEMA.\n"
375                     "Use ^C to stop monitoring.\n";
376       synopsis = "SCHEMA[:PATH] [KEY]";
377     }
378   else
379     {
380       g_string_printf (string, "Unknown command %s\n\n", command);
381       requested = FALSE;
382       command = NULL;
383     }
384
385   if (command == NULL)
386     {
387       g_string_append (string,
388         "Usage:\n"
389         "  gsettings COMMAND [ARGS...]\n"
390         "\n"
391         "Commands:\n"
392         "  help                      Show this information\n"
393         "  list-schemas              List installed schemas\n"
394         "  list-relocatable-schemas  List relocatable schemas\n"
395         "  list-keys                 List keys in a schema\n"
396         "  list-children             List children of a schema\n"
397         "  get                       Get the value of a key\n"
398         "  set                       Set the value of a key\n"
399         "  reset                     Reset the value of a key\n"
400         "  writable                  Check if a key is writable\n"
401         "  monitor                   Watch for changes\n"
402         "\n"
403         "Use 'gsettings help COMMAND' to get detailed help.\n\n");
404     }
405   else
406     {
407       g_string_append_printf (string, "Usage:\n  gsettings %s %s\n\n%s\n\n",
408                               command, synopsis, description);
409
410       if (synopsis[0])
411         {
412           g_string_append (string, "Arguments:\n");
413
414           if (strstr (synopsis, "SCHEMA"))
415             g_string_append (string,
416                              "  SCHEMA    The name of the schema\n"
417                              "  PATH      The path, for relocatable schemas\n");
418
419           if (strstr (synopsis, "[KEY]"))
420             g_string_append (string,
421                              "  KEY       The (optional) key within the schema\n");
422
423           else if (strstr (synopsis, "KEY"))
424             g_string_append (string,
425                              "  KEY       The key within the schema\n");
426
427           if (strstr (synopsis, "VALUE"))
428             g_string_append (string,
429                              "  VALUE     The value to set\n");
430
431           g_string_append (string, "\n");
432         }
433     }
434
435   if (requested)
436     g_print ("%s", string->str);
437   else
438     g_printerr ("%s", string->str);
439
440   g_string_free (string, TRUE);
441
442   return requested ? 0 : 1;
443 }
444
445
446 int
447 main (int argc, char **argv)
448 {
449   void (* function) (GSettings *, const gchar *, const gchar *);
450   GSettings *settings;
451   const gchar *key;
452
453   if (argc < 2)
454     return gsettings_help (FALSE, NULL);
455
456   else if (strcmp (argv[1], "help") == 0)
457     return gsettings_help (TRUE, argv[2]);
458
459   else if (argc == 2 && strcmp (argv[1], "list-schemas") == 0)
460     function = gsettings_list_schemas;
461
462   else if (argc == 2 && strcmp (argv[1], "list-relocatable-schemas") == 0)
463     function = gsettings_list_relocatable_schemas;
464
465   else if (argc == 3 && strcmp (argv[1], "list-keys") == 0)
466     function = gsettings_list_keys;
467
468   else if (argc == 3 && strcmp (argv[1], "list-children") == 0)
469     function = gsettings_list_children;
470
471   else if (argc == 4 && strcmp (argv[1], "get") == 0)
472     function = gsettings_get;
473
474   else if (argc == 5 && strcmp (argv[1], "set") == 0)
475     function = gsettings_set;
476
477   else if (argc == 4 && strcmp (argv[1], "reset") == 0)
478     function = gsettings_reset;
479
480   else if (argc == 4 && strcmp (argv[1], "writable") == 0)
481     function = gsettings_writable;
482
483   else if ((argc == 3 || argc == 4) && strcmp (argv[1], "monitor") == 0)
484     function = gsettings_monitor;
485
486   else
487     return gsettings_help (FALSE, argv[1]);
488
489   g_type_init ();
490
491   if (argc > 2)
492     {
493       gchar **parts;
494
495       if (argv[2][0] == '\0')
496         {
497           g_printerr ("Empty schema name given");
498           return 1;
499         }
500
501       parts = g_strsplit (argv[2], ":", 2);
502
503       if (parts[1])
504         {
505           if (!check_relocatable_schema (parts[0]) || !check_path (parts[1]))
506             return 1;
507
508           settings = g_settings_new_with_path (parts[0], parts[1]);
509         }
510       else
511         {
512           if (!check_schema (parts[0]))
513             return 1;
514
515           settings = g_settings_new (parts[0]);
516         }
517
518       g_strfreev (parts);
519     }
520   else
521     settings = NULL;
522
523   if (argc > 3)
524     {
525       if (!check_key (settings, argv[3]))
526         return 1;
527
528       key = argv[3];
529     }
530   else
531     key = NULL;
532
533   (* function) (settings, key, argc > 4 ? argv[4] : NULL);
534
535   if (settings != NULL)
536     g_object_unref (settings);
537
538   return 0;
539 }