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