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