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