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