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