gsettings.c 88.3 KB
Newer Older
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright © 2009, 2010 Codethink Limited
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the licence, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Ryan Lortie <desrt@desrt.ca>
 */

22
/* Prelude {{{1 */
Allison Karlitskaya's avatar
Allison Karlitskaya committed
23
#include "config.h"
24

Allison Karlitskaya's avatar
Allison Karlitskaya committed
25 26 27 28 29 30 31 32
#include <glib.h>
#include <glibintl.h>

#include "gsettings.h"

#include "gdelayedsettingsbackend.h"
#include "gsettingsbackendinternal.h"
#include "gsettings-mapping.h"
33
#include "gsettingsschema-internal.h"
Allison Karlitskaya's avatar
Allison Karlitskaya committed
34

35 36
#include "strinfo.c"

Allison Karlitskaya's avatar
Allison Karlitskaya committed
37 38
/**
 * SECTION:gsettings
Matthias Clasen's avatar
Matthias Clasen committed
39
 * @short_description: High-level API for application settings
Allison Karlitskaya's avatar
Allison Karlitskaya committed
40 41 42 43
 *
 * The #GSettings class provides a convenient API for storing and retrieving
 * application settings.
 *
44 45 46 47 48
 * Reads and writes can be considered to be non-blocking.  Reading
 * settings with #GSettings is typically extremely fast: on
 * approximately the same order of magnitude (but slower than) a
 * #GHashTable lookup.  Writing settings is also extremely fast in terms
 * of time to return to your application, but can be extremely expensive
49
 * for other threads and other processes.  Many settings backends
50 51 52 53 54 55
 * (including dconf) have lazy initialisation which means in the common
 * case of the user using their computer without modifying any settings
 * a lot of work can be avoided.  For dconf, the D-Bus service doesn't
 * even need to be started in this case.  For this reason, you should
 * only ever modify #GSettings keys in response to explicit user action.
 * Particular care should be paid to ensure that modifications are not
56
 * made during startup -- for example, when setting the initial value
57
 * of preferences widgets.  The built-in g_settings_bind() functionality
58 59 60
 * is careful not to write settings in response to notify signals as a
 * result of modifications that it makes to widgets.
 *
Allison Karlitskaya's avatar
Allison Karlitskaya committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
 * When creating a GSettings instance, you have to specify a schema
 * that describes the keys in your settings and their types and default
 * values, as well as some other information.
 *
 * Normally, a schema has as fixed path that determines where the settings
 * are stored in the conceptual global tree of settings. However, schemas
 * can also be 'relocatable', i.e. not equipped with a fixed path. This is
 * useful e.g. when the schema describes an 'account', and you want to be
 * able to store a arbitrary number of accounts.
 *
 * Unlike other configuration systems (like GConf), GSettings does not
 * restrict keys to basic types like strings and numbers. GSettings stores
 * values as #GVariant, and allows any #GVariantType for keys. Key names
 * are restricted to lowercase characters, numbers and '-'. Furthermore,
 * the names must begin with a lowercase character, must not end
76
 * with a '-', and must not contain consecutive dashes.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
77 78 79
 *
 * Similar to GConf, the default values in GSettings schemas can be
 * localized, but the localized values are stored in gettext catalogs
80 81 82 83 84
 * and looked up with the domain that is specified in the
 * <tag class="attribute">gettext-domain</tag> attribute of the
 * <tag class="starttag">schemalist</tag> or <tag class="starttag">schema</tag>
 * elements and the category that is specified in the l10n attribute of the
 * <tag class="starttag">key</tag> element.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
85 86
 *
 * GSettings uses schemas in a compact binary form that is created
87 88 89
 * by the <link linkend="glib-compile-schemas">glib-compile-schemas</link>
 * utility. The input is a schema description in an XML format that can be
 * described by the following DTD:
Matthias Clasen's avatar
Matthias Clasen committed
90
 * |[<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/gschema.dtd"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include>]|
Allison Karlitskaya's avatar
Allison Karlitskaya committed
91
 *
92 93
 * glib-compile-schemas expects schema files to have the extension <filename>.gschema.xml</filename>
 *
94 95 96 97
 * At runtime, schemas are identified by their id (as specified
 * in the <tag class="attribute">id</tag> attribute of the
 * <tag class="starttag">schema</tag> element). The
 * convention for schema ids is to use a dotted name, similar in
98 99 100 101 102
 * style to a D-Bus bus name, e.g. "org.gnome.SessionManager". In particular,
 * if the settings are for a specific service that owns a D-Bus bus name,
 * the D-Bus bus name and schema id should match. For schemas which deal
 * with settings not associated with one named application, the id should
 * not use StudlyCaps, e.g. "org.gnome.font-rendering".
103
 *
104
 * In addition to #GVariant types, keys can have types that have enumerated
Matthias Clasen's avatar
Matthias Clasen committed
105 106
 * types. These can be described by a <tag class="starttag">choice</tag>,
 * <tag class="starttag">enum</tag> or <tag class="starttag">flags</tag> element, see
107 108 109 110 111 112 113
 * <xref linkend="schema-enumerated"/>. The underlying type of
 * such a key is string, but you can use g_settings_get_enum(),
 * g_settings_set_enum(), g_settings_get_flags(), g_settings_set_flags()
 * access the numeric values corresponding to the string value of enum
 * and flags keys.
 *
 * <example id="schema-default-values"><title>Default values</title>
114 115
 * <programlisting><![CDATA[
 * <schemalist>
116
 *   <schema id="org.gtk.Test" path="/tests/" gettext-domain="test">
117
 *
118 119 120 121 122 123 124
 *     <key name="greeting" type="s">
 *       <default l10n="messages">"Hello, earthlings"</default>
 *       <summary>A greeting</summary>
 *       <description>
 *         Greeting of the invading martians
 *       </description>
 *     </key>
125
 *
126 127 128
 *     <key name="box" type="(ii)">
 *       <default>(20,30)</default>
 *     </key>
129
 *
130 131 132 133
 *   </schema>
 * </schemalist>
 * ]]></programlisting></example>
 *
134
 * <example id="schema-enumerated"><title>Ranges, choices and enumerated types</title>
135 136
 * <programlisting><![CDATA[
 * <schemalist>
137
 *
138
 *   <enum id="org.gtk.Test.myenum">
139 140 141
 *     <value nick="first" value="1"/>
 *     <value nick="second" value="2"/>
 *   </enum>
142
 *
143
 *   <flags id="org.gtk.Test.myflags">
144 145 146
 *     <value nick="flag1" value="1"/>
 *     <value nick="flag2" value="2"/>
 *     <value nick="flag3" value="4"/>
147
 *   </flags>
148
 *
149
 *   <schema id="org.gtk.Test">
150
 *
151 152 153 154
 *     <key name="key-with-range" type="i">
 *       <range min="1" max="100"/>
 *       <default>10</default>
 *     </key>
155
 *
156 157 158 159 160 161 162
 *     <key name="key-with-choices" type="s">
 *       <choices>
 *         <choice value='Elisabeth'/>
 *         <choice value='Annabeth'/>
 *         <choice value='Joe'/>
 *       </choices>
 *       <aliases>
163 164
 *         <alias value='Anna' target='Annabeth'/>
 *         <alias value='Beth' target='Elisabeth'/>
165 166 167
 *       </aliases>
 *       <default>'Joe'</default>
 *     </key>
168
 *
169
 *     <key name='enumerated-key' enum='org.gtk.Test.myenum'>
170 171
 *       <default>'first'</default>
 *     </key>
172
 *
173
 *     <key name='flags-key' flags='org.gtk.Test.myflags'>
174 175
 *       <default>["flag1",flag2"]</default>
 *     </key>
176 177 178 179
 *   </schema>
 * </schemalist>
 * ]]></programlisting></example>
 *
Allison Karlitskaya's avatar
Allison Karlitskaya committed
180
 * <refsect2>
181
 *   <title>Vendor overrides</title>
Allison Karlitskaya's avatar
Allison Karlitskaya committed
182
 *   <para>
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
 *     Default values are defined in the schemas that get installed by
 *     an application. Sometimes, it is necessary for a vendor or distributor
 *     to adjust these defaults. Since patching the XML source for the schema
 *     is inconvenient and error-prone,
 *     <link linkend="glib-compile-schemas">glib-compile-schemas</link> reads
 *     so-called 'vendor override' files. These are keyfiles in the same
 *     directory as the XML schema sources which can override default values.
 *     The schema id serves as the group name in the key file, and the values
 *     are expected in serialized GVariant form, as in the following example:
 *     <informalexample><programlisting>
 *     [org.gtk.Example]
 *     key1='string'
 *     key2=1.5
 *     </programlisting></informalexample>
 *   </para>
198 199 200 201
 *   <para>
 *     glib-compile-schemas expects schema files to have the extension
 *     <filename>.gschema.override</filename>
 *   </para>
202 203 204 205 206 207 208 209 210 211 212
 * </refsect2>
 *
 * <refsect2>
 *   <title>Binding</title>
 *   <para>
 *     A very convenient feature of GSettings lets you bind #GObject properties
 *     directly to settings, using g_settings_bind(). Once a GObject property
 *     has been bound to a setting, changes on either side are automatically
 *     propagated to the other side. GSettings handles details like
 *     mapping between GObject and GVariant types, and preventing infinite
 *     cycles.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
213 214
 *   </para>
 *   <para>
215 216 217 218 219 220
 *     This makes it very easy to hook up a preferences dialog to the
 *     underlying settings. To make this even more convenient, GSettings
 *     looks for a boolean property with the name "sensitivity" and
 *     automatically binds it to the writability of the bound setting.
 *     If this 'magic' gets in the way, it can be suppressed with the
 *     #G_SETTINGS_BIND_NO_SENSITIVITY flag.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
221
 *   </para>
222
 * </refsect2>
223
 **/
