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