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