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