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