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