Clarify GSettings documentation wrt errors
[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='Anna' target='Annabeth'/>
197  *         <alias value='Beth' target='Elisabeth'/>
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 *path;
240
241   GDelayedSettingsBackend *delayed;
242 };
243
244 enum
245 {
246   PROP_0,
247   PROP_SCHEMA,
248   PROP_BACKEND,
249   PROP_PATH,
250   PROP_HAS_UNAPPLIED,
251 };
252
253 enum
254 {
255   SIGNAL_WRITABLE_CHANGE_EVENT,
256   SIGNAL_WRITABLE_CHANGED,
257   SIGNAL_CHANGE_EVENT,
258   SIGNAL_CHANGED,
259   N_SIGNALS
260 };
261
262 static guint g_settings_signals[N_SIGNALS];
263
264 G_DEFINE_TYPE (GSettings, g_settings, G_TYPE_OBJECT)
265
266 /* Signals {{{1 */
267 static gboolean
268 g_settings_real_change_event (GSettings    *settings,
269                               const GQuark *keys,
270                               gint          n_keys)
271 {
272   gint i;
273
274   if (keys == NULL)
275     keys = g_settings_schema_list (settings->priv->schema, &n_keys);
276
277   for (i = 0; i < n_keys; i++)
278     g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
279                    keys[i], g_quark_to_string (keys[i]));
280
281   return FALSE;
282 }
283
284 static gboolean
285 g_settings_real_writable_change_event (GSettings *settings,
286                                        GQuark     key)
287 {
288   const GQuark *keys = &key;
289   gint n_keys = 1;
290   gint i;
291
292   if (key == 0)
293     keys = g_settings_schema_list (settings->priv->schema, &n_keys);
294
295   for (i = 0; i < n_keys; i++)
296     {
297       const gchar *string = g_quark_to_string (keys[i]);
298
299       g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED],
300                      keys[i], string);
301       g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
302                      keys[i], string);
303     }
304
305   return FALSE;
306 }
307
308 static void
309 settings_backend_changed (GSettingsBackend    *backend,
310                           GObject             *target,
311                           const gchar         *key,
312                           gpointer             origin_tag)
313 {
314   GSettings *settings = G_SETTINGS (target);
315   gboolean ignore_this;
316   gint i;
317
318   g_assert (settings->priv->backend == backend);
319
320   for (i = 0; key[i] == settings->priv->path[i]; i++);
321
322   if (settings->priv->path[i] == '\0' &&
323       g_settings_schema_has_key (settings->priv->schema, key + i))
324     {
325       GQuark quark;
326
327       quark = g_quark_from_string (key + i);
328       g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
329                      0, &quark, 1, &ignore_this);
330     }
331 }
332
333 static void
334 settings_backend_path_changed (GSettingsBackend *backend,
335                                GObject          *target,
336                                const gchar      *path,
337                                gpointer          origin_tag)
338 {
339   GSettings *settings = G_SETTINGS (target);
340   gboolean ignore_this;
341
342   g_assert (settings->priv->backend == backend);
343
344   if (g_str_has_prefix (settings->priv->path, path))
345     g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
346                    0, NULL, 0, &ignore_this);
347 }
348
349 static void
350 settings_backend_keys_changed (GSettingsBackend    *backend,
351                                GObject             *target,
352                                const gchar         *path,
353                                const gchar * const *items,
354                                gpointer             origin_tag)
355 {
356   GSettings *settings = G_SETTINGS (target);
357   gboolean ignore_this;
358   gint i;
359
360   g_assert (settings->priv->backend == backend);
361
362   for (i = 0; settings->priv->path[i] &&
363               settings->priv->path[i] == path[i]; i++);
364
365   if (path[i] == '\0')
366     {
367       GQuark quarks[256];
368       gint j, l = 0;
369
370       for (j = 0; items[j]; j++)
371          {
372            const gchar *item = items[j];
373            gint k;
374
375            for (k = 0; item[k] == settings->priv->path[i + k]; k++);
376
377            if (settings->priv->path[i + k] == '\0' &&
378                g_settings_schema_has_key (settings->priv->schema, item + k))
379              quarks[l++] = g_quark_from_string (item + k);
380
381            /* "256 quarks ought to be enough for anybody!"
382             * If this bites you, I'm sorry.  Please file a bug.
383             */
384            g_assert (l < 256);
385          }
386
387       if (l > 0)
388         g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
389                        0, quarks, l, &ignore_this);
390     }
391 }
392
393 static void
394 settings_backend_writable_changed (GSettingsBackend *backend,
395                                    GObject          *target,
396                                    const gchar      *key)
397 {
398   GSettings *settings = G_SETTINGS (target);
399   gboolean ignore_this;
400   gint i;
401
402   g_assert (settings->priv->backend == backend);
403
404   for (i = 0; key[i] == settings->priv->path[i]; i++);
405
406   if (settings->priv->path[i] == '\0' &&
407       g_settings_schema_has_key (settings->priv->schema, key + i))
408     g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
409                    0, g_quark_from_string (key + i), &ignore_this);
410 }
411
412 static void
413 settings_backend_path_writable_changed (GSettingsBackend *backend,
414                                         GObject          *target,
415                                         const gchar      *path)
416 {
417   GSettings *settings = G_SETTINGS (target);
418   gboolean ignore_this;
419
420   g_assert (settings->priv->backend == backend);
421
422   if (g_str_has_prefix (settings->priv->path, path))
423     g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
424                    0, (GQuark) 0, &ignore_this);
425 }
426
427 /* Properties, Construction, Destruction {{{1 */
428 static void
429 g_settings_set_property (GObject      *object,
430                          guint         prop_id,
431                          const GValue *value,
432                          GParamSpec   *pspec)
433 {
434   GSettings *settings = G_SETTINGS (object);
435
436   switch (prop_id)
437     {
438     case PROP_SCHEMA:
439       g_assert (settings->priv->schema_name == NULL);
440       settings->priv->schema_name = g_value_dup_string (value);
441       break;
442
443     case PROP_PATH:
444       settings->priv->path = g_value_dup_string (value);
445       break;
446
447     case PROP_BACKEND:
448       settings->priv->backend = g_value_dup_object (value);
449       break;
450
451     default:
452       g_assert_not_reached ();
453     }
454 }
455
456 static void
457 g_settings_get_property (GObject    *object,
458                          guint       prop_id,
459                          GValue     *value,
460                          GParamSpec *pspec)
461 {
462   GSettings *settings = G_SETTINGS (object);
463
464   switch (prop_id)
465     {
466      case PROP_SCHEMA:
467       g_value_set_object (value, settings->priv->schema);
468       break;
469
470      case PROP_HAS_UNAPPLIED:
471       g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
472       break;
473
474      default:
475       g_assert_not_reached ();
476     }
477 }
478
479 static void
480 g_settings_constructed (GObject *object)
481 {
482   GSettings *settings = G_SETTINGS (object);
483   const gchar *schema_path;
484
485   settings->priv->schema = g_settings_schema_new (settings->priv->schema_name);
486   schema_path = g_settings_schema_get_path (settings->priv->schema);
487
488   if (settings->priv->path && schema_path && strcmp (settings->priv->path, schema_path) != 0)
489     g_error ("settings object created with schema '%s' and path '%s', but "
490              "path '%s' is specified by schema",
491              settings->priv->schema_name, settings->priv->path, schema_path);
492
493   if (settings->priv->path == NULL)
494     {
495       if (schema_path == NULL)
496         g_error ("attempting to create schema '%s' without a path",
497                  settings->priv->schema_name);
498
499       settings->priv->path = g_strdup (schema_path);
500     }
501
502   if (settings->priv->backend == NULL)
503     settings->priv->backend = g_settings_backend_get_default ();
504
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->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_BACKEND,
671     g_param_spec_object ("backend",
672                          P_("GSettingsBackend"),
673                          P_("The GSettingsBackend for this settings object"),
674                          G_TYPE_SETTINGS_BACKEND, 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_backend:
776  * @schema: the name of the schema
777  * @backend: the #GSettingsBackend to use
778  * @returns: a new #GSettings object
779  *
780  * Creates a new #GSettings object with a given schema and backend.
781  *
782  * Creating settings objects with an different backend allows accessing settings
783  * from a database other than the usual one.  For example, it may make
784  * sense to pass a backend corresponding to the "defaults" settings database on
785  * the system to get a settings object that modifies the system default
786  * settings instead of the settings for this user.
787  *
788  * Since: 2.26
789  */
790 GSettings *
791 g_settings_new_with_backend (const gchar      *schema,
792                              GSettingsBackend *backend)
793 {
794   g_return_val_if_fail (schema != NULL, NULL);
795   g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
796
797   return g_object_new (G_TYPE_SETTINGS,
798                        "schema", schema,
799                        "backend", backend,
800                        NULL);
801 }
802
803 /**
804  * g_settings_new_with_backend_and_path:
805  * @schema: the name of the schema
806  * @backend: the #GSettingsBackend to use
807  * @path: the path to use
808  * @returns: a new #GSettings object
809  *
810  * Creates a new #GSettings object with a given schema, backend and
811  * path.
812  *
813  * This is a mix of g_settings_new_with_backend() and
814  * g_settings_new_with_path().
815  *
816  * Since: 2.26
817  */
818 GSettings *
819 g_settings_new_with_backend_and_path (const gchar      *schema,
820                                       GSettingsBackend *backend,
821                                       const gchar      *path)
822 {
823   g_return_val_if_fail (schema != NULL, NULL);
824   g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
825   g_return_val_if_fail (path != NULL, NULL);
826
827   return g_object_new (G_TYPE_SETTINGS,
828                        "schema", schema,
829                        "backend", backend,
830                        "path", path,
831                        NULL);
832 }
833
834 /* Internal read/write utilities, enum conversion, validation {{{1 */
835 typedef struct
836 {
837   GSettings *settings;
838   const gchar *key;
839
840   GSettingsSchema *schema;
841
842   gboolean is_enum;
843   const guint32 *strinfo;
844   gsize strinfo_length;
845
846   const gchar *unparsed;
847   gchar lc_char;
848
849   const GVariantType *type;
850   GVariant *minimum, *maximum;
851   GVariant *default_value;
852 } GSettingsKeyInfo;
853
854 static void
855 g_settings_get_key_info (GSettingsKeyInfo *info,
856                          GSettings        *settings,
857                          const gchar      *key)
858 {
859   GVariantIter *iter;
860   GVariant *data;
861   guchar code;
862
863   memset (info, 0, sizeof *info);
864
865   iter = g_settings_schema_get_value (settings->priv->schema, key);
866
867   info->default_value = g_variant_iter_next_value (iter);
868   info->type = g_variant_get_type (info->default_value);
869   info->schema = settings->priv->schema;
870   info->settings = settings;
871   info->key = key;
872
873   while (g_variant_iter_next (iter, "(y*)", &code, &data))
874     {
875       switch (code)
876         {
877         case 'l':
878           /* translation requested */
879           g_variant_get (data, "(y&s)", &info->lc_char, &info->unparsed);
880           break;
881
882         case 'e':
883           /* enumerated types, ... */
884           info->is_enum = TRUE;
885         case 'c':
886           /* ..., choices, aliases */
887           info->strinfo = g_variant_get_fixed_array (data,
888                                                      &info->strinfo_length,
889                                                      sizeof (guint32));
890           break;
891
892         case 'r':
893           g_variant_get (data, "(**)", &info->minimum, &info->maximum);
894           break;
895
896         default:
897           g_warning ("unknown schema extension '%c'", code);
898           break;
899         }
900
901       g_variant_unref (data);
902     }
903
904   g_variant_iter_free (iter);
905 }
906
907 static void
908 g_settings_free_key_info (GSettingsKeyInfo *info)
909 {
910   if (info->minimum)
911     g_variant_unref (info->minimum);
912
913   if (info->maximum)
914     g_variant_unref (info->maximum);
915
916   g_variant_unref (info->default_value);
917 }
918
919 static gboolean
920 g_settings_write_to_backend (GSettingsKeyInfo *info,
921                              GVariant         *value)
922 {
923   gboolean success;
924   gchar *path;
925
926   path = g_strconcat (info->settings->priv->path, info->key, NULL);
927   success = g_settings_backend_write (info->settings->priv->backend,
928                                       path, value, NULL);
929   g_free (path);
930
931   return success;
932 }
933
934 static gboolean
935 g_settings_type_check (GSettingsKeyInfo *info,
936                        GVariant         *value)
937 {
938   g_return_val_if_fail (value != NULL, FALSE);
939
940   return g_variant_is_of_type (value, info->type);
941 }
942
943 static gboolean
944 g_settings_range_check (GSettingsKeyInfo *info,
945                         GVariant         *value)
946 {
947   if (info->minimum == NULL && info->strinfo == NULL)
948     return TRUE;
949
950   if (g_variant_is_container (value))
951     {
952       gboolean ok = TRUE;
953       GVariantIter iter;
954       GVariant *child;
955
956       g_variant_iter_init (&iter, value);
957       while (ok && (child = g_variant_iter_next_value (&iter)))
958         {
959           ok = g_settings_range_check (info, value);
960           g_variant_unref (child);
961         }
962
963       return ok;
964     }
965
966   if (info->minimum)
967     {
968       return g_variant_compare (info->minimum, value) <= 0 &&
969              g_variant_compare (value, info->maximum) <= 0;
970     }
971
972   return strinfo_is_string_valid (info->strinfo,
973                                   info->strinfo_length,
974                                   g_variant_get_string (value, NULL));
975 }
976
977 static GVariant *
978 g_settings_range_fixup (GSettingsKeyInfo *info,
979                         GVariant         *value)
980 {
981   const gchar *target;
982
983   if (g_settings_range_check (info, value))
984     return g_variant_ref (value);
985
986   if (info->strinfo == NULL)
987     return NULL;
988
989   if (g_variant_is_container (value))
990     {
991       GVariantBuilder builder;
992       GVariantIter iter;
993       GVariant *child;
994
995       g_variant_builder_init (&builder, g_variant_get_type (value));
996
997       while ((child = g_variant_iter_next_value (&iter)))
998         {
999           GVariant *fixed;
1000
1001           fixed = g_settings_range_fixup (info, child);
1002           g_variant_unref (child);
1003
1004           if (fixed == NULL)
1005             {
1006               g_variant_builder_clear (&builder);
1007               return NULL;
1008             }
1009
1010           g_variant_builder_add_value (&builder, fixed);
1011           g_variant_unref (fixed);
1012         }
1013
1014       return g_variant_ref_sink (g_variant_builder_end (&builder));
1015     }
1016
1017   target = strinfo_string_from_alias (info->strinfo, info->strinfo_length,
1018                                       g_variant_get_string (value, NULL));
1019   return target ? g_variant_ref_sink (g_variant_new_string (target)) : NULL;
1020 }
1021
1022 static GVariant *
1023 g_settings_read_from_backend (GSettingsKeyInfo *info)
1024 {
1025   GVariant *value;
1026   GVariant *fixup;
1027   gchar *path;
1028
1029   path = g_strconcat (info->settings->priv->path, info->key, NULL);
1030   value = g_settings_backend_read (info->settings->priv->backend,
1031                                    path, info->type, FALSE);
1032   g_free (path);
1033
1034   if (value != NULL)
1035     {
1036       fixup = g_settings_range_fixup (info, value);
1037       g_variant_unref (value);
1038     }
1039   else
1040     fixup = NULL;
1041
1042   return fixup;
1043 }
1044
1045 static GVariant *
1046 g_settings_get_translated_default (GSettingsKeyInfo *info)
1047 {
1048   const gchar *translated;
1049   GError *error = NULL;
1050   const gchar *domain;
1051   GVariant *value;
1052
1053   if (info->lc_char == '\0')
1054     /* translation not requested for this key */
1055     return NULL;
1056
1057   domain = g_settings_schema_get_gettext_domain (info->schema);
1058
1059   if (info->lc_char == 't')
1060     translated = g_dcgettext (domain, info->unparsed, LC_TIME);
1061   else
1062     translated = g_dgettext (domain, info->unparsed);
1063
1064   if (translated == info->unparsed)
1065     /* the default value was not translated */
1066     return NULL;
1067
1068   /* try to parse the translation of the unparsed default */
1069   value = g_variant_parse (info->type, translated, NULL, NULL, &error);
1070
1071   if (value == NULL)
1072     {
1073       g_warning ("Failed to parse translated string `%s' for "
1074                  "key `%s' in schema `%s': %s", info->unparsed, info->key,
1075                  info->settings->priv->schema_name, error->message);
1076       g_warning ("Using untranslated default instead.");
1077       g_error_free (error);
1078     }
1079
1080   else if (!g_settings_range_check (info, value))
1081     {
1082       g_warning ("Translated default `%s' for key `%s' in schema `%s' "
1083                  "is outside of valid range", info->unparsed, info->key,
1084                  info->settings->priv->schema_name);
1085       g_variant_unref (value);
1086       value = NULL;
1087     }
1088
1089   return value;
1090 }
1091
1092 static gint
1093 g_settings_to_enum (GSettingsKeyInfo *info,
1094                     GVariant         *value)
1095 {
1096   gboolean it_worked;
1097   guint result;
1098
1099   it_worked = strinfo_enum_from_string (info->strinfo, info->strinfo_length,
1100                                         g_variant_get_string (value, NULL),
1101                                         &result);
1102
1103   /* 'value' can only come from the backend after being filtered for validity,
1104    * from the translation after being filtered for validity, or from the schema
1105    * itself (which the schema compiler checks for validity).  If this assertion
1106    * fails then it's really a bug in GSettings or the schema compiler...
1107    */
1108   g_assert (it_worked);
1109
1110   return result;
1111 }
1112
1113 static GVariant *
1114 g_settings_from_enum (GSettingsKeyInfo *info,
1115                       gint              value)
1116 {
1117   const gchar *string;
1118
1119   string = strinfo_string_from_enum (info->strinfo,
1120                                      info->strinfo_length,
1121                                      value);
1122
1123   if (string == NULL)
1124     return NULL;
1125
1126   return g_variant_ref_sink (g_variant_new_string (string));
1127 }
1128
1129 /* Public Get/Set API {{{1 (get, get_value, set, set_value) */
1130 /**
1131  * g_settings_get_value:
1132  * @settings: a #GSettings object
1133  * @key: the key to get the value for
1134  * @returns: a new #GVariant
1135  *
1136  * Gets the value that is stored in @settings for @key.
1137  *
1138  * It is a programmer error to give a @key that isn't contained in the
1139  * schema for @settings.
1140  *
1141  * Since: 2.26
1142  */
1143 GVariant *
1144 g_settings_get_value (GSettings   *settings,
1145                       const gchar *key)
1146 {
1147   GSettingsKeyInfo info;
1148   GVariant *value;
1149
1150   g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1151   g_return_val_if_fail (key != NULL, NULL);
1152
1153   g_settings_get_key_info (&info, settings, key);
1154   value = g_settings_read_from_backend (&info);
1155
1156   if (value == NULL)
1157     value = g_settings_get_translated_default (&info);
1158
1159   if (value == NULL)
1160     value = g_variant_ref (info.default_value);
1161
1162   g_settings_free_key_info (&info);
1163
1164   return value;
1165 }
1166
1167 /**
1168  * g_settings_get_enum:
1169  * @settings: a #GSettings object
1170  * @key: the key to get the value for
1171  * @returns: the enum value
1172  *
1173  * Gets the value that is stored in @settings for @key and converts it
1174  * to the enum value that it represents.
1175  *
1176  * In order to use this function the type of the value must be a string
1177  * and it must be marked in the schema file as an enumerated type.
1178  *
1179  * It is a programmer error to give a @key that isn't contained in the
1180  * schema for @settings or is not marked as an enumerated type.
1181  *
1182  * If the value stored in the configuration database is not a valid
1183  * value for the enumerated type then this function will return the
1184  * default value.
1185  *
1186  * Since: 2.26
1187  **/
1188 gint
1189 g_settings_get_enum (GSettings   *settings,
1190                      const gchar *key)
1191 {
1192   GSettingsKeyInfo info;
1193   GVariant *value;
1194   gint result;
1195
1196   g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
1197   g_return_val_if_fail (key != NULL, -1);
1198
1199   g_settings_get_key_info (&info, settings, key);
1200
1201   if (!info.is_enum)
1202     {
1203       g_critical ("g_settings_get_enum() called on key `%s' which is not "
1204                   "associated with an enumerated type", info.key);
1205       g_settings_free_key_info (&info);
1206       return -1;
1207     }
1208
1209   value = g_settings_read_from_backend (&info);
1210
1211   if (value == NULL)
1212     value = g_settings_get_translated_default (&info);
1213
1214   if (value == NULL)
1215     value = g_variant_ref (info.default_value);
1216
1217   result = g_settings_to_enum (&info, value);
1218   g_settings_free_key_info (&info);
1219   g_variant_unref (value);
1220
1221   return result;
1222 }
1223
1224 /**
1225  * g_settings_set_enum:
1226  * @settings: a #GSettings object
1227  * @key: a key, within @settings
1228  * @value: an enumerated value
1229  * @returns: %TRUE, if the set succeeds
1230  *
1231  * Looks up the enumerated type nick for @value and writes it to @key,
1232  * within @settings.
1233  *
1234  * It is a programmer error to give a @key that isn't contained in the
1235  * schema for @settings or is not marked as an enumerated type, or for
1236  * @value not to be a valid value for the named type.
1237  *
1238  * After performing the write, accessing @key directly
1239  * g_settings_get_string() will return the 'nick' associated with
1240  * @value.
1241  **/
1242 gboolean
1243 g_settings_set_enum (GSettings   *settings,
1244                      const gchar *key,
1245                      gint         value)
1246 {
1247   GSettingsKeyInfo info;
1248   GVariant *variant;
1249   gboolean success;
1250
1251   g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1252   g_return_val_if_fail (key != NULL, FALSE);
1253
1254   g_settings_get_key_info (&info, settings, key);
1255
1256   if (!info.is_enum)
1257     {
1258       g_critical ("g_settings_set_enum() called on key `%s' which is not "
1259                   "associated with an enumerated type", info.key);
1260       return FALSE;
1261     }
1262
1263   if (!(variant = g_settings_from_enum (&info, value)))
1264     {
1265       g_critical ("g_settings_set_enum(): invalid enum value %d for key `%s' "
1266                   "in schema `%s'.  Doing nothing.", value, info.key,
1267                   info.settings->priv->schema_name);
1268       g_settings_free_key_info (&info);
1269       return FALSE;
1270     }
1271
1272   success = g_settings_write_to_backend (&info, variant);
1273   g_settings_free_key_info (&info);
1274   g_variant_unref (variant);
1275
1276   return success;
1277 }
1278
1279 /**
1280  * g_settings_set_value:
1281  * @settings: a #GSettings object
1282  * @key: the name of the key to set
1283  * @value: a #GVariant of the correct type
1284  * @returns: %TRUE if setting the key succeeded,
1285  *     %FALSE if the key was not writable
1286  *
1287  * Sets @key in @settings to @value.
1288  *
1289  * It is a programmer error to give a @key that isn't contained in the
1290  * schema for @settings or for @value to have the incorrect type, per
1291  * the schema.
1292  *
1293  * If @value is floating then this function consumes the reference.
1294  *
1295  * Since: 2.26
1296  **/
1297 gboolean
1298 g_settings_set_value (GSettings   *settings,
1299                       const gchar *key,
1300                       GVariant    *value)
1301 {
1302   GSettingsKeyInfo info;
1303
1304   g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1305   g_return_val_if_fail (key != NULL, FALSE);
1306
1307   g_settings_get_key_info (&info, settings, key);
1308   g_return_val_if_fail (g_settings_type_check (&info, value), FALSE);
1309   g_return_val_if_fail (g_settings_range_check (&info, value), FALSE);
1310   g_settings_free_key_info (&info);
1311
1312   return g_settings_write_to_backend (&info, value);
1313 }
1314
1315 /**
1316  * g_settings_get:
1317  * @settings: a #GSettings object
1318  * @key: the key to get the value for
1319  * @format: a #GVariant format string
1320  * @...: arguments as per @format
1321  *
1322  * Gets the value that is stored at @key in @settings.
1323  *
1324  * A convenience function that combines g_settings_get_value() with
1325  * g_variant_get().
1326  *
1327  * It is a programmer error to give a @key that isn't contained in the
1328  * schema for @settings or for the #GVariantType of @format to mismatch
1329  * the type given in the schema.
1330  *
1331  * Since: 2.26
1332  */
1333 void
1334 g_settings_get (GSettings   *settings,
1335                 const gchar *key,
1336                 const gchar *format,
1337                 ...)
1338 {
1339   GVariant *value;
1340   va_list ap;
1341
1342   value = g_settings_get_value (settings, key);
1343
1344   va_start (ap, format);
1345   g_variant_get_va (value, format, NULL, &ap);
1346   va_end (ap);
1347
1348   g_variant_unref (value);
1349 }
1350
1351 /**
1352  * g_settings_set:
1353  * @settings: a #GSettings object
1354  * @key: the name of the key to set
1355  * @format: a #GVariant format string
1356  * @...: arguments as per @format
1357  * @returns: %TRUE if setting the key succeeded,
1358  *     %FALSE if the key was not writable
1359  *
1360  * Sets @key in @settings to @value.
1361  *
1362  * A convenience function that combines g_settings_set_value() with
1363  * g_variant_new().
1364  *
1365  * It is a programmer error to give a @key that isn't contained in the
1366  * schema for @settings or for the #GVariantType of @format to mismatch
1367  * the type given in the schema.
1368  *
1369  * Since: 2.26
1370  */
1371 gboolean
1372 g_settings_set (GSettings   *settings,
1373                 const gchar *key,
1374                 const gchar *format,
1375                 ...)
1376 {
1377   GVariant *value;
1378   va_list ap;
1379
1380   va_start (ap, format);
1381   value = g_variant_new_va (format, NULL, &ap);
1382   va_end (ap);
1383
1384   return g_settings_set_value (settings, key, value);
1385 }
1386
1387 /* Convenience API (get, set_string, int, double, boolean, strv) {{{1 */
1388 /**
1389  * g_settings_get_string:
1390  * @settings: a #GSettings object
1391  * @key: the key to get the value for
1392  * @returns: a newly-allocated string
1393  *
1394  * Gets the value that is stored at @key in @settings.
1395  *
1396  * A convenience variant of g_settings_get() for strings.
1397  *
1398  * It is a programmer error to give a @key that isn't specified as
1399  * having a string type in the schema for @settings.
1400  *
1401  * Since: 2.26
1402  */
1403 gchar *
1404 g_settings_get_string (GSettings   *settings,
1405                        const gchar *key)
1406 {
1407   GVariant *value;
1408   gchar *result;
1409
1410   value = g_settings_get_value (settings, key);
1411   result = g_variant_dup_string (value, NULL);
1412   g_variant_unref (value);
1413
1414   return result;
1415 }
1416
1417 /**
1418  * g_settings_set_string:
1419  * @settings: a #GSettings object
1420  * @key: the name of the key to set
1421  * @value: the value to set it to
1422  * @returns: %TRUE if setting the key succeeded,
1423  *     %FALSE if the key was not writable
1424  *
1425  * Sets @key in @settings to @value.
1426  *
1427  * A convenience variant of g_settings_set() for strings.
1428  *
1429  * It is a programmer error to give a @key that isn't specified as
1430  * having a string type in the schema for @settings.
1431  *
1432  * Since: 2.26
1433  */
1434 gboolean
1435 g_settings_set_string (GSettings   *settings,
1436                        const gchar *key,
1437                        const gchar *value)
1438 {
1439   return g_settings_set_value (settings, key, g_variant_new_string (value));
1440 }
1441
1442 /**
1443  * g_settings_get_int:
1444  * @settings: a #GSettings object
1445  * @key: the key to get the value for
1446  * @returns: an integer
1447  *
1448  * Gets the value that is stored at @key in @settings.
1449  *
1450  * A convenience variant of g_settings_get() for 32-bit integers.
1451  *
1452  * It is a programmer error to give a @key that isn't specified as
1453  * having a int32 type in the schema for @settings.
1454  *
1455  * Since: 2.26
1456  */
1457 gint
1458 g_settings_get_int (GSettings   *settings,
1459                     const gchar *key)
1460 {
1461   GVariant *value;
1462   gint result;
1463
1464   value = g_settings_get_value (settings, key);
1465   result = g_variant_get_int32 (value);
1466   g_variant_unref (value);
1467
1468   return result;
1469 }
1470
1471 /**
1472  * g_settings_set_int:
1473  * @settings: a #GSettings object
1474  * @key: the name of the key to set
1475  * @value: the value to set it to
1476  * @returns: %TRUE if setting the key succeeded,
1477  *     %FALSE if the key was not writable
1478  *
1479  * Sets @key in @settings to @value.
1480  *
1481  * A convenience variant of g_settings_set() for 32-bit integers.
1482  *
1483  * It is a programmer error to give a @key that isn't specified as
1484  * having a int32 type in the schema for @settings.
1485  *
1486  * Since: 2.26
1487  */
1488 gboolean
1489 g_settings_set_int (GSettings   *settings,
1490                     const gchar *key,
1491                     gint         value)
1492 {
1493   return g_settings_set_value (settings, key, g_variant_new_int32 (value));
1494 }
1495
1496 /**
1497  * g_settings_get_double:
1498  * @settings: a #GSettings object
1499  * @key: the key to get the value for
1500  * @returns: a double
1501  *
1502  * Gets the value that is stored at @key in @settings.
1503  *
1504  * A convenience variant of g_settings_get() for doubles.
1505  *
1506  * It is a programmer error to give a @key that isn't specified as
1507  * having a 'double' type in the schema for @settings.
1508  *
1509  * Since: 2.26
1510  */
1511 gdouble
1512 g_settings_get_double (GSettings   *settings,
1513                        const gchar *key)
1514 {
1515   GVariant *value;
1516   gdouble result;
1517
1518   value = g_settings_get_value (settings, key);
1519   result = g_variant_get_double (value);
1520   g_variant_unref (value);
1521
1522   return result;
1523 }
1524
1525 /**
1526  * g_settings_set_double:
1527  * @settings: a #GSettings object
1528  * @key: the name of the key to set
1529  * @value: the value to set it to
1530  * @returns: %TRUE if setting the key succeeded,
1531  *     %FALSE if the key was not writable
1532  *
1533  * Sets @key in @settings to @value.
1534  *
1535  * A convenience variant of g_settings_set() for doubles.
1536  *
1537  * It is a programmer error to give a @key that isn't specified as
1538  * having a 'double' type in the schema for @settings.
1539  *
1540  * Since: 2.26
1541  */
1542 gboolean
1543 g_settings_set_double (GSettings   *settings,
1544                        const gchar *key,
1545                        gdouble      value)
1546 {
1547   return g_settings_set_value (settings, key, g_variant_new_double (value));
1548 }
1549
1550 /**
1551  * g_settings_get_boolean:
1552  * @settings: a #GSettings object
1553  * @key: the key to get the value for
1554  * @returns: a boolean
1555  *
1556  * Gets the value that is stored at @key in @settings.
1557  *
1558  * A convenience variant of g_settings_get() for booleans.
1559  *
1560  * It is a programmer error to give a @key that isn't specified as
1561  * having a boolean type in the schema for @settings.
1562  *
1563  * Since: 2.26
1564  */
1565 gboolean
1566 g_settings_get_boolean (GSettings  *settings,
1567                        const gchar *key)
1568 {
1569   GVariant *value;
1570   gboolean result;
1571
1572   value = g_settings_get_value (settings, key);
1573   result = g_variant_get_boolean (value);
1574   g_variant_unref (value);
1575
1576   return result;
1577 }
1578
1579 /**
1580  * g_settings_set_boolean:
1581  * @settings: a #GSettings object
1582  * @key: the name of the key to set
1583  * @value: the value to set it to
1584  * @returns: %TRUE if setting the key succeeded,
1585  *     %FALSE if the key was not writable
1586  *
1587  * Sets @key in @settings to @value.
1588  *
1589  * A convenience variant of g_settings_set() for booleans.
1590  *
1591  * It is a programmer error to give a @key that isn't specified as
1592  * having a boolean type in the schema for @settings.
1593  *
1594  * Since: 2.26
1595  */
1596 gboolean
1597 g_settings_set_boolean (GSettings  *settings,
1598                        const gchar *key,
1599                        gboolean     value)
1600 {
1601   return g_settings_set_value (settings, key, g_variant_new_boolean (value));
1602 }
1603
1604 /**
1605  * g_settings_get_strv:
1606  * @settings: a #GSettings object
1607  * @key: the key to get the value for
1608  * @returns: a newly-allocated, %NULL-terminated array of strings
1609  *
1610  * Gets the value that is stored at @key in @settings.
1611  *
1612  * A convenience variant of g_settings_get() for string arrays.
1613  *
1614  * It is a programmer error to give a @key that isn't specified as
1615  * having an array of strings type in the schema for @settings.
1616  *
1617  * Since: 2.26
1618  */
1619 gchar **
1620 g_settings_get_strv (GSettings   *settings,
1621                      const gchar *key)
1622 {
1623   GVariant *value;
1624   gchar **result;
1625
1626   value = g_settings_get_value (settings, key);
1627   result = g_variant_dup_strv (value, NULL);
1628   g_variant_unref (value);
1629
1630   return result;
1631 }
1632
1633 /**
1634  * g_settings_set_strv:
1635  * @settings: a #GSettings object
1636  * @key: the name of the key to set
1637  * @value: (allow-none): the value to set it to, or %NULL
1638  * @returns: %TRUE if setting the key succeeded,
1639  *     %FALSE if the key was not writable
1640  *
1641  * Sets @key in @settings to @value.
1642  *
1643  * A convenience variant of g_settings_set() for string arrays.  If
1644  * @value is %NULL, then @key is set to be the empty array.
1645  *
1646  * It is a programmer error to give a @key that isn't specified as
1647  * having an array of strings type in the schema for @settings.
1648  *
1649  * Since: 2.26
1650  */
1651 gboolean
1652 g_settings_set_strv (GSettings           *settings,
1653                      const gchar         *key,
1654                      const gchar * const *value)
1655 {
1656   GVariant *array;
1657
1658   if (value != NULL)
1659     array = g_variant_new_strv (value, -1);
1660   else
1661     array = g_variant_new_strv (NULL, 0);
1662
1663   return g_settings_set_value (settings, key, array);
1664 }
1665
1666 /* Delayed apply (delay, apply, revert, get_has_unapplied) {{{1 */
1667 /**
1668  * g_settings_delay:
1669  * @settings: a #GSettings object
1670  *
1671  * Changes the #GSettings object into 'delay-apply' mode. In this
1672  * mode, changes to @settings are not immediately propagated to the
1673  * backend, but kept locally until g_settings_apply() is called.
1674  *
1675  * Since: 2.26
1676  */
1677 void
1678 g_settings_delay (GSettings *settings)
1679 {
1680   g_return_if_fail (G_IS_SETTINGS (settings));
1681
1682   if (settings->priv->delayed)
1683     return;
1684
1685   settings->priv->delayed =
1686     g_delayed_settings_backend_new (settings->priv->backend,
1687                                     settings,
1688                                     settings->priv->main_context);
1689   g_settings_backend_unwatch (settings->priv->backend, G_OBJECT (settings));
1690   g_object_unref (settings->priv->backend);
1691
1692   settings->priv->backend = G_SETTINGS_BACKEND (settings->priv->delayed);
1693   g_settings_backend_watch (settings->priv->backend, G_OBJECT (settings),
1694                             settings->priv->main_context,
1695                             settings_backend_changed,
1696                             settings_backend_path_changed,
1697                             settings_backend_keys_changed,
1698                             settings_backend_writable_changed,
1699                             settings_backend_path_writable_changed);
1700 }
1701
1702 /**
1703  * g_settings_apply:
1704  * @settings: a #GSettings instance
1705  *
1706  * Applies any changes that have been made to the settings.  This
1707  * function does nothing unless @settings is in 'delay-apply' mode;
1708  * see g_settings_set_delay_apply().  In the normal case settings are
1709  * always applied immediately.
1710  **/
1711 void
1712 g_settings_apply (GSettings *settings)
1713 {
1714   if (settings->priv->delayed)
1715     {
1716       GDelayedSettingsBackend *delayed;
1717
1718       delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
1719       g_delayed_settings_backend_apply (delayed);
1720     }
1721 }
1722
1723 /**
1724  * g_settings_revert:
1725  * @settings: a #GSettings instance
1726  *
1727  * Reverts all non-applied changes to the settings.  This function
1728  * does nothing unless @settings is in 'delay-apply' mode; see
1729  * g_settings_set_delay_apply().  In the normal case settings are
1730  * always applied immediately.
1731  *
1732  * Change notifications will be emitted for affected keys.
1733  **/
1734 void
1735 g_settings_revert (GSettings *settings)
1736 {
1737   if (settings->priv->delayed)
1738     {
1739       GDelayedSettingsBackend *delayed;
1740
1741       delayed = G_DELAYED_SETTINGS_BACKEND (settings->priv->backend);
1742       g_delayed_settings_backend_revert (delayed);
1743     }
1744 }
1745
1746 /**
1747  * g_settings_get_has_unapplied:
1748  * @settings: a #GSettings object
1749  * @returns: %TRUE if @settings has unapplied changes
1750  *
1751  * Returns whether the #GSettings object has any unapplied
1752  * changes.  This can only be the case if it is in 'delayed-apply' mode.
1753  *
1754  * Since: 2.26
1755  */
1756 gboolean
1757 g_settings_get_has_unapplied (GSettings *settings)
1758 {
1759   g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1760
1761   return settings->priv->delayed &&
1762          g_delayed_settings_backend_get_has_unapplied (
1763            G_DELAYED_SETTINGS_BACKEND (settings->priv->backend));
1764 }
1765
1766 /* Extra API (sync, get_child, is_writable, list_keys) {{{1 */
1767 /**
1768  * g_settings_sync:
1769  * @context: the context to sync, or %NULL
1770  *
1771  * Ensures that all pending operations for the given context are
1772  * complete.
1773  *
1774  * Writes made to a #GSettings are handled asynchronously.  For this
1775  * reason, it is very unlikely that the changes have it to disk by the
1776  * time g_settings_set() returns.
1777  *
1778  * This call will block until all of the writes have made it to the
1779  * backend.  Since the mainloop is not running, no change notifications
1780  * will be dispatched during this call (but some may be queued by the
1781  * time the call is done).
1782  **/
1783 void
1784 g_settings_sync (void)
1785 {
1786   g_settings_backend_sync_default ();
1787 }
1788
1789 /**
1790  * g_settings_is_writable:
1791  * @settings: a #GSettings object
1792  * @name: the name of a key
1793  * @returns: %TRUE if the key @name is writable
1794  *
1795  * Finds out if a key can be written or not
1796  *
1797  * Since: 2.26
1798  */
1799 gboolean
1800 g_settings_is_writable (GSettings   *settings,
1801                         const gchar *name)
1802 {
1803   gboolean writable;
1804   gchar *path;
1805
1806   g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1807
1808   path = g_strconcat (settings->priv->path, name, NULL);
1809   writable = g_settings_backend_get_writable (settings->priv->backend, path);
1810   g_free (path);
1811
1812   return writable;
1813 }
1814
1815 /**
1816  * g_settings_get_child:
1817  * @settings: a #GSettings object
1818  * @name: the name of the 'child' schema
1819  * @returns: a 'child' settings object
1820  *
1821  * Creates a 'child' settings object which has a base path of
1822  * <replaceable>base-path</replaceable>/@name", where
1823  * <replaceable>base-path</replaceable> is the base path of @settings.
1824  *
1825  * The schema for the child settings object must have been declared
1826  * in the schema of @settings using a <tag class="starttag">child</tag> element.
1827  *
1828  * Since: 2.26
1829  */
1830 GSettings *
1831 g_settings_get_child (GSettings   *settings,
1832                       const gchar *name)
1833 {
1834   const gchar *child_schema;
1835   gchar *child_path;
1836   gchar *child_name;
1837   GSettings *child;
1838
1839   g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
1840
1841   child_name = g_strconcat (name, "/", NULL);
1842   child_schema = g_settings_schema_get_string (settings->priv->schema,
1843                                                child_name);
1844   if (child_schema == NULL)
1845     g_error ("Schema '%s' has no child '%s'",
1846              settings->priv->schema_name, name);
1847
1848   child_path = g_strconcat (settings->priv->path, child_name, NULL);
1849   child = g_object_new (G_TYPE_SETTINGS,
1850                         "schema", child_schema,
1851                         "path", child_path,
1852                         NULL);
1853   g_free (child_path);
1854   g_free (child_name);
1855
1856   return child;
1857 }
1858
1859 /**
1860  * g_settings_list_keys:
1861  * @settings: a #GSettings object
1862  * Returns: a list of the keys on @settings
1863  *
1864  * Introspects the list of keys on @settings.
1865  *
1866  * You should probably not be calling this function from "normal" code
1867  * (since you should already know what keys are in your schema).  This
1868  * function is intended for introspection reasons.
1869  *
1870  * You should free the return value with g_strfreev() when you are done
1871  * with it.
1872  */
1873 gchar **
1874 g_settings_list_keys (GSettings *settings)
1875 {
1876   const GQuark *keys;
1877   gchar **strv;
1878   gint n_keys;
1879   gint i;
1880
1881   keys = g_settings_schema_list (settings->priv->schema, &n_keys);
1882   strv = g_new (gchar *, n_keys + 1);
1883   for (i = 0; i < n_keys; i++)
1884     strv[i] = g_strdup (g_quark_to_string (keys[i]));
1885   strv[i] = NULL;
1886
1887   return strv;
1888 }
1889
1890 /* Binding {{{1 */
1891 typedef struct
1892 {
1893   GSettings *settings;
1894   GObject *object;
1895
1896   GSettingsBindGetMapping get_mapping;
1897   GSettingsBindSetMapping set_mapping;
1898   gpointer user_data;
1899   GDestroyNotify destroy;
1900
1901   guint writable_handler_id;
1902   guint property_handler_id;
1903   const GParamSpec *property;
1904   guint key_handler_id;
1905   GVariantType *type;
1906   const gchar *key;
1907
1908   /* prevent recursion */
1909   gboolean running;
1910 } GSettingsBinding;
1911
1912 static void
1913 g_settings_binding_free (gpointer data)
1914 {
1915   GSettingsBinding *binding = data;
1916
1917   g_assert (!binding->running);
1918
1919   if (binding->writable_handler_id)
1920     g_signal_handler_disconnect (binding->settings,
1921                                  binding->writable_handler_id);
1922
1923   if (binding->key_handler_id)
1924     g_signal_handler_disconnect (binding->settings,
1925                                  binding->key_handler_id);
1926
1927   if (g_signal_handler_is_connected (binding->object,
1928                                      binding->property_handler_id))
1929   g_signal_handler_disconnect (binding->object,
1930                                binding->property_handler_id);
1931
1932   g_variant_type_free (binding->type);
1933   g_object_unref (binding->settings);
1934
1935   if (binding->destroy)
1936     binding->destroy (binding->user_data);
1937
1938   g_slice_free (GSettingsBinding, binding);
1939 }
1940
1941 static GQuark
1942 g_settings_binding_quark (const char *property)
1943 {
1944   GQuark quark;
1945   gchar *tmp;
1946
1947   tmp = g_strdup_printf ("gsettingsbinding-%s", property);
1948   quark = g_quark_from_string (tmp);
1949   g_free (tmp);
1950
1951   return quark;
1952 }
1953
1954 static void
1955 g_settings_binding_key_changed (GSettings   *settings,
1956                                 const gchar *key,
1957                                 gpointer     user_data)
1958 {
1959   GSettingsBinding *binding = user_data;
1960   GValue value = { 0, };
1961   GVariant *variant;
1962
1963   g_assert (settings == binding->settings);
1964   g_assert (key == binding->key);
1965
1966   if (binding->running)
1967     return;
1968
1969   binding->running = TRUE;
1970
1971   g_value_init (&value, binding->property->value_type);
1972   variant = g_settings_get_value (settings, key);
1973   if (binding->get_mapping (&value, variant, binding->user_data))
1974     g_object_set_property (binding->object,
1975                            binding->property->name,
1976                            &value);
1977   g_variant_unref (variant);
1978   g_value_unset (&value);
1979
1980   binding->running = FALSE;
1981 }
1982
1983 static void
1984 g_settings_binding_property_changed (GObject          *object,
1985                                      const GParamSpec *pspec,
1986                                      gpointer          user_data)
1987 {
1988   GSettingsBinding *binding = user_data;
1989   GValue value = { 0, };
1990   GVariant *variant;
1991
1992   g_assert (object == binding->object);
1993   g_assert (pspec == binding->property);
1994
1995   if (binding->running)
1996     return;
1997
1998   binding->running = TRUE;
1999
2000   g_value_init (&value, pspec->value_type);
2001   g_object_get_property (object, pspec->name, &value);
2002   if ((variant = binding->set_mapping (&value, binding->type,
2003                                        binding->user_data)))
2004     {
2005       g_settings_set_value (binding->settings,
2006                             binding->key,
2007                             g_variant_ref_sink (variant));
2008       g_variant_unref (variant);
2009     }
2010   g_value_unset (&value);
2011
2012   binding->running = FALSE;
2013 }
2014
2015 /**
2016  * g_settings_bind:
2017  * @settings: a #GSettings object
2018  * @key: the key to bind
2019  * @object: a #GObject
2020  * @property: the name of the property to bind
2021  * @flags: flags for the binding
2022  *
2023  * Create a binding between the @key in the @settings object
2024  * and the property @property of @object.
2025  *
2026  * The binding uses the default GIO mapping functions to map
2027  * between the settings and property values. These functions
2028  * handle booleans, numeric types and string types in a
2029  * straightforward way. Use g_settings_bind_with_mapping() if
2030  * you need a custom mapping, or map between types that are not
2031  * supported by the default mapping functions.
2032  *
2033  * Unless the @flags include %G_SETTINGS_BIND_NO_SENSITIVITY, this
2034  * function also establishes a binding between the writability of
2035  * @key and the "sensitive" property of @object (if @object has
2036  * a boolean property by that name). See g_settings_bind_writable()
2037  * for more details about writable bindings.
2038  *
2039  * Note that the lifecycle of the binding is tied to the object,
2040  * and that you can have only one binding per object property.
2041  * If you bind the same property twice on the same object, the second
2042  * binding overrides the first one.
2043  *
2044  * Since: 2.26
2045  */
2046 void
2047 g_settings_bind (GSettings          *settings,
2048                  const gchar        *key,
2049                  gpointer            object,
2050                  const gchar        *property,
2051                  GSettingsBindFlags  flags)
2052 {
2053   g_settings_bind_with_mapping (settings, key, object, property,
2054                                 flags, NULL, NULL, NULL, NULL);
2055 }
2056
2057 /**
2058  * g_settings_bind_with_mapping:
2059  * @settings: a #GSettings object
2060  * @key: the key to bind
2061  * @object: a #GObject
2062  * @property: the name of the property to bind
2063  * @flags: flags for the binding
2064  * @get_mapping: a function that gets called to convert values
2065  *     from @settings to @object, or %NULL to use the default GIO mapping
2066  * @set_mapping: a function that gets called to convert values
2067  *     from @object to @settings, or %NULL to use the default GIO mapping
2068  * @user_data: data that gets passed to @get_mapping and @set_mapping
2069  * @destroy: #GDestroyNotify function for @user_data
2070  *
2071  * Create a binding between the @key in the @settings object
2072  * and the property @property of @object.
2073  *
2074  * The binding uses the provided mapping functions to map between
2075  * settings and property values.
2076  *
2077  * Note that the lifecycle of the binding is tied to the object,
2078  * and that you can have only one binding per object property.
2079  * If you bind the same property twice on the same object, the second
2080  * binding overrides the first one.
2081  *
2082  * Since: 2.26
2083  */
2084 void
2085 g_settings_bind_with_mapping (GSettings               *settings,
2086                               const gchar             *key,
2087                               gpointer                 object,
2088                               const gchar             *property,
2089                               GSettingsBindFlags       flags,
2090                               GSettingsBindGetMapping  get_mapping,
2091                               GSettingsBindSetMapping  set_mapping,
2092                               gpointer                 user_data,
2093                               GDestroyNotify           destroy)
2094 {
2095   GSettingsBinding *binding;
2096   GObjectClass *objectclass;
2097   gchar *detailed_signal;
2098   GQuark binding_quark;
2099
2100   g_return_if_fail (G_IS_SETTINGS (settings));
2101
2102   objectclass = G_OBJECT_GET_CLASS (object);
2103
2104   binding = g_slice_new0 (GSettingsBinding);
2105   binding->settings = g_object_ref (settings);
2106   binding->object = object;
2107   binding->key = g_intern_string (key);
2108   binding->property = g_object_class_find_property (objectclass, property);
2109   binding->user_data = user_data;
2110   binding->destroy = destroy;
2111   binding->get_mapping = get_mapping ? get_mapping : g_settings_get_mapping;
2112   binding->set_mapping = set_mapping ? set_mapping : g_settings_set_mapping;
2113
2114   if (!(flags & (G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET)))
2115     flags |= G_SETTINGS_BIND_GET | G_SETTINGS_BIND_SET;
2116
2117   if (binding->property == NULL)
2118     {
2119       g_critical ("g_settings_bind: no property '%s' on class '%s'",
2120                   property, G_OBJECT_TYPE_NAME (object));
2121       return;
2122     }
2123
2124   if ((flags & G_SETTINGS_BIND_GET) && (binding->property->flags & G_PARAM_WRITABLE) == 0)
2125     {
2126       g_critical ("g_settings_bind: property '%s' on class '%s' is not writable",
2127                   property, G_OBJECT_TYPE_NAME (object));
2128       return;
2129     }
2130   if ((flags & G_SETTINGS_BIND_SET) && (binding->property->flags & G_PARAM_READABLE) == 0)
2131     {
2132       g_critical ("g_settings_bind: property '%s' on class '%s' is not readable",
2133                   property, G_OBJECT_TYPE_NAME (object));
2134       return;
2135     }
2136
2137   {
2138     GVariantIter *iter;
2139     GVariant *value;
2140
2141     iter = g_settings_schema_get_value (settings->priv->schema, key);
2142     value = g_variant_iter_next_value (iter);
2143     binding->type = g_variant_type_copy (g_variant_get_type (value));
2144     g_variant_iter_free (iter);
2145     g_variant_unref (value);
2146   }
2147
2148   if (binding->type == NULL)
2149     {
2150       g_critical ("g_settings_bind: no key '%s' on schema '%s'",
2151                   key, settings->priv->schema_name);
2152       return;
2153     }
2154
2155   if (((get_mapping == NULL && (flags & G_SETTINGS_BIND_GET)) ||
2156        (set_mapping == NULL && (flags & G_SETTINGS_BIND_SET))) &&
2157       !g_settings_mapping_is_compatible (binding->property->value_type,
2158                                          binding->type))
2159     {
2160       g_critical ("g_settings_bind: property '%s' on class '%s' has type "
2161                   "'%s' which is not compatible with type '%s' of key '%s' "
2162                   "on schema '%s'", property, G_OBJECT_TYPE_NAME (object),
2163                   g_type_name (binding->property->value_type),
2164                   g_variant_type_dup_string (binding->type), key,
2165                   settings->priv->schema_name);
2166       return;
2167     }
2168
2169   if ((flags & G_SETTINGS_BIND_SET) &&
2170       (~flags & G_SETTINGS_BIND_NO_SENSITIVITY))
2171     {
2172       GParamSpec *sensitive;
2173
2174       sensitive = g_object_class_find_property (objectclass, "sensitive");
2175
2176       if (sensitive && sensitive->value_type == G_TYPE_BOOLEAN &&
2177           (sensitive->flags & G_PARAM_WRITABLE))
2178         g_settings_bind_writable (settings, binding->key,
2179                                   object, "sensitive", FALSE);
2180     }
2181
2182   if (flags & G_SETTINGS_BIND_SET)
2183     {
2184       detailed_signal = g_strdup_printf ("notify::%s", property);
2185       binding->property_handler_id =
2186         g_signal_connect (object, detailed_signal,
2187                           G_CALLBACK (g_settings_binding_property_changed),
2188                           binding);
2189       g_free (detailed_signal);
2190
2191       if (~flags & G_SETTINGS_BIND_GET)
2192         g_settings_binding_property_changed (object,
2193                                              binding->property,
2194                                              binding);
2195     }
2196
2197   if (flags & G_SETTINGS_BIND_GET)
2198     {
2199       if (~flags & G_SETTINGS_BIND_GET_NO_CHANGES)
2200         {
2201           detailed_signal = g_strdup_printf ("changed::%s", key);
2202           binding->key_handler_id =
2203             g_signal_connect (settings, detailed_signal,
2204                               G_CALLBACK (g_settings_binding_key_changed),
2205                               binding);
2206           g_free (detailed_signal);
2207         }
2208
2209       g_settings_binding_key_changed (settings, binding->key, binding);
2210     }
2211
2212   binding_quark = g_settings_binding_quark (property);
2213   g_object_set_qdata_full (object, binding_quark,
2214                            binding, g_settings_binding_free);
2215 }
2216
2217 /* Writability binding {{{1 */
2218 typedef struct
2219 {
2220   GSettings *settings;
2221   gpointer object;
2222   const gchar *key;
2223   const gchar *property;
2224   gboolean inverted;
2225   gulong handler_id;
2226 } GSettingsWritableBinding;
2227
2228 static void
2229 g_settings_writable_binding_free (gpointer data)
2230 {
2231   GSettingsWritableBinding *binding = data;
2232
2233   g_signal_handler_disconnect (binding->settings, binding->handler_id);
2234   g_object_unref (binding->settings);
2235   g_slice_free (GSettingsWritableBinding, binding);
2236 }
2237
2238 static void
2239 g_settings_binding_writable_changed (GSettings   *settings,
2240                                      const gchar *key,
2241                                      gpointer     user_data)
2242 {
2243   GSettingsWritableBinding *binding = user_data;
2244   gboolean writable;
2245
2246   g_assert (settings == binding->settings);
2247   g_assert (key == binding->key);
2248
2249   writable = g_settings_is_writable (settings, key);
2250
2251   if (binding->inverted)
2252     writable = !writable;
2253
2254   g_object_set (binding->object, binding->property, writable, NULL);
2255 }
2256
2257 /**
2258  * g_settings_bind_writable:
2259  * @settings: a #GSettings object
2260  * @key: the key to bind
2261  * @object: a #GObject
2262  * @property: the name of a boolean property to bind
2263  * @inverted: whether to 'invert' the value
2264  *
2265  * Create a binding between the writability of @key in the
2266  * @settings object and the property @property of @object.
2267  * The property must be boolean; "sensitive" or "visible"
2268  * properties of widgets are the most likely candidates.
2269  *
2270  * Writable bindings are always uni-directional; changes of the
2271  * writability of the setting will be propagated to the object
2272  * property, not the other way.
2273  *
2274  * When the @inverted argument is %TRUE, the binding inverts the
2275  * value as it passes from the setting to the object, i.e. @property
2276  * will be set to %TRUE if the key is <emphasis>not</emphasis>
2277  * writable.
2278  *
2279  * Note that the lifecycle of the binding is tied to the object,
2280  * and that you can have only one binding per object property.
2281  * If you bind the same property twice on the same object, the second
2282  * binding overrides the first one.
2283  *
2284  * Since: 2.26
2285  */
2286 void
2287 g_settings_bind_writable (GSettings   *settings,
2288                           const gchar *key,
2289                           gpointer     object,
2290                           const gchar *property,
2291                           gboolean     inverted)
2292 {
2293   GSettingsWritableBinding *binding;
2294   gchar *detailed_signal;
2295   GParamSpec *pspec;
2296
2297   g_return_if_fail (G_IS_SETTINGS (settings));
2298
2299   pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object), property);
2300   if (pspec == NULL)
2301     {
2302       g_critical ("g_settings_bind_writable: no property '%s' on class '%s'",
2303                   property, G_OBJECT_TYPE_NAME (object));
2304       return;
2305     }
2306   if ((pspec->flags & G_PARAM_WRITABLE) == 0)
2307     {
2308       g_critical ("g_settings_bind_writable: property '%s' on class '%s' is not writable",
2309                   property, G_OBJECT_TYPE_NAME (object));
2310       return;
2311     }
2312
2313   binding = g_slice_new (GSettingsWritableBinding);
2314   binding->settings = g_object_ref (settings);
2315   binding->object = object;
2316   binding->key = g_intern_string (key);
2317   binding->property = g_intern_string (property);
2318   binding->inverted = inverted;
2319
2320   detailed_signal = g_strdup_printf ("writable-changed::%s", key);
2321   binding->handler_id =
2322     g_signal_connect (settings, detailed_signal,
2323                       G_CALLBACK (g_settings_binding_writable_changed),
2324                       binding);
2325   g_free (detailed_signal);
2326
2327   g_object_set_qdata_full (object, g_settings_binding_quark (property),
2328                            binding, g_settings_writable_binding_free);
2329
2330   g_settings_binding_writable_changed (settings, binding->key, binding);
2331 }
2332
2333 /**
2334  * g_settings_unbind:
2335  * @object: the object
2336  * @property: the property whose binding is removed
2337  *
2338  * Removes an existing binding for @property on @object.
2339  *
2340  * Note that bindings are automatically removed when the
2341  * object is finalized, so it is rarely necessary to call this
2342  * function.
2343  *
2344  * Since: 2.26
2345  */
2346 void
2347 g_settings_unbind (gpointer     object,
2348                    const gchar *property)
2349 {
2350   GQuark binding_quark;
2351
2352   binding_quark = g_settings_binding_quark (property);
2353   g_object_set_qdata (object, binding_quark, NULL);
2354 }
2355
2356 /* Epilogue {{{1 */
2357 #define __G_SETTINGS_C__
2358 #include "gioaliasdef.c"
2359
2360 /* vim:set foldmethod=marker: */