Allison Karlitskaya's avatar
Allison Karlitskaya committed
224 225 226

struct _GSettingsPrivate
{
227 228 229
  /* where the signals go... */
  GMainContext *main_context;

Allison Karlitskaya's avatar
Allison Karlitskaya committed
230 231 232 233 234 235 236 237 238 239
  GSettingsBackend *backend;
  GSettingsSchema *schema;
  gchar *path;

  GDelayedSettingsBackend *delayed;
};

enum
{
  PROP_0,
240
  PROP_SCHEMA,
241
  PROP_SCHEMA_ID,
242
  PROP_BACKEND,
Allison Karlitskaya's avatar
Allison Karlitskaya committed
243 244
  PROP_PATH,
  PROP_HAS_UNAPPLIED,
245
  PROP_DELAY_APPLY
Allison Karlitskaya's avatar
Allison Karlitskaya committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
};

enum
{
  SIGNAL_WRITABLE_CHANGE_EVENT,
  SIGNAL_WRITABLE_CHANGED,
  SIGNAL_CHANGE_EVENT,
  SIGNAL_CHANGED,
  N_SIGNALS
};

static guint g_settings_signals[N_SIGNALS];

G_DEFINE_TYPE (GSettings, g_settings, G_TYPE_OBJECT)

261
/* Signals {{{1 */
Allison Karlitskaya's avatar
Allison Karlitskaya committed
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
static gboolean
g_settings_real_change_event (GSettings    *settings,
                              const GQuark *keys,
                              gint          n_keys)
{
  gint i;

  if (keys == NULL)
    keys = g_settings_schema_list (settings->priv->schema, &n_keys);

  for (i = 0; i < n_keys; i++)
    g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGED],
                   keys[i], g_quark_to_string (keys[i]));

  return FALSE;
}

static gboolean
g_settings_real_writable_change_event (GSettings *settings,
                                       GQuark     key)
{
  const GQuark *keys = &key;
  gint n_keys = 1;
  gint i;

  if (key == 0)
    keys = g_settings_schema_list (settings->priv->schema, &n_keys);

  for (i = 0; i < n_keys; i++)
291 292
    g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGED],
                   keys[i], g_quark_to_string (keys[i]));
Allison Karlitskaya's avatar
Allison Karlitskaya committed
293 294 295 296 297

  return FALSE;
}

static void
298 299
settings_backend_changed (GObject             *target,
                          GSettingsBackend    *backend,
Allison Karlitskaya's avatar
Allison Karlitskaya committed
300
                          const gchar         *key,
301
                          gpointer             origin_tag)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
302
{
303
  GSettings *settings = G_SETTINGS (target);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
304 305 306
  gboolean ignore_this;
  gint i;

307 308 309 310 311 312 313 314 315 316 317
  /* We used to assert here:
   *
   *   settings->priv->backend == backend
   *
   * but it could be the case that a notification is queued for delivery
   * while someone calls g_settings_delay() (which changes the backend).
   *
   * Since the delay backend would just pass that straight through
   * anyway, it doesn't make sense to try to detect this case.
   * Therefore, we just accept it.
   */
Allison Karlitskaya's avatar
Allison Karlitskaya committed
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332

  for (i = 0; key[i] == settings->priv->path[i]; i++);

  if (settings->priv->path[i] == '\0' &&
      g_settings_schema_has_key (settings->priv->schema, key + i))
    {
      GQuark quark;

      quark = g_quark_from_string (key + i);
      g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
                     0, &quark, 1, &ignore_this);
    }
}

static void
333 334
settings_backend_path_changed (GObject          *target,
                               GSettingsBackend *backend,
Allison Karlitskaya's avatar
Allison Karlitskaya committed
335
                               const gchar      *path,
336
                               gpointer          origin_tag)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
337
{
338
  GSettings *settings = G_SETTINGS (target);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
339 340 341 342 343 344 345 346
  gboolean ignore_this;

  if (g_str_has_prefix (settings->priv->path, path))
    g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
                   0, NULL, 0, &ignore_this);
}

static void
347 348
settings_backend_keys_changed (GObject             *target,
                               GSettingsBackend    *backend,
Allison Karlitskaya's avatar
Allison Karlitskaya committed
349 350
                               const gchar         *path,
                               const gchar * const *items,
351
                               gpointer             origin_tag)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
