<alias> takes mandatory target='' arg
[platform/upstream/glib.git] / gio / gsettings.c
1 /*
2  * Copyright © 2009, 2010 Codethink Limited
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the licence, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  *
19  * Author: Ryan Lortie <desrt@desrt.ca>
20  */
21
22 /* Prelude {{{1 */
23 #define _GNU_SOURCE
24 #include "config.h"
25
26 #include <glib.h>
27 #include <glibintl.h>
28 #include <locale.h>
29
30 #include "gsettings.h"
31
32 #include "gdelayedsettingsbackend.h"
33 #include "gsettingsbackendinternal.h"
34 #include "gsettings-mapping.h"
35 #include "gio-marshal.h"
36 #include "gsettingsschema.h"
37
38 #include <string.h>
39
40 #include "gioalias.h"
41
42 #include "strinfo.c"
43
44 /**
45  * SECTION:gsettings
46  * @short_description: a high-level API for application settings
47  *
48  * The #GSettings class provides a convenient API for storing and retrieving
49  * application settings.
50  *
51  * When creating a GSettings instance, you have to specify a schema
52  * that describes the keys in your settings and their types and default
53  * values, as well as some other information.
54  *
55  * Normally, a schema has as fixed path that determines where the settings
56  * are stored in the conceptual global tree of settings. However, schemas
57  * can also be 'relocatable', i.e. not equipped with a fixed path. This is
58  * useful e.g. when the schema describes an 'account', and you want to be
59  * able to store a arbitrary number of accounts.
60  *
61  * Unlike other configuration systems (like GConf), GSettings does not
62  * restrict keys to basic types like strings and numbers. GSettings stores
63  * values as #GVariant, and allows any #GVariantType for keys. Key names
64  * are restricted to lowercase characters, numbers and '-'. Furthermore,
65  * the names must begin with a lowercase character, must not end
66  * with a '-', and must not contain consecutive dashes. Key names can
67  * be up to 32 characters long.
68  *
69  * Similar to GConf, the default values in GSettings schemas can be
70  * localized, but the localized values are stored in gettext catalogs
71  * and looked up with the domain that is specified in the
72  * <tag class="attribute">gettext-domain</tag> attribute of the
73  * <tag class="starttag">schemalist</tag> or <tag class="starttag">schema</tag>
74  * elements and the category that is specified in the l10n attribute of the
75  * <tag class="starttag">key</tag> element.
76  *
77  * GSettings uses schemas in a compact binary form that is created
78  * by the <link linkend="glib-compile-schemas">glib-compile-schemas</link>
79  * utility. The input is a schema description in an XML format that can be
80  * described by the following DTD:
81  * |[<![CDATA[
82  * <!ELEMENT schemalist (schema|enum)* >
83  * <!ATTLIST schemalist gettext-domain #IMPLIED >
84  *
85  * <!ELEMENT schema (key|child)* >
86  * <!ATTLIST schema id             CDATA #REQUIRED
87  *                  path           CDATA #IMPLIED
88  *                  gettext-domain CDATA #IMPLIED >
89  *
90  * <!-- defines an enumerated type -->
91  * <!-- each value element maps a nick to a numeric value -->
92  * <!ELEMENT enum (value*) >
93  * <!ATTLIST enum id CDATA #REQUIRED >
94  * <!ELEMENT value EMPTY >
95  * <!-- nick must be at least 2 characters long -->
96  * <!-- value must be parsable as a 32-bit integer -->
97  * <!ELEMENT value nick  #REQUIRED
98  *                 value #REQUIRED >
99  *
100  * <!ELEMENT key (default|summary?|description?|range?|choices?|aliases?) >
101  * <!-- name can only contain lowercase letters, numbers and '-' -->
102  * <!-- type must be a GVariant type string -->
103  * <!-- enum must be the id of an enum that has been defined earlier -->
104  * <!-- exactly one of enum or type must be given -->
105  * <!ATTLIST key name CDATA #REQUIRED
106  *               type CDATA #IMPLIED
107  *               enum CDATA #IMPLIED >
108  *
109  * <!-- the default value is specified a a serialized GVariant,
110  *      i.e. you have to include the quotes when specifying a string -->
111  * <!ELEMENT default (#PCDATA) >
112  * <!-- the presence of the l10n attribute marks a default value for
113  *      translation, its value is the gettext category to use -->
114  * <!-- if context is present, it specifies msgctxt to use -->
115  * <!ATTLIST default l10n (messages|time) #IMPLIED
116  *                   context CDATA #IMPLIED >
117  *
118  * <!ELEMENT summary (#PCDATA) >
119  * <!ELEMENT description (#PCDATA) >
120  *
121  * <!-- range is only allowed for keys with numeric type -->
122  * <!ELEMENT range EMPTY >
123  * <!-- min and max must be parseable as values of the key type and min < max -->
124  * <!ATTLIST range min CDATA #REQUIRED
125  *                 max CDATA #REQUIRED >
126  *
127  * <!-- choices is only allowed for keys with string or string array type -->
128  * <!ELEMENT choices (choice+) >
129  * <!-- each choice element specifies one possible value -->
130  * <!ELEMENT choice EMPTY >
131  * <!ATTLIST choice value CDATA #REQUIRED >
132  *
133  * <!-- aliases is only allowed for keys with enumerated type or with choices -->
134  * <!ELEMENT aliases (alias+) >
135  * <!-- each alias element specifies an alias for one of the possible values -->
136  * <!ELEMENT alias EMPTY >
137  * <!ATTLIST alias value CDATA #REQUIRED
138  *                 target CDATA #REQUIRED >
139  *
140  * <!ELEMENT child EMPTY >
141  * <!ATTLIST child name  CDATA #REQUIRED
142  *                 schema CDATA #REQUIRED >
143  * ]]>
144  * ]|
145  *
146  * At runtime, schemas are identified by their id (as specified
147  * in the <tag class="attribute">id</tag> attribute of the
148  * <tag class="starttag">schema</tag> element). The
149  * convention for schema ids is to use a dotted name, similar in
150  * style to a DBus bus name, e.g. "org.gnome.font-rendering".
151  *
152  * <example><title>Default values</title>
153  * <programlisting><![CDATA[
154  * <schemalist>
155  *   <schema id="org.gtk.test" path="/tests/" gettext-domain="test">
156  *
157  *     <key name="greeting" type="s">
158  *       <default l10n="messages">"Hello, earthlings"</default>
159  *       <summary>A greeting</summary>
160  *       <description>
161  *         Greeting of the invading martians
162  *       </description>
163  *     </key>
164  *
165  *     <key name="box" type="(ii)">
166  *       <default>(20,30)</default>
167  *     </key>
168  *
169  *   </schema>
170  * </schemalist>
171  * ]]></programlisting></example>
172  *
173  * <example><title>Ranges, choices and enumerated types</title>
174  * <programlisting><![CDATA[
175  * <schemalist>
176  *
177  *   <enum id="myenum">
178  *     <value nick="first" value="1"/>
179  *     <value nick="second" value="2"/>
180  *   </enum>
181  *
182  *   <schema id="org.gtk.test">
183  *
184  *     <key name="key-with-range" type="i">
185  *       <range min="1" max="100"/>
186  *       <default>10</default>
187  *     </key>
188  *
189  *     <key name="key-with-choices" type="s">
190  *       <choices>
191  *         <choice value='Elisabeth'/>
192  *         <choice value='Annabeth'/>
193  *         <choice value='Joe'/>
194  *       </choices>
195  *       <aliases>
196  *         <alias value="Annabeth" alias="Anna"/>
197  *         <alias value="Elisabeth" alias="Beth"/>
198  *       </aliases>
199  *       <default>'Joe'</default>
200  *     </key>
201  *
202  *     <key name="enumerated-key" enum="myenum">
203  *       <default>'first'</default>
204  *     </key>
205  *
206  *   </schema>
207  * </schemalist>
208  * ]]></programlisting></example>
209  *
210  * <refsect2>
211  *  <title>Binding</title>
212  *   <para>
213  *    A very convenient feature of GSettings lets you bind #GObject properties
214  *    directly to settings, using g_settings_bind(). Once a GObject property
215  *    has been bound to a setting, changes on either side are automatically
216  *    propagated to the other side. GSettings handles details like
217  *    mapping between GObject and GVariant types, and preventing infinite
218  *    cycles.
219  *   </para>
220  *   <para>
221  *    This makes it very easy to hook up a preferences dialog to the
222  *    underlying settings. To make this even more convenient, GSettings
223  *    looks for a boolean property with the name "sensitivity" and
224  *    automatically binds it to the writability of the bound setting.
225  *    If this 'magic' gets in the way, it can be suppressed with the
226  *    #G_SETTINGS_BIND_NO_SENSITIVITY flag.
227  *   </para>
228  *  </refsect2>
229  **/
230
231 struct _GSettingsPrivate
232 {
233   /* where the signals go... */
234   GMainContext *main_context;
235
236   GSettingsBackend *backend;
237   GSettingsSchema *schema;
238   gchar *schema_name;
239   gchar *context;
240   gchar *path;
241
242   GDelayedSettingsBackend *delayed;
243 };
244
245 enum
246 {
247   PROP_0,
248   PROP_BACKEND,
249   PROP_SCHEMA,
250   PROP_CONTEXT,
251   PROP_PATH,
252   PROP_HAS_UNAPPLIED,
253 };
254
255 enum
256 {
257   SIGNAL_WRITABLE_CHANGE_EVENT,
258   SIGNAL_WRITABLE_CHANGED,
259   SIGNAL_CHANGE_EVENT,
260   SIGNAL_CHANGED,
261   N_SIGNALS
262 };
263
264 static guint g_settings_signals[N_SIGNALS];
265
266 G_DEFINE_TYPE (GSettings, g_settings, G_TYPE_OBJECT)
267
268 /* Signals {{{1 */
269 static gboolean
270 g_settings_real_change_event (GSettings    *settings,
271                               const GQuark *keys,
272                               gint          n_keys)
273 {
274   gint i;
275
276   if (keys == NULL)
277     keys = g_settings_schema_list (settings->priv->schema, &n_keys);
278
279   for (i = 0; i < n_keys; i++)
280     g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
281                    keys[i], g_quark_to_string (keys[i]));
282
283   return FALSE;
284 }
285
286 static gboolean
287 g_settings_real_writable_change_event (GSettings *settings,
288                                        GQuark     key)
289 {
290   const GQuark *keys = &key;
291   gint n_keys = 1;
292   gint i;
293
294   if (key == 0)
295     keys = g_settings_schema_list (settings->priv->schema, &n_keys);
296
297   for (i = 0; i < n_keys; i++)
298     {
299       const gchar *string = g_quark_to_string (keys[i]);
300
301       g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED],
302                      keys[i], string);
303       g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
304                      keys[i], string);
305     }
306
307   return FALSE;
308 }
309
310 static void
311 settings_backend_changed (GSettingsBackend    *backend,
312                           GObject             *target,
313                           const gchar         *key,
314                           gpointer             origin_tag)
315 {
316   GSettings *settings = G_SETTINGS (target);
317   gboolean ignore_this;
318   gint i;
319
320   g_assert (settings->priv->backend == backend);
321
322   for (i = 0; key[i] == settings->priv->path[i]; i++);
323
324   if (settings->priv->path[i] == '\0' &&
325       g_settings_schema_has_key (settings->priv->schema, key + i))
326     {
327       GQuark quark;
328
329       quark = g_quark_from_string (key + i);
330       g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
331                      0, &quark, 1, &ignore_this);
332     }
333 }
334
335 static void
336 settings_backend_path_changed (GSettingsBackend *backend,
337                                GObject          *target,
338                                const gchar      *path,
339                                gpointer          origin_tag)
340 {
341   GSettings *settings = G_SETTINGS (target);
342   gboolean ignore_this;
343
344   g_assert (settings->priv->backend == backend);
345
346   if (g_str_has_prefix (settings->priv->path, path))
347     g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
348                    0, NULL, 0, &ignore_this);
349 }
350
351 static void
352 settings_backend_keys_changed (GSettingsBackend    *backend,
353                                GObject             *target,
354                                const gchar         *path,
355                                const gchar * const *items,
356                                gpointer             origin_tag)
357 {
358   GSettings *settings = G_SETTINGS (target);
359   gboolean ignore_this;
360   gint i;
361
362   g_assert (settings->priv->backend == backend);
363
364   for (i = 0; settings->priv->path[i] &&
365               settings->priv->path[i] == path[i]; i++);
366
367   if (path[i] == '\0')
368     {
369       GQuark quarks[256];
370       gint j, l = 0;
371
372       for (j = 0; items[j]; j++)
373          {
374            const gchar *item = items[j];
375            gint k;
376
377            for (k = 0; item[k] == settings->priv->path[i + k]; k++);
378
379            if (settings->priv->path[i + k] == '\0' &&
380                g_settings_schema_has_key (settings->priv->schema, item + k))
381              quarks[l++] = g_quark_from_string (item + k);
382
383            /* "256 quarks ought to be enough for anybody!"
384             * If this bites you, I'm sorry.  Please file a bug.
385             */
386            g_assert (l < 256);
387          }
388
389       if (l > 0)
390         g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
391                        0, quarks, l, &ignore_this);
392     }
393 }
394
395 static void
396 settings_backend_writable_changed (GSettingsBackend *backend,
397                                    GObject          *target,
398                                    const gchar      *key)
399 {
400   GSettings *settings = G_SETTINGS (target);
401   gboolean ignore_this;
402   gint i;
403
404   g_assert (settings->priv->backend == backend);
405
406   for (i = 0; key[i] == settings->priv->path[i]; i++);
407
408   if (settings->priv->path[i] == '\0' &&
409       g_settings_schema_has_key (settings->priv->schema, key + i))
410     g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
411                    0, g_quark_from_string (key + i), &ignore_this);
412 }
413
414 static void
415 settings_backend_path_writable_changed (GSettingsBackend *backend,
416                                         GObject          *target,
417                                         const gchar      *path)
418 {
419   GSettings *settings = G_SETTINGS (target);
420   gboolean ignore_this;
421
422   g_assert (settings->priv->backend == backend);
423
424   if (g_str_has_prefix (settings->priv->path, path))
425     g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
426                    0, (GQuark) 0, &ignore_this);
427 }
428
429 /* Properties, Construction, Destruction {{{1 */
430 static void
431 g_settings_set_property (GObject      *object,
432                          guint         prop_id,
433                          const GValue *value,
434                          GParamSpec   *pspec)
435 {
436   GSettings *settings = G_SETTINGS (object);
437
438   switch (prop_id)
439     {
440     case PROP_SCHEMA:
441       g_assert (settings->priv->schema_name == NULL);
442       settings->priv->schema_name = g_value_dup_string (value);
443       break;
444
445     case PROP_PATH:
446       settings->priv->path = g_value_dup_string (value);
447       break;
448
449     case PROP_CONTEXT:
450       settings->priv->context = g_value_dup_string (value);
451       break;
452
453     default:
454       g_assert_not_reached ();
455     }
456 }
457
458 static void
459 g_settings_get_property (GObject    *object,
460                          guint       prop_id,
461                          GValue     *value,
462                          GParamSpec *pspec)
463 {
464   GSettings *settings = G_SETTINGS (object);
465
466   switch (prop_id)
467     {
468      case PROP_SCHEMA:
469       g_value_set_object (value, settings->priv->schema);
470       break;
471
472      case PROP_HAS_UNAPPLIED:
473       g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
474       break;
475
476      default:
477       g_assert_not_reached ();
478     }
479 }
480
481 static void
482 g_settings_constructed (GObject *object)
483 {
484   GSettings *settings = G_SETTINGS (object);
485   const gchar *schema_path;
486
487   settings->priv->schema = g_settings_schema_new (settings->priv->schema_name);
488   schema_path = g_settings_schema_get_path (settings->priv->schema);
489
490   if (settings->priv->path && schema_path && strcmp (settings->priv->path, schema_path) != 0)
491     g_error ("settings object created with schema '%s' and path '%s', but "
492              "path '%s' is specified by schema",
493              settings->priv->schema_name, settings->priv->path, schema_path);
494
495   if (settings->priv->path == NULL)
496     {
497       if (schema_path == NULL)
498         g_error ("attempting to create schema '%s' without a path",
499                  settings->priv->schema_name);
500
501       settings->priv->path = g_strdup (schema_path);
502     }
503
504   settings->priv->backend = g_settings_backend_get_with_context (settings->priv->context);
505   g_settings_backend_watch (settings->priv->backend, G_OBJECT (settings),
506                             settings->priv->main_context,
507                             settings_backend_changed,
508                             settings_backend_path_changed,
509                             settings_backend_keys_changed,
510                             settings_backend_writable_changed,
511                             settings_backend_path_writable_changed);
512   g_settings_backend_subscribe (settings->priv->backend,
513                                 settings->priv->path);
514 }
515
516 static void
517 g_settings_finalize (GObject *object)
518 {
519   GSettings *settings = G_SETTINGS (object);
520
521   g_settings_backend_unsubscribe (settings->priv->backend,
522                                   settings->priv->path);
523   g_main_context_unref (settings->priv->main_context);
524   g_object_unref (settings->priv->backend);
525   g_object_unref (settings->priv->schema);
526   g_free (settings->priv->schema_name);
527   g_free (settings->priv->context);
528   g_free (settings->priv->path);
529 }
530
531 static void
532 g_settings_init (GSettings *settings)
533 {
534   settings->priv = G_TYPE_INSTANCE_GET_PRIVATE (settings,
535                                                 G_TYPE_SETTINGS,
536                                                 GSettingsPrivate);
537
538   settings->priv->main_context = g_main_context_get_thread_default ();
539
540   if (settings->priv->main_context == NULL)
541     settings->priv->main_context = g_main_context_default ();
542
543   g_main_context_ref (settings->priv->main_context);
544 }
545
546 static void
547 g_settings_class_init (GSettingsClass *class)
548 {
549   GObjectClass *object_class = G_OBJECT_CLASS (class);
550
551   class->writable_change_event = g_settings_real_writable_change_event;
552   class->change_event = g_settings_real_change_event;
553
554   object_class->set_property = g_settings_set_property;
555   object_class->get_property = g_settings_get_property;
556   object_class->constructed = g_settings_constructed;
557   object_class->finalize = g_settings_finalize;
558
559   g_type_class_add_private (object_class, sizeof (GSettingsPrivate));
560
561   /**
562    * GSettings::changed:
563    * @settings: the object on which the signal was emitted
564    * @key: the name of the key that changed
565    *
566    * The "changed" signal is emitted when a key has potentially changed.
567    * You should call one of the g_settings_get() calls to check the new
568    * value.
569    *
570    * This signal supports detailed connections.  You can connect to the
571    * detailed signal "changed::x" in order to only receive callbacks
572    * when key "x" changes.
573    */
574   g_settings_signals[SIGNAL_CHANGED] =
575     g_signal_new ("changed", G_TYPE_SETTINGS,
576                   G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
577                   G_STRUCT_OFFSET (GSettingsClass, changed),
578                   NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
579                   1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
580
581   /**
582    * GSettings::change-event:
583    * @settings: the object on which the signal was emitted
584    * @keys: an array of #GQuark<!-- -->s for the changed keys, or %NULL
585    * @n_keys: the length of the @keys array, or 0
586    * @returns: %TRUE to stop other handlers from being invoked for the
587    *           event. FALSE to propagate the event further.
588    *
589    * The "change-event" signal is emitted once per change event that
590    * affects this settings object.  You should connect to this signal
591    * only if you are interested in viewing groups of changes before they
592    * are split out into multiple emissions of the "changed" signal.
593    * For most use cases it is more appropriate to use the "changed" signal.
594    *
595    * In the event that the change event applies to one or more specified
596    * keys, @keys will be an array of #GQuark of length @n_keys.  In the
597    * event that the change event applies to the #GSettings object as a
598    * whole (ie: potentially every key has been changed) then @keys will
599    * be %NULL and @n_keys will be 0.
600    *
601    * The default handler for this signal invokes the "changed" signal
602    * for each affected key.  If any other connected handler returns
603    * %TRUE then this default functionality will be supressed.
604    */
605   g_settings_signals[SIGNAL_CHANGE_EVENT] =
606     g_signal_new ("change-event", G_TYPE_SETTINGS,
607                   G_SIGNAL_RUN_LAST,
608                   G_STRUCT_OFFSET (GSettingsClass, change_event),
609                   g_signal_accumulator_true_handled, NULL,
610                   _gio_marshal_BOOL__POINTER_INT,
611                   G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);
612
613   /**
614    * GSettings::writable-changed:
615    * @settings: the object on which the signal was emitted
616    * @key: the key
617    *
618    * The "writable-changed" signal is emitted when the writability of a
619    * key has potentially changed.  You should call
620    * g_settings_is_writable() in order to determine the new status.
621    *
622    * This signal supports detailed connections.  You can connect to the
623    * detailed signal "writable-changed::x" in order to only receive
624    * callbacks when the writability of "x" changes.
625    */
626   g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
627     g_signal_new ("writable-changed", G_TYPE_SETTINGS,
628                   G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
629                   G_STRUCT_OFFSET (GSettingsClass, changed),
630                   NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
631                   1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);
632
633   /**
634    * GSettings::writable-change-event:
635    * @settings: the object on which the signal was emitted
636    * @key: the quark of the key, or 0
637    * @returns: %TRUE to stop other handlers from being invoked for the
638    *           event. FALSE to propagate the event further.
639    *
640    * The "writable-change-event" signal is emitted once per writability
641    * change event that affects this settings object.  You should connect
642    * to this signal if you are interested in viewing groups of changes
643    * before they are split out into multiple emissions of the
644    * "writable-changed" signal.  For most use cases it is more
645    * appropriate to use the "writable-changed" signal.
646    *
647    * In the event that the writability change applies only to a single
648    * key, @key will be set to the #GQuark for that key.  In the event
649    * that the writability change affects the entire settings object,
650    * @key will be 0.
651    *
652    * The default handler for this signal invokes the "writable-changed"
653    * and "changed" signals for each affected key.  This is done because
654    * changes in writability might also imply changes in value (if for
655    * example, a new mandatory setting is introduced).  If any other
656    * connected handler returns %TRUE then this default functionality
657    * will be supressed.
658    */
659   g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
660     g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
661                   G_SIGNAL_RUN_LAST,
662                   G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
663                   g_signal_accumulator_true_handled, NULL,
664                   _gio_marshal_BOOLEAN__UINT, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
665
666   /**
667    * GSettings:context:
668    *
669    * The name of the context that the settings are stored in.
670    */
671   g_object_class_install_property (object_class, PROP_CONTEXT,
672     g_param_spec_string ("context",
673                          P_("Context name"),
674                          P_("The name of the context for this settings object"),
675                          "", G_PARAM_CONSTRUCT_ONLY |
676                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
677
678   /**
679    * GSettings:schema:
680    *
681    * The name of the schema that describes the types of keys
682    * for this #GSettings object.
683    */
684   g_object_class_install_property (object_class, PROP_SCHEMA,
685     g_param_spec_string ("schema",
686                          P_("Schema name"),
687                          P_("The name of the schema for this settings object"),
688                          NULL,
689                          G_PARAM_CONSTRUCT_ONLY |
690                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
691
692    /**
693     * GSettings:path:
694     *
695     * The path within the backend where the settings are stored.
696     */
697    g_object_class_install_property (object_class, PROP_PATH,
698      g_param_spec_string ("path",
699                           P_("Base path"),
700                           P_("The path within the backend where the settings are"),
701                           NULL,
702                           G_PARAM_CONSTRUCT_ONLY |
703                           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
704
705    /**
706     * GSettings:has-unapplied:
707     *
708     * If this property is %TRUE, the #GSettings object has outstanding
709     * changes that will be applied when g_settings_apply() is called.
710     */
711    g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
712      g_param_spec_boolean ("has-unapplied",
713                            P_("Has unapplied changes"),
714                            P_("TRUE if there are outstanding changes to apply()"),
715                            FALSE,
716                            G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
717
718 }
719
720 /* Construction (new, new_with_path, etc.) {{{1 */
721 /**
722  * g_settings_new:
723  * @schema: the name of the schema
724  * @returns: a new #GSettings object
725  *
726  * Creates a new #GSettings object with a given schema.
727  *
728  * Signals on the newly created #GSettings object will be dispatched
729  * via the thread-default #GMainContext in effect at the time of the
730  * call to g_settings_new().  The new #GSettings will hold a reference
731  * on the context.  See g_main_context_push_thread_default().
732  *
733  * Since: 2.26
734  */
735 GSettings *
736 g_settings_new (const gchar *schema)
737 {
738   g_return_val_if_fail (schema != NULL, NULL);
739
740   return g_object_new (G_TYPE_SETTINGS,
741                        "schema", schema,
742                        NULL);
743 }
744
745 /**
746  * g_settings_new_with_path:
747  * @schema: the name of the schema
748  * @path: the path to use
749  * @returns: a new #GSettings object
750  *
751  * Creates a new #GSettings object with a given schema and path.
752  *
753  * You only need to do this if you want to directly create a settings
754  * object with a schema that doesn't have a specified path of its own.
755  * That's quite rare.
756  *
757  * It is a programmer error to call this function for a schema that
758  * has an explicitly specified path.
759  *
760  * Since: 2.26
761  */
762 GSettings *
763 g_settings_new_with_path (const gchar *schema,
764                           const gchar *path)
765 {
766   g_return_val_if_fail (schema != NULL, NULL);
767   g_return_val_if_fail (path != NULL, NULL);
768
769   return g_object_new (G_TYPE_SETTINGS,
770                        "schema", schema,
771                        "path", path,
772                        NULL);
773 }
774
775 /**
776  * g_settings_new_with_context:
777  * @schema: the name of the schema
778  * @context: the context to use
779  * @returns: a new #GSettings object
780  *
781  * Creates a new #GSettings object with a given schema and context.
782  *
783  * Creating settings objects with a context allow accessing settings
784  * from a database other than the usual one.  For example, it may make
785  * sense to specify "defaults" in order to get a settings object that
786  * modifies the system default settings instead of the settings for this
787  * user.
788  *
789  * It is a programmer error to call this function for an unsupported
790  * context.  Use g_settings_supports_context() to determine if a context
791  * is supported if you are unsure.
792  *
793  * Since: 2.26
794  */
795 GSettings *
796 g_settings_new_with_context (const gchar *schema,
797                              const gchar *context)
798 {
799   g_return_val_if_fail (schema != NULL, NULL);
800   g_return_val_if_fail (context != NULL, NULL);
801
802   return g_object_new (G_TYPE_SETTINGS,
803                        "schema", schema,
804                        "context", context,
805                        NULL);
806 }
807
808 /**
809  * g_settings_new_with_context_and_path:
810  * @schema: the name of the schema
811  * @path: the path to use
812  * @returns: a new #GSettings object
813  *
814  * Creates a new #GSettings object with a given schema, context and
815  * path.
816  *
817  * This is a mix of g_settings_new_with_context() and
818  * g_settings_new_with_path().
819  *
820  * Since: 2.26
821  */
822 GSettings *
823 g_settings_new_with_context_and_path (const gchar *schema,
824                                       const gchar *context,
825                                       const gchar *path)
826 {
827   g_return_val_if_fail (schema != NULL, NULL);
828   g_return_val_if_fail (context != NULL, NULL);
829   g_return_val_if_fail (path != NULL, NULL);
830
831   return g_object_new (G_TYPE_SETTINGS,
832                        "schema", schema,
833                         "context", context,
834                         "path", path,
835                         NULL);
836 }
837
838 /* Internal read/write utilities, enum conversion, validation {{{1 */
839 typedef struct
840 {
841   GSettings *settings;
842   const gchar *key;
843
844   GSettingsSchema *schema;
845
846   gboolean is_enum;
847   const guint32 *strinfo;
848   gsize strinfo_length;
849
850   const gchar *unparsed;
851   gchar lc_char;
852
853   const GVariantType *type;
854   GVariant *minimum, *maximum;
855   GVariant *default_value;
856 } GSettingsKeyInfo;
857
858 static void
859 g_settings_get_key_info (GSettingsKeyInfo *info,
860                          GSettings        *settings,
861                          const gchar      *key)
862 {
863   GVariantIter *iter;
864   GVariant *data;
865   guchar code;
866
867   memset (info, 0, sizeof *info);
868
869   iter = g_settings_schema_get_value (settings->priv->schema, key);
870
871   info->default_value = g_variant_iter_next_value (iter);
872   info->type = g_variant_get_type (info->default_value);
873   info->schema = settings->priv->schema;
874   info->settings = settings;
875   info->key = key;
876
877   while (g_variant_iter_next (iter, "(y*)", &code, &data))
878     {
879       switch (code)
880         {
881         case 'l':
882           /* translation requested */
883           g_variant_get (data, "(y&s)", &info->lc_char, &info->unparsed);
884           break;
885
886         case 'e':
887           /* enumerated types, ... */
888           info->is_enum = TRUE;
889         case 'c':
890           /* ..., choices, aliases */
891           info->strinfo = g_variant_get_fixed_array (data,
892                                                      &info->strinfo_length,
893                                                      sizeof (guint32));
894           break;
895
896         case 'r':
897           g_variant_get (data, "(**)", &info->minimum, &info->maximum);
898           break;
899
900         default:
901           g_warning ("unknown schema extension '%c'", code);
902           break;
903         }
904
905       g_variant_unref (data);
906     }
907
908   g_variant_iter_free (iter);
909 }
910
911 static void
912 g_settings_free_key_info (GSettingsKeyInfo *info)
913 {
914   if (info->minimum)
915     g_variant_unref (info->minimum);
916
917   if (info->maximum)
918     g_variant_unref (info->maximum);
919
920   g_variant_unref (info->default_value);
921 }
922
923 static gboolean
924 g_settings_write_to_backend (GSettingsKeyInfo *info,
925                              GVariant         *value)
926 {
927   gboolean success;
928   gchar *path;
929
930   path = g_strconcat (info->settings->priv->path, info->key, NULL);
931   success = g_settings_backend_write (info->settings->priv->backend,
932                                       path, value, NULL);
933   g_free (path);
934
935   return success;
936 }
937
938 static gboolean
939 g_settings_type_check (GSettingsKeyInfo *info,
940                        GVariant         *value)
941 {
942   g_return_val_if_fail (value != NULL, FALSE);
943
944   return g_variant_is_of_type (value, info->type);
945 }
946
947 static gboolean
948 g_settings_range_check (GSettingsKeyInfo *info,
949                         GVariant         *value)
950 {
951   if (info->minimum == NULL && info->strinfo == NULL)
952     return TRUE;
953
954   if (g_variant_is_container (value))
955     {
956       gboolean ok = TRUE;
957       GVariantIter iter;
958       GVariant *child;
959
960       g_variant_iter_init (&iter, value);
961       while (ok && (child = g_variant_iter_next_value (&iter)))
962         {
963           ok = g_settings_range_check (info, value);
964           g_variant_unref (child);
965         }
966
967       return ok;
968     }
969
970   if (info->minimum)
971     {
972       return g_variant_compare (info->minimum, value) <= 0 &&
973              g_variant_compare (value, info->maximum) <= 0;
974     }
975
976   return strinfo_is_string_valid (info->strinfo,
977                                   info->strinfo_length,
978                                   g_variant_get_string (value, NULL));
979 }
980
981 static GVariant *
982 g_settings_range_fixup (GSettingsKeyInfo *info,
983                         GVariant         *value)
984 {
985   const gchar *target;
986
987   if (g_settings_range_check (info, value))
988     return g_variant_ref (value);
989
990   if (info->strinfo == NULL)
991     return NULL;
992
993   if (g_variant_is_container (value))
994     {
995       GVariantBuilder builder;
996       GVariantIter iter;
997       GVariant *child;
998
999       g_variant_builder_init (&builder, g_variant_get_type (value));
1000
1001       while ((child = g_variant_iter_next_value (&iter)))
1002         {
1003           GVariant *fixed;
1004
1005           fixed = g_settings_range_fixup (info, child);
1006           g_variant_unref (child);
1007
1008           if (fixed == NULL)
1009             {
1010               g_variant_builder_clear (&builder);
1011               return NULL;
1012             }
1013
1014           g_variant_builder_add_value (&builder, fixed);
1015           g_variant_unref (fixed);
1016         }
1017
1018       return g_variant_ref_sink (g_variant_builder_end (&builder));
1019     }
1020
1021   target = strinfo_string_from_alias (info->strinfo, info->strinfo_length,
1022                                       g_variant_get_string (value, NULL));
1023   return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
1024 }
1025
1026 static GVariant *
1027 g_settings_read_from_backend (GSettingsKeyInfo *info)
1028 {
1029   GVariant *value;
1030   GVariant *fixup;
1031   gchar *path;
1032
1033   path = g_strconcat (info->settings->priv->path, info->key, NULL);
1034   value = g_settings_backend_read (info->settings->priv->backend,
1035                                    path, info->type, FALSE);
1036   g_free (path);
1037
1038   if (value != NULL)
1039     {
1040       fixup = g_settings_range_fixup (info, value);
1041       g_variant_unref (value);
1042     }
1043   else
1044     fixup = NULL;
1045
1046   return fixup;
1047 }
1048
1049 static GVariant *
1050 g_settings_get_translated_default (GSettingsKeyInfo *info)
1051 {
1052   const gchar *translated;
1053   GError *error = NULL;
1054   const gchar *domain;
1055   GVariant *value;
1056
1057   if (info->lc_char == '\0')
1058     /* translation not requested for this key */
1059     return NULL;
1060
1061   domain = g_settings_schema_get_gettext_domain (info->schema);
1062
1063   if (info->lc_char == 't')
1064     translated = g_dcgettext (domain, info->unparsed, LC_TIME);
1065   else
1066     translated = g_dgettext (domain, info->unparsed);
1067
1068   if (translated == info->unparsed)
1069     /* the default value was not translated */
1070     return NULL;
1071
1072   /* try to parse the translation of the unparsed default */
1073   value = g_variant_parse (info->type, translated, NULL, NULL, &error);
1074
1075   if (value == NULL)
1076     {
1077       g_warning ("Failed to parse translated string `%s' for "
1078                  "key `%s' in schema `%s': %s", info->unparsed, info->key,
1079                  info->settings->priv->schema_name, error->message);
1080       g_warning ("Using untranslated default instead.");
1081       g_error_free (error);
1082     }
1083
1084   else if (!g_settings_range_check (info, value))
1085     {
1086       g_warning ("Translated default `%s' for key `%s' in schema `%s' "
1087                  "is outside of valid range", info->unparsed, info->key,
1088                  info->settings->priv->schema_name);
1089       g_variant_unref (value);
1090       value = NULL;
1091     }
1092
1093   return value;
1094 }
1095
1096 static gint
1097 g_settings_to_enum (GSettingsKeyInfo *info,
1098                     GVariant         *value)
1099 {
1100   gboolean it_worked;
1101   guint result;
1102
1103   it_worked = strinfo_enum_from_string (info->strinfo, info->strinfo_length,
1104                                         g_variant_get_string (value, NULL),
1105                                         &result);
1106
1107   /* 'value' can only come from the backend after being filtered for validity,
1108    * from the translation after being filtered for validity, or from the schema
1109    * itself (which the schema compiler checks for validity).  If this assertion
1110    * fails then it's really a bug in GSettings or the schema compiler...
1111    */
1112   g_assert (it_worked);
1113
1114   return result;
1115 }
1116
1117 static GVariant *
1118 g_settings_from_enum (GSettingsKeyInfo *info,
1119                       gint              value)
1120 {
1121   const gchar *string;
1122
1123   string = strinfo_string_from_enum (info->strinfo,
1124                                      info->strinfo_length,
1125                                      value);
1126
1127   if (string == NULL)
1128     return NULL;
1129
1130   return g_variant_ref_sink (g_variant_new_string (string));
1131 }
1132
1133 /* Public Get/Set API {{{1 (get, get_value, set, set_value) */
1134 /**
1135  * g_settings_get_value:
1136  * @settings: a #GSettings object
1137  * @key: the key to get the value for
1138  * @returns: a new #GVariant
1139  *
1140  * Gets the value that is stored in @settings for @key.
1141  *
1142  * It is a programmer error to give a @key that isn't valid for
1143  * @settings.
1144  *
1145  * Since: 2.26
1146  */
1147 GVariant *
1148 g_settings_get_value (GSettings   *settings,
1149                       const gchar *key)
1150 {
1151   GSettingsKeyInfo info;
1152   GVariant *value;
1153
1154   g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1155   g_return_val_if_fail (key != NULL, NULL);
1156
1157   g_settings_get_key_info (&info, settings, key);
1158   value = g_settings_read_from_backend (&info);
1159
1160   if (value == NULL)
1161     value = g_settings_get_translated_default (&info);
1162
1163   if (value == NULL)
1164     value = g_variant_ref (info.default_value);
1165
1166   g_settings_free_key_info (&info);
1167
1168   return value;
1169 }
1170
1171 /**
1172  * g_settings_get_enum:
1173  * @settings: a #GSettings object
1174  * @key: the key to get the value for
1175  * @returns: the enum value
1176  *
1177  * Gets the value that is stored in @settings for @key and converts it
1178  * to the enum value that it represents.
1179  *
1180  * In order to use this function the type of the value must be a string
1181  * and it must be marked in the schema file as an enumerated type.
1182  *
1183  * It is a programmer error to give a @key that isn't valid for
1184  * @settings, or is not marked as an enumerated type in the schema.
1185  *
1186  * If the value stored in the configuration database is not a valid
1187  * value for the enumerated type then this function will return the
1188  * default value.
1189  *
1190  * Since: 2.26
1191  **/
1192 gint
1193 g_settings_get_enum (GSettings   *settings,
1194                      const gchar *key)
1195 {
1196   GSettingsKeyInfo info;
1197   GVariant *value;
1198   gint result;
1199
1200   g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1201   g_return_val_if_fail (key != NULL, -1);
1202
1203   g_settings_get_key_info (&info, settings, key);
1204
1205   if (!info.is_enum)
1206     {
1207       g_critical ("g_settings_get_enum() called on key `%s' which is not "
1208                   "associated with an enumerated type", info.key);
1209       g_settings_free_key_info (&info);
1210       return -1;
1211     }
1212
1213   value = g_settings_read_from_backend (&info);
1214
1215   if (value == NULL)
1216     value = g_settings_get_translated_default (&info);
1217
1218   if (value == NULL)
1219     value = g_variant_ref (info.default_value);
1220
1221   result = g_settings_to_enum (&info, value);
1222   g_settings_free_key_info (&info);
1223   g_variant_unref (value);
1224
1225   return result;
1226 }
1227
1228 /**
1229  * g_settings_set_enum:
1230  * @settings: a #GSettings object
1231  * @key: a key, within @settings
1232  * @value: an enumerated value
1233  * Returns: %TRUE, if the set succeeds
1234  *
1235  * Looks up the enumerated type nick for @value and writes it to @key,
1236  * within @settings.
1237  *
1238  * It is a programmer error for @key to not be listed in the schema or
1239  * for it not to be tagged as an enumerated type, or for @value not to
1240  * be a valid value for the named type.
1241  *
1242  * After performing the write, accessing @key directly
1243  * g_settings_get_string() will return the 'nick' associated with
1244  * @value.
1245  **/
1246 gboolean
1247 g_settings_set_enum (GSettings   *settings,
1248                      const gchar *key,
1249                      gint         value)
1250 {
1251   GSettingsKeyInfo info;
1252   GVariant *variant;
1253   gboolean success;
1254
1255   g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1256   g_return_val_if_fail (key != NULL, FALSE);
1257
1258   g_settings_get_key_info (&info, settings, key);
1259
1260   if (!info.is_enum)
1261     {
1262       g_critical ("g_settings_set_enum() called on key `%s' which is not "
1263                   "associated with an enumerated type", info.key);
1264       return FALSE;
1265     }
1266
1267   if (!(variant = g_settings_from_enum (&info, value)))
1268     {
1269       g_critical ("g_settings_set_enum(): invalid enum value %d for key `%s' "
1270                   "in schema `%s'.  Doing nothing.", value, info.key,
1271                   info.settings->priv->schema_name);
1272       g_settings_free_key_info (&info);
1273       return FALSE;
1274     }
1275
1276   success = g_settings_write_to_backend (&info, variant);
1277   g_settings_free_key_info (&info);
1278   g_variant_unref (variant);
1279
1280   return success;
1281 }
1282
1283 /**
1284  * g_settings_set_value:
1285  * @settings: a #GSettings object
1286  * @key: the name of the key to set
1287  * @value: a #GVariant of the correct type
1288  * @returns: %TRUE if setting the key succeeded,
1289  *     %FALSE if the key was not writable
1290  *
1291  * Sets @key in @settings to @value.
1292  *
1293  * It is a programmer error to give a @key that isn't valid for
1294  * @settings.  It is a programmer error to give a @value of the
1295  * incorrect type.
1296  *
1297  * If @value is floating then this function consumes the reference.
1298  *
1299  * Since: 2.26
1300  **/
1301 gboolean
1302 g_settings_set_value (GSettings   *settings,
1303                       const gchar *key,
1304                       GVariant    *value)
1305 {
1306   GSettingsKeyInfo info;
1307
1308   g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1309   g_return_val_if_fail (key != NULL, FALSE);
1310
1311   g_settings_get_key_info (&info, settings, key);
1312   g_return_val_if_fail (g_settings_type_check (&info, value), FALSE);
1313   g_return_val_if_fail (g_settings_range_check (&info, value), FALSE);
1314   g_settings_free_key_info (&info);
1315
1316   return g_settings_write_to_backend (&info, value);
1317 }
1318
1319 /**
1320  * g_settings_get:
1321  * @settings: a #GSettings object
1322  * @key: the key to get the value for
1323  * @format: a #GVariant format string
1324  * @...: arguments as per @format
1325  *
1326  * Gets the value that is stored at @key in @settings.
1327  *
1328  * A convenience function that combines g_settings_get_value() with
1329  * g_variant_get().
1330  *
1331  * It is a programmer error to pass a @key that isn't valid for
1332  * @settings or a @format_string that doesn't match the type of @key according
1333  * to the schema of @settings.
1334  *
1335  * Since: 2.26
1336  */
1337 void
1338 g_settings_get (GSettings   *settings,
1339                 const gchar *key,
1340                 const gchar *format,
1341                 ...)
1342 {
1343   GVariant *value;
1344   va_list ap;
1345
1346   value = g_settings_get_value (settings, key);
1347
1348   va_start (ap, format);
1349   g_variant_get_va (value, format, NULL, &ap);
1350   va_end (ap);
1351
1352   g_variant_unref (value);
1353 }
1354
1355 /**
1356  * g_settings_set:
1357  * @settings: a #GSettings object
1358  * @key: the name of the key to set
1359  * @format: a #GVariant format string
1360  * @...: arguments as per @format
1361  * @returns: %TRUE if setting the key succeeded,
1362  *     %FALSE if the key was not writable
1363  *
1364  * Sets @key in @settings to @value.
1365  *
1366  * A convenience function that combines g_settings_set_value() with
1367  * g_variant_new().
1368  *
1369  * It is a programmer error to pass a @key that isn't valid for
1370  * @settings or a @format that doesn't match the type of @key according
1371  * to the schema of @settings.
1372  *
1373  * Since: 2.26
1374  */
1375 gboolean
1376 g_settings_set (GSettings   *settings,
1377                 const gchar *key,
1378                 const gchar *format,
1379                 ...)
1380 {
1381   GVariant *value;
1382   va_list ap;
1383
1384   va_start (ap, format);
1385   value = g_variant_new_va (format, NULL, &ap);
1386   va_end (ap);
1387
1388   return g_settings_set_value (settings, key, value);
1389 }
1390
1391 /* Convenience API (get, set_string, int, double, boolean, strv) {{{1 */
1392 /**
1393  * g_settings_get_string:
1394  * @settings: a #GSettings object
1395  * @key: the key to get the value for
1396  * @returns: a newly-allocated string
1397  *
1398  * Gets the value that is stored at @key in @settings.
1399  *
1400  * A convenience variant of g_settings_get() for strings.
1401  *
1402  * It is a programmer error to pass a @key that isn't valid for
1403  * @settings or is not of type string.
1404  *
1405  * Since: 2.26
1406  */
1407 gchar *
1408 g_settings_get_string (GSettings   *settings,
1409                        const gchar *key)
1410 {
1411   GVariant *value;
1412   gchar *result;
1413
1414   value = g_settings_get_value (settings, key);
1415   result = g_variant_dup_string (value, NULL);
1416   g_variant_unref (value);
1417
1418   return result;
1419 }
1420
1421 /**
1422  * g_settings_set_string:
1423  * @settings: a #GSettings object
1424  * @key: the name of the key to set
1425  * @value: the value to set it to
1426  * @returns: %TRUE if setting the key succeeded,
1427  *     %FALSE if the key was not writable
1428  *
1429  * Sets @key in @settings to @value.
1430  *
1431  * A convenience variant of g_settings_set() for strings.
1432  *
1433  * It is a programmer error to pass a @key that isn't valid for
1434  * @settings or is not of type string.
1435  *
1436  * Since: 2.26
1437  */
1438 gboolean
1439 g_settings_set_string (GSettings   *settings,
1440                        const gchar *key,
1441                        const gchar *value)
1442 {
1443   return g_settings_set_value (settings, key, g_variant_new_string (value));
1444 }
1445
1446 /**
1447  * g_settings_get_int:
1448  * @settings: a #GSettings object
1449  * @key: the key to get the value for
1450  * @returns: an integer
1451  *
1452  * Gets the value that is stored at @key in @settings.
1453  *
1454  * A convenience variant of g_settings_get() for 32-bit integers.
1455  *
1456  * It is a programmer error to pass a @key that isn't valid for
1457  * @settings or is not of type int32.
1458  *
1459  * Since: 2.26
1460  */
1461 gint
1462 g_settings_get_int (GSettings   *settings,
1463                     const gchar *key)
1464 {
1465   GVariant *value;
1466   gint result;
1467
1468   value = g_settings_get_value (settings, key);
1469   result = g_variant_get_int32 (value);
1470   g_variant_unref (value);
1471
1472   return result;
1473 }
1474
1475 /**
1476  * g_settings_set_int:
1477  * @settings: a #GSettings object
1478  * @key: the name of the key to set
1479  * @value: the value to set it to
1480  * @returns: %TRUE if setting the key succeeded,
1481  *     %FALSE if the key was not writable
1482  *
1483  * Sets @key in @settings to @value.
1484  *
1485  * A convenience variant of g_settings_set() for 32-bit integers.
1486  *
1487  * It is a programmer error to pass a @key that isn't valid for
1488  * @settings or is not of type int32.
1489  *
1490  * Since: 2.26
1491  */
1492 gboolean
1493 g_settings_set_int (GSettings   *settings,
1494                     const gchar *key,
1495                     gint         value)
1496 {
1497   return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1498 }
1499
1500 /**
1501  * g_settings_get_double:
1502  * @settings: a #GSettings object
1503  * @key: the key to get the value for
1504  * @returns: a double
1505  *
1506  * Gets the value that is stored at @key in @settings.
1507  *
1508  * A convenience variant of g_settings_get() for doubles.
1509  *
1510  * It is a programmer error to pass a @key that isn't valid for
1511  * @settings or is not of type double.
1512  *
1513  * Since: 2.26
1514  */
1515 gdouble
1516 g_settings_get_double (GSettings   *settings,
1517                        const gchar *key)
1518 {
1519   GVariant *value;
1520   gdouble result;
1521
1522   value = g_settings_get_value (settings, key);
1523   result = g_variant_get_double (value);
1524   g_variant_unref (value);
1525
1526   return result;
1527 }
1528
1529 /**
1530  * g_settings_set_double:
1531  * @settings: a #GSettings object
1532  * @key: the name of the key to set
1533  * @value: the value to set it to
1534  * @returns: %TRUE if setting the key succeeded,
1535  *     %FALSE if the key was not writable
1536  *
1537  * Sets @key in @settings to @value.
1538  *
1539  * A convenience variant of g_settings_set() for doubles.
1540  *
1541  * It is a programmer error to pass a @key that isn't valid for
1542  * @settings or is not of type double.
1543  *
1544  * Since: 2.26
1545  */
1546 gboolean
1547 g_settings_set_double (GSettings   *settings,
1548                        const gchar *key,
1549                        gdouble      value)
1550 {
1551   return g_settings_set_value (settings, key, g_variant_new_double (value));
1552 }
1553
1554 /**
1555  * g_settings_get_boolean:
1556  * @settings: a #GSettings object
1557  * @key: the key to get the value for
1558  * @returns: a boolean
1559  *
1560  * Gets the value that is stored at @key in @settings.
1561  *
1562  * A convenience variant of g_settings_get() for booleans.
1563  *
1564  * It is a programmer error to pass a @key that isn't valid for
1565  * @settings or is not of type boolean.
1566  *
1567  * Since: 2.26
1568  */
1569 gboolean
1570 g_settings_get_boolean (GSettings  *settings,
1571                        const gchar *key)
1572 {
1573   GVariant *value;
1574   gboolean result;
1575
1576   value = g_settings_get_value (settings, key);
1577   result = g_variant_get_boolean (value);
1578   g_variant_unref (value);
1579
1580   return result;
1581 }
1582
1583 /**
1584  * g_settings_set_boolean:
1585  * @settings: a #GSettings object
1586  * @key: the name of the key to set
1587  * @value: the value to set it to
1588  * @returns: %TRUE if setting the key succeeded,
1589  *     %FALSE if the key was not writable
1590  *
1591  * Sets @key in @settings to @value.
1592  *
1593  * A convenience variant of g_settings_set() for booleans.
1594  *
1595  * It is a programmer error to pass a @key that isn't valid for
1596  * @settings or is not of type boolean.
1597  *
1598  * Since: 2.26
1599  */
1600 gboolean
1601 g_settings_set_boolean (GSettings  *settings,
1602                        const gchar *key,
1603                        gboolean     value)
1604 {
1605   return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1606 }
1607
1608 /**
1609  * g_settings_get_strv:
1610  * @settings: a #GSettings object
1611  * @key: the key to get the value for
1612  * @returns: a newly-allocated, %NULL-terminated array of strings
1613  *
1614  * Gets the value that is stored at @key in @settings.
1615  *
1616  * A convenience variant of g_settings_get() for string arrays.
1617  *
1618  * It is a programmer error to pass a @key that isn't valid for
1619  * @settings or is not of type 'string array'.
1620  *
1621  * Since: 2.26
1622  */
1623 gchar **
1624 g_settings_get_strv (GSettings   *settings,
1625                      const gchar *key)
1626 {
1627   GVariant *value;
1628   gchar **result;
1629
1630   value = g_settings_get_value (settings, key);
1631   result = g_variant_dup_strv (value, NULL);
1632   g_variant_unref (value);
1633
1634   return result;
1635 }
1636
1637 /**
1638  * g_settings_set_strv:
1639  * @settings: a #GSettings object
1640  * @key: the name of the key to set
1641  * @value: (allow-none): the value to set it to, or %NULL
1642  * @returns: %TRUE if setting the key succeeded,
1643  *     %FALSE if the key was not writable
1644  *
1645  * Sets @key in @settings to @value.
1646  *
1647  * A convenience variant of g_settings_set() for string arrays.  If
1648  * @value is %NULL, then @key is set to be the empty array.
1649  *
1650  * It is a programmer error to pass a @key that isn't valid for
1651  * @settings or is not of type 'string array'.
1652  *
1653  * Since: 2.26
1654  */
1655 gboolean
1656 g_settings_set_strv (GSettings           *settings,
1657                      const gchar         *key,
1658                      const gchar * const *value)
1659 {
1660   GVariant *array;
1661
1662   if (value != NULL)
1663     array = g_variant_new_strv (value, -1);
1664   else
1665     array = g_variant_new_strv (NULL, 0);
1666
1667   return g_settings_set_value (settings, key, array);
1668 }
1669 /* Delayed apply (delay, apply, revert, get_has_unapplied) {{{1 */
1670 /**
1671  * g_settings_delay:
1672  * @settings: a #GSettings object
1673  *
1674  * Changes the #GSettings object into 'delay-apply' mode. In this
1675  * mode, changes to @settings are not immediately propagated to the
1676  * backend, but kept locally until g_settings_apply() is called.
1677  *
1678  * Since: 2.26
1679  */
1680 void
1681 g_settings_delay (GSettings *settings)
1682 {
1683   g_return_if_fail (G_IS_SETTINGS (settings));
1684
1685   if (settings->priv->delayed)
1686     return;
1687
1688   settings->priv->delayed =
1689     g_delayed_settings_backend_new (settings->priv->backend,
1690                                     settings,
1691                                     settings->priv->main_context);
1692   g_settings_backend_unwatch (settings->priv->backend, G_OBJECT (settings));
1693   g_object_unref (settings->priv->backend);
1694
1695   settings->priv->backend = G_SETTINGS_BACKEND (settings->priv->delayed);
1696   g_settings_backend_watch (settings->priv->backend, G_OBJECT (settings),
1697                             settings->priv->main_context,
1698                             settings_backend_changed,
1699                             settings_backend_path_changed,
1700                             settings_backend_keys_changed,
1701                             settings_backend_writable_changed,
1702                             settings_backend_path_writable_changed);
1703 }
1704
1705 /**
1706  * g_settings_apply:
1707  * @settings: a #GSettings instance
1708  *
1709  * Applies any changes that have been made to the settings.  This
1710  * function does nothing unless @settings is in 'delay-apply' mode;
1711  * see g_settings_set_delay_apply().  In the normal case settings are
1712  * always applied immediately.
1713  **/
1714 void
1715 g_settings_apply (GSettings *settings)
1716 {
1717   if (settings->priv->delayed)
1718     {
1719       GDelayedSettingsBackend *delayed;
1720
1721       delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
1722       g_delayed_settings_backend_apply (delayed);
1723     }
1724 }
1725
1726 /**
1727  * g_settings_revert:
1728  * @settings: a #GSettings instance
1729  *
1730  * Reverts all non-applied changes to the settings.  This function
1731  * does nothing unless @settings is in 'delay-apply' mode; see
1732  * g_settings_set_delay_apply().  In the normal case settings are
1733  * always applied immediately.
1734  *
1735  * Change notifications will be emitted for affected keys.
1736  **/
1737 void
1738 g_settings_revert (GSettings *settings)
1739 {
1740   if (settings->priv->delayed)
1741     {
1742       GDelayedSettingsBackend *delayed;
1743
1744       delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
1745       g_delayed_settings_backend_revert (delayed);
1746     }
1747 }
1748
1749 /**
1750  * g_settings_get_has_unapplied:
1751  * @settings: a #GSettings object
1752  * @returns: %TRUE if @settings has unapplied changes
1753  *
1754  * Returns whether the #GSettings object has any unapplied
1755  * changes.  This can only be the case if it is in 'delayed-apply' mode.
1756  *
1757  * Since: 2.26
1758  */
1759 gboolean
1760 g_settings_get_has_unapplied (GSettings *settings)
1761 {
1762   g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1763
1764   return settings->priv->delayed &&
1765          g_delayed_settings_backend_get_has_unapplied (
1766            G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
1767 }
1768
1769 /* Extra API (sync, get_child, is_writable) {{{1 */
1770 /**
1771  * g_settings_sync:
1772  * @context: the context to sync, or %NULL
1773  *
1774  * Ensures that all pending operations for the given context are
1775  * complete.
1776  *
1777  * Writes made to a #GSettings are handled asynchronously.  For this
1778  * reason, it is very unlikely that the changes have it to disk by the
1779  * time g_settings_set() returns.
1780  *
1781  * This call will block until all of the writes have made it to the
1782  * backend.  Since the mainloop is not running, no change notifications
1783  * will be dispatched during this call (but some may be queued by the
1784  * time the call is done).
1785  **/
1786 void
1787 g_settings_sync (const gchar *context)
1788 {
1789   GSettingsBackend *backend;
1790
1791   if (context == NULL)
1792     context = "";
1793
1794   backend = g_settings_backend_get_with_context (context);
1795   g_settings_backend_sync (backend);
1796 }
1797
1798 /**
1799  * g_settings_is_writable:
1800  * @settings: a #GSettings object
1801  * @name: the name of a key
1802  * @returns: %TRUE if the key @name is writable
1803  *
1804  * Finds out if a key can be written or not
1805  *
1806  * Since: 2.26
1807  */
1808 gboolean
1809 g_settings_is_writable (GSettings   *settings,
1810                         const gchar *name)
1811 {
1812   gboolean writable;
1813   gchar *path;
1814
1815   g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1816
1817   path = g_strconcat (settings->priv->path, name, NULL);
1818   writable = g_settings_backend_get_writable (settings->priv->backend, path);
1819   g_free (path);
1820
1821   return writable;
1822 }
1823
1824 /**
1825  * g_settings_get_child:
1826  * @settings: a #GSettings object
1827  * @name: the name of the 'child' schema
1828  * @returns: a 'child' settings object
1829  *
1830  * Creates a 'child' settings object which has a base path of
1831  * <replaceable>base-path</replaceable>/@name", where
1832  * <replaceable>base-path</replaceable> is the base path of @settings.
1833  *
1834  * The schema for the child settings object must have been declared
1835  * in the schema of @settings using a <tag class="starttag">child</tag> element.
1836  *
1837  * Since: 2.26
1838  */
1839 GSettings *
1840 g_settings_get_child (GSettings   *settings,
1841                       const gchar *name)
1842 {
1843   const gchar *child_schema;
1844   gchar *child_path;
1845   gchar *child_name;
1846   GSettings *child;
1847
1848   g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1849
1850   child_name = g_strconcat (name, "/", NULL);
1851   child_schema = g_settings_schema_get_string (settings->priv->schema,
1852                                                child_name);
1853   if (child_schema == NULL)
1854     g_error ("Schema '%s' has no child '%s'",
1855              settings->priv->schema_name, name);
1856
1857   child_path = g_strconcat (settings->priv->path, child_name, NULL);
1858   child = g_object_new (G_TYPE_SETTINGS,
1859                         "schema", child_schema,
1860                         "path", child_path,
1861                         NULL);
1862   g_free (child_path);
1863   g_free (child_name);
1864
1865   return child;
1866 }
1867
1868 /* Binding {{{1 */
1869 typedef struct
1870 {
1871   GSettings *settings;
1872   GObject *object;
1873
1874   GSettingsBindGetMapping get_mapping;
1875   GSettingsBindSetMapping set_mapping;
1876   gpointer user_data;
1877   GDestroyNotify destroy;
1878
1879   guint writable_handler_id;
1880   guint property_handler_id;
1881   const GParamSpec *property;
1882   guint key_handler_id;
1883   GVariantType *type;
1884   const gchar *key;
1885
1886   /* prevent recursion */
1887   gboolean running;
1888 } GSettingsBinding;
1889
1890 static void
1891 g_settings_binding_free (gpointer data)
1892 {
1893   GSettingsBinding *binding = data;
1894
1895   g_assert (!binding->running);
1896
1897   if (binding->writable_handler_id)
1898     g_signal_handler_disconnect (binding->settings,
1899                                  binding->writable_handler_id);
1900
1901   if (binding->key_handler_id)
1902     g_signal_handler_disconnect (binding->settings,
1903                                  binding->key_handler_id);
1904
1905   if (g_signal_handler_is_connected (binding->object,
1906                                      binding->property_handler_id))
1907   g_signal_handler_disconnect (binding->object,
1908                                binding->property_handler_id);
1909
1910   g_variant_type_free (binding->type);
1911   g_object_unref (binding->settings);
1912
1913   if (binding->destroy)
1914     binding->destroy (binding->user_data);
1915
1916   g_slice_free (GSettingsBinding, binding);
1917 }
1918
1919 static GQuark
1920 g_settings_binding_quark (const char *property)
1921 {
1922   GQuark quark;
1923   gchar *tmp;
1924
1925   tmp = g_strdup_printf ("gsettingsbinding-%s", property);
1926   quark = g_quark_from_string (tmp);
1927   g_free (tmp);
1928
1929   return quark;
1930 }
1931
1932 static void
1933 g_settings_binding_key_changed (GSettings   *settings,
1934                                 const gchar *key,
1935                                 gpointer     user_data)
1936 {
1937   GSettingsBinding *binding = user_data;
1938   GValue value = { 0, };
1939   GVariant *variant;
1940
1941   g_assert (settings == binding->settings);
1942   g_assert (key == binding->key);
1943
1944   if (binding->running)
1945     return;
1946
1947   binding->running = TRUE;
1948
1949   g_value_init (&value, binding->property->value_type);
1950   variant = g_settings_get_value (settings, key);
1951   if (binding->get_mapping (&value, variant, binding->user_data))
1952     g_object_set_property (binding->object,
1953                            binding->property->name,
1954                            &value);
1955   g_variant_unref (variant);
1956   g_value_unset (&value);
1957
1958   binding->running = FALSE;
1959 }
1960
1961 static void
1962 g_settings_binding_property_changed (GObject          *object,
1963                                      const GParamSpec *pspec,
1964                                      gpointer          user_data)
1965 {
1966   GSettingsBinding *binding = user_data;
1967   GValue value = { 0, };
1968   GVariant *variant;
1969
1970   g_assert (object == binding->object);
1971   g_assert (pspec == binding->property);
1972
1973   if (binding->running)
1974     return;
1975
1976   binding->running = TRUE;
1977
1978   g_value_init (&value, pspec->value_type);
1979   g_object_get_property (object, pspec->name, &value);
1980   if ((variant = binding->set_mapping (&value, binding->type,
1981                                        binding->user_data)))
1982     {
1983       g_settings_set_value (binding->settings,
1984                             binding->key,
1985                             g_variant_ref_sink (variant));
1986       g_variant_unref (variant);
1987     }
1988   g_value_unset (&value);
1989
1990   binding->running = FALSE;
1991 }
1992
1993 /**
1994  * g_settings_bind:
1995  * @settings: a #GSettings object
1996  * @key: the key to bind
1997  * @object: a #GObject
1998  * @property: the name of the property to bind
1999  * @flags: flags for the binding
2000  *
2001  * Create a binding between the @key in the @settings object
2002  * and the property @property of @object.
2003  *
2004  * The binding uses the default GIO mapping functions to map
2005  * between the settings and property values. These functions
2006  * handle booleans, numeric types and string types in a
2007  * straightforward way. Use g_settings_bind_with_mapping() if
2008  * you need a custom mapping, or map between types that are not
2009  * supported by the default mapping functions.
2010  *
2011  * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
2012  * function also establishes a binding between the writability of
2013  * @key and the "sensitive" property of @object (if @object has
2014  * a boolean property by that name). See g_settings_bind_writable()
2015  * for more details about writable bindings.
2016  *
2017  * Note that the lifecycle of the binding is tied to the object,
2018  * and that you can have only one binding per object property.
2019  * If you bind the same property twice on the same object, the second
2020  * binding overrides the first one.
2021  *
2022  * Since: 2.26
2023  */
2024 void
2025 g_settings_bind (GSettings          *settings,
2026                  const gchar        *key,
2027                  gpointer            object,
2028                  const gchar        *property,
2029                  GSettingsBindFlags  flags)
2030 {
2031   g_settings_bind_with_mapping (settings, key, object, property,
2032                                 flags, NULL, NULL, NULL, NULL);
2033 }
2034
2035 /**
2036  * g_settings_bind_with_mapping:
2037  * @settings: a #GSettings object
2038  * @key: the key to bind
2039  * @object: a #GObject
2040  * @property: the name of the property to bind
2041  * @flags: flags for the binding
2042  * @get_mapping: a function that gets called to convert values
2043  *     from @settings to @object, or %NULL to use the default GIO mapping
2044  * @set_mapping: a function that gets called to convert values
2045  *     from @object to @settings, or %NULL to use the default GIO mapping
2046  * @user_data: data that gets passed to @get_mapping and @set_mapping
2047  * @destroy: #GDestroyNotify function for @user_data
2048  *
2049  * Create a binding between the @key in the @settings object
2050  * and the property @property of @object.
2051  *
2052  * The binding uses the provided mapping functions to map between
2053  * settings and property values.
2054  *
2055  * Note that the lifecycle of the binding is tied to the object,
2056  * and that you can have only one binding per object property.
2057  * If you bind the same property twice on the same object, the second
2058  * binding overrides the first one.
2059  *
2060  * Since: 2.26
2061  */
2062 void
2063 g_settings_bind_with_mapping (GSettings               *settings,
2064                               const gchar             *key,
2065                               gpointer                 object,
2066                               const gchar             *property,
2067                               GSettingsBindFlags       flags,
2068                               GSettingsBindGetMapping  get_mapping,
2069                               GSettingsBindSetMapping  set_mapping,
2070                               gpointer                 user_data,
2071                               GDestroyNotify           destroy)
2072 {
2073   GSettingsBinding *binding;
2074   GObjectClass *objectclass;
2075   gchar *detailed_signal;
2076   GQuark binding_quark;
2077
2078   g_return_if_fail (G_IS_SETTINGS (settings));
2079
2080   objectclass = G_OBJECT_GET_CLASS (object);
2081
2082   binding = g_slice_new0 (GSettingsBinding);
2083   binding->settings = g_object_ref (settings);
2084   binding->object = object;
2085   binding->key = g_intern_string (key);
2086   binding->property = g_object_class_find_property (objectclass, property);
2087   binding->user_data = user_data;
2088   binding->destroy = destroy;
2089   binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
2090   binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
2091
2092   if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
2093     flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
2094
2095   if (binding->property == NULL)
2096     {
2097       g_critical ("g_settings_bind: no property '%s' on class '%s'",
2098                   property, G_OBJECT_TYPE_NAME (object));
2099       return;
2100     }
2101
2102   if ((flags & G_SETTINGS_BIND_GET) && (binding->property->flags & G_PARAM_WRITABLE) == 0)
2103     {
2104       g_critical ("g_settings_bind: property '%s' on class '%s' is not writable",
2105                   property, G_OBJECT_TYPE_NAME (object));
2106       return;
2107     }
2108   if ((flags & G_SETTINGS_BIND_SET) && (binding->property->flags & G_PARAM_READABLE) == 0)
2109     {
2110       g_critical ("g_settings_bind: property '%s' on class '%s' is not readable",
2111                   property, G_OBJECT_TYPE_NAME (object));
2112       return;
2113     }
2114
2115   {
2116     GVariantIter *iter;
2117     GVariant *value;
2118
2119     iter = g_settings_schema_get_value (settings->priv->schema, key);
2120     value = g_variant_iter_next_value (iter);
2121     binding->type = g_variant_type_copy (g_variant_get_type (value));
2122     g_variant_iter_free (iter);
2123     g_variant_unref (value);
2124   }
2125
2126   if (binding->type == NULL)
2127     {
2128       g_critical ("g_settings_bind: no key '%s' on schema '%s'",
2129                   key, settings->priv->schema_name);
2130       return;
2131     }
2132
2133   if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
2134        (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
2135       !g_settings_mapping_is_compatible (binding->property->value_type,
2136                                          binding->type))
2137     {
2138       g_critical ("g_settings_bind: property '%s' on class '%s' has type "
2139                   "'%s' which is not compatible with type '%s' of key '%s' "
2140                   "on schema '%s'", property, G_OBJECT_TYPE_NAME (object),
2141                   g_type_name (binding->property->value_type),
2142                   g_variant_type_dup_string (binding->type), key,
2143                   settings->priv->schema_name);
2144       return;
2145     }
2146
2147   if ((flags & G_SETTINGS_BIND_SET) &&
2148       (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
2149     {
2150       GParamSpec *sensitive;
2151
2152       sensitive = g_object_class_find_property (objectclass, "sensitive");
2153
2154       if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
2155           (sensitive->flags & G_PARAM_WRITABLE))
2156         g_settings_bind_writable (settings, binding->key,
2157                                   object, "sensitive", FALSE);
2158     }
2159
2160   if (flags & G_SETTINGS_BIND_SET)
2161     {
2162       detailed_signal = g_strdup_printf ("notify::%s", property);
2163       binding->property_handler_id =
2164         g_signal_connect (object, detailed_signal,
2165                           G_CALLBACK (g_settings_binding_property_changed),
2166                           binding);
2167       g_free (detailed_signal);
2168
2169       if (~flags & G_SETTINGS_BIND_GET)
2170         g_settings_binding_property_changed (object,
2171                                              binding->property,
2172                                              binding);
2173     }
2174
2175   if (flags & G_SETTINGS_BIND_GET)
2176     {
2177       if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
2178         {
2179           detailed_signal = g_strdup_printf ("changed::%s", key);
2180           binding->key_handler_id =
2181             g_signal_connect (settings, detailed_signal,
2182                               G_CALLBACK (g_settings_binding_key_changed),
2183                               binding);
2184           g_free (detailed_signal);
2185         }
2186
2187       g_settings_binding_key_changed (settings, binding->key, binding);
2188     }
2189
2190   binding_quark = g_settings_binding_quark (property);
2191   g_object_set_qdata_full (object, binding_quark,
2192                            binding, g_settings_binding_free);
2193 }
2194
2195 /* Writability binding {{{1 */
2196 typedef struct
2197 {
2198   GSettings *settings;
2199   gpointer object;
2200   const gchar *key;
2201   const gchar *property;
2202   gboolean inverted;
2203   gulong handler_id;
2204 } GSettingsWritableBinding;
2205
2206 static void
2207 g_settings_writable_binding_free (gpointer data)
2208 {
2209   GSettingsWritableBinding *binding = data;
2210
2211   g_signal_handler_disconnect (binding->settings, binding->handler_id);
2212   g_object_unref (binding->settings);
2213   g_slice_free (GSettingsWritableBinding, binding);
2214 }
2215
2216 static void
2217 g_settings_binding_writable_changed (GSettings   *settings,
2218                                      const gchar *key,
2219                                      gpointer     user_data)
2220 {
2221   GSettingsWritableBinding *binding = user_data;
2222   gboolean writable;
2223
2224   g_assert (settings == binding->settings);
2225   g_assert (key == binding->key);
2226
2227   writable = g_settings_is_writable (settings, key);
2228
2229   if (binding->inverted)
2230     writable = !writable;
2231
2232   g_object_set (binding->object, binding->property, writable, NULL);
2233 }
2234
2235 /**
2236  * g_settings_bind_writable:
2237  * @settings: a #GSettings object
2238  * @key: the key to bind
2239  * @object: a #GObject
2240  * @property: the name of a boolean property to bind
2241  * @inverted: whether to 'invert' the value
2242  *
2243  * Create a binding between the writability of @key in the
2244  * @settings object and the property @property of @object.
2245  * The property must be boolean; "sensitive" or "visible"
2246  * properties of widgets are the most likely candidates.
2247  *
2248  * Writable bindings are always uni-directional; changes of the
2249  * writability of the setting will be propagated to the object
2250  * property, not the other way.
2251  *
2252  * When the @inverted argument is %TRUE, the binding inverts the
2253  * value as it passes from the setting to the object, i.e. @property
2254  * will be set to %TRUE if the key is <emphasis>not</emphasis>
2255  * writable.
2256  *
2257  * Note that the lifecycle of the binding is tied to the object,
2258  * and that you can have only one binding per object property.
2259  * If you bind the same property twice on the same object, the second
2260  * binding overrides the first one.
2261  *
2262  * Since: 2.26
2263  */
2264 void
2265 g_settings_bind_writable (GSettings   *settings,
2266                           const gchar *key,
2267                           gpointer     object,
2268                           const gchar *property,
2269                           gboolean     inverted)
2270 {
2271   GSettingsWritableBinding *binding;
2272   gchar *detailed_signal;
2273   GParamSpec *pspec;
2274
2275   g_return_if_fail (G_IS_SETTINGS (settings));
2276
2277   pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
2278   if (pspec == NULL)
2279     {
2280       g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
2281                   property, G_OBJECT_TYPE_NAME (object));
2282       return;
2283     }
2284   if ((pspec->flags & G_PARAM_WRITABLE) == 0)
2285     {
2286       g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
2287                   property, G_OBJECT_TYPE_NAME (object));
2288       return;
2289     }
2290
2291   binding = g_slice_new (GSettingsWritableBinding);
2292   binding->settings = g_object_ref (settings);
2293   binding->object = object;
2294   binding->key = g_intern_string (key);
2295   binding->property = g_intern_string (property);
2296   binding->inverted = inverted;
2297
2298   detailed_signal = g_strdup_printf ("writable-changed::%s", key);
2299   binding->handler_id =
2300     g_signal_connect (settings, detailed_signal,
2301                       G_CALLBACK (g_settings_binding_writable_changed),
2302                       binding);
2303   g_free (detailed_signal);
2304
2305   g_object_set_qdata_full (object, g_settings_binding_quark (property),
2306                            binding, g_settings_writable_binding_free);
2307
2308   g_settings_binding_writable_changed (settings, binding->key, binding);
2309 }
2310
2311 /**
2312  * g_settings_unbind:
2313  * @object: the object
2314  * @property: the property whose binding is removed
2315  *
2316  * Removes an existing binding for @property on @object.
2317  *
2318  * Note that bindings are automatically removed when the
2319  * object is finalized, so it is rarely necessary to call this
2320  * function.
2321  *
2322  * Since: 2.26
2323  */
2324 void
2325 g_settings_unbind (gpointer     object,
2326                    const gchar *property)
2327 {
2328   GQuark binding_quark;
2329
2330   binding_quark = g_settings_binding_quark (property);
2331   g_object_set_qdata (object, binding_quark, NULL);
2332 }
2333
2334 /* Epilogue {{{1 */
2335 #define __G_SETTINGS_C__
2336 #include "gioaliasdef.c"
2337
2338 /* vim:set foldmethod=marker: */