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