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