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