GSettings: store (default, options) in gvdb
[platform/upstream/glib.git] / gio / gsettingsschema.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
20 #include "config.h"
21
22 #include "gsettingsschema.h"
23 #include "gvdb/gvdb-reader.h"
24
25 #include <glibintl.h>
26
27 G_DEFINE_TYPE (GSettingsSchema, g_settings_schema, G_TYPE_OBJECT)
28
29 struct _GSettingsSchemaPrivate
30 {
31   const gchar *gettext_domain;
32   const gchar *path;
33   GQuark *items;
34   gint n_items;
35   GvdbTable *table;
36   gchar *name;
37 };
38
39 static GSList *schema_sources;
40
41 static void
42 initialise_schema_sources (void)
43 {
44   static gsize initialised;
45
46   if G_UNLIKELY (g_once_init_enter (&initialised))
47     {
48       const gchar * const *dir;
49       const gchar *path;
50
51       for (dir = g_get_system_data_dirs (); *dir; dir++)
52         {
53           gchar *filename;
54           GvdbTable *table;
55
56           filename = g_build_filename (*dir, "glib-2.0", "schemas", "gschemas.compiled", NULL);
57           table = gvdb_table_new (filename, TRUE, NULL);
58
59           if (table != NULL)
60             schema_sources = g_slist_prepend (schema_sources, table);
61
62           g_free (filename);
63         }
64
65       schema_sources = g_slist_reverse (schema_sources);
66
67       if ((path = g_getenv ("GSETTINGS_SCHEMA_DIR")) != NULL)
68         {
69           gchar *filename;
70           GvdbTable *table;
71
72           filename = g_build_filename (path, "gschemas.compiled", NULL);
73           table = gvdb_table_new (filename, TRUE, NULL);
74
75           if (table != NULL)
76             schema_sources = g_slist_prepend (schema_sources, table);
77
78           g_free (filename);
79         }
80
81       g_once_init_leave (&initialised, TRUE);
82     }
83 }
84
85 static void
86 g_settings_schema_finalize (GObject *object)
87 {
88   GSettingsSchema *schema = G_SETTINGS_SCHEMA (object);
89
90   gvdb_table_unref (schema->priv->table);
91   g_free (schema->priv->items);
92   g_free (schema->priv->name);
93
94   G_OBJECT_CLASS (g_settings_schema_parent_class)
95     ->finalize (object);
96 }
97
98 static void
99 g_settings_schema_init (GSettingsSchema *schema)
100 {
101   schema->priv = G_TYPE_INSTANCE_GET_PRIVATE (schema, G_TYPE_SETTINGS_SCHEMA,
102                                               GSettingsSchemaPrivate);
103 }
104
105 static void
106 g_settings_schema_class_init (GSettingsSchemaClass *class)
107 {
108   GObjectClass *object_class = G_OBJECT_CLASS (class);
109
110   object_class->finalize = g_settings_schema_finalize;
111
112   g_type_class_add_private (class, sizeof (GSettingsSchemaPrivate));
113 }
114
115 const gchar *
116 g_settings_schema_get_string (GSettingsSchema *schema,
117                               const gchar     *key)
118 {
119   const gchar *result = NULL;
120   GVariant *value;
121
122   if ((value = gvdb_table_get_value (schema->priv->table, key)))
123     {
124       result = g_variant_get_string (value, NULL);
125       g_variant_unref (value);
126     }
127
128   return result;
129 }
130
131 GSettingsSchema *
132 g_settings_schema_new (const gchar *name)
133 {
134   GSettingsSchema *schema;
135   GvdbTable *table = NULL;
136   GSList *source;
137
138   initialise_schema_sources ();
139
140   for (source = schema_sources; source; source = source->next)
141     {
142       GvdbTable *file = source->data;
143
144       if ((table = gvdb_table_get_table (file, name)))
145         break;
146     }
147
148   if (table == NULL)
149     g_error ("Settings schema '%s' is not installed\n", name);
150
151   schema = g_object_new (G_TYPE_SETTINGS_SCHEMA, NULL);
152   schema->priv->name = g_strdup (name);
153   schema->priv->table = table;
154   schema->priv->path =
155     g_settings_schema_get_string (schema, ".path");
156   schema->priv->gettext_domain =
157     g_settings_schema_get_string (schema, ".gettext-domain");
158
159   if (schema->priv->gettext_domain)
160     bind_textdomain_codeset (schema->priv->gettext_domain, "UTF-8");
161
162   return schema;
163 }
164
165 GVariant *
166 g_settings_schema_get_value (GSettingsSchema  *schema,
167                              const gchar      *key,
168                              GVariant        **options)
169 {
170   GVariant *variant, *value;
171 #if G_BYTE_ORDER == G_BIG_ENDIAN
172   GVariant *tmp;
173
174   tmp = gvdb_table_get_value (schema->priv->table, key);
175
176   if (tmp)
177     {
178       variant = g_variant_byteswap (tmp);
179       g_variant_unref (tmp);
180     }
181   else
182     variant = NULL;
183 #else
184   variant = gvdb_table_get_value (schema->priv->table, key);
185 #endif
186
187   if (variant == NULL)
188     return NULL;
189
190   value = g_variant_get_child_value (variant, 0);
191   if (options != NULL)
192     *options = g_variant_get_child_value (variant, 1);
193   g_variant_unref (variant);
194
195   return value;
196 }
197
198 const gchar *
199 g_settings_schema_get_path (GSettingsSchema *schema)
200 {
201   return schema->priv->path;
202 }
203
204 const gchar *
205 g_settings_schema_get_gettext_domain (GSettingsSchema *schema)
206 {
207   return schema->priv->gettext_domain;
208 }
209
210 gboolean
211 g_settings_schema_has_key (GSettingsSchema *schema,
212                            const gchar     *key)
213 {
214   return gvdb_table_has_value (schema->priv->table, key);
215 }
216
217 const GQuark *
218 g_settings_schema_list (GSettingsSchema *schema,
219                         gint            *n_items)
220 {
221   gint i, j;
222
223   if (schema->priv->items == NULL)
224     {
225       gchar **list;
226       gint len;
227
228       list = gvdb_table_list (schema->priv->table, "");
229       len = g_strv_length (list);
230
231       schema->priv->items = g_new (GQuark, len);
232       j = 0;
233
234       for (i = 0; i < len; i++)
235         if (list[i][0] != '.')
236           schema->priv->items[j++] = g_quark_from_string (list[i]);
237       schema->priv->n_items = j;
238
239       g_strfreev (list);
240     }
241
242   *n_items = schema->priv->n_items;
243   return schema->priv->items;
244 }