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