Add a minimal commandline utility to poke GSettings
[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 #include <gio.h>
24
25 static void
26 key_changed (GSettings   *settings,
27              const gchar *key)
28 {
29   GVariant *v;
30   gchar *value;
31
32   v = g_settings_get_value (settings, key);
33   value = g_variant_print (v, FALSE);
34   g_print ("%s\n", value);
35   g_free (value);
36   g_variant_unref (v);
37 }
38
39 int
40 main (int argc, char *argv[])
41 {
42   gboolean do_get = FALSE;
43   gboolean do_set = FALSE;
44   gboolean do_writable = FALSE;
45   gboolean do_monitor = FALSE;
46
47   gchar *path = NULL;
48   gchar *schema;
49   gchar *key;
50   gchar *value;
51
52   GSettings *settings;
53   GVariant *v;
54
55   GOptionContext *context;
56   GOptionEntry entries[] = {
57     { "get", 'g', 0, G_OPTION_ARG_NONE, &do_get, "get a key", NULL },
58     { "set", 's', 0, G_OPTION_ARG_NONE, &do_set, "set a key", NULL },
59     { "writable", 'w', 0, G_OPTION_ARG_NONE, &do_writable, "check if key is writable", NULL },
60     { "monitor", 'm', 0, G_OPTION_ARG_NONE, &do_monitor, "monitor key for changes", NULL },
61     { "path", 'p', 0, G_OPTION_ARG_STRING, &path, "path for the schema" },
62     { NULL }
63   };
64   GError *error;
65
66   g_type_init ();
67
68   context = g_option_context_new ("SCHEMA KEY [VALUE]");
69
70   g_option_context_set_summary (context, "Manipulate GSettings configuration");
71
72   g_option_context_add_main_entries (context, entries, NULL);
73
74   error = NULL;
75   if (!g_option_context_parse (context, &argc, &argv, &error))
76     {
77       g_printerr ("%s\n", error->message);
78       return 1;
79     }
80
81   if (do_get + do_set + do_writable + do_monitor != 1)
82     {
83       g_printerr ("You must specify exactly one of --get, --set, --writable or --monitor\n");
84       return 1;
85     }
86
87   if (do_get || do_writable || do_monitor)
88     {
89       if (argc != 3)
90         {
91           g_printerr ("You must specify a schema and a key\n");
92           return 1;
93         }
94
95       schema = argv[1];
96       key = argv[2];
97     }
98   else if (do_set)
99     {
100       if (argc != 4)
101         {
102           g_printerr ("You must specify a schema, a key and a value\n");
103           return 1;
104         }
105
106       schema = argv[1];
107       key = argv[2];
108       value = argv[3];
109     }
110
111   settings = g_settings_new_with_path (schema, path);
112
113   if (do_get)
114     {
115       v = g_settings_get_value (settings, key);
116
117       g_print ("%s\n", g_variant_print (v, FALSE));
118
119       return 0;
120     }
121   else if (do_writable)
122     {
123       gboolean writable;
124
125       if (g_settings_is_writable (settings, key))
126         g_print ("true\n");
127       else
128         g_print ("false\n");
129
130       return 0;
131     }
132   else if (do_set)
133     {
134       const GVariantType *type;
135       GError *error;
136
137       v = g_settings_get_value (settings, key);
138       type = g_variant_get_type (v);
139       g_variant_unref (v);
140
141       error = NULL;
142       v = g_variant_parse (type, value, NULL, NULL, &error);
143       if (v == NULL)
144         {
145           g_printerr ("%s\n", error->message);
146           return 1;
147         }
148
149       if (!g_settings_set_value (settings, key, v))
150         {
151           g_printerr ("Key %s is not writable\n", key);
152           return 1;
153         }
154     }
155   else if (do_monitor)
156     {
157       gchar *detailed_signal;
158       GMainLoop *loop;
159
160       detailed_signal = g_strdup_printf ("changed::%s", key);
161       g_signal_connect (settings, detailed_signal,
162                         G_CALLBACK (key_changed), NULL);
163
164       loop = g_main_loop_new (NULL, FALSE);
165       g_main_loop_run (loop);
166     }
167
168   return 0;
169 }