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