352
{
353
  GSettings *settings = G_SETTINGS (target);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
  gboolean ignore_this;
  gint i;

  for (i = 0; settings->priv->path[i] &&
              settings->priv->path[i] == path[i]; i++);

  if (path[i] == '\0')
    {
      GQuark quarks[256];
      gint j, l = 0;

      for (j = 0; items[j]; j++)
         {
           const gchar *item = items[j];
           gint k;

           for (k = 0; item[k] == settings->priv->path[i + k]; k++);

           if (settings->priv->path[i + k] == '\0' &&
               g_settings_schema_has_key (settings->priv->schema, item + k))
             quarks[l++] = g_quark_from_string (item + k);

           /* "256 quarks ought to be enough for anybody!"
            * If this bites you, I'm sorry.  Please file a bug.
            */
           g_assert (l < 256);
         }

      if (l > 0)
        g_signal_emit (settings, g_settings_signals[SIGNAL_CHANGE_EVENT],
                       0, quarks, l, &ignore_this);
    }
}

static void
389 390
settings_backend_writable_changed (GObject          *target,
                                   GSettingsBackend *backend,
391
                                   const gchar      *key)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
392
{
393
  GSettings *settings = G_SETTINGS (target);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
394 395 396 397 398 399 400 401 402 403 404 405
  gboolean ignore_this;
  gint i;

  for (i = 0; key[i] == settings->priv->path[i]; i++);

  if (settings->priv->path[i] == '\0' &&
      g_settings_schema_has_key (settings->priv->schema, key + i))
    g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
                   0, g_quark_from_string (key + i), &ignore_this);
}

static void
406 407
settings_backend_path_writable_changed (GObject          *target,
                                        GSettingsBackend *backend,
408
                                        const gchar      *path)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
409
{
410
  GSettings *settings = G_SETTINGS (target);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
411 412 413 414 415 416 417
  gboolean ignore_this;

  if (g_str_has_prefix (settings->priv->path, path))
    g_signal_emit (settings, g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT],
                   0, (GQuark) 0, &ignore_this);
}

418
/* Properties, Construction, Destruction {{{1 */
Allison Karlitskaya's avatar
Allison Karlitskaya committed
419 420 421 422 423 424 425 426 427 428
static void
g_settings_set_property (GObject      *object,
                         guint         prop_id,
                         const GValue *value,
                         GParamSpec   *pspec)
{
  GSettings *settings = G_SETTINGS (object);

  switch (prop_id)
    {
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
    case PROP_SCHEMA:
      {
        GSettingsSchema *schema;

        schema = g_value_dup_boxed (value);

        /* we receive a set_property() call for "settings-schema" even
         * if it was not specified (ie: with NULL value).  ->schema
         * could already be set at this point (ie: via "schema-id").
         * check for NULL to avoid clobbering the existing value.
         */
        if (schema != NULL)
          {
            g_assert (settings->priv->schema == NULL);
            settings->priv->schema = schema;
          }
      }
      break;

448
    case PROP_SCHEMA_ID:
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
      {
        const gchar *schema_id;

        schema_id = g_value_get_string (value);

        /* we receive a set_property() call for both "schema" and
         * "schema-id", even if they are not set.  Hopefully only one of
         * them is non-NULL.
         */
        if (schema_id != NULL)
          {
            GSettingsSchemaSource *default_source;

            g_assert (settings->priv->schema == NULL);
            default_source = g_settings_schema_source_get_default ();

            if (default_source == NULL)
              g_error ("No GSettings schemas are installed on the system");

            settings->priv->schema = g_settings_schema_source_lookup (default_source, schema_id, TRUE);

            if (settings->priv->schema == NULL)
              g_error ("Settings schema '%s' is not installed\n", schema_id);
          }
      }
Allison Karlitskaya's avatar
Allison Karlitskaya committed
474 475 476 477 478 479
      break;

    case PROP_PATH:
      settings->priv->path = g_value_dup_string (value);
      break;

480 481
    case PROP_BACKEND:
      settings->priv->backend = g_value_dup_object (value);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
      break;

    default:
      g_assert_not_reached ();
    }
}

static void
g_settings_get_property (GObject    *object,
                         guint       prop_id,
                         GValue     *value,
                         GParamSpec *pspec)
{
  GSettings *settings = G_SETTINGS (object);

  switch (prop_id)
    {
499 500 501 502
    case PROP_SCHEMA:
      g_value_set_boxed (value, settings->priv->schema);
      break;

503
     case PROP_SCHEMA_ID:
504
      g_value_set_string (value, g_settings_schema_get_id (settings->priv->schema));
Allison Karlitskaya's avatar
Allison Karlitskaya committed
505 506
      break;

507 508 509 510
     case PROP_BACKEND:
      g_value_set_object (value, settings->priv->backend);
      break;

511 512 513 514
     case PROP_PATH:
      g_value_set_string (value, settings->priv->path);
      break;

Allison Karlitskaya's avatar
Allison Karlitskaya committed
515 516 517 518
     case PROP_HAS_UNAPPLIED:
      g_value_set_boolean (value, g_settings_get_has_unapplied (settings));
      break;

519 520 521 522
     case PROP_DELAY_APPLY:
      g_value_set_boolean (value, settings->priv->delayed != NULL);
      break;

Allison Karlitskaya's avatar
Allison Karlitskaya committed
523 524 525 526 527
     default:
      g_assert_not_reached ();
    }
}

528 529 530 531 532 533 534 535
static const GSettingsListenerVTable listener_vtable = {
  settings_backend_changed,
  settings_backend_path_changed,
  settings_backend_keys_changed,
  settings_backend_writable_changed,
  settings_backend_path_writable_changed
};

536 537 538 539 540 541 542 543 544
static void
g_settings_constructed (GObject *object)
{
  GSettings *settings = G_SETTINGS (object);
  const gchar *schema_path;

  schema_path = g_settings_schema_get_path (settings->priv->schema);

  if (settings->priv->path && schema_path && strcmp (settings->priv->path, schema_path) != 0)
545 546
    g_error ("settings object created with schema '%s' and path '%s', but path '%s' is specified by schema",
             g_settings_schema_get_id (settings->priv->schema), settings->priv->path, schema_path);
547 548 549 550

  if (settings->priv->path == NULL)
    {
      if (schema_path == NULL)
551
        g_error ("attempting to create schema '%s' without a path",
552
                 g_settings_schema_get_id (settings->priv->schema));
553 554 555 556

      settings->priv->path = g_strdup (schema_path);
    }

557 558 559
  if (settings->priv->backend == NULL)
    settings->priv->backend = g_settings_backend_get_default ();

560 561 562
  g_settings_backend_watch (settings->priv->backend,
                            &listener_vtable, G_OBJECT (settings),
                            settings->priv->main_context);
563 564 565 566
  g_settings_backend_subscribe (settings->priv->backend,
                                settings->priv->path);
}

Allison Karlitskaya's avatar
Allison Karlitskaya committed
567 568 569 570 571 572 573
static void
g_settings_finalize (GObject *object)
{
  GSettings *settings = G_SETTINGS (object);

  g_settings_backend_unsubscribe (settings->priv->backend,
                                  settings->priv->path);
574
  g_main_context_unref (settings->priv->main_context);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
575
  g_object_unref (settings->priv->backend);
576
  g_settings_schema_unref (settings->priv->schema);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
577
  g_free (settings->priv->path);
578 579

  G_OBJECT_CLASS (g_settings_parent_class)->finalize (object);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
580 581
}

582 583 584 585 586 587 588
static void
g_settings_init (GSettings *settings)
{
  settings->priv = G_TYPE_INSTANCE_GET_PRIVATE (settings,
                                                G_TYPE_SETTINGS,
                                                GSettingsPrivate);

589
  settings->priv->main_context = g_main_context_ref_thread_default ();
590 591
}

