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