724d67f6ea808573e6c5a84c279a4f33191c3ef9
[platform/upstream/glib.git] / gio / gsettings.c
1 /*
2  * Copyright © 2009, 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 /* Prelude {{{1 */
23 #define _GNU_SOURCE
24 #include "config.h"
25
26 #include <glib.h>
27 #include <glibintl.h>
28 #include <locale.h>
29
30 #include "gsettings.h"
31
32 #include "gdelayedsettingsbackend.h"
33 #include "gsettingsbackendinternal.h"
34 #include "gsettings-mapping.h"
35 #include "gio-marshal.h"
36 #include "gsettingsschema.h"
37
38 #include <string.h>
39
40
41 #include "strinfo.c"
42
43 /**
44  * SECTION:gsettings
45  * @short_description: a high-level API for application settings
46  *
47  * The #GSettings class provides a convenient API for storing and retrieving
48  * application settings.
49  *
50  * When creating a GSettings instance, you have to specify a schema
51  * that describes the keys in your settings and their types and default
52  * values, as well as some other information.
53  *
54  * Normally, a schema has as fixed path that determines where the settings
55  * are stored in the conceptual global tree of settings. However, schemas
56  * can also be 'relocatable', i.e. not equipped with a fixed path. This is
57  * useful e.g. when the schema describes an 'account', and you want to be
58  * able to store a arbitrary number of accounts.
59  *
60  * Unlike other configuration systems (like GConf), GSettings does not
61  * restrict keys to basic types like strings and numbers. GSettings stores
62  * values as #GVariant, and allows any #GVariantType for keys. Key names
63  * are restricted to lowercase characters, numbers and '-'. Furthermore,
64  * the names must begin with a lowercase character, must not end
65  * with a '-', and must not contain consecutive dashes. Key names can
66  * be up to 32 characters long.
67  *
68  * Similar to GConf, the default values in GSettings schemas can be
69  * localized, but the localized values are stored in gettext catalogs
70  * and looked up with the domain that is specified in the
71  * <tag class="attribute">gettext-domain</tag> attribute of the
72  * <tag class="starttag">schemalist</tag> or <tag class="starttag">schema</tag>
73  * elements and the category that is specified in the l10n attribute of the
74  * <tag class="starttag">key</tag> element.
75  *
76  * GSettings uses schemas in a compact binary form that is created
77  * by the <link linkend="glib-compile-schemas">glib-compile-schemas</link>
78  * utility. The input is a schema description in an XML format that can be
79  * described by the following DTD:
80  * |[<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/gschema.dtd"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include>]|
81  *
82  * At runtime, schemas are identified by their id (as specified
83  * in the <tag class="attribute">id</tag> attribute of the
84  * <tag class="starttag">schema</tag> element). The
85  * convention for schema ids is to use a dotted name, similar in
86  * style to a DBus bus name, e.g. "org.gnome.font-rendering".
87  *
88  * In addition to #GVariant types, keys can have types that have enumerated
89  * types. These can be described by a <tag class="starttag">choice</tag>,
90  * <tag class="starttag">enum</tag> or <tag class="starttag">flags</tag> element, see
91  * <xref linkend="schema-enumerated"/>. The underlying type of
92  * such a key is string, but you can use g_settings_get_enum(),
93  * g_settings_set_enum(), g_settings_get_flags(), g_settings_set_flags()
94  * access the numeric values corresponding to the string value of enum
95  * and flags keys.
96  *
97  * <example id="schema-default-values"><title>Default values</title>
98  * <programlisting><![CDATA[
99  * <schemalist>
100  *   <schema id="org.gtk.test" path="/tests/" gettext-domain="test">
101  *
102  *     <key name="greeting" type="s">
103  *       <default l10n="messages">"Hello, earthlings"</default>
104  *       <summary>A greeting</summary>
105  *       <description>
106  *         Greeting of the invading martians
107  *       </description>
108  *     </key>
109  *
110  *     <key name="box" type="(ii)">
111  *       <default>(20,30)</default>
112  *     </key>
113  *
114  *   </schema>
115  * </schemalist>
116  * ]]></programlisting></example>
117  *
118  * <example id="schema-enumerated"><title>Ranges, choices and enumerated types</title>
119  * <programlisting><![CDATA[
120  * <schemalist>
121  *
122  *   <enum id="myenum">
123  *     <value nick="first" value="1"/>
124  *     <value nick="second" value="2"/>
125  *   </enum>
126  *
127  *   <enum id="myflags">
128  *     <value nick="flag1" value="1"/>
129  *     <value nick="flag2" value="2"/>
130  *     <value nick="flag3" value="4"/>
131  *   </enum>
132  *
133  *   <schema id="org.gtk.test">
134  *
135  *     <key name="key-with-range" type="i">
136  *       <range min="1" max="100"/>
137  *       <default>10</default>
138  *     </key>
139  *
140  *     <key name="key-with-choices" type="s">
141  *       <choices>
142  *         <choice value='Elisabeth'/>
143  *         <choice value='Annabeth'/>
144  *         <choice value='Joe'/>
145  *       </choices>
146  *       <aliases>
147  *         <alias value='Anna' target='Annabeth'/>
148  *         <alias value='Beth' target='Elisabeth'/>
149  *       </aliases>
150  *       <default>'Joe'</default>
151  *     </key>
152  *
153  *     <key name='enumerated-key' enum='myenum'>
154  *       <default>'first'</default>
155  *     </key>
156  *
157  *     <key name='flags-key' flags='myflags'>
158  *       <default>["flag1",flag2"]</default>
159  *     </key>
160  *   </schema>
161  * </schemalist>
162  * ]]></programlisting></example>
163  *
164  * <refsect2>
165  *   <title>Vendor overrides</title>
166  *   <para>
167  *     Default values are defined in the schemas that get installed by
168  *     an application. Sometimes, it is necessary for a vendor or distributor
169  *     to adjust these defaults. Since patching the XML source for the schema
170  *     is inconvenient and error-prone,
171  *     <link linkend="glib-compile-schemas">glib-compile-schemas</link> reads
172  *     so-called 'vendor override' files. These are keyfiles in the same
173  *     directory as the XML schema sources which can override default values.
174  *     The schema id serves as the group name in the key file, and the values
175  *     are expected in serialized GVariant form, as in the following example:
176  *     <informalexample><programlisting>
177  *     [org.gtk.Example]
178  *     key1='string'
179  *     key2=1.5
180  *     </programlisting></informalexample>
181  *   </para>
182  * </refsect2>
183  *
184  * <refsect2>
185  *   <title>Binding</title>
186  *   <para>
187  *     A very convenient feature of GSettings lets you bind #GObject properties
188  *     directly to settings, using g_settings_bind(). Once a GObject property
189  *     has been bound to a setting, changes on either side are automatically
190  *     propagated to the other side. GSettings handles details like
191  *     mapping between GObject and GVariant types, and preventing infinite
192  *     cycles.
193  *   </para>
194  *   <para>
195  *     This makes it very easy to hook up a preferences dialog to the
196  *     underlying settings. To make this even more convenient, GSettings
197  *     looks for a boolean property with the name "sensitivity" and
198  *     automatically binds it to the writability of the bound setting.
199  *     If this 'magic' gets in the way, it can be suppressed with the
200  *     #G_SETTINGS_BIND_NO_SENSITIVITY flag.
201  *   </para>
202  * </refsect2>
203  **/
204
205 struct _GSettingsPrivate
206 {
207   /* where the signals go... */
208   GMainContext *main_context;
209
210   GSettingsBackend *backend;
211   GSettingsSchema *schema;
212   gchar *schema_name;
213   gchar *path;
214
215   GDelayedSettingsBackend *delayed;
216 };
217
218 enum
219 {
220   PROP_0,
221   PROP_SCHEMA,
222   PROP_BACKEND,
223   PROP_PATH,
224   PROP_HAS_UNAPPLIED,
225 };
226
227 enum
228 {
229   SIGNAL_WRITABLE_CHANGE_EVENT,
230   SIGNAL_WRITABLE_CHANGED,
231   SIGNAL_CHANGE_EVENT,
232   SIGNAL_CHANGED,
233   N_SIGNALS
234 };
235
236 static guint g_settings_signals[N_SIGNALS];
237
238 G_DEFINE_TYPE (GSettings, g_settings, G_TYPE_OBJECT)
239
240 /* Signals {{{1 */
241 static gboolean
242 g_settings_real_change_event (GSettings    *settings,
243                               const GQuark *keys,
244                               gint          n_keys)
245 {
246   gint i;
247
248   if (keys == NULL)
249     keys = g_settings_schema_list (settings->priv->schema, &n_keys);
250
251   for (i = 0; i < n_keys; i++)
252     g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
253                    keys[i], g_quark_to_string (keys[i]));
254
255   return FALSE;
256 }
257
258 static gboolean
259 g_settings_real_writable_change_event (GSettings *settings,
260                                        GQuark     key)
261 {
262   const GQuark *keys = &key;
263   gint n_keys = 1;
264   gint i;
265
266   if (key == 0)
267     keys = g_settings_schema_list (settings->priv->schema, &n_keys);
268
269   for (i = 0; i < n_keys; i++)
270     g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED],
271                    keys[i], g_quark_to_string (keys[i]));
272
273   return FALSE;
274 }
275
276 static void
277 settings_backend_changed (GObject             *target,
278                           GSettingsBackend    *backend,
279                           const gchar         *key,
280                           gpointer             origin_tag)
281 {
282   GSettings *settings = G_SETTINGS (target);
283   gboolean ignore_this;
284   gint i;
285
286   g_assert (settings->priv->backend == backend);
287
288   for (i = 0; key[i] == settings->priv->path[i]; i++);
289
290   if (settings->priv->path[i] == '\0' &&
291       g_settings_schema_has_key (settings->priv->schema, key + i))
292     {
293       GQuark quark;
294
295       quark = g_quark_from_string (key + i);
296       g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
297                      0, &quark, 1, &ignore_this);
298     }
299 }
300
301 static void
302 settings_backend_path_changed (GObject          *target,
303                                GSettingsBackend *backend,
304                                const gchar      *path,
305                                gpointer          origin_tag)
306 {
307   GSettings *settings = G_SETTINGS (target);
308   gboolean ignore_this;
309
310   g_assert (settings->priv->backend == backend);
311
312   if (g_str_has_prefix (settings->priv->path, path))
313     g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
314                    0, NULL, 0, &ignore_this);
315 }
316
317 static void
318 settings_backend_keys_changed (GObject             *target,
319                                GSettingsBackend    *backend,
320                                const gchar         *path,
321                                const gchar * const *items,
322                                gpointer             origin_tag)
323 {
324   GSettings *settings = G_SETTINGS (target);
325   gboolean ignore_this;
326   gint i;
327
328   g_assert (settings->priv->backend == backend);
329
330   for (i = 0; settings->priv->path[i] &&
331               settings->priv->path[i] == path[i]; i++);
332
333   if (path[i] == '\0')
334     {
335       GQuark quarks[256];
336       gint j, l = 0;
337
338       for (j = 0; items[j]; j++)
339          {
340            const gchar *item = items[j];
341            gint k;
342
343            for (k = 0; item[k] == settings->priv->path[i + k]; k++);
344
345            if (settings->priv->path[i + k] == '\0' &&
346                g_settings_schema_has_key (settings->priv->schema, item + k))
347              quarks[l++] = g_quark_from_string (item + k);
348
349            /* "256 quarks ought to be enough for anybody!"
350             * If this bites you, I'm sorry.  Please file a bug.
351             */
352            g_assert (l < 256);
353          }
354
355       if (l > 0)
356         g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
357                        0, quarks, l, &ignore_this);
358     }
359 }
360
361 static void
362 settings_backend_writable_changed (GObject          *target,
363                                    GSettingsBackend *backend,
364                                    const gchar      *key)
365 {
366   GSettings *settings = G_SETTINGS (target);
367   gboolean ignore_this;
368   gint i;
369
370   g_assert (settings->priv->backend == backend);
371
372   for (i = 0; key[i] == settings->priv->path[i]; i++);
373
374   if (settings->priv->path[i] == '\0' &&
375       g_settings_schema_has_key (settings->priv->schema, key + i))
376     g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
377                    0, g_quark_from_string (key + i), &ignore_this);
378 }
379
380 static void
381 settings_backend_path_writable_changed (GObject          *target,
382                                         GSettingsBackend *backend,
383                                         const gchar      *path)
384 {
385   GSettings *settings = G_SETTINGS (target);
386   gboolean ignore_this;
387
388   g_assert (settings->priv->backend == backend);
389
390   if (g_str_has_prefix (settings->priv->path, path))
391     g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
392                    0, (GQuark) 0, &ignore_this);
393 }
394
395 /* Properties, Construction, Destruction {{{1 */
396 static void
397 g_settings_set_property (GObject      *object,
398                          guint         prop_id,
399                          const GValue *value,
400                          GParamSpec   *pspec)
401 {
402   GSettings *settings = G_SETTINGS (object);
403
404   switch (prop_id)
405     {
406     case PROP_SCHEMA:
407       g_assert (settings->priv->schema_name == NULL);
408       settings->priv->schema_name = g_value_dup_string (value);
409       break;
410
411     case PROP_PATH:
412       settings->priv->path = g_value_dup_string (value);
413       break;
414
415     case PROP_BACKEND:
416       settings->priv->backend = g_value_dup_object (value);
417       break;
418
419     default:
420       g_assert_not_reached ();
421     }
422 }
423
424 static void
425 g_settings_get_property (GObject    *object,
426                          guint       prop_id,
427                          GValue     *value,
428                          GParamSpec *pspec)
429 {
430   GSettings *settings = G_SETTINGS (object);
431
432   switch (prop_id)
433     {
434      case PROP_SCHEMA:
435       g_value_set_string (value, settings->priv->schema_name);
436       break;
437
438      case PROP_PATH:
439       g_value_set_string (value, settings->priv->path);
440       break;
441
442      case PROP_HAS_UNAPPLIED:
443       g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
444       break;
445
446      default:
447       g_assert_not_reached ();
448     }
449 }
450
451 static const GSettingsListenerVTable listener_vtable = {
452   settings_backend_changed,
453   settings_backend_path_changed,
454   settings_backend_keys_changed,
455   settings_backend_writable_changed,
456   settings_backend_path_writable_changed
457 };
458
459 static void
460 g_settings_constructed (GObject *object)
461 {
462   GSettings *settings = G_SETTINGS (object);
463   const gchar *schema_path;
464
465   settings->priv->schema = g_settings_schema_new (settings->priv->schema_name);
466   schema_path = g_settings_schema_get_path (settings->priv->schema);
467
468   if (settings->priv->path && schema_path && strcmp (settings->priv->path, schema_path) != 0)
469     g_error ("settings object created with schema '%s' and path '%s', but "
470              "path '%s' is specified by schema",
471              settings->priv->schema_name, settings->priv->path, schema_path);
472
473   if (settings->priv->path == NULL)
474     {
475       if (schema_path == NULL)
476         g_error ("attempting to create schema '%s' without a path",
477                  settings->priv->schema_name);
478
479       settings->priv->path = g_strdup (schema_path);
480     }
481
482   if (settings->priv->backend == NULL)
483     settings->priv->backend = g_settings_backend_get_default ();
484
485   g_settings_backend_watch (settings->priv->backend,
486                             &listener_vtable, G_OBJECT (settings),
487                             settings->priv->main_context);
488   g_settings_backend_subscribe (settings->priv->backend,
489                                 settings->priv->path);
490 }
491
492 static void
493 g_settings_finalize (GObject *object)
494 {
495   GSettings *settings = G_SETTINGS (object);
496
497   g_settings_backend_unsubscribe (settings->priv->backend,
498                                   settings->priv->path);
499   g_main_context_unref (settings->priv->main_context);
500   g_object_unref (settings->priv->backend);
501   g_object_unref (settings->priv->schema);
502   g_free (settings->priv->schema_name);
503   g_free (settings->priv->path);
504
505   G_OBJECT_CLASS (g_settings_parent_class)->finalize (object);
506 }
507
508 static void
509 g_settings_init (GSettings *settings)
510 {
511   settings->priv = G_TYPE_INSTANCE_GET_PRIVATE (settings,
512                                                 G_TYPE_SETTINGS,
513                                                 GSettingsPrivate);
514
515   settings->priv->main_context = g_main_context_get_thread_default ();
516
517   if (settings->priv->main_context == NULL)
518     settings->priv->main_context = g_main_context_default ();
519
520   g_main_context_ref (settings->priv->main_context);
521 }
522
523 static void
524 g_settings_class_init (GSettingsClass *class)
525 {
526   GObjectClass *object_class = G_OBJECT_CLASS (class);
527
528   class->writable_change_event = g_settings_real_writable_change_event;
529   class->change_event = g_settings_real_change_event;
530
531   object_class->set_property = g_settings_set_property;
532   object_class->get_property = g_settings_get_property;
533   object_class->constructed = g_settings_constructed;
534   object_class->finalize = g_settings_finalize;
535
536   g_type_class_add_private (object_class, sizeof (GSettingsPrivate));
537
538   /**
539    * GSettings::changed:
540    * @settings: the object on which the signal was emitted
541    * @key: the name of the key that changed
542    *
543    * The "changed" signal is emitted when a key has potentially changed.
544    * You should call one of the g_settings_get() calls to check the new
545    * value.
546    *
547    * This signal supports detailed connections.  You can connect to the
548    * detailed signal "changed::x" in order to only receive callbacks
549    * when key "x" changes.
550    */
551   g_settings_signals[SIGNAL_CHANGED] =
552     g_signal_new ("changed", G_TYPE_SETTINGS,
553                   G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
554                   G_STRUCT_OFFSET (GSettingsClass, changed),
555                   NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
556                   1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
557
558   /**
559    * GSettings::change-event:
560    * @settings: the object on which the signal was emitted
561    * @keys: an array of #GQuark<!-- -->s for the changed keys, or %NULL
562    * @n_keys: the length of the @keys array, or 0
563    * @returns: %TRUE to stop other handlers from being invoked for the
564    *           event. FALSE to propagate the event further.
565    *
566    * The "change-event" signal is emitted once per change event that
567    * affects this settings object.  You should connect to this signal
568    * only if you are interested in viewing groups of changes before they
569    * are split out into multiple emissions of the "changed" signal.
570    * For most use cases it is more appropriate to use the "changed" signal.
571    *
572    * In the event that the change event applies to one or more specified
573    * keys, @keys will be an array of #GQuark of length @n_keys.  In the
574    * event that the change event applies to the #GSettings object as a
575    * whole (ie: potentially every key has been changed) then @keys will
576    * be %NULL and @n_keys will be 0.
577    *
578    * The default handler for this signal invokes the "changed" signal
579    * for each affected key.  If any other connected handler returns
580    * %TRUE then this default functionality will be supressed.
581    */
582   g_settings_signals[SIGNAL_CHANGE_EVENT] =
583     g_signal_new ("change-event", G_TYPE_SETTINGS,
584                   G_SIGNAL_RUN_LAST,
585                   G_STRUCT_OFFSET (GSettingsClass, change_event),
586                   g_signal_accumulator_true_handled, NULL,
587                   _gio_marshal_BOOL__POINTER_INT,
588                   G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);
589
590   /**
591    * GSettings::writable-changed:
592    * @settings: the object on which the signal was emitted
593    * @key: the key
594    *
595    * The "writable-changed" signal is emitted when the writability of a
596    * key has potentially changed.  You should call
597    * g_settings_is_writable() in order to determine the new status.
598    *
599    * This signal supports detailed connections.  You can connect to the
600    * detailed signal "writable-changed::x" in order to only receive
601    * callbacks when the writability of "x" changes.
602    */
603   g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
604     g_signal_new ("writable-changed", G_TYPE_SETTINGS,
605                   G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
606                   G_STRUCT_OFFSET (GSettingsClass, changed),
607                   NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
608                   1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
609
610   /**
611    * GSettings::writable-change-event:
612    * @settings: the object on which the signal was emitted
613    * @key: the quark of the key, or 0
614    * @returns: %TRUE to stop other handlers from being invoked for the
615    *           event. FALSE to propagate the event further.
616    *
617    * The "writable-change-event" signal is emitted once per writability
618    * change event that affects this settings object.  You should connect
619    * to this signal if you are interested in viewing groups of changes
620    * before they are split out into multiple emissions of the
621    * "writable-changed" signal.  For most use cases it is more
622    * appropriate to use the "writable-changed" signal.
623    *
624    * In the event that the writability change applies only to a single
625    * key, @key will be set to the #GQuark for that key.  In the event
626    * that the writability change affects the entire settings object,
627    * @key will be 0.
628    *
629    * The default handler for this signal invokes the "writable-changed"
630    * and "changed" signals for each affected key.  This is done because
631    * changes in writability might also imply changes in value (if for
632    * example, a new mandatory setting is introduced).  If any other
633    * connected handler returns %TRUE then this default functionality
634    * will be supressed.
635    */
636   g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
637     g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
638                   G_SIGNAL_RUN_LAST,
639                   G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
640                   g_signal_accumulator_true_handled, NULL,
641                   _gio_marshal_BOOLEAN__UINT, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
642
643   /**
644    * GSettings:context:
645    *
646    * The name of the context that the settings are stored in.
647    */
648   g_object_class_install_property (object_class, PROP_BACKEND,
649     g_param_spec_object ("backend",
650                          P_("GSettingsBackend"),
651                          P_("The GSettingsBackend for this settings object"),
652                          G_TYPE_SETTINGS_BACKEND, G_PARAM_CONSTRUCT_ONLY |
653                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
654
655   /**
656    * GSettings:schema:
657    *
658    * The name of the schema that describes the types of keys
659    * for this #GSettings object.
660    */
661   g_object_class_install_property (object_class, PROP_SCHEMA,
662     g_param_spec_string ("schema",
663                          P_("Schema name"),
664                          P_("The name of the schema for this settings object"),
665                          NULL,
666                          G_PARAM_CONSTRUCT_ONLY |
667                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
668
669    /**
670     * GSettings:path:
671     *
672     * The path within the backend where the settings are stored.
673     */
674    g_object_class_install_property (object_class, PROP_PATH,
675      g_param_spec_string ("path",
676                           P_("Base path"),
677                           P_("The path within the backend where the settings are"),
678                           NULL,
679                           G_PARAM_CONSTRUCT_ONLY |
680                           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
681
682    /**
683     * GSettings:has-unapplied:
684     *
685     * If this property is %TRUE, the #GSettings object has outstanding
686     * changes that will be applied when g_settings_apply() is called.
687     */
688    g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
689      g_param_spec_boolean ("has-unapplied",
690                            P_("Has unapplied changes"),
691                            P_("TRUE if there are outstanding changes to apply()"),
692                            FALSE,
693                            G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
694
695 }
696
697 /* Construction (new, new_with_path, etc.) {{{1 */
698 /**
699  * g_settings_new:
700  * @schema: the name of the schema
701  * @returns: a new #GSettings object
702  *
703  * Creates a new #GSettings object with a given schema.
704  *
705  * Signals on the newly created #GSettings object will be dispatched
706  * via the thread-default #GMainContext in effect at the time of the
707  * call to g_settings_new().  The new #GSettings will hold a reference
708  * on the context.  See g_main_context_push_thread_default().
709  *
710  * Since: 2.26
711  */
712 GSettings *
713 g_settings_new (const gchar *schema)
714 {
715   g_return_val_if_fail (schema != NULL, NULL);
716
717   return g_object_new (G_TYPE_SETTINGS,
718                        "schema", schema,
719                        NULL);
720 }
721
722 /**
723  * g_settings_new_with_path:
724  * @schema: the name of the schema
725  * @path: the path to use
726  * @returns: a new #GSettings object
727  *
728  * Creates a new #GSettings object with a given schema and path.
729  *
730  * You only need to do this if you want to directly create a settings
731  * object with a schema that doesn't have a specified path of its own.
732  * That's quite rare.
733  *
734  * It is a programmer error to call this function for a schema that
735  * has an explicitly specified path.
736  *
737  * Since: 2.26
738  */
739 GSettings *
740 g_settings_new_with_path (const gchar *schema,
741                           const gchar *path)
742 {
743   g_return_val_if_fail (schema != NULL, NULL);
744   g_return_val_if_fail (path != NULL, NULL);
745
746   return g_object_new (G_TYPE_SETTINGS,
747                        "schema", schema,
748                        "path", path,
749                        NULL);
750 }
751
752 /**
753  * g_settings_new_with_backend:
754  * @schema: the name of the schema
755  * @backend: the #GSettingsBackend to use
756  * @returns: a new #GSettings object
757  *
758  * Creates a new #GSettings object with a given schema and backend.
759  *
760  * Creating settings objects with an different backend allows accessing settings
761  * from a database other than the usual one.  For example, it may make
762  * sense to pass a backend corresponding to the "defaults" settings database on
763  * the system to get a settings object that modifies the system default
764  * settings instead of the settings for this user.
765  *
766  * Since: 2.26
767  */
768 GSettings *
769 g_settings_new_with_backend (const gchar      *schema,
770                              GSettingsBackend *backend)
771 {
772   g_return_val_if_fail (schema != NULL, NULL);
773   g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
774
775   return g_object_new (G_TYPE_SETTINGS,
776                        "schema", schema,
777                        "backend", backend,
778                        NULL);
779 }
780
781 /**
782  * g_settings_new_with_backend_and_path:
783  * @schema: the name of the schema
784  * @backend: the #GSettingsBackend to use
785  * @path: the path to use
786  * @returns: a new #GSettings object
787  *
788  * Creates a new #GSettings object with a given schema, backend and
789  * path.
790  *
791  * This is a mix of g_settings_new_with_backend() and
792  * g_settings_new_with_path().
793  *
794  * Since: 2.26
795  */
796 GSettings *
797 g_settings_new_with_backend_and_path (const gchar      *schema,
798                                       GSettingsBackend *backend,
799                                       const gchar      *path)
800 {
801   g_return_val_if_fail (schema != NULL, NULL);
802   g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
803   g_return_val_if_fail (path != NULL, NULL);
804
805   return g_object_new (G_TYPE_SETTINGS,
806                        "schema", schema,
807                        "backend", backend,
808                        "path", path,
809                        NULL);
810 }
811
812 /* Internal read/write utilities, enum/flags conversion, validation {{{1 */
813 typedef struct
814 {
815   GSettings *settings;
816   const gchar *key;
817
818   GSettingsSchema *schema;
819
820   guint is_flags : 1;
821   guint is_enum  : 1;
822
823   const guint32 *strinfo;
824   gsize strinfo_length;
825
826   const gchar *unparsed;
827   gchar lc_char;
828
829   const GVariantType *type;
830   GVariant *minimum, *maximum;
831   GVariant *default_value;
832 } GSettingsKeyInfo;
833
834 static inline void
835 endian_fixup (GVariant **value)
836 {
837 #if G_BYTE_ORDER == G_BIG_ENDIAN
838   GVariant *tmp;
839
840   tmp = g_variant_byteswap (*value);
841   g_variant_unref (*value);
842   *value = tmp;
843 #endif
844 }
845
846 static void
847 g_settings_get_key_info (GSettingsKeyInfo *info,
848                          GSettings        *settings,
849                          const gchar      *key)
850 {
851   GVariantIter *iter;
852   GVariant *data;
853   guchar code;
854
855   memset (info, 0, sizeof *info);
856
857   iter = g_settings_schema_get_value (settings->priv->schema, key);
858
859   info->default_value = g_variant_iter_next_value (iter);
860   endian_fixup (&info->default_value);
861   info->type = g_variant_get_type (info->default_value);
862   info->settings = g_object_ref (settings);
863   info->key = g_intern_string (key);
864
865   while (g_variant_iter_next (iter, "(y*)", &code, &data))
866     {
867       switch (code)
868         {
869         case 'l':
870           /* translation requested */
871           g_variant_get (data, "(y&s)", &info->lc_char, &info->unparsed);
872           break;
873
874         case 'e':
875           /* enumerated types... */
876           info->is_enum = TRUE;
877           goto choice;
878
879         case 'f':
880           /* flags... */
881           info->is_flags = TRUE;
882           goto choice;
883
884         choice: case 'c':
885           /* ..., choices, aliases */
886           info->strinfo = g_variant_get_fixed_array (data,
887                                                      &info->strinfo_length,
888                                                      sizeof (guint32));
889           break;
890
891         case 'r':
892           g_variant_get (data, "(**)", &info->minimum, &info->maximum);
893           endian_fixup (&info->minimum);
894           endian_fixup (&info->maximum);
895           break;
896
897         default:
898           g_warning ("unknown schema extension '%c'", code);
899           break;
900         }
901
902       g_variant_unref (data);
903     }
904
905   g_variant_iter_free (iter);
906 }
907
908 static void
909 g_settings_free_key_info (GSettingsKeyInfo *info)
910 {
911   if (info->minimum)
912     g_variant_unref (info->minimum);
913
914   if (info->maximum)
915     g_variant_unref (info->maximum);
916
917   g_variant_unref (info->default_value);
918   g_object_unref (info->settings);
919 }
920
921 static gboolean
922 g_settings_write_to_backend (GSettingsKeyInfo *info,
923                              GVariant         *value)
924 {
925   gboolean success;
926   gchar *path;
927
928   path = g_strconcat (info->settings->priv->path, info->key, NULL);
929   success = g_settings_backend_write (info->settings->priv->backend,
930                                       path, value, NULL);
931   g_free (path);
932
933   return success;
934 }
935
936 static gboolean
937 g_settings_type_check (GSettingsKeyInfo *info,
938                        GVariant         *value)
939 {
940   g_return_val_if_fail (value != NULL, FALSE);
941
942   return g_variant_is_of_type (value, info->type);
943 }
944
945 static gboolean
946 g_settings_key_info_range_check (GSettingsKeyInfo *info,
947                                  GVariant         *value)
948 {
949   if (info->minimum == NULL && info->strinfo == NULL)
950     return TRUE;
951
952   if (g_variant_is_container (value))
953     {
954       gboolean ok = TRUE;
955       GVariantIter iter;
956       GVariant *child;
957
958       g_variant_iter_init (&iter, value);
959       while (ok && (child = g_variant_iter_next_value (&iter)))
960         {
961           ok = g_settings_key_info_range_check (info, child);
962           g_variant_unref (child);
963         }
964
965       return ok;
966     }
967
968   if (info->minimum)
969     {
970       return g_variant_compare (info->minimum, value) <= 0 &&
971              g_variant_compare (value, info->maximum) <= 0;
972     }
973
974   return strinfo_is_string_valid (info->strinfo,
975                                   info->strinfo_length,
976                                   g_variant_get_string (value, NULL));
977 }
978
979 static GVariant *
980 g_settings_range_fixup (GSettingsKeyInfo *info,
981                         GVariant         *value)
982 {
983   const gchar *target;
984
985   if (g_settings_key_info_range_check (info, value))
986     return g_variant_ref (value);
987
988   if (info->strinfo == NULL)
989     return NULL;
990
991   if (g_variant_is_container (value))
992     {
993       GVariantBuilder builder;
994       GVariantIter iter;
995       GVariant *child;
996
997       g_variant_iter_init (&iter, value);
998       g_variant_builder_init (&builder, g_variant_get_type (value));
999
1000       while ((child = g_variant_iter_next_value (&iter)))
1001         {
1002           GVariant *fixed;
1003
1004           fixed = g_settings_range_fixup (info, child);
1005           g_variant_unref (child);
1006
1007           if (fixed == NULL)
1008             {
1009               g_variant_builder_clear (&builder);
1010               return NULL;
1011             }
1012
1013           g_variant_builder_add_value (&builder, fixed);
1014           g_variant_unref (fixed);
1015         }
1016
1017       return g_variant_ref_sink (g_variant_builder_end (&builder));
1018     }
1019
1020   target = strinfo_string_from_alias (info->strinfo, info->strinfo_length,
1021                                       g_variant_get_string (value, NULL));
1022   return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
1023 }
1024
1025 static GVariant *
1026 g_settings_read_from_backend (GSettingsKeyInfo *info)
1027 {
1028   GVariant *value;
1029   GVariant *fixup;
1030   gchar *path;
1031
1032   path = g_strconcat (info->settings->priv->path, info->key, NULL);
1033   value = g_settings_backend_read (info->settings->priv->backend,
1034                                    path, info->type, FALSE);
1035   g_free (path);
1036
1037   if (value != NULL)
1038     {
1039       fixup = g_settings_range_fixup (info, value);
1040       g_variant_unref (value);
1041     }
1042   else
1043     fixup = NULL;
1044
1045   return fixup;
1046 }
1047
1048 static GVariant *
1049 g_settings_get_translated_default (GSettingsKeyInfo *info)
1050 {
1051   const gchar *translated;
1052   GError *error = NULL;
1053   const gchar *domain;
1054   GVariant *value;
1055
1056   if (info->lc_char == '\0')
1057     /* translation not requested for this key */
1058     return NULL;
1059
1060   domain = g_settings_schema_get_gettext_domain (info->settings->priv->schema);
1061
1062   if (info->lc_char == 't')
1063     translated = g_dcgettext (domain, info->unparsed, LC_TIME);
1064   else
1065     translated = g_dgettext (domain, info->unparsed);
1066
1067   if (translated == info->unparsed)
1068     /* the default value was not translated */
1069     return NULL;
1070
1071   /* try to parse the translation of the unparsed default */
1072   value = g_variant_parse (info->type, translated, NULL, NULL, &error);
1073
1074   if (value == NULL)
1075     {
1076       g_warning ("Failed to parse translated string `%s' for "
1077                  "key `%s' in schema `%s': %s", info->unparsed, info->key,
1078                  info->settings->priv->schema_name, error->message);
1079       g_warning ("Using untranslated default instead.");
1080       g_error_free (error);
1081     }
1082
1083   else if (!g_settings_key_info_range_check (info, value))
1084     {
1085       g_warning ("Translated default `%s' for key `%s' in schema `%s' "
1086                  "is outside of valid range", info->unparsed, info->key,
1087                  info->settings->priv->schema_name);
1088       g_variant_unref (value);
1089       value = NULL;
1090     }
1091
1092   return value;
1093 }
1094
1095 static gint
1096 g_settings_to_enum (GSettingsKeyInfo *info,
1097                     GVariant         *value)
1098 {
1099   gboolean it_worked;
1100   guint result;
1101
1102   it_worked = strinfo_enum_from_string (info->strinfo, info->strinfo_length,
1103                                         g_variant_get_string (value, NULL),
1104                                         &result);
1105
1106   /* 'value' can only come from the backend after being filtered for validity,
1107    * from the translation after being filtered for validity, or from the schema
1108    * itself (which the schema compiler checks for validity).  If this assertion
1109    * fails then it's really a bug in GSettings or the schema compiler...
1110    */
1111   g_assert (it_worked);
1112
1113   return result;
1114 }
1115
1116 static GVariant *
1117 g_settings_from_enum (GSettingsKeyInfo *info,
1118                       gint              value)
1119 {
1120   const gchar *string;
1121
1122   string = strinfo_string_from_enum (info->strinfo,
1123                                      info->strinfo_length,
1124                                      value);
1125
1126   if (string == NULL)
1127     return NULL;
1128
1129   return g_variant_new_string (string);
1130 }
1131
1132 static guint
1133 g_settings_to_flags (GSettingsKeyInfo *info,
1134                      GVariant         *value)
1135 {
1136   GVariantIter iter;
1137   const gchar *flag;
1138   guint result;
1139
1140   result = 0;
1141   g_variant_iter_init (&iter, value);
1142   while (g_variant_iter_next (&iter, "&s", &flag))
1143     {
1144       gboolean it_worked;
1145       guint flag_value;
1146
1147       it_worked = strinfo_enum_from_string (info->strinfo,
1148                                             info->strinfo_length,
1149                                             flag, &flag_value);
1150       /* as in g_settings_to_enum() */
1151       g_assert (it_worked);
1152
1153       result |= flag_value;
1154     }
1155
1156   return result;
1157 }
1158
1159 static GVariant *
1160 g_settings_from_flags (GSettingsKeyInfo *info,
1161                        guint             value)
1162 {
1163   GVariantBuilder builder;
1164   gint i;
1165
1166   g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
1167
1168   for (i = 0; i < 32; i++)
1169     if (value & (1u << i))
1170       {
1171         const gchar *string;
1172
1173         string = strinfo_string_from_enum (info->strinfo,
1174                                            info->strinfo_length,
1175                                            1u << i);
1176
1177         if (string == NULL)
1178           {
1179             g_variant_builder_clear (&builder);
1180             return NULL;
1181           }
1182
1183         g_variant_builder_add (&builder, "s", string);
1184       }
1185
1186   return g_variant_builder_end (&builder);
1187 }
1188
1189 /* Public Get/Set API {{{1 (get, get_value, set, set_value, get_mapped) */
1190 /**
1191  * g_settings_get_value:
1192  * @settings: a #GSettings object
1193  * @key: the key to get the value for
1194  * @returns: a new #GVariant
1195  *
1196  * Gets the value that is stored in @settings for @key.
1197  *
1198  * It is a programmer error to give a @key that isn't contained in the
1199  * schema for @settings.
1200  *
1201  * Since: 2.26
1202  */
1203 GVariant *
1204 g_settings_get_value (GSettings   *settings,
1205                       const gchar *key)
1206 {
1207   GSettingsKeyInfo info;
1208   GVariant *value;
1209
1210   g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1211   g_return_val_if_fail (key != NULL, NULL);
1212
1213   g_settings_get_key_info (&info, settings, key);
1214   value = g_settings_read_from_backend (&info);
1215
1216   if (value == NULL)
1217     value = g_settings_get_translated_default (&info);
1218
1219   if (value == NULL)
1220     value = g_variant_ref (info.default_value);
1221
1222   g_settings_free_key_info (&info);
1223
1224   return value;
1225 }
1226
1227 /**
1228  * g_settings_get_enum:
1229  * @settings: a #GSettings object
1230  * @key: the key to get the value for
1231  * @returns: the enum value
1232  *
1233  * Gets the value that is stored in @settings for @key and converts it
1234  * to the enum value that it represents.
1235  *
1236  * In order to use this function the type of the value must be a string
1237  * and it must be marked in the schema file as an enumerated type.
1238  *
1239  * It is a programmer error to give a @key that isn't contained in the
1240  * schema for @settings or is not marked as an enumerated type.
1241  *
1242  * If the value stored in the configuration database is not a valid
1243  * value for the enumerated type then this function will return the
1244  * default value.
1245  *
1246  * Since: 2.26
1247  **/
1248 gint
1249 g_settings_get_enum (GSettings   *settings,
1250                      const gchar *key)
1251 {
1252   GSettingsKeyInfo info;
1253   GVariant *value;
1254   gint result;
1255
1256   g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1257   g_return_val_if_fail (key != NULL, -1);
1258
1259   g_settings_get_key_info (&info, settings, key);
1260
1261   if (!info.is_enum)
1262     {
1263       g_critical ("g_settings_get_enum() called on key `%s' which is not "
1264                   "associated with an enumerated type", info.key);
1265       g_settings_free_key_info (&info);
1266       return -1;
1267     }
1268
1269   value = g_settings_read_from_backend (&info);
1270
1271   if (value == NULL)
1272     value = g_settings_get_translated_default (&info);
1273
1274   if (value == NULL)
1275     value = g_variant_ref (info.default_value);
1276
1277   result = g_settings_to_enum (&info, value);
1278   g_settings_free_key_info (&info);
1279   g_variant_unref (value);
1280
1281   return result;
1282 }
1283
1284 /**
1285  * g_settings_set_enum:
1286  * @settings: a #GSettings object
1287  * @key: a key, within @settings
1288  * @value: an enumerated value
1289  * @returns: %TRUE, if the set succeeds
1290  *
1291  * Looks up the enumerated type nick for @value and writes it to @key,
1292  * within @settings.
1293  *
1294  * It is a programmer error to give a @key that isn't contained in the
1295  * schema for @settings or is not marked as an enumerated type, or for
1296  * @value not to be a valid value for the named type.
1297  *
1298  * After performing the write, accessing @key directly with
1299  * g_settings_get_string() will return the 'nick' associated with
1300  * @value.
1301  **/
1302 gboolean
1303 g_settings_set_enum (GSettings   *settings,
1304                      const gchar *key,
1305                      gint         value)
1306 {
1307   GSettingsKeyInfo info;
1308   GVariant *variant;
1309   gboolean success;
1310
1311   g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1312   g_return_val_if_fail (key != NULL, FALSE);
1313
1314   g_settings_get_key_info (&info, settings, key);
1315
1316   if (!info.is_enum)
1317     {
1318       g_critical ("g_settings_set_enum() called on key `%s' which is not "
1319                   "associated with an enumerated type", info.key);
1320       return FALSE;
1321     }
1322
1323   if (!(variant = g_settings_from_enum (&info, value)))
1324     {
1325       g_critical ("g_settings_set_enum(): invalid enum value %d for key `%s' "
1326                   "in schema `%s'.  Doing nothing.", value, info.key,
1327                   info.settings->priv->schema_name);
1328       g_settings_free_key_info (&info);
1329       return FALSE;
1330     }
1331
1332   success = g_settings_write_to_backend (&info, variant);
1333   g_settings_free_key_info (&info);
1334
1335   return success;
1336 }
1337
1338 /**
1339  * g_settings_get_flags:
1340  * @settings: a #GSettings object
1341  * @key: the key to get the value for
1342  * @returns: the flags value
1343  *
1344  * Gets the value that is stored in @settings for @key and converts it
1345  * to the flags value that it represents.
1346  *
1347  * In order to use this function the type of the value must be an array
1348  * of strings and it must be marked in the schema file as an flags type.
1349  *
1350  * It is a programmer error to give a @key that isn't contained in the
1351  * schema for @settings or is not marked as a flags type.
1352  *
1353  * If the value stored in the configuration database is not a valid
1354  * value for the flags type then this function will return the default
1355  * value.
1356  *
1357  * Since: 2.26
1358  **/
1359 guint
1360 g_settings_get_flags (GSettings   *settings,
1361                       const gchar *key)
1362 {
1363   GSettingsKeyInfo info;
1364   GVariant *value;
1365   guint result;
1366
1367   g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1368   g_return_val_if_fail (key != NULL, -1);
1369
1370   g_settings_get_key_info (&info, settings, key);
1371
1372   if (!info.is_flags)
1373     {
1374       g_critical ("g_settings_get_flags() called on key `%s' which is not "
1375                   "associated with a flags type", info.key);
1376       g_settings_free_key_info (&info);
1377       return -1;
1378     }
1379
1380   value = g_settings_read_from_backend (&info);
1381
1382   if (value == NULL)
1383     value = g_settings_get_translated_default (&info);
1384
1385   if (value == NULL)
1386     value = g_variant_ref (info.default_value);
1387
1388   result = g_settings_to_flags (&info, value);
1389   g_settings_free_key_info (&info);
1390   g_variant_unref (value);
1391
1392   return result;
1393 }
1394
1395 /**
1396  * g_settings_set_flags:
1397  * @settings: a #GSettings object
1398  * @key: a key, within @settings
1399  * @value: a flags value
1400  * @returns: %TRUE, if the set succeeds
1401  *
1402  * Looks up the flags type nicks for the bits specified by @value, puts
1403  * them in an array of strings and writes the array to @key, withing
1404  * @settings.
1405  *
1406  * It is a programmer error to give a @key that isn't contained in the
1407  * schema for @settings or is not marked as a flags type, or for @value
1408  * to contain any bits that are not value for the named type.
1409  *
1410  * After performing the write, accessing @key directly with
1411  * g_settings_get_strv() will return an array of 'nicks'; one for each
1412  * bit in @value.
1413  **/
1414 gboolean
1415 g_settings_set_flags (GSettings   *settings,
1416                       const gchar *key,
1417                       guint        value)
1418 {
1419   GSettingsKeyInfo info;
1420   GVariant *variant;
1421   gboolean success;
1422
1423   g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1424   g_return_val_if_fail (key != NULL, FALSE);
1425
1426   g_settings_get_key_info (&info, settings, key);
1427
1428   if (!info.is_flags)
1429     {
1430       g_critical ("g_settings_set_flags() called on key `%s' which is not "
1431                   "associated with a flags type", info.key);
1432       return FALSE;
1433     }
1434
1435   if (!(variant = g_settings_from_flags (&info, value)))
1436     {
1437       g_critical ("g_settings_set_flags(): invalid flags value 0x%08x "
1438                   "for key `%s' in schema `%s'.  Doing nothing.",
1439                   value, info.key, info.settings->priv->schema_name);
1440       g_settings_free_key_info (&info);
1441       return FALSE;
1442     }
1443
1444   success = g_settings_write_to_backend (&info, variant);
1445   g_settings_free_key_info (&info);
1446
1447   return success;
1448 }
1449
1450 /**
1451  * g_settings_set_value:
1452  * @settings: a #GSettings object
1453  * @key: the name of the key to set
1454  * @value: a #GVariant of the correct type
1455  * @returns: %TRUE if setting the key succeeded,
1456  *     %FALSE if the key was not writable
1457  *
1458  * Sets @key in @settings to @value.
1459  *
1460  * It is a programmer error to give a @key that isn't contained in the
1461  * schema for @settings or for @value to have the incorrect type, per
1462  * the schema.
1463  *
1464  * If @value is floating then this function consumes the reference.
1465  *
1466  * Since: 2.26
1467  **/
1468 gboolean
1469 g_settings_set_value (GSettings   *settings,
1470                       const gchar *key,
1471                       GVariant    *value)
1472 {
1473   GSettingsKeyInfo info;
1474
1475   g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1476   g_return_val_if_fail (key != NULL, FALSE);
1477
1478   g_settings_get_key_info (&info, settings, key);
1479
1480   if (!g_settings_type_check (&info, value))
1481     {
1482       g_critical ("g_settings_set_value: key '%s' in '%s' expects type '%s', but a GVariant of type '%s' was given",
1483                   key,
1484                   settings->priv->schema_name,
1485                   g_variant_type_peek_string (info.type),
1486                   g_variant_get_type_string (value));
1487
1488         return FALSE;
1489       }
1490
1491   if (!g_settings_key_info_range_check (&info, value))
1492     {
1493       g_warning ("g_settings_set_value: value for key '%s' in schema '%s' "
1494                  "is outside of valid range",
1495                  key,
1496                  settings->priv->schema_name);
1497
1498         return FALSE;
1499     }
1500
1501   g_settings_free_key_info (&info);
1502
1503   return g_settings_write_to_backend (&info, value);
1504 }
1505
1506 /**
1507  * g_settings_get:
1508  * @settings: a #GSettings object
1509  * @key: the key to get the value for
1510  * @format: a #GVariant format string
1511  * @...: arguments as per @format
1512  *
1513  * Gets the value that is stored at @key in @settings.
1514  *
1515  * A convenience function that combines g_settings_get_value() with
1516  * g_variant_get().
1517  *
1518  * It is a programmer error to give a @key that isn't contained in the
1519  * schema for @settings or for the #GVariantType of @format to mismatch
1520  * the type given in the schema.
1521  *
1522  * Since: 2.26
1523  */
1524 void
1525 g_settings_get (GSettings   *settings,
1526                 const gchar *key,
1527                 const gchar *format,
1528                 ...)
1529 {
1530   GVariant *value;
1531   va_list ap;
1532
1533   value = g_settings_get_value (settings, key);
1534
1535   va_start (ap, format);
1536   g_variant_get_va (value, format, NULL, &ap);
1537   va_end (ap);
1538
1539   g_variant_unref (value);
1540 }
1541
1542 /**
1543  * g_settings_set:
1544  * @settings: a #GSettings object
1545  * @key: the name of the key to set
1546  * @format: a #GVariant format string
1547  * @...: arguments as per @format
1548  * @returns: %TRUE if setting the key succeeded,
1549  *     %FALSE if the key was not writable
1550  *
1551  * Sets @key in @settings to @value.
1552  *
1553  * A convenience function that combines g_settings_set_value() with
1554  * g_variant_new().
1555  *
1556  * It is a programmer error to give a @key that isn't contained in the
1557  * schema for @settings or for the #GVariantType of @format to mismatch
1558  * the type given in the schema.
1559  *
1560  * Since: 2.26
1561  */
1562 gboolean
1563 g_settings_set (GSettings   *settings,
1564                 const gchar *key,
1565                 const gchar *format,
1566                 ...)
1567 {
1568   GVariant *value;
1569   va_list ap;
1570
1571   va_start (ap, format);
1572   value = g_variant_new_va (format, NULL, &ap);
1573   va_end (ap);
1574
1575   return g_settings_set_value (settings, key, value);
1576 }
1577
1578 /**
1579  * g_settings_get_mapped:
1580  * @settings: a #GSettings object
1581  * @key: the key to get the value for
1582  * @mapping: the function to map the value in the settings database to
1583  *           the value used by the application
1584  * @user_data: user data for @mapping
1585  * @returns: (transfer full): the result, which may be %NULL
1586  *
1587  * Gets the value that is stored at @key in @settings, subject to
1588  * application-level validation/mapping.
1589  *
1590  * You should use this function when the application needs to perform
1591  * some processing on the value of the key (for example, parsing).  The
1592  * @mapping function performs that processing.  If the function
1593  * indicates that the processing was unsuccessful (due to a parse error,
1594  * for example) then the mapping is tried again with another value.
1595
1596  * This allows a robust 'fall back to defaults' behaviour to be
1597  * implemented somewhat automatically.
1598  *
1599  * The first value that is tried is the user's setting for the key.  If
1600  * the mapping function fails to map this value, other values may be
1601  * tried in an unspecified order (system or site defaults, translated
1602  * schema default values, untranslated schema default values, etc).
1603  *
1604  * If the mapping function fails for all possible values, one additional
1605  * attempt is made: the mapping function is called with a %NULL value.
1606  * If the mapping function still indicates failure at this point then
1607  * the application will be aborted.
1608  *
1609  * The result parameter for the @mapping function is pointed to a
1610  * #gpointer which is initially set to %NULL.  The same pointer is given
1611  * to each invocation of @mapping.  The final value of that #gpointer is
1612  * what is returned by this function.  %NULL is valid; it is returned
1613  * just as any other value would be.
1614  **/
1615 gpointer
1616 g_settings_get_mapped (GSettings           *settings,
1617                        const gchar         *key,
1618                        GSettingsGetMapping  mapping,
1619                        gpointer             user_data)
1620 {
1621   gpointer result = NULL;
1622   GSettingsKeyInfo info;
1623   GVariant *value;
1624   gboolean okay;
1625
1626   g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1627   g_return_val_if_fail (key != NULL, NULL);
1628   g_return_val_if_fail (mapping != NULL, NULL);
1629
1630   g_settings_get_key_info (&info, settings, key);
1631
1632   if ((value = g_settings_read_from_backend (&info)))
1633     {
1634       okay = mapping (value, &result, user_data);
1635       g_variant_unref (value);
1636       if (okay) goto okay;
1637     }
1638
1639   if ((value = g_settings_get_translated_default (&info)))
1640     {
1641       okay = mapping (value, &result, user_data);
1642       g_variant_unref (value);
1643       if (okay) goto okay;
1644     }
1645
1646   if (mapping (info.default_value, &result, user_data))
1647     goto okay;
1648
1649   if (!mapping (NULL, &result, user_data))
1650     g_error ("The mapping function given to g_settings_get_mapped() for key "
1651              "`%s' in schema `%s' returned FALSE when given a NULL value.",
1652              key, settings->priv->schema_name);
1653
1654  okay:
1655   g_settings_free_key_info (&info);
1656
1657   return result;
1658 }
1659
1660 /* Convenience API (get, set_string, int, double, boolean, strv) {{{1 */
1661 /**
1662  * g_settings_get_string:
1663  * @settings: a #GSettings object
1664  * @key: the key to get the value for
1665  * @returns: a newly-allocated string
1666  *
1667  * Gets the value that is stored at @key in @settings.
1668  *
1669  * A convenience variant of g_settings_get() for strings.
1670  *
1671  * It is a programmer error to give a @key that isn't specified as
1672  * having a string type in the schema for @settings.
1673  *
1674  * Since: 2.26
1675  */
1676 gchar *
1677 g_settings_get_string (GSettings   *settings,
1678                        const gchar *key)
1679 {
1680   GVariant *value;
1681   gchar *result;
1682
1683   value = g_settings_get_value (settings, key);
1684   result = g_variant_dup_string (value, NULL);
1685   g_variant_unref (value);
1686
1687   return result;
1688 }
1689
1690 /**
1691  * g_settings_set_string:
1692  * @settings: a #GSettings object
1693  * @key: the name of the key to set
1694  * @value: the value to set it to
1695  * @returns: %TRUE if setting the key succeeded,
1696  *     %FALSE if the key was not writable
1697  *
1698  * Sets @key in @settings to @value.
1699  *
1700  * A convenience variant of g_settings_set() for strings.
1701  *
1702  * It is a programmer error to give a @key that isn't specified as
1703  * having a string type in the schema for @settings.
1704  *
1705  * Since: 2.26
1706  */
1707 gboolean
1708 g_settings_set_string (GSettings   *settings,
1709                        const gchar *key,
1710                        const gchar *value)
1711 {
1712   return g_settings_set_value (settings, key, g_variant_new_string (value));
1713 }
1714
1715 /**
1716  * g_settings_get_int:
1717  * @settings: a #GSettings object
1718  * @key: the key to get the value for
1719  * @returns: an integer
1720  *
1721  * Gets the value that is stored at @key in @settings.
1722  *
1723  * A convenience variant of g_settings_get() for 32-bit integers.
1724  *
1725  * It is a programmer error to give a @key that isn't specified as
1726  * having a int32 type in the schema for @settings.
1727  *
1728  * Since: 2.26
1729  */
1730 gint
1731 g_settings_get_int (GSettings   *settings,
1732                     const gchar *key)
1733 {
1734   GVariant *value;
1735   gint result;
1736
1737   value = g_settings_get_value (settings, key);
1738   result = g_variant_get_int32 (value);
1739   g_variant_unref (value);
1740
1741   return result;
1742 }
1743
1744 /**
1745  * g_settings_set_int:
1746  * @settings: a #GSettings object
1747  * @key: the name of the key to set
1748  * @value: the value to set it to
1749  * @returns: %TRUE if setting the key succeeded,
1750  *     %FALSE if the key was not writable
1751  *
1752  * Sets @key in @settings to @value.
1753  *
1754  * A convenience variant of g_settings_set() for 32-bit integers.
1755  *
1756  * It is a programmer error to give a @key that isn't specified as
1757  * having a int32 type in the schema for @settings.
1758  *
1759  * Since: 2.26
1760  */
1761 gboolean
1762 g_settings_set_int (GSettings   *settings,
1763                     const gchar *key,
1764                     gint         value)
1765 {
1766   return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1767 }
1768
1769 /**
1770  * g_settings_get_double:
1771  * @settings: a #GSettings object
1772  * @key: the key to get the value for
1773  * @returns: a double
1774  *
1775  * Gets the value that is stored at @key in @settings.
1776  *
1777  * A convenience variant of g_settings_get() for doubles.
1778  *
1779  * It is a programmer error to give a @key that isn't specified as
1780  * having a 'double' type in the schema for @settings.
1781  *
1782  * Since: 2.26
1783  */
1784 gdouble
1785 g_settings_get_double (GSettings   *settings,
1786                        const gchar *key)
1787 {
1788   GVariant *value;
1789   gdouble result;
1790
1791   value = g_settings_get_value (settings, key);
1792   result = g_variant_get_double (value);
1793   g_variant_unref (value);
1794
1795   return result;
1796 }
1797
1798 /**
1799  * g_settings_set_double:
1800  * @settings: a #GSettings object
1801  * @key: the name of the key to set
1802  * @value: the value to set it to
1803  * @returns: %TRUE if setting the key succeeded,
1804  *     %FALSE if the key was not writable
1805  *
1806  * Sets @key in @settings to @value.
1807  *
1808  * A convenience variant of g_settings_set() for doubles.
1809  *
1810  * It is a programmer error to give a @key that isn't specified as
1811  * having a 'double' type in the schema for @settings.
1812  *
1813  * Since: 2.26
1814  */
1815 gboolean
1816 g_settings_set_double (GSettings   *settings,
1817                        const gchar *key,
1818                        gdouble      value)
1819 {
1820   return g_settings_set_value (settings, key, g_variant_new_double (value));
1821 }
1822
1823 /**
1824  * g_settings_get_boolean:
1825  * @settings: a #GSettings object
1826  * @key: the key to get the value for
1827  * @returns: a boolean
1828  *
1829  * Gets the value that is stored at @key in @settings.
1830  *
1831  * A convenience variant of g_settings_get() for booleans.
1832  *
1833  * It is a programmer error to give a @key that isn't specified as
1834  * having a boolean type in the schema for @settings.
1835  *
1836  * Since: 2.26
1837  */
1838 gboolean
1839 g_settings_get_boolean (GSettings  *settings,
1840                        const gchar *key)
1841 {
1842   GVariant *value;
1843   gboolean result;
1844
1845   value = g_settings_get_value (settings, key);
1846   result = g_variant_get_boolean (value);
1847   g_variant_unref (value);
1848
1849   return result;
1850 }
1851
1852 /**
1853  * g_settings_set_boolean:
1854  * @settings: a #GSettings object
1855  * @key: the name of the key to set
1856  * @value: the value to set it to
1857  * @returns: %TRUE if setting the key succeeded,
1858  *     %FALSE if the key was not writable
1859  *
1860  * Sets @key in @settings to @value.
1861  *
1862  * A convenience variant of g_settings_set() for booleans.
1863  *
1864  * It is a programmer error to give a @key that isn't specified as
1865  * having a boolean type in the schema for @settings.
1866  *
1867  * Since: 2.26
1868  */
1869 gboolean
1870 g_settings_set_boolean (GSettings  *settings,
1871                        const gchar *key,
1872                        gboolean     value)
1873 {
1874   return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1875 }
1876
1877 /**
1878  * g_settings_get_strv:
1879  * @settings: a #GSettings object
1880  * @key: the key to get the value for
1881  * @returns: a newly-allocated, %NULL-terminated array of strings
1882  *
1883  * A convenience variant of g_settings_get() for string arrays.
1884  *
1885  * It is a programmer error to give a @key that isn't specified as
1886  * having an array of strings type in the schema for @settings.
1887  *
1888  * Returns: (array zero-terminated=1) (transfer full): the value that is
1889  * stored at @key in @settings.
1890  *
1891  * Since: 2.26
1892  */
1893 gchar **
1894 g_settings_get_strv (GSettings   *settings,
1895                      const gchar *key)
1896 {
1897   GVariant *value;
1898   gchar **result;
1899
1900   value = g_settings_get_value (settings, key);
1901   result = g_variant_dup_strv (value, NULL);
1902   g_variant_unref (value);
1903
1904   return result;
1905 }
1906
1907 /**
1908  * g_settings_set_strv:
1909  * @settings: a #GSettings object
1910  * @key: the name of the key to set
1911  * @value: (allow-none) (array zero-terminated=1): the value to set it to, or %NULL
1912  * @returns: %TRUE if setting the key succeeded,
1913  *     %FALSE if the key was not writable
1914  *
1915  * Sets @key in @settings to @value.
1916  *
1917  * A convenience variant of g_settings_set() for string arrays.  If
1918  * @value is %NULL, then @key is set to be the empty array.
1919  *
1920  * It is a programmer error to give a @key that isn't specified as
1921  * having an array of strings type in the schema for @settings.
1922  *
1923  * Since: 2.26
1924  */
1925 gboolean
1926 g_settings_set_strv (GSettings           *settings,
1927                      const gchar         *key,
1928                      const gchar * const *value)
1929 {
1930   GVariant *array;
1931
1932   if (value != NULL)
1933     array = g_variant_new_strv (value, -1);
1934   else
1935     array = g_variant_new_strv (NULL, 0);
1936
1937   return g_settings_set_value (settings, key, array);
1938 }
1939
1940 /* Delayed apply (delay, apply, revert, get_has_unapplied) {{{1 */
1941 /**
1942  * g_settings_delay:
1943  * @settings: a #GSettings object
1944  *
1945  * Changes the #GSettings object into 'delay-apply' mode. In this
1946  * mode, changes to @settings are not immediately propagated to the
1947  * backend, but kept locally until g_settings_apply() is called.
1948  *
1949  * Since: 2.26
1950  */
1951 void
1952 g_settings_delay (GSettings *settings)
1953 {
1954   g_return_if_fail (G_IS_SETTINGS (settings));
1955
1956   if (settings->priv->delayed)
1957     return;
1958
1959   settings->priv->delayed =
1960     g_delayed_settings_backend_new (settings->priv->backend,
1961                                     settings,
1962                                     settings->priv->main_context);
1963   g_settings_backend_unwatch (settings->priv->backend, G_OBJECT (settings));
1964   g_object_unref (settings->priv->backend);
1965
1966   settings->priv->backend = G_SETTINGS_BACKEND (settings->priv->delayed);
1967   g_settings_backend_watch (settings->priv->backend,
1968                             &listener_vtable, G_OBJECT (settings),
1969                             settings->priv->main_context);
1970 }
1971
1972 /**
1973  * g_settings_apply:
1974  * @settings: a #GSettings instance
1975  *
1976  * Applies any changes that have been made to the settings.  This
1977  * function does nothing unless @settings is in 'delay-apply' mode;
1978  * see g_settings_delay().  In the normal case settings are always
1979  * applied immediately.
1980  **/
1981 void
1982 g_settings_apply (GSettings *settings)
1983 {
1984   if (settings->priv->delayed)
1985     {
1986       GDelayedSettingsBackend *delayed;
1987
1988       delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
1989       g_delayed_settings_backend_apply (delayed);
1990     }
1991 }
1992
1993 /**
1994  * g_settings_revert:
1995  * @settings: a #GSettings instance
1996  *
1997  * Reverts all non-applied changes to the settings.  This function
1998  * does nothing unless @settings is in 'delay-apply' mode; see
1999  * g_settings_delay().  In the normal case settings are always applied
2000  * immediately.
2001  *
2002  * Change notifications will be emitted for affected keys.
2003  **/
2004 void
2005 g_settings_revert (GSettings *settings)
2006 {
2007   if (settings->priv->delayed)
2008     {
2009       GDelayedSettingsBackend *delayed;
2010
2011       delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
2012       g_delayed_settings_backend_revert (delayed);
2013     }
2014 }
2015
2016 /**
2017  * g_settings_get_has_unapplied:
2018  * @settings: a #GSettings object
2019  * @returns: %TRUE if @settings has unapplied changes
2020  *
2021  * Returns whether the #GSettings object has any unapplied
2022  * changes.  This can only be the case if it is in 'delayed-apply' mode.
2023  *
2024  * Since: 2.26
2025  */
2026 gboolean
2027 g_settings_get_has_unapplied (GSettings *settings)
2028 {
2029   g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
2030
2031   return settings->priv->delayed &&
2032          g_delayed_settings_backend_get_has_unapplied (
2033            G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
2034 }
2035
2036 /* Extra API (reset, sync, get_child, is_writable, list_*, ranges) {{{1 */
2037 /**
2038  * g_settings_reset:
2039  * @settings: a #GSettings object
2040  * @key: the name of a key
2041  *
2042  * Resets @key to its default value.
2043  *
2044  * This call resets the key, as much as possible, to its default value.
2045  * That might the value specified in the schema or the one set by the
2046  * administrator.
2047  **/
2048 void
2049 g_settings_reset (GSettings *settings,
2050                   const gchar *key)
2051 {
2052   gchar *path;
2053
2054   path = g_strconcat (settings->priv->path, key, NULL);
2055   g_settings_backend_reset (settings->priv->backend, path, NULL);
2056   g_free (path);
2057 }
2058
2059 /**
2060  * g_settings_sync:
2061  *
2062  * Ensures that all pending operations for the given are complete for
2063  * the default backend.
2064  *
2065  * Writes made to a #GSettings are handled asynchronously.  For this
2066  * reason, it is very unlikely that the changes have it to disk by the
2067  * time g_settings_set() returns.
2068  *
2069  * This call will block until all of the writes have made it to the
2070  * backend.  Since the mainloop is not running, no change notifications
2071  * will be dispatched during this call (but some may be queued by the
2072  * time the call is done).
2073  **/
2074 void
2075 g_settings_sync (void)
2076 {
2077   g_settings_backend_sync_default ();
2078 }
2079
2080 /**
2081  * g_settings_is_writable:
2082  * @settings: a #GSettings object
2083  * @name: the name of a key
2084  * @returns: %TRUE if the key @name is writable
2085  *
2086  * Finds out if a key can be written or not
2087  *
2088  * Since: 2.26
2089  */
2090 gboolean
2091 g_settings_is_writable (GSettings   *settings,
2092                         const gchar *name)
2093 {
2094   gboolean writable;
2095   gchar *path;
2096
2097   g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
2098
2099   path = g_strconcat (settings->priv->path, name, NULL);
2100   writable = g_settings_backend_get_writable (settings->priv->backend, path);
2101   g_free (path);
2102
2103   return writable;
2104 }
2105
2106 /**
2107  * g_settings_get_child:
2108  * @settings: a #GSettings object
2109  * @name: the name of the 'child' schema
2110  * @returns: (transfer full): a 'child' settings object
2111  *
2112  * Creates a 'child' settings object which has a base path of
2113  * <replaceable>base-path</replaceable>/@name", where
2114  * <replaceable>base-path</replaceable> is the base path of @settings.
2115  *
2116  * The schema for the child settings object must have been declared
2117  * in the schema of @settings using a <tag class="starttag">child</tag> element.
2118  *
2119  * Since: 2.26
2120  */
2121 GSettings *
2122 g_settings_get_child (GSettings   *settings,
2123                       const gchar *name)
2124 {
2125   const gchar *child_schema;
2126   gchar *child_path;
2127   gchar *child_name;
2128   GSettings *child;
2129
2130   g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
2131
2132   child_name = g_strconcat (name, "/", NULL);
2133   child_schema = g_settings_schema_get_string (settings->priv->schema,
2134                                                child_name);
2135   if (child_schema == NULL)
2136     g_error ("Schema '%s' has no child '%s'",
2137              settings->priv->schema_name, name);
2138
2139   child_path = g_strconcat (settings->priv->path, child_name, NULL);
2140   child = g_object_new (G_TYPE_SETTINGS,
2141                         "schema", child_schema,
2142                         "path", child_path,
2143                         NULL);
2144   g_free (child_path);
2145   g_free (child_name);
2146
2147   return child;
2148 }
2149
2150 /**
2151  * g_settings_list_keys:
2152  * @settings: a #GSettings object
2153  * @returns: (transfer full) (element-type utf8): a list of the keys on @settings
2154  *
2155  * Introspects the list of keys on @settings.
2156  *
2157  * You should probably not be calling this function from "normal" code
2158  * (since you should already know what keys are in your schema).  This
2159  * function is intended for introspection reasons.
2160  *
2161  * You should free the return value with g_strfreev() when you are done
2162  * with it.
2163  */
2164 gchar **
2165 g_settings_list_keys (GSettings *settings)
2166 {
2167   const GQuark *keys;
2168   gchar **strv;
2169   gint n_keys;
2170   gint i, j;
2171
2172   keys = g_settings_schema_list (settings->priv->schema, &n_keys);
2173   strv = g_new (gchar *, n_keys + 1);
2174   for (i = j = 0; i < n_keys; i++)
2175     {
2176       const gchar *key = g_quark_to_string (keys[i]);
2177
2178       if (!g_str_has_suffix (key, "/"))
2179         strv[j++] = g_strdup (key);
2180     }
2181   strv[j] = NULL;
2182
2183   return strv;
2184 }
2185
2186 /**
2187  * g_settings_list_children:
2188  * @settings: a #GSettings object
2189  * @returns: (transfer full) (element-type utf8): a list of the children on @settings
2190  *
2191  * Gets the list of children on @settings.
2192  *
2193  * The list is exactly the list of strings for which it is not an error
2194  * to call g_settings_get_child().
2195  *
2196  * For GSettings objects that are lists, this value can change at any
2197  * time and you should connect to the "children-changed" signal to watch
2198  * for those changes.  Note that there is a race condition here: you may
2199  * request a child after listing it only for it to have been destroyed
2200  * in the meantime.  For this reason, g_settings_get_child() may return
2201  * %NULL even for a child that was listed by this function.
2202  *
2203  * For GSettings objects that are not lists, you should probably not be
2204  * calling this function from "normal" code (since you should already
2205  * know what children are in your schema).  This function may still be
2206  * useful there for introspection reasons, however.
2207  *
2208  * You should free the return value with g_strfreev() when you are done
2209  * with it.
2210  */
2211 gchar **
2212 g_settings_list_children (GSettings *settings)
2213 {
2214   const GQuark *keys;
2215   gchar **strv;
2216   gint n_keys;
2217   gint i, j;
2218
2219   keys = g_settings_schema_list (settings->priv->schema, &n_keys);
2220   strv = g_new (gchar *, n_keys + 1);
2221   for (i = j = 0; i < n_keys; i++)
2222     {
2223       const gchar *key = g_quark_to_string (keys[i]);
2224
2225       if (g_str_has_suffix (key, "/"))
2226         {
2227           gint length = strlen (key);
2228
2229           strv[j] = g_memdup (key, length);
2230           strv[j][length - 1] = '\0';
2231           j++;
2232         }
2233     }
2234   strv[j] = NULL;
2235
2236   return strv;
2237 }
2238
2239 /**
2240  * g_settings_get_range:
2241  * @settings: a #GSettings
2242  * @key: the key to query the range of
2243  * @returns: a #GVariant describing the range
2244  *
2245  * Queries the range of a key.
2246  *
2247  * This function will return a #GVariant that fully describes the range
2248  * of values that are valid for @key.
2249  *
2250  * The type of #GVariant returned is <literal>(sv)</literal>.  The
2251  * string describes the type of range restriction in effect.  The type
2252  * and meaning of the value contained in the variant depends on the
2253  * string.
2254  *
2255  * If the string is <literal>'type'</literal> then the variant contains
2256  * an empty array.  The element type of that empty array is the expected
2257  * type of value and all values of that type are valid.
2258  *
2259  * If the string is <literal>'enum'</literal> then the variant contains
2260  * an array enumerating the possible values.  Each item in the array is
2261  * a possible valid value and no other values are valid.
2262  *
2263  * If the string is <literal>'flags'</literal> then the variant contains
2264  * an array.  Each item in the array is a value that may appear zero or
2265  * one times in an array to be used as the value for this key.  For
2266  * example, if the variant contained the array <literal>['x',
2267  * 'y']</literal> then the valid values for the key would be
2268  * <literal>[]</literal>, <literal>['x']</literal>,
2269  * <literal>['y']</literal>, <literal>['x', 'y']</literal> and
2270  * <literal>['y', 'x']</literal>.
2271  *
2272  * Finally, if the string is <literal>'range'</literal> then the variant
2273  * contains a pair of like-typed values -- the minimum and maximum
2274  * permissible values for this key.
2275  *
2276  * This information should not be used by normal programs.  It is
2277  * considered to be a hint for introspection purposes.  Normal programs
2278  * should already know what is permitted by their own schema.  The
2279  * format may change in any way in the future -- but particularly, new
2280  * forms may be added to the possibilities described above.
2281  *
2282  * It is a programmer error to give a @key that isn't contained in the
2283  * schema for @settings.
2284  *
2285  * You should free the returned value with g_variant_unref() when it is
2286  * no longer needed.
2287  *
2288  * Since: 2.28
2289  **/
2290 GVariant *
2291 g_settings_get_range (GSettings   *settings,
2292                       const gchar *key)
2293 {
2294   GSettingsKeyInfo info;
2295   const gchar *type;
2296   GVariant *range;
2297
2298   g_settings_get_key_info (&info, settings, key);
2299
2300   if (info.minimum)
2301     {
2302       range = g_variant_new ("(**)", info.minimum, info.maximum);
2303       type = "range";
2304     }
2305   else if (info.strinfo)
2306     {
2307       range = strinfo_enumerate (info.strinfo, info.strinfo_length);
2308       type = info.is_flags ? "flags" : "enum";
2309     }
2310   else
2311     {
2312       range = g_variant_new_array (info.type, NULL, 0);
2313       type = "type";
2314     }
2315
2316   g_settings_free_key_info (&info);
2317
2318   return g_variant_ref_sink (g_variant_new ("(sv)", type, range));
2319 }
2320
2321 /**
2322  * g_settings_range_check:
2323  * @settings: a #GSettings
2324  * @key: the key to check
2325  * @value: the value to check
2326  * @returns: %TRUE if @value is valid for @key
2327  *
2328  * Checks if the given @value is of the correct type and within the
2329  * permitted range for @key.
2330  *
2331  * This API is not intended to be used by normal programs -- they should
2332  * already know what is permitted by their own schemas.  This API is
2333  * meant to be used by programs such as editors or commandline tools.
2334  *
2335  * It is a programmer error to give a @key that isn't contained in the
2336  * schema for @settings.
2337  *
2338  * Since: 2.28
2339  **/
2340 gboolean
2341 g_settings_range_check (GSettings   *settings,
2342                         const gchar *key,
2343                         GVariant    *value)
2344 {
2345   GSettingsKeyInfo info;
2346   gboolean good;
2347
2348   g_settings_get_key_info (&info, settings, key);
2349   good = g_settings_type_check (&info, value) &&
2350          g_settings_key_info_range_check (&info, value);
2351   g_settings_free_key_info (&info);
2352
2353   return good;
2354 }
2355
2356 /* Binding {{{1 */
2357 typedef struct
2358 {
2359   GSettingsKeyInfo info;
2360   GObject *object;
2361
2362   GSettingsBindGetMapping get_mapping;
2363   GSettingsBindSetMapping set_mapping;
2364   gpointer user_data;
2365   GDestroyNotify destroy;
2366
2367   guint writable_handler_id;
2368   guint property_handler_id;
2369   const GParamSpec *property;
2370   guint key_handler_id;
2371
2372   /* prevent recursion */
2373   gboolean running;
2374 } GSettingsBinding;
2375
2376 static void
2377 g_settings_binding_free (gpointer data)
2378 {
2379   GSettingsBinding *binding = data;
2380
2381   g_assert (!binding->running);
2382
2383   if (binding->writable_handler_id)
2384     g_signal_handler_disconnect (binding->info.settings,
2385                                  binding->writable_handler_id);
2386
2387   if (binding->key_handler_id)
2388     g_signal_handler_disconnect (binding->info.settings,
2389                                  binding->key_handler_id);
2390
2391   if (g_signal_handler_is_connected (binding->object,
2392                                      binding->property_handler_id))
2393   g_signal_handler_disconnect (binding->object,
2394                                binding->property_handler_id);
2395
2396   g_settings_free_key_info (&binding->info);
2397
2398   if (binding->destroy)
2399     binding->destroy (binding->user_data);
2400
2401   g_slice_free (GSettingsBinding, binding);
2402 }
2403
2404 static GQuark
2405 g_settings_binding_quark (const char *property)
2406 {
2407   GQuark quark;
2408   gchar *tmp;
2409
2410   tmp = g_strdup_printf ("gsettingsbinding-%s", property);
2411   quark = g_quark_from_string (tmp);
2412   g_free (tmp);
2413
2414   return quark;
2415 }
2416
2417 static void
2418 g_settings_binding_key_changed (GSettings   *settings,
2419                                 const gchar *key,
2420                                 gpointer     user_data)
2421 {
2422   GSettingsBinding *binding = user_data;
2423   GValue value = { 0, };
2424   GVariant *variant;
2425
2426   g_assert (settings == binding->info.settings);
2427   g_assert (key == binding->info.key);
2428
2429   if (binding->running)
2430     return;
2431
2432   binding->running = TRUE;
2433
2434   g_value_init (&value, binding->property->value_type);
2435
2436   variant = g_settings_read_from_backend (&binding->info);
2437   if (variant && !binding->get_mapping (&value, variant, binding->user_data))
2438     {
2439       /* silently ignore errors in the user's config database */
2440       g_variant_unref (variant);
2441       variant = NULL;
2442     }
2443
2444   if (variant == NULL)
2445     {
2446       variant = g_settings_get_translated_default (&binding->info);
2447       if (variant &&
2448           !binding->get_mapping (&value, variant, binding->user_data))
2449         {
2450           /* flag translation errors with a warning */
2451           g_warning ("Translated default `%s' for key `%s' in schema `%s' "
2452                      "was rejected by the binding mapping function",
2453                      binding->info.unparsed, binding->info.key,
2454                      binding->info.settings->priv->schema_name);
2455           g_variant_unref (variant);
2456           variant = NULL;
2457         }
2458     }
2459
2460   if (variant == NULL)
2461     {
2462       variant = g_variant_ref (binding->info.default_value);
2463       if (!binding->get_mapping (&value, variant, binding->user_data))
2464         g_error ("The schema default value for key `%s' in schema `%s' "
2465                  "was rejected by the binding mapping function.",
2466                  binding->info.key,
2467                  binding->info.settings->priv->schema_name);
2468     }
2469
2470   g_object_set_property (binding->object, binding->property->name, &value);
2471   g_variant_unref (variant);
2472   g_value_unset (&value);
2473
2474   binding->running = FALSE;
2475 }
2476
2477 static void
2478 g_settings_binding_property_changed (GObject          *object,
2479                                      const GParamSpec *pspec,
2480                                      gpointer          user_data)
2481 {
2482   GSettingsBinding *binding = user_data;
2483   GValue value = { 0, };
2484   GVariant *variant;
2485
2486   g_assert (object == binding->object);
2487   g_assert (pspec == binding->property);
2488
2489   if (binding->running)
2490     return;
2491
2492   binding->running = TRUE;
2493
2494   g_value_init (&value, pspec->value_type);
2495   g_object_get_property (object, pspec->name, &value);
2496   if ((variant = binding->set_mapping (&value, binding->info.type,
2497                                        binding->user_data)))
2498     {
2499       if (g_variant_is_floating (variant))
2500         g_variant_ref_sink (variant);
2501
2502       if (!g_settings_type_check (&binding->info, variant))
2503         {
2504           g_critical ("binding mapping function for key `%s' returned "
2505                       "GVariant of type `%s' when type `%s' was requested",
2506                       binding->info.key, g_variant_get_type_string (variant),
2507                       g_variant_type_dup_string (binding->info.type));
2508           return;
2509         }
2510
2511       if (!g_settings_key_info_range_check (&binding->info, variant))
2512         {
2513           g_critical ("GObject property `%s' on a `%s' object is out of "
2514                       "schema-specified range for key `%s' of `%s': %s",
2515                       binding->property->name,
2516                       g_type_name (binding->property->owner_type),
2517                       binding->info.key,
2518                       binding->info.settings->priv->schema_name,
2519                       g_variant_print (variant, TRUE));
2520           return;
2521         }
2522
2523       g_settings_write_to_backend (&binding->info, variant);
2524       g_variant_unref (variant);
2525     }
2526   g_value_unset (&value);
2527
2528   binding->running = FALSE;
2529 }
2530
2531 static gboolean
2532 g_settings_bind_invert_boolean_get_mapping (GValue   *value,
2533                                             GVariant *variant,
2534                                             gpointer  user_data)
2535 {
2536   g_value_set_boolean (value, !g_variant_get_boolean (variant));
2537   return TRUE;
2538 }
2539
2540 static GVariant *
2541 g_settings_bind_invert_boolean_set_mapping (const GValue       *value,
2542                                             const GVariantType *expected_type,
2543                                             gpointer            user_data)
2544 {
2545   return g_variant_new_boolean (!g_value_get_boolean (value));
2546 }
2547
2548 /**
2549  * g_settings_bind:
2550  * @settings: a #GSettings object
2551  * @key: the key to bind
2552  * @object: a #GObject
2553  * @property: the name of the property to bind
2554  * @flags: flags for the binding
2555  *
2556  * Create a binding between the @key in the @settings object
2557  * and the property @property of @object.
2558  *
2559  * The binding uses the default GIO mapping functions to map
2560  * between the settings and property values. These functions
2561  * handle booleans, numeric types and string types in a
2562  * straightforward way. Use g_settings_bind_with_mapping() if
2563  * you need a custom mapping, or map between types that are not
2564  * supported by the default mapping functions.
2565  *
2566  * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
2567  * function also establishes a binding between the writability of
2568  * @key and the "sensitive" property of @object (if @object has
2569  * a boolean property by that name). See g_settings_bind_writable()
2570  * for more details about writable bindings.
2571  *
2572  * Note that the lifecycle of the binding is tied to the object,
2573  * and that you can have only one binding per object property.
2574  * If you bind the same property twice on the same object, the second
2575  * binding overrides the first one.
2576  *
2577  * Since: 2.26
2578  */
2579 void
2580 g_settings_bind (GSettings          *settings,
2581                  const gchar        *key,
2582                  gpointer            object,
2583                  const gchar        *property,
2584                  GSettingsBindFlags  flags)
2585 {
2586   GSettingsBindGetMapping get_mapping = NULL;
2587   GSettingsBindSetMapping set_mapping = NULL;
2588
2589   if (flags & G_SETTINGS_BIND_INVERT_BOOLEAN)
2590     {
2591       get_mapping = g_settings_bind_invert_boolean_get_mapping;
2592       set_mapping = g_settings_bind_invert_boolean_set_mapping;
2593
2594       /* can't pass this flag to g_settings_bind_with_mapping() */
2595       flags &= ~G_SETTINGS_BIND_INVERT_BOOLEAN;
2596     }
2597
2598   g_settings_bind_with_mapping (settings, key, object, property, flags,
2599                                 get_mapping, set_mapping, NULL, NULL);
2600 }
2601
2602 /**
2603  * g_settings_bind_with_mapping:
2604  * @settings: a #GSettings object
2605  * @key: the key to bind
2606  * @object: a #GObject
2607  * @property: the name of the property to bind
2608  * @flags: flags for the binding
2609  * @get_mapping: a function that gets called to convert values
2610  *     from @settings to @object, or %NULL to use the default GIO mapping
2611  * @set_mapping: a function that gets called to convert values
2612  *     from @object to @settings, or %NULL to use the default GIO mapping
2613  * @user_data: data that gets passed to @get_mapping and @set_mapping
2614  * @destroy: #GDestroyNotify function for @user_data
2615  *
2616  * Create a binding between the @key in the @settings object
2617  * and the property @property of @object.
2618  *
2619  * The binding uses the provided mapping functions to map between
2620  * settings and property values.
2621  *
2622  * Note that the lifecycle of the binding is tied to the object,
2623  * and that you can have only one binding per object property.
2624  * If you bind the same property twice on the same object, the second
2625  * binding overrides the first one.
2626  *
2627  * Since: 2.26
2628  */
2629 void
2630 g_settings_bind_with_mapping (GSettings               *settings,
2631                               const gchar             *key,
2632                               gpointer                 object,
2633                               const gchar             *property,
2634                               GSettingsBindFlags       flags,
2635                               GSettingsBindGetMapping  get_mapping,
2636                               GSettingsBindSetMapping  set_mapping,
2637                               gpointer                 user_data,
2638                               GDestroyNotify           destroy)
2639 {
2640   GSettingsBinding *binding;
2641   GObjectClass *objectclass;
2642   gchar *detailed_signal;
2643   GQuark binding_quark;
2644
2645   g_return_if_fail (G_IS_SETTINGS (settings));
2646   g_return_if_fail (~flags & G_SETTINGS_BIND_INVERT_BOOLEAN);
2647
2648   objectclass = G_OBJECT_GET_CLASS (object);
2649
2650   binding = g_slice_new0 (GSettingsBinding);
2651   g_settings_get_key_info (&binding->info, settings, key);
2652   binding->object = object;
2653   binding->property = g_object_class_find_property (objectclass, property);
2654   binding->user_data = user_data;
2655   binding->destroy = destroy;
2656   binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
2657   binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
2658
2659   if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
2660     flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
2661
2662   if (binding->property == NULL)
2663     {
2664       g_critical ("g_settings_bind: no property '%s' on class '%s'",
2665                   property, G_OBJECT_TYPE_NAME (object));
2666       return;
2667     }
2668
2669   if ((flags & G_SETTINGS_BIND_GET) &&
2670       (binding->property->flags & G_PARAM_WRITABLE) == 0)
2671     {
2672       g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2673                   "writable", property, G_OBJECT_TYPE_NAME (object));
2674       return;
2675     }
2676   if ((flags & G_SETTINGS_BIND_SET) &&
2677       (binding->property->flags & G_PARAM_READABLE) == 0)
2678     {
2679       g_critical ("g_settings_bind: property '%s' on class '%s' is not "
2680                   "readable", property, G_OBJECT_TYPE_NAME (object));
2681       return;
2682     }
2683
2684   if (get_mapping == g_settings_bind_invert_boolean_get_mapping)
2685     {
2686       /* g_settings_bind_invert_boolean_get_mapping() is a private
2687        * function, so if we are here it means that g_settings_bind() was
2688        * called with G_SETTINGS_BIND_INVERT_BOOLEAN.
2689        *
2690        * Ensure that both sides are boolean.
2691        */
2692
2693       if (binding->property->value_type != G_TYPE_BOOLEAN)
2694         {
2695           g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2696                       "was specified, but property `%s' on type `%s' has "
2697                       "type `%s'", property, G_OBJECT_TYPE_NAME (object),
2698                       g_type_name ((binding->property->value_type)));
2699           return;
2700         }
2701
2702       if (!g_variant_type_equal (binding->info.type, G_VARIANT_TYPE_BOOLEAN))
2703         {
2704           g_critical ("g_settings_bind: G_SETTINGS_BIND_INVERT_BOOLEAN "
2705                       "was specified, but key `%s' on schema `%s' has "
2706                       "type `%s'", key, settings->priv->schema_name,
2707                       g_variant_type_dup_string (binding->info.type));
2708           return;
2709         }
2710
2711     }
2712
2713   else if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
2714             (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
2715            !g_settings_mapping_is_compatible (binding->property->value_type,
2716                                               binding->info.type))
2717     {
2718       g_critical ("g_settings_bind: property '%s' on class '%s' has type "
2719                   "'%s' which is not compatible with type '%s' of key '%s' "
2720                   "on schema '%s'", property, G_OBJECT_TYPE_NAME (object),
2721                   g_type_name (binding->property->value_type),
2722                   g_variant_type_dup_string (binding->info.type), key,
2723                   settings->priv->schema_name);
2724       return;
2725     }
2726
2727   if ((flags & G_SETTINGS_BIND_SET) &&
2728       (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
2729     {
2730       GParamSpec *sensitive;
2731
2732       sensitive = g_object_class_find_property (objectclass, "sensitive");
2733
2734       if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
2735           (sensitive->flags & G_PARAM_WRITABLE))
2736         g_settings_bind_writable (settings, binding->info.key,
2737                                   object, "sensitive", FALSE);
2738     }
2739
2740   if (flags & G_SETTINGS_BIND_SET)
2741     {
2742       detailed_signal = g_strdup_printf ("notify::%s", property);
2743       binding->property_handler_id =
2744         g_signal_connect (object, detailed_signal,
2745                           G_CALLBACK (g_settings_binding_property_changed),
2746                           binding);
2747       g_free (detailed_signal);
2748
2749       if (~flags & G_SETTINGS_BIND_GET)
2750         g_settings_binding_property_changed (object,
2751                                              binding->property,
2752                                              binding);
2753     }
2754
2755   if (flags & G_SETTINGS_BIND_GET)
2756     {
2757       if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
2758         {
2759           detailed_signal = g_strdup_printf ("changed::%s", key);
2760           binding->key_handler_id =
2761             g_signal_connect (settings, detailed_signal,
2762                               G_CALLBACK (g_settings_binding_key_changed),
2763                               binding);
2764           g_free (detailed_signal);
2765         }
2766
2767       g_settings_binding_key_changed (settings, binding->info.key, binding);
2768     }
2769
2770   binding_quark = g_settings_binding_quark (property);
2771   g_object_set_qdata_full (object, binding_quark,
2772                            binding, g_settings_binding_free);
2773 }
2774
2775 /* Writability binding {{{1 */
2776 typedef struct
2777 {
2778   GSettings *settings;
2779   gpointer object;
2780   const gchar *key;
2781   const gchar *property;
2782   gboolean inverted;
2783   gulong handler_id;
2784 } GSettingsWritableBinding;
2785
2786 static void
2787 g_settings_writable_binding_free (gpointer data)
2788 {
2789   GSettingsWritableBinding *binding = data;
2790
2791   g_signal_handler_disconnect (binding->settings, binding->handler_id);
2792   g_object_unref (binding->settings);
2793   g_slice_free (GSettingsWritableBinding, binding);
2794 }
2795
2796 static void
2797 g_settings_binding_writable_changed (GSettings   *settings,
2798                                      const gchar *key,
2799                                      gpointer     user_data)
2800 {
2801   GSettingsWritableBinding *binding = user_data;
2802   gboolean writable;
2803
2804   g_assert (settings == binding->settings);
2805   g_assert (key == binding->key);
2806
2807   writable = g_settings_is_writable (settings, key);
2808
2809   if (binding->inverted)
2810     writable = !writable;
2811
2812   g_object_set (binding->object, binding->property, writable, NULL);
2813 }
2814
2815 /**
2816  * g_settings_bind_writable:
2817  * @settings: a #GSettings object
2818  * @key: the key to bind
2819  * @object: a #GObject
2820  * @property: the name of a boolean property to bind
2821  * @inverted: whether to 'invert' the value
2822  *
2823  * Create a binding between the writability of @key in the
2824  * @settings object and the property @property of @object.
2825  * The property must be boolean; "sensitive" or "visible"
2826  * properties of widgets are the most likely candidates.
2827  *
2828  * Writable bindings are always uni-directional; changes of the
2829  * writability of the setting will be propagated to the object
2830  * property, not the other way.
2831  *
2832  * When the @inverted argument is %TRUE, the binding inverts the
2833  * value as it passes from the setting to the object, i.e. @property
2834  * will be set to %TRUE if the key is <emphasis>not</emphasis>
2835  * writable.
2836  *
2837  * Note that the lifecycle of the binding is tied to the object,
2838  * and that you can have only one binding per object property.
2839  * If you bind the same property twice on the same object, the second
2840  * binding overrides the first one.
2841  *
2842  * Since: 2.26
2843  */
2844 void
2845 g_settings_bind_writable (GSettings   *settings,
2846                           const gchar *key,
2847                           gpointer     object,
2848                           const gchar *property,
2849                           gboolean     inverted)
2850 {
2851   GSettingsWritableBinding *binding;
2852   gchar *detailed_signal;
2853   GParamSpec *pspec;
2854
2855   g_return_if_fail (G_IS_SETTINGS (settings));
2856
2857   pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
2858   if (pspec == NULL)
2859     {
2860       g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
2861                   property, G_OBJECT_TYPE_NAME (object));
2862       return;
2863     }
2864   if ((pspec->flags & G_PARAM_WRITABLE) == 0)
2865     {
2866       g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
2867                   property, G_OBJECT_TYPE_NAME (object));
2868       return;
2869     }
2870
2871   binding = g_slice_new (GSettingsWritableBinding);
2872   binding->settings = g_object_ref (settings);
2873   binding->object = object;
2874   binding->key = g_intern_string (key);
2875   binding->property = g_intern_string (property);
2876   binding->inverted = inverted;
2877
2878   detailed_signal = g_strdup_printf ("writable-changed::%s", key);
2879   binding->handler_id =
2880     g_signal_connect (settings, detailed_signal,
2881                       G_CALLBACK (g_settings_binding_writable_changed),
2882                       binding);
2883   g_free (detailed_signal);
2884
2885   g_object_set_qdata_full (object, g_settings_binding_quark (property),
2886                            binding, g_settings_writable_binding_free);
2887
2888   g_settings_binding_writable_changed (settings, binding->key, binding);
2889 }
2890
2891 /**
2892  * g_settings_unbind:
2893  * @object: the object
2894  * @property: the property whose binding is removed
2895  *
2896  * Removes an existing binding for @property on @object.
2897  *
2898  * Note that bindings are automatically removed when the
2899  * object is finalized, so it is rarely necessary to call this
2900  * function.
2901  *
2902  * Since: 2.26
2903  */
2904 void
2905 g_settings_unbind (gpointer     object,
2906                    const gchar *property)
2907 {
2908   GQuark binding_quark;
2909
2910   binding_quark = g_settings_binding_quark (property);
2911   g_object_set_qdata (object, binding_quark, NULL);
2912 }
2913
2914 /* Epilogue {{{1 */
2915
2916 /* vim:set foldmethod=marker: */