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