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