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