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