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