Allison Karlitskaya's avatar
Allison Karlitskaya committed
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
static void
g_settings_class_init (GSettingsClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);

  class->writable_change_event = g_settings_real_writable_change_event;
  class->change_event = g_settings_real_change_event;

  object_class->set_property = g_settings_set_property;
  object_class->get_property = g_settings_get_property;
  object_class->constructed = g_settings_constructed;
  object_class->finalize = g_settings_finalize;

  g_type_class_add_private (object_class, sizeof (GSettingsPrivate));

  /**
   * GSettings::changed:
   * @settings: the object on which the signal was emitted
610
   * @key: the name of the key that changed
Allison Karlitskaya's avatar
Allison Karlitskaya committed
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
   *
   * The "changed" signal is emitted when a key has potentially changed.
   * You should call one of the g_settings_get() calls to check the new
   * value.
   *
   * This signal supports detailed connections.  You can connect to the
   * detailed signal "changed::x" in order to only receive callbacks
   * when key "x" changes.
   */
  g_settings_signals[SIGNAL_CHANGED] =
    g_signal_new ("changed", G_TYPE_SETTINGS,
                  G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
                  G_STRUCT_OFFSET (GSettingsClass, changed),
                  NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
                  1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);

  /**
   * GSettings::change-event:
   * @settings: the object on which the signal was emitted
630 631
   * @keys: (array length=n_keys) (element-type GQuark) (allow-none):
   *        an array of #GQuark<!-- -->s for the changed keys, or %NULL
Allison Karlitskaya's avatar
Allison Karlitskaya committed
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
   * @n_keys: the length of the @keys array, or 0
   *
   * The "change-event" signal is emitted once per change event that
   * affects this settings object.  You should connect to this signal
   * only if you are interested in viewing groups of changes before they
   * are split out into multiple emissions of the "changed" signal.
   * For most use cases it is more appropriate to use the "changed" signal.
   *
   * In the event that the change event applies to one or more specified
   * keys, @keys will be an array of #GQuark of length @n_keys.  In the
   * event that the change event applies to the #GSettings object as a
   * whole (ie: potentially every key has been changed) then @keys will
   * be %NULL and @n_keys will be 0.
   *
   * The default handler for this signal invokes the "changed" signal
   * for each affected key.  If any other connected handler returns
Matthias Clasen's avatar
Matthias Clasen committed
648
   * %TRUE then this default functionality will be suppressed.
649 650 651
   *
   * Returns: %TRUE to stop other handlers from being invoked for the
   *          event. FALSE to propagate the event further.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
652 653 654 655 656 657
   */
  g_settings_signals[SIGNAL_CHANGE_EVENT] =
    g_signal_new ("change-event", G_TYPE_SETTINGS,
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GSettingsClass, change_event),
                  g_signal_accumulator_true_handled, NULL,
658
                  NULL,
Allison Karlitskaya's avatar
Allison Karlitskaya committed
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
                  G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_INT);

  /**
   * GSettings::writable-changed:
   * @settings: the object on which the signal was emitted
   * @key: the key
   *
   * The "writable-changed" signal is emitted when the writability of a
   * key has potentially changed.  You should call
   * g_settings_is_writable() in order to determine the new status.
   *
   * This signal supports detailed connections.  You can connect to the
   * detailed signal "writable-changed::x" in order to only receive
   * callbacks when the writability of "x" changes.
   */
  g_settings_signals[SIGNAL_WRITABLE_CHANGED] =
    g_signal_new ("writable-changed", G_TYPE_SETTINGS,
                  G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
677
                  G_STRUCT_OFFSET (GSettingsClass, writable_changed),
Allison Karlitskaya's avatar
Allison Karlitskaya committed
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
                  NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE,
                  1, G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE);

  /**
   * GSettings::writable-change-event:
   * @settings: the object on which the signal was emitted
   * @key: the quark of the key, or 0
   *
   * The "writable-change-event" signal is emitted once per writability
   * change event that affects this settings object.  You should connect
   * to this signal if you are interested in viewing groups of changes
   * before they are split out into multiple emissions of the
   * "writable-changed" signal.  For most use cases it is more
   * appropriate to use the "writable-changed" signal.
   *
   * In the event that the writability change applies only to a single
   * key, @key will be set to the #GQuark for that key.  In the event
   * that the writability change affects the entire settings object,
   * @key will be 0.
   *
   * The default handler for this signal invokes the "writable-changed"
   * and "changed" signals for each affected key.  This is done because
   * changes in writability might also imply changes in value (if for
   * example, a new mandatory setting is introduced).  If any other
   * connected handler returns %TRUE then this default functionality
Matthias Clasen's avatar
Matthias Clasen committed
703
   * will be suppressed.
704 705 706
   *
   * Returns: %TRUE to stop other handlers from being invoked for the
   *          event. FALSE to propagate the event further.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
707 708 709 710 711 712
   */
  g_settings_signals[SIGNAL_WRITABLE_CHANGE_EVENT] =
    g_signal_new ("writable-change-event", G_TYPE_SETTINGS,
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GSettingsClass, writable_change_event),
                  g_signal_accumulator_true_handled, NULL,
713
                  NULL, G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
714 715 716 717 718 719

  /**
   * GSettings:context:
   *
   * The name of the context that the settings are stored in.
   */
