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