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