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