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