720 721 722 723 724
  g_object_class_install_property (object_class, PROP_BACKEND,
    g_param_spec_object ("backend",
                         P_("GSettingsBackend"),
                         P_("The GSettingsBackend for this settings object"),
                         G_TYPE_SETTINGS_BACKEND, G_PARAM_CONSTRUCT_ONLY |
Allison Karlitskaya's avatar
Allison Karlitskaya committed
725 726
                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
  /**
   * GSettings:settings-schema:
   *
   * The #GSettingsSchema describing the types of keys for this
   * #GSettings object.
   *
   * Ideally, this property would be called 'schema'.  #GSettingsSchema
   * has only existed since version 2.32, however, and before then the
   * 'schema' property was used to refer to the ID of the schema rather
   * than the schema itself.  Take care.
   */
  g_object_class_install_property (object_class, PROP_SCHEMA,
    g_param_spec_boxed ("settings-schema",
                        P_("schema"),
                        P_("The GSettingsSchema for this settings object"),
                        G_TYPE_SETTINGS_SCHEMA,
                        G_PARAM_CONSTRUCT_ONLY |
                        G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

Allison Karlitskaya's avatar
Allison Karlitskaya committed
746 747 748 749 750
  /**
   * GSettings:schema:
   *
   * The name of the schema that describes the types of keys
   * for this #GSettings object.
751
   *
752 753 754 755 756 757 758 759 760
   * The type of this property is *not* #GSettingsSchema.
   * #GSettingsSchema has only existed since version 2.32 and
   * unfortunately this name was used in previous versions to refer to
   * the schema ID rather than the schema itself.  Take care to use the
   * 'settings-schema' property if you wish to pass in a
   * #GSettingsSchema.
   *
   * Deprecated:2.32:Use the 'schema-id' property instead.  In a future
   * version, this property may instead refer to a #GSettingsSchema.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
761
   */
762
  g_object_class_install_property (object_class, PROP_SCHEMA_ID,
Allison Karlitskaya's avatar
Allison Karlitskaya committed
763
    g_param_spec_string ("schema",
764 765 766 767 768 769 770
                         P_("Schema name"),
                         P_("The name of the schema for this settings object"),
                         NULL,
                         G_PARAM_CONSTRUCT_ONLY |
                         G_PARAM_DEPRECATED | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  /**
771
   * GSettings:schema-id:
772 773 774 775
   *
   * The name of the schema that describes the types of keys
   * for this #GSettings object.
   */
776 777
  g_object_class_install_property (object_class, PROP_SCHEMA_ID,
    g_param_spec_string ("schema-id",
Allison Karlitskaya's avatar
Allison Karlitskaya committed
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
                         P_("Schema name"),
                         P_("The name of the schema for this settings object"),
                         NULL,
                         G_PARAM_CONSTRUCT_ONLY |
                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

   /**
    * GSettings:path:
    *
    * The path within the backend where the settings are stored.
    */
   g_object_class_install_property (object_class, PROP_PATH,
     g_param_spec_string ("path",
                          P_("Base path"),
                          P_("The path within the backend where the settings are"),
                          NULL,
                          G_PARAM_CONSTRUCT_ONLY |
                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

   /**
    * GSettings:has-unapplied:
    *
    * If this property is %TRUE, the #GSettings object has outstanding
    * changes that will be applied when g_settings_apply() is called.
    */
   g_object_class_install_property (object_class, PROP_HAS_UNAPPLIED,
     g_param_spec_boolean ("has-unapplied",
                           P_("Has unapplied changes"),
                           P_("TRUE if there are outstanding changes to apply()"),
                           FALSE,
                           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));

810 811 812 813 814 815 816 817 818 819 820 821 822 823
   /**
    * GSettings:delay-apply:
    *
    * Whether the #GSettings object is in 'delay-apply' mode. See
    * g_settings_delay() for details.
    *
    * Since: 2.28
    */
   g_object_class_install_property (object_class, PROP_DELAY_APPLY,
     g_param_spec_boolean ("delay-apply",
                           P_("Delay-apply mode"),
                           P_("Whether this settings object is in 'delay-apply' mode"),
                           FALSE,
                           G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
Allison Karlitskaya's avatar
Allison Karlitskaya committed
824 825
}

826
/* Construction (new, new_with_path, etc.) {{{1 */
Allison Karlitskaya's avatar
Allison Karlitskaya committed
827
/**
828
 * g_settings_new:
829
 * @schema_id: the id of the schema
Allison Karlitskaya's avatar
Allison Karlitskaya committed
830
 *
831 832
 * Creates a new #GSettings object with the schema specified by
 * @schema_id.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
833
 *
834 835 836 837
 * Signals on the newly created #GSettings object will be dispatched
 * via the thread-default #GMainContext in effect at the time of the
 * call to g_settings_new().  The new #GSettings will hold a reference
 * on the context.  See g_main_context_push_thread_default().
Allison Karlitskaya's avatar
Allison Karlitskaya committed
838
 *
839 840
 * Returns: a new #GSettings object
 *
Allison Karlitskaya's avatar
Allison Karlitskaya committed
841 842
 * Since: 2.26
 */
843
GSettings *
844
g_settings_new (const gchar *schema_id)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
845
{
846
  g_return_val_if_fail (schema_id != NULL, NULL);
847

848
  return g_object_new (G_TYPE_SETTINGS,
849
                       "schema-id", schema_id,
850 851
                       NULL);
}
Allison Karlitskaya's avatar
Allison Karlitskaya committed
852

853 854
/**
 * g_settings_new_with_path:
855
 * @schema_id: the id of the schema
856 857
 * @path: the path to use
 *
858 859
 * Creates a new #GSettings object with the relocatable schema specified
 * by @schema_id and a given path.
860 861 862 863 864 865 866 867
 *
 * You only need to do this if you want to directly create a settings
 * object with a schema that doesn't have a specified path of its own.
 * That's quite rare.
 *
 * It is a programmer error to call this function for a schema that
 * has an explicitly specified path.
 *
868 869
 * Returns: a new #GSettings object
 *
870 871 872
 * Since: 2.26
 */
GSettings *
873
g_settings_new_with_path (const gchar *schema_id,
874 875
                          const gchar *path)
{
876
  g_return_val_if_fail (schema_id != NULL, NULL);
877
  g_return_val_if_fail (path != NULL, NULL);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
878

879
  return g_object_new (G_TYPE_SETTINGS,
880
                       "schema-id", schema_id,
881 882 883
                       "path", path,
                       NULL);
}
Allison Karlitskaya's avatar
Allison Karlitskaya committed
884

885
/**
886
 * g_settings_new_with_backend:
887
 * @schema_id: the id of the schema
888
 * @backend: the #GSettingsBackend to use
889
 *
890 891
 * Creates a new #GSettings object with the schema specified by
 * @schema_id and a given #GSettingsBackend.
892
 *
Matthias Clasen's avatar
Matthias Clasen committed
893 894
 * Creating a #GSettings object with a different backend allows accessing
 * settings from a database other than the usual one. For example, it may make
Matthias Clasen's avatar
Matthias Clasen committed
895
 * sense to pass a backend corresponding to the "defaults" settings database on
896 897
 * the system to get a settings object that modifies the system default
 * settings instead of the settings for this user.
898
 *
899 900
 * Returns: a new #GSettings object
 *
901 902 903
 * Since: 2.26
 */
GSettings *
904
g_settings_new_with_backend (const gchar      *schema_id,
905
                             GSettingsBackend *backend)
906
{
907
  g_return_val_if_fail (schema_id != NULL, NULL);
908
  g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
909 910

  return g_object_new (G_TYPE_SETTINGS,
911
                       "schema-id", schema_id,
912
                       "backend", backend,
913 914 915 916
                       NULL);
}

/**
917
 * g_settings_new_with_backend_and_path:
918
 * @schema_id: the id of the schema
919
 * @backend: the #GSettingsBackend to use
920 921
 * @path: the path to use
 *
922 923
 * Creates a new #GSettings object with the schema specified by
 * @schema_id and a given #GSettingsBackend and path.
924
 *
925
 * This is a mix of g_settings_new_with_backend() and
926 927
 * g_settings_new_with_path().
 *
928 929
 * Returns: a new #GSettings object
 *
930 931 932
 * Since: 2.26
 */
GSettings *
933
g_settings_new_with_backend_and_path (const gchar      *schema_id,
934 935
                                      GSettingsBackend *backend,
                                      const gchar      *path)
936
{
937
  g_return_val_if_fail (schema_id != NULL, NULL);
938
  g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
939 940 941
  g_return_val_if_fail (path != NULL, NULL);

  return g_object_new (G_TYPE_SETTINGS,
942
                       "schema-id", schema_id,
943 944 945
                       "backend", backend,
                       "path", path,
                       NULL);
946 947 948 949 950
}

/**
 * g_settings_new_full:
 * @schema: a #GSettingsSchema
951 952
 * @backend: (allow-none): a #GSettingsBackend
 * @path: (allow-none): the path to use
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
 *
 * Creates a new #GSettings object with a given schema, backend and
 * path.
 *
 * It should be extremely rare that you ever want to use this function.
 * It is made available for advanced use-cases (such as plugin systems
 * that want to provide access to schemas loaded from custom locations,
 * etc).
 *
 * At the most basic level, a #GSettings object is a pure composition of
 * 4 things: a #GSettingsSchema, a #GSettingsBackend, a path within that
 * backend, and a #GMainContext to which signals are dispatched.
 *
 * This constructor therefore gives you full control over constructing
 * #GSettings instances.  The first 4 parameters are given directly as
 * @schema, @backend and @path, and the main context is taken from the
 * thread-default (as per g_settings_new()).
 *
971 972 973 974 975 976 977
 * If @backend is %NULL then the default backend is used.
 *
 * If @path is %NULL then the path from the schema is used.  It is an
 * error f @path is %NULL and the schema has no path of its own or if
 * @path is non-%NULL and not equal to the path that the schema does
 * have.
 *
978 979
 * Returns: a new #GSettings object
 *
980 981 982 983 984 985 986 987 988 989
 * Since: 2.32
 */
GSettings *
g_settings_new_full (GSettingsSchema  *schema,
                     GSettingsBackend *backend,
                     const gchar      *path)
{
  return g_object_new (G_TYPE_SETTINGS,
                       "settings-schema", schema,
                       "backend", backend,
990 991
                       "path", path,
                       NULL);
992 993
}

994
/* Internal read/write utilities {{{1 */
995
static gboolean
996 997 998
g_settings_write_to_backend (GSettings          *settings,
                             GSettingsSchemaKey *key,
                             GVariant           *value)
999 1000 1001 1002
{
  gboolean success;
  gchar *path;

1003
  path = g_strconcat (settings->priv->path, key->name, NULL);
1004
  success = g_settings_backend_write (settings->priv->backend, path, value, NULL);
1005 1006 1007 1008 1009 1010
  g_free (path);

  return success;
}

static GVariant *
1011 1012
g_settings_read_from_backend (GSettings          *settings,
                              GSettingsSchemaKey *key)
1013 1014 1015 1016 1017
{
  GVariant *value;
  GVariant *fixup;
  gchar *path;

1018 1019
  path = g_strconcat (settings->priv->path, key->name, NULL);
  value = g_settings_backend_read (settings->priv->backend, path, key->type, FALSE);
1020 1021 1022 1023
  g_free (path);

  if (value != NULL)
    {
1024
      fixup = g_settings_schema_key_range_fixup (key, value);
1025 1026 1027 1028 1029 1030 1031 1032
      g_variant_unref (value);
    }
  else
    fixup = NULL;

  return fixup;
}

1033
/* Public Get/Set API {{{1 (get, get_value, set, set_value, get_mapped) */
1034 1035 1036 1037 1038 1039 1040
/**
 * g_settings_get_value:
 * @settings: a #GSettings object
 * @key: the key to get the value for
 *
 * Gets the value that is stored in @settings for @key.
 *
1041 1042
 * It is a programmer error to give a @key that isn't contained in the
 * schema for @settings.
1043
 *
1044 1045
 * Returns: a new #GVariant
 *
1046 1047 1048 1049 1050 1051
 * Since: 2.26
 */
GVariant *
g_settings_get_value (GSettings   *settings,
                      const gchar *key)
{
1052
  GSettingsSchemaKey skey;
1053 1054 1055 1056 1057
  GVariant *value;

  g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
  g_return_val_if_fail (key != NULL, NULL);

1058
  g_settings_schema_key_init (&skey, settings->priv->schema, key);
1059
  value = g_settings_read_from_backend (settings, &skey);
1060 1061

  if (value == NULL)
1062
    value = g_settings_schema_key_get_translated_default (&skey);
1063

Allison Karlitskaya's avatar
Allison Karlitskaya committed
1064
  if (value == NULL)
1065
    value = g_variant_ref (skey.default_value);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1066

1067
  g_settings_schema_key_clear (&skey);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1068 1069 1070 1071

  return value;
}

1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
/**
 * g_settings_get_enum:
 * @settings: a #GSettings object
 * @key: the key to get the value for
 *
 * Gets the value that is stored in @settings for @key and converts it
 * to the enum value that it represents.
 *
 * In order to use this function the type of the value must be a string
 * and it must be marked in the schema file as an enumerated type.
 *
1083 1084
 * It is a programmer error to give a @key that isn't contained in the
 * schema for @settings or is not marked as an enumerated type.
1085 1086 1087 1088 1089
 *
 * If the value stored in the configuration database is not a valid
 * value for the enumerated type then this function will return the
 * default value.
 *
1090 1091
 * Returns: the enum value
 *
1092 1093 1094 1095 1096 1097
 * Since: 2.26
 **/
gint
g_settings_get_enum (GSettings   *settings,
                     const gchar *key)
{
1098
  GSettingsSchemaKey skey;
1099 1100 1101 1102 1103 1104
  GVariant *value;
  gint result;

  g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
  g_return_val_if_fail (key != NULL, -1);

1105
  g_settings_schema_key_init (&skey, settings->priv->schema, key);
1106

1107
  if (!skey.is_enum)
1108 1109
    {
      g_critical ("g_settings_get_enum() called on key `%s' which is not "
1110 1111
                  "associated with an enumerated type", skey.name);
      g_settings_schema_key_clear (&skey);
1112 1113 1114
      return -1;
    }

1115
  value = g_settings_read_from_backend (settings, &skey);
1116 1117

  if (value == NULL)
1118
    value = g_settings_schema_key_get_translated_default (&skey);
1119 1120

  if (value == NULL)
1121
    value = g_variant_ref (skey.default_value);
1122

1123 1124
  result = g_settings_schema_key_to_enum (&skey, value);
  g_settings_schema_key_clear (&skey);
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
  g_variant_unref (value);

  return result;
}

/**
 * g_settings_set_enum:
 * @settings: a #GSettings object
 * @key: a key, within @settings
 * @value: an enumerated value
 *
 * Looks up the enumerated type nick for @value and writes it to @key,
 * within @settings.
 *
1139 1140 1141
 * It is a programmer error to give a @key that isn't contained in the
 * schema for @settings or is not marked as an enumerated type, or for
 * @value not to be a valid value for the named type.
1142
 *
1143
 * After performing the write, accessing @key directly with
1144 1145
 * g_settings_get_string() will return the 'nick' associated with
 * @value.
1146 1147
 *
 * Returns: %TRUE, if the set succeeds
1148 1149 1150 1151 1152 1153
 **/
gboolean
g_settings_set_enum (GSettings   *settings,
                     const gchar *key,
                     gint         value)
{
1154
  GSettingsSchemaKey skey;
1155 1156 1157 1158 1159 1160
  GVariant *variant;
  gboolean success;

  g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
  g_return_val_if_fail (key != NULL, FALSE);

1161
  g_settings_schema_key_init (&skey, settings->priv->schema, key);
1162

1163
  if (!skey.is_enum)
1164 1165
    {
      g_critical ("g_settings_set_enum() called on key `%s' which is not "
1166
                  "associated with an enumerated type", skey.name);
1167 1168 1169
      return FALSE;
    }

1170
  if (!(variant = g_settings_schema_key_from_enum (&skey, value)))
1171 1172
    {
      g_critical ("g_settings_set_enum(): invalid enum value %d for key `%s' "
1173
                  "in schema `%s'.  Doing nothing.", value, skey.name,
1174
                  g_settings_schema_get_id (skey.schema));
1175
      g_settings_schema_key_clear (&skey);
1176 1177 1178
      return FALSE;
    }

1179 1180
  success = g_settings_write_to_backend (settings, &skey, variant);
  g_settings_schema_key_clear (&skey);
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202

  return success;
}

/**
 * g_settings_get_flags:
 * @settings: a #GSettings object
 * @key: the key to get the value for
 *
 * Gets the value that is stored in @settings for @key and converts it
 * to the flags value that it represents.
 *
 * In order to use this function the type of the value must be an array
 * of strings and it must be marked in the schema file as an flags type.
 *
 * It is a programmer error to give a @key that isn't contained in the
 * schema for @settings or is not marked as a flags type.
 *
 * If the value stored in the configuration database is not a valid
 * value for the flags type then this function will return the default
 * value.
 *
1203 1204
 * Returns: the flags value
 *
1205 1206 1207 1208 1209 1210
 * Since: 2.26
 **/
guint
g_settings_get_flags (GSettings   *settings,
                      const gchar *key)
{
1211
  GSettingsSchemaKey skey;
1212 1213 1214 1215 1216 1217
  GVariant *value;
  guint result;

  g_return_val_if_fail (G_IS_SETTINGS (settings), -1);
  g_return_val_if_fail (key != NULL, -1);

1218
  g_settings_schema_key_init (&skey, settings->priv->schema, key);
1219

1220
  if (!skey.is_flags)
1221 1222
    {
      g_critical ("g_settings_get_flags() called on key `%s' which is not "
1223 1224
                  "associated with a flags type", skey.name);
      g_settings_schema_key_clear (&skey);
1225 1226 1227
      return -1;
    }

1228
  value = g_settings_read_from_backend (settings, &skey);
1229 1230

  if (value == NULL)
1231
    value = g_settings_schema_key_get_translated_default (&skey);
1232 1233

  if (value == NULL)
1234
    value = g_variant_ref (skey.default_value);
1235

1236 1237
  result = g_settings_schema_key_to_flags (&skey, value);
  g_settings_schema_key_clear (&skey);
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
  g_variant_unref (value);

  return result;
}

/**
 * g_settings_set_flags:
 * @settings: a #GSettings object
 * @key: a key, within @settings
 * @value: a flags value
 *
 * Looks up the flags type nicks for the bits specified by @value, puts
Matthias Clasen's avatar
Matthias Clasen committed
1250
 * them in an array of strings and writes the array to @key, within
1251 1252 1253 1254 1255 1256 1257 1258 1259
 * @settings.
 *
 * It is a programmer error to give a @key that isn't contained in the
 * schema for @settings or is not marked as a flags type, or for @value
 * to contain any bits that are not value for the named type.
 *
 * After performing the write, accessing @key directly with
 * g_settings_get_strv() will return an array of 'nicks'; one for each
 * bit in @value.
1260 1261
 *
 * Returns: %TRUE, if the set succeeds
1262 1263 1264 1265 1266 1267
 **/
gboolean
g_settings_set_flags (GSettings   *settings,
                      const gchar *key,
                      guint        value)
{
1268
  GSettingsSchemaKey skey;
1269 1270 1271 1272 1273 1274
  GVariant *variant;
  gboolean success;

  g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
  g_return_val_if_fail (key != NULL, FALSE);

1275
  g_settings_schema_key_init (&skey, settings->priv->schema, key);
1276

1277
  if (!skey.is_flags)
1278 1279
    {
      g_critical ("g_settings_set_flags() called on key `%s' which is not "
1280
                  "associated with a flags type", skey.name);
1281 1282 1283
      return FALSE;
    }

1284
  if (!(variant = g_settings_schema_key_from_flags (&skey, value)))
1285 1286 1287
    {
      g_critical ("g_settings_set_flags(): invalid flags value 0x%08x "
                  "for key `%s' in schema `%s'.  Doing nothing.",
1288
                  value, skey.name, g_settings_schema_get_id (skey.schema));
1289
      g_settings_schema_key_clear (&skey);
1290 1291 1292
      return FALSE;
    }

1293 1294
  success = g_settings_write_to_backend (settings, &skey, variant);
  g_settings_schema_key_clear (&skey);
1295 1296 1297 1298

  return success;
}

Allison Karlitskaya's avatar
Allison Karlitskaya committed
1299 1300 1301 1302 1303 1304 1305 1306
/**
 * g_settings_set_value:
 * @settings: a #GSettings object
 * @key: the name of the key to set
 * @value: a #GVariant of the correct type
 *
 * Sets @key in @settings to @value.
 *
1307 1308 1309
 * It is a programmer error to give a @key that isn't contained in the
 * schema for @settings or for @value to have the incorrect type, per
 * the schema.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1310 1311 1312
 *
 * If @value is floating then this function consumes the reference.
 *
1313 1314 1315
 * Returns: %TRUE if setting the key succeeded,
 *     %FALSE if the key was not writable
 *
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1316 1317 1318 1319 1320 1321 1322
 * Since: 2.26
 **/
gboolean
g_settings_set_value (GSettings   *settings,
                      const gchar *key,
                      GVariant    *value)
{
1323
  GSettingsSchemaKey skey;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1324

1325
  g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
1326
  g_return_val_if_fail (key != NULL, FALSE);
1327

1328
  g_settings_schema_key_init (&skey, settings->priv->schema, key);
1329

1330
  if (!g_settings_schema_key_type_check (&skey, value))
1331 1332 1333
    {
      g_critical ("g_settings_set_value: key '%s' in '%s' expects type '%s', but a GVariant of type '%s' was given",
                  key,
1334
                  g_settings_schema_get_id (settings->priv->schema),
1335
                  g_variant_type_peek_string (skey.type),
1336 1337 1338 1339 1340
                  g_variant_get_type_string (value));

        return FALSE;
      }

1341
  if (!g_settings_schema_key_range_check (&skey, value))
1342
    {
Dan Winship's avatar
Dan Winship committed
1343
      g_warning ("g_settings_set_value: value for key '%s' in schema '%s' "
1344 1345
                 "is outside of valid range",
                 key,
1346
                 g_settings_schema_get_id (settings->priv->schema));
1347 1348 1349 1350

        return FALSE;
    }

1351
  g_settings_schema_key_clear (&skey);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1352

1353
  return g_settings_write_to_backend (settings, &skey, value);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
}

/**
 * g_settings_get:
 * @settings: a #GSettings object
 * @key: the key to get the value for
 * @format: a #GVariant format string
 * @...: arguments as per @format
 *
 * Gets the value that is stored at @key in @settings.
 *
 * A convenience function that combines g_settings_get_value() with
 * g_variant_get().
 *
1368 1369 1370
 * It is a programmer error to give a @key that isn't contained in the
 * schema for @settings or for the #GVariantType of @format to mismatch
 * the type given in the schema.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
 *
 * Since: 2.26
 */
void
g_settings_get (GSettings   *settings,
                const gchar *key,
                const gchar *format,
                ...)
{
  GVariant *value;
  va_list ap;

  value = g_settings_get_value (settings, key);

  va_start (ap, format);
  g_variant_get_va (value, format, NULL, &ap);
  va_end (ap);

  g_variant_unref (value);
}

/**
 * g_settings_set:
 * @settings: a #GSettings object
 * @key: the name of the key to set
 * @format: a #GVariant format string
 * @...: arguments as per @format
 *
 * Sets @key in @settings to @value.
 *
 * A convenience function that combines g_settings_set_value() with
 * g_variant_new().
 *
1404 1405 1406
 * It is a programmer error to give a @key that isn't contained in the
 * schema for @settings or for the #GVariantType of @format to mismatch
 * the type given in the schema.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1407
 *
1408 1409 1410
 * Returns: %TRUE if setting the key succeeded,
 *     %FALSE if the key was not writable
 *
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
 * Since: 2.26
 */
gboolean
g_settings_set (GSettings   *settings,
                const gchar *key,
                const gchar *format,
                ...)
{
  GVariant *value;
  va_list ap;

  va_start (ap, format);
  value = g_variant_new_va (format, NULL, &ap);
  va_end (ap);

  return g_settings_set_value (settings, key, value);
}

1429 1430 1431 1432
/**
 * g_settings_get_mapped:
 * @settings: a #GSettings object
 * @key: the key to get the value for
1433 1434
 * @mapping: (scope call): the function to map the value in the
 *           settings database to the value used by the application
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
 * @user_data: user data for @mapping
 *
 * Gets the value that is stored at @key in @settings, subject to
 * application-level validation/mapping.
 *
 * You should use this function when the application needs to perform
 * some processing on the value of the key (for example, parsing).  The
 * @mapping function performs that processing.  If the function
 * indicates that the processing was unsuccessful (due to a parse error,
 * for example) then the mapping is tried again with another value.
1445
 *
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
 * This allows a robust 'fall back to defaults' behaviour to be
 * implemented somewhat automatically.
 *
 * The first value that is tried is the user's setting for the key.  If
 * the mapping function fails to map this value, other values may be
 * tried in an unspecified order (system or site defaults, translated
 * schema default values, untranslated schema default values, etc).
 *
 * If the mapping function fails for all possible values, one additional
 * attempt is made: the mapping function is called with a %NULL value.
 * If the mapping function still indicates failure at this point then
 * the application will be aborted.
 *
 * The result parameter for the @mapping function is pointed to a
 * #gpointer which is initially set to %NULL.  The same pointer is given
 * to each invocation of @mapping.  The final value of that #gpointer is
 * what is returned by this function.  %NULL is valid; it is returned
 * just as any other value would be.
1464 1465
 *
 * Returns: (transfer full): the result, which may be %NULL
1466 1467 1468 1469 1470 1471 1472 1473
 **/
gpointer
g_settings_get_mapped (GSettings           *settings,
                       const gchar         *key,
                       GSettingsGetMapping  mapping,
                       gpointer             user_data)
{
  gpointer result = NULL;
1474
  GSettingsSchemaKey skey;
1475 1476 1477 1478 1479 1480 1481
  GVariant *value;
  gboolean okay;

  g_return_val_if_fail (G_IS_SETTINGS (settings), NULL);
  g_return_val_if_fail (key != NULL, NULL);
  g_return_val_if_fail (mapping != NULL, NULL);

1482
  g_settings_schema_key_init (&skey, settings->priv->schema, key);
1483

1484
  if ((value = g_settings_read_from_backend (settings, &skey)))
1485 1486 1487 1488 1489 1490
    {
      okay = mapping (value, &result, user_data);
      g_variant_unref (value);
      if (okay) goto okay;
    }

1491
  if ((value = g_settings_schema_key_get_translated_default (&skey)))
1492 1493 1494 1495 1496 1497
    {
      okay = mapping (value, &result, user_data);
      g_variant_unref (value);
      if (okay) goto okay;
    }

1498
  if (mapping (skey.default_value, &result, user_data))
1499 1500 1501 1502 1503
    goto okay;

  if (!mapping (NULL, &result, user_data))
    g_error ("The mapping function given to g_settings_get_mapped() for key "
             "`%s' in schema `%s' returned FALSE when given a NULL value.",
1504
             key, g_settings_schema_get_id (settings->priv->schema));
1505 1506

 okay:
1507
  g_settings_schema_key_clear (&skey);
1508 1509 1510 1511

  return result;
}

1512
/* Convenience API (get, set_string, int, double, boolean, strv) {{{1 */
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1513
/**
1514
 * g_settings_get_string:
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1515
 * @settings: a #GSettings object
1516
 * @key: the key to get the value for
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1517
 *
1518 1519 1520 1521
 * Gets the value that is stored at @key in @settings.
 *
 * A convenience variant of g_settings_get() for strings.
 *
1522 1523
 * It is a programmer error to give a @key that isn't specified as
 * having a string type in the schema for @settings.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1524
 *
1525 1526
 * Returns: a newly-allocated string
 *
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1527 1528
 * Since: 2.26
 */
1529 1530 1531
gchar *
g_settings_get_string (GSettings   *settings,
                       const gchar *key)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1532
{
1533 1534
  GVariant *value;
  gchar *result;
1535

1536 1537 1538
  value = g_settings_get_value (settings, key);
  result = g_variant_dup_string (value, NULL);
  g_variant_unref (value);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1539

1540
  return result;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1541 1542 1543
}

/**
1544
 * g_settings_set_string:
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1545
 * @settings: a #GSettings object
1546 1547
 * @key: the name of the key to set
 * @value: the value to set it to
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1548
 *
1549
 * Sets @key in @settings to @value.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1550
 *
1551 1552
 * A convenience variant of g_settings_set() for strings.
 *
1553 1554
 * It is a programmer error to give a @key that isn't specified as
 * having a string type in the schema for @settings.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1555
 *
1556 1557 1558
 * Returns: %TRUE if setting the key succeeded,
 *     %FALSE if the key was not writable
 *
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1559 1560
 * Since: 2.26
 */
1561 1562 1563 1564
gboolean
g_settings_set_string (GSettings   *settings,
                       const gchar *key,
                       const gchar *value)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1565
{
1566
  return g_settings_set_value (settings, key, g_variant_new_string (value));
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1567 1568 1569
}

/**
1570 1571 1572
 * g_settings_get_int:
 * @settings: a #GSettings object
 * @key: the key to get the value for
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1573
 *
1574
 * Gets the value that is stored at @key in @settings.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1575
 *
1576 1577
 * A convenience variant of g_settings_get() for 32-bit integers.
 *
1578 1579
 * It is a programmer error to give a @key that isn't specified as
 * having a int32 type in the schema for @settings.
1580
 *
1581 1582
 * Returns: an integer
 *
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1583 1584
 * Since: 2.26
 */
1585 1586 1587
gint
g_settings_get_int (GSettings   *settings,
                    const gchar *key)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1588
{
1589 1590 1591 1592 1593 1594 1595 1596
  GVariant *value;
  gint result;

  value = g_settings_get_value (settings, key);
  result = g_variant_get_int32 (value);
  g_variant_unref (value);

  return result;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1597 1598 1599
}

/**
1600 1601 1602 1603
 * g_settings_set_int:
 * @settings: a #GSettings object
 * @key: the name of the key to set
 * @value: the value to set it to
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1604
 *
1605
 * Sets @key in @settings to @value.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1606
 *
1607
 * A convenience variant of g_settings_set() for 32-bit integers.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
1608
 *