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