gsettings-tool: Add 'range' subcommand
[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_range (GSettings   *settings,
210                  const gchar *key,
211                  const gchar *value)
212 {
213   GVariant *range, *detail;
214   const gchar *type;
215
216   range = g_settings_get_range (settings, key);
217   g_variant_get (range, "(&sv)", &type, &detail);
218
219   if (strcmp (type, "type") == 0)
220     g_print ("type %s\n", g_variant_get_type_string (detail) + 1);
221
222   else if (strcmp (type, "range") == 0)
223     {
224       GVariant *min, *max;
225       gchar *smin, *smax;
226
227       g_variant_get (detail, "(**)", &min, &max);
228       smin = g_variant_print (min, FALSE);
229       smax = g_variant_print (max, FALSE);
230
231       g_print ("range %s %s %s\n",
232                g_variant_get_type_string (min), smin, smax);
233       g_variant_unref (min);
234       g_variant_unref (max);
235       g_free (smin);
236       g_free (smax);
237     }
238
239   else if (strcmp (type, "enum") == 0 || strcmp (type, "flags") == 0)
240     {
241       GVariantIter iter;
242       GVariant *item;
243
244       g_print ("%s\n", type);
245
246       g_variant_iter_init (&iter, detail);
247       while (g_variant_iter_loop (&iter, "*", &item))
248         {
249           gchar *printed;
250
251           printed = g_variant_print (item, FALSE);
252           g_print ("%s\n", printed);
253           g_free (printed);
254         }
255     }
256
257   g_variant_unref (detail);
258   g_variant_unref (range);
259 }
260
261 static void
262 gsettings_get (GSettings   *settings,
263                const gchar *key,
264                const gchar *value_)
265 {
266   GVariant *value;
267   gchar *printed;
268
269   value = g_settings_get_value (settings, key);
270   printed = g_variant_print (value, TRUE);
271   g_print ("%s\n", printed);
272   g_variant_unref (value);
273   g_free (printed);
274 }
275
276 static void
277 gsettings_reset (GSettings   *settings,
278                  const gchar *key,
279                  const gchar *value)
280 {
281   g_settings_reset (settings, key);
282   g_settings_sync ();
283 }
284
285 static void
286 gsettings_writable (GSettings   *settings,
287                     const gchar *key,
288                     const gchar *value)
289 {
290   g_print ("%s\n",
291            g_settings_is_writable (settings, key) ?
292            "true" : "false");
293 }
294
295 static void
296 value_changed (GSettings   *settings,
297                const gchar *key,
298                gpointer     user_data)
299 {
300   GVariant *value;
301   gchar *printed;
302
303   value = g_settings_get_value (settings, key);
304   printed = g_variant_print (value, TRUE);
305   g_print ("%s: %s\n", key, printed);
306   g_variant_unref (value);
307   g_free (printed);
308 }
309
310 static void
311 gsettings_monitor (GSettings   *settings,
312                    const gchar *key,
313                    const gchar *value)
314 {
315   if (key)
316     {
317       gchar *name;
318
319       name = g_strdup_printf ("changed::%s", key);
320       g_signal_connect (settings, name, G_CALLBACK (value_changed), NULL);
321     }
322   else
323     g_signal_connect (settings, "changed", G_CALLBACK (value_changed), NULL);
324
325   g_main_loop_run (g_main_loop_new (NULL, FALSE));
326 }
327
328 static void
329 gsettings_set (GSettings   *settings,
330                const gchar *key,
331                const gchar *value)
332 {
333   const GVariantType *type;
334   GError *error = NULL;
335   GVariant *existing;
336   GVariant *new;
337
338   existing = g_settings_get_value (settings, key);
339   type = g_variant_get_type (existing);
340
341   new = g_variant_parse (type, value, NULL, NULL, &error);
342
343   if (new == NULL)
344     {
345       g_printerr ("%s\n", error->message);
346       exit (1);
347     }
348
349   if (!g_settings_range_check (settings, key, new))
350     {
351       g_printerr ("The provided value is outside of the valid range\n");
352       g_variant_unref (new);
353       exit (1);
354     }
355
356   g_settings_set_value (settings, key, new);
357   g_variant_unref (existing);
358   g_variant_unref (new);
359
360   g_settings_sync ();
361 }
362
363 static int
364 gsettings_help (gboolean     requested,
365                 const gchar *command)
366 {
367   const gchar *description;
368   const gchar *synopsis;
369   GString *string;
370
371   string = g_string_new (NULL);
372
373   if (command == NULL)
374     ;
375
376   else if (strcmp (command, "list-schemas") == 0)
377     {
378       description = "List the installed (non-relocatable) schemas";
379       synopsis = "";
380     }
381
382   else if (strcmp (command, "list-relocatable-schemas") == 0)
383     {
384       description = "List the installed relocatable schemas";
385       synopsis = "";
386     }
387
388   else if (strcmp (command, "list-keys") == 0)
389     {
390       description = "Lists the keys in SCHEMA";
391       synopsis = "SCHEMA[:PATH]";
392     }
393
394   else if (strcmp (command, "list-children") == 0)
395     {
396       description = "Lists the children of SCHEMA";
397       synopsis = "SCHEMA[:PATH]";
398     }
399
400   else if (strcmp (command, "get") == 0)
401     {
402       description = "Gets the value of KEY";
403       synopsis = "SCHEMA[:PATH] KEY";
404     }
405
406   else if (strcmp (command, "range") == 0)
407     {
408       description = "Queries the range of valid values for KEY";
409       synopsis = "SCHEMA[:PATH] KEY";
410     }
411
412   else if (strcmp (command, "set") == 0)
413     {
414       description = "Sets the value of KEY to VALUE";
415       synopsis = "SCHEMA[:PATH] KEY VALUE";
416     }
417
418   else if (strcmp (command, "reset") == 0)
419     {
420       description = "Resets KEY to its default value";
421       synopsis = "SCHEMA[:PATH] KEY";
422     }
423
424   else if (strcmp (command, "writable") == 0)
425     {
426       description = "Checks if KEY is writable";
427       synopsis = "SCHEMA[:PATH] KEY";
428     }
429
430   else if (strcmp (command, "monitor") == 0)
431     {
432       description = "Monitors KEY for changes.\n"
433                     "If no KEY is specified, monitor all keys in SCHEMA.\n"
434                     "Use ^C to stop monitoring.\n";
435       synopsis = "SCHEMA[:PATH] [KEY]";
436     }
437   else
438     {
439       g_string_printf (string, "Unknown command %s\n\n", command);
440       requested = FALSE;
441       command = NULL;
442     }
443
444   if (command == NULL)
445     {
446       g_string_append (string,
447         "Usage:\n"
448         "  gsettings COMMAND [ARGS...]\n"
449         "\n"
450         "Commands:\n"
451         "  help                      Show this information\n"
452         "  list-schemas              List installed schemas\n"
453         "  list-relocatable-schemas  List relocatable schemas\n"
454         "  list-keys                 List keys in a schema\n"
455         "  list-children             List children of a schema\n"
456         "  range                     Queries the range of a key\n"
457         "  get                       Get the value of a key\n"
458         "  set                       Set the value of a key\n"
459         "  reset                     Reset the value of a key\n"
460         "  writable                  Check if a key is writable\n"
461         "  monitor                   Watch for changes\n"
462         "\n"
463         "Use 'gsettings help COMMAND' to get detailed help.\n\n");
464     }
465   else
466     {
467       g_string_append_printf (string, "Usage:\n  gsettings %s %s\n\n%s\n\n",
468                               command, synopsis, description);
469
470       if (synopsis[0])
471         {
472           g_string_append (string, "Arguments:\n");
473
474           if (strstr (synopsis, "SCHEMA"))
475             g_string_append (string,
476                              "  SCHEMA    The name of the schema\n"
477                              "  PATH      The path, for relocatable schemas\n");
478
479           if (strstr (synopsis, "[KEY]"))
480             g_string_append (string,
481                              "  KEY       The (optional) key within the schema\n");
482
483           else if (strstr (synopsis, "KEY"))
484             g_string_append (string,
485                              "  KEY       The key within the schema\n");
486
487           if (strstr (synopsis, "VALUE"))
488             g_string_append (string,
489                              "  VALUE     The value to set\n");
490
491           g_string_append (string, "\n");
492         }
493     }
494
495   if (requested)
496     g_print ("%s", string->str);
497   else
498     g_printerr ("%s", string->str);
499
500   g_string_free (string, TRUE);
501
502   return requested ? 0 : 1;
503 }
504
505
506 int
507 main (int argc, char **argv)
508 {
509   void (* function) (GSettings *, const gchar *, const gchar *);
510   GSettings *settings;
511   const gchar *key;
512
513   if (argc < 2)
514     return gsettings_help (FALSE, NULL);
515
516   else if (strcmp (argv[1], "help") == 0)
517     return gsettings_help (TRUE, argv[2]);
518
519   else if (argc == 2 && strcmp (argv[1], "list-schemas") == 0)
520     function = gsettings_list_schemas;
521
522   else if (argc == 2 && strcmp (argv[1], "list-relocatable-schemas") == 0)
523     function = gsettings_list_relocatable_schemas;
524
525   else if (argc == 3 && strcmp (argv[1], "list-keys") == 0)
526     function = gsettings_list_keys;
527
528   else if (argc == 3 && strcmp (argv[1], "list-children") == 0)
529     function = gsettings_list_children;
530
531   else if (argc == 4 && strcmp (argv[1], "range") == 0)
532     function = gsettings_range;
533
534   else if (argc == 4 && strcmp (argv[1], "get") == 0)
535     function = gsettings_get;
536
537   else if (argc == 5 && strcmp (argv[1], "set") == 0)
538     function = gsettings_set;
539
540   else if (argc == 4 && strcmp (argv[1], "reset") == 0)
541     function = gsettings_reset;
542
543   else if (argc == 4 && strcmp (argv[1], "writable") == 0)
544     function = gsettings_writable;
545
546   else if ((argc == 3 || argc == 4) && strcmp (argv[1], "monitor") == 0)
547     function = gsettings_monitor;
548
549   else
550     return gsettings_help (FALSE, argv[1]);
551
552   g_type_init ();
553
554   if (argc > 2)
555     {
556       gchar **parts;
557
558       if (argv[2][0] == '\0')
559         {
560           g_printerr ("Empty schema name given");
561           return 1;
562         }
563
564       parts = g_strsplit (argv[2], ":", 2);
565
566       if (parts[1])
567         {
568           if (!check_relocatable_schema (parts[0]) || !check_path (parts[1]))
569             return 1;
570
571           settings = g_settings_new_with_path (parts[0], parts[1]);
572         }
573       else
574         {
575           if (!check_schema (parts[0]))
576             return 1;
577
578           settings = g_settings_new (parts[0]);
579         }
580
581       g_strfreev (parts);
582     }
583   else
584     settings = NULL;
585
586   if (argc > 3)
587     {
588       if (!check_key (settings, argv[3]))
589         return 1;
590
591       key = argv[3];
592     }
593   else
594     key = NULL;
595
596   (* function) (settings, key, argc > 4 ? argv[4] : NULL);
597
598   if (settings != NULL)
599     g_object_unref (settings);
600
601   return 0;
602 }