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