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