gsettingsbackend.c 32.6 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 22 23 24 25 26
/*
 * Copyright © 2009, 2010 Codethink Limited
 * Copyright © 2010 Red Hat, Inc.
 *
 * 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.
 *
 * Authors: Ryan Lortie <desrt@desrt.ca>
 *          Matthias Clasen <mclasen@redhat.com>
 */

#include "config.h"

#include "gsettingsbackendinternal.h"
27
#include "gnullsettingsbackend.h"
28
#include "gsimplepermission.h"
Allison Karlitskaya's avatar
Allison Karlitskaya committed
29 30 31 32 33 34 35 36 37 38 39
#include "giomodule-priv.h"
#include "gio-marshal.h"

#include <string.h>
#include <stdlib.h>
#include <glib.h>
#include <glibintl.h>


G_DEFINE_ABSTRACT_TYPE (GSettingsBackend, g_settings_backend, G_TYPE_OBJECT)

40 41
typedef struct _GSettingsBackendClosure GSettingsBackendClosure;
typedef struct _GSettingsBackendWatch   GSettingsBackendWatch;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
42 43 44 45

struct _GSettingsBackendPrivate
{
  GSettingsBackendWatch *watches;
46
  GStaticMutex lock;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
47 48 49 50 51
};

/**
 * SECTION:gsettingsbackend
 * @title: GSettingsBackend
Matthias Clasen's avatar
Matthias Clasen committed
52
 * @short_description: Interface for settings backend implementations
Allison Karlitskaya's avatar
Allison Karlitskaya committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
 * @include: gio/gsettingsbackend.h
 * @see_also: #GSettings, #GIOExtensionPoint
 *
 * The #GSettingsBackend interface defines a generic interface for
 * non-strictly-typed data that is stored in a hierarchy. To implement
 * an alternative storage backend for #GSettings, you need to implement
 * the #GSettingsBackend interface and then make it implement the
 * extension point #G_SETTINGS_BACKEND_EXTENSION_POINT_NAME.
 *
 * The interface defines methods for reading and writing values, a
 * method for determining if writing of certain values will fail
 * (lockdown) and a change notification mechanism.
 *
 * The semantics of the interface are very precisely defined and
 * implementations must carefully adhere to the expectations of
 * callers that are documented on each of the interface methods.
 *
 * Some of the GSettingsBackend functions accept or return a #GTree.
 * These trees always have strings as keys and #GVariant as values.
 * g_settings_backend_create_tree() is a convenience function to create
 * suitable trees.
 *
 * <note><para>
 * The #GSettingsBackend API is exported to allow third-party
 * implementations, but does not carry the same stability guarantees
 * as the public GIO API. For this reason, you have to define the
 * C preprocessor symbol #G_SETTINGS_ENABLE_BACKEND before including
 * <filename>gio/gsettingsbackend.h</filename>
 * </para></note>
 **/

static gboolean
is_key (const gchar *key)
{
  gint length;
  gint i;

  g_return_val_if_fail (key != NULL, FALSE);
  g_return_val_if_fail (key[0] == '/', FALSE);

  for (i = 1; key[i]; i++)
    g_return_val_if_fail (key[i] != '/' || key[i + 1] != '/', FALSE);

  length = i;

  g_return_val_if_fail (key[length - 1] != '/', FALSE);

  return TRUE;
}

static gboolean
is_path (const gchar *path)
{
  gint length;
  gint i;

  g_return_val_if_fail (path != NULL, FALSE);
  g_return_val_if_fail (path[0] == '/', FALSE);

  for (i = 1; path[i]; i++)
    g_return_val_if_fail (path[i] != '/' || path[i + 1] != '/', FALSE);

  length = i;

  g_return_val_if_fail (path[length - 1] == '/', FALSE);

  return TRUE;
}

122 123
struct _GSettingsBackendWatch
{
124 125 126 127
  GObject                       *target;
  const GSettingsListenerVTable *vtable;
  GMainContext                  *context;
  GSettingsBackendWatch         *next;
128 129 130
};

struct _GSettingsBackendClosure
131
{
132 133
  void (*function) (GObject          *target,
                    GSettingsBackend *backend,
134 135
                    const gchar      *name,
                    gpointer          data1,
136 137
                    gpointer          data2);

138
  GSettingsBackend *backend;
139 140 141 142 143 144
  GObject          *target;
  gchar            *name;
  gpointer          data1;
  GBoxedFreeFunc    data1_free;
  gpointer          data2;
};
145

146 147
static void
g_settings_backend_watch_weak_notify (gpointer  data,
148
                                      GObject  *where_the_object_was)
149 150 151
{
  GSettingsBackend *backend = data;
  GSettingsBackendWatch **ptr;
152

153 154 155
  /* search and remove */
  g_static_mutex_lock (&backend->priv->lock);
  for (ptr = &backend->priv->watches; *ptr; ptr = &(*ptr)->next)
156
    if ((*ptr)->target == where_the_object_was)
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
      {
        GSettingsBackendWatch *tmp = *ptr;

        *ptr = tmp->next;
        g_slice_free (GSettingsBackendWatch, tmp);

        g_static_mutex_unlock (&backend->priv->lock);
        return;
      }

  /* we didn't find it.  that shouldn't happen. */
  g_assert_not_reached ();
}

/*< private >
 * g_settings_backend_watch:
 * @backend: a #GSettingsBackend
 * @target: the GObject (typically GSettings instance) to call back to
 * @context: a #GMainContext, or %NULL
 * ...: callbacks...
 *
 * Registers a new watch on a #GSettingsBackend.
 *
 * note: %NULL @context does not mean "default main context" but rather,
 * "it is okay to dispatch in any context".  If the default main context
 * is specifically desired then it must be given.
 *
 * note also: if you want to get meaningful values for the @origin_tag
 * that appears as an argument to some of the callbacks, you *must* have
 * @context as %NULL.  Otherwise, you are subject to cross-thread
 * dispatching and whatever owned @origin_tag at the time that the event
 * occured may no longer own it.  This is a problem if you consider that
 * you may now be the new owner of that address and mistakenly think
 * that the event in question originated from yourself.
 *
 * tl;dr: If you give a non-%NULL @context then you must ignore the
 * value of @origin_tag given to any callbacks.
 **/
void
196 197 198 199
g_settings_backend_watch (GSettingsBackend              *backend,
                          const GSettingsListenerVTable *vtable,
                          GObject                       *target,
                          GMainContext                  *context)
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
{
  GSettingsBackendWatch *watch;

  /* For purposes of discussion, we assume that our target is a
   * GSettings instance.
   *
   * Our strategy to defend against the final reference dropping on the
   * GSettings object in a thread other than the one that is doing the
   * dispatching is as follows:
   *
   *  1) hold a GObject reference on the GSettings during an outstanding
   *     dispatch.  This ensures that the delivery is always possible.
   *
   *  2) hold a weak reference on the GSettings at other times.  This
   *     allows us to receive early notification of pending destruction
   *     of the object.  At this point, it is still safe to obtain a
   *     reference on the GObject to keep it alive, so #1 will work up
   *     to that point.  After that point, we'll have been able to drop
   *     the watch from the list.
   *
   * Note, in particular, that it's not possible to simply have an
   * "unwatch" function that gets called from the finalize function of
   * the GSettings instance because, by that point it is no longer
   * possible to keep the object alive using g_object_ref() and we would
   * have no way of knowing this.
   *
   * Note also that we do not need to hold a reference on the main
   * context here since the GSettings instance does that for us and we
   * will receive the weak notify long before it is dropped.  We don't
   * even need to hold it during dispatches because our reference on the
   * GSettings will prevent the finalize from running and dropping the
   * ref on the context.
   *
   * All access to the list holds a mutex.  We have some strategies to
   * avoid some of the pain that would be associated with that.
   */
236

237 238
  watch = g_slice_new (GSettingsBackendWatch);
  watch->context = context;
239
  watch->vtable = vtable;
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
  watch->target = target;
  g_object_weak_ref (target, g_settings_backend_watch_weak_notify, backend);

  /* linked list prepend */
  g_static_mutex_lock (&backend->priv->lock);
  watch->next = backend->priv->watches;
  backend->priv->watches = watch;
  g_static_mutex_unlock (&backend->priv->lock);
}

void
g_settings_backend_unwatch (GSettingsBackend *backend,
                            GObject          *target)
{
  /* Our caller surely owns a reference on 'target', so the order of
   * these two calls is unimportant.
   */
  g_object_weak_unref (target, g_settings_backend_watch_weak_notify, backend);
  g_settings_backend_watch_weak_notify (backend, target);
}
260 261 262 263 264 265

static gboolean
g_settings_backend_invoke_closure (gpointer user_data)
{
  GSettingsBackendClosure *closure = user_data;

266
  closure->function (closure->target, closure->backend, closure->name,
267
                     closure->data1, closure->data2);
268

269
  closure->data1_free (closure->data1);
270
  g_object_unref (closure->backend);
271
  g_object_unref (closure->target);
272 273 274 275 276 277 278
  g_free (closure->name);

  g_slice_free (GSettingsBackendClosure, closure);

  return FALSE;
}

279 280 281 282 283 284 285 286 287 288 289
static gpointer
pointer_id (gpointer a)
{
  return a;
}

static void
pointer_ignore (gpointer a)
{
}

290 291
static void
g_settings_backend_dispatch_signal (GSettingsBackend *backend,
292
                                    gsize             function_offset,
293 294
                                    const gchar      *name,
                                    gpointer          data1,
295 296 297
                                    GBoxedCopyFunc    data1_copy,
                                    GBoxedFreeFunc    data1_free,
                                    gpointer          data2)
298
{
299
  GSettingsBackendWatch *suffix, *watch, *next;
300

301 302
  if (data1_copy == NULL)
    data1_copy = pointer_id;
303

304 305
  if (data1_free == NULL)
    data1_free = pointer_ignore;
306

307 308 309 310 311 312 313 314 315 316 317 318 319 320
  /* We're in a little bit of a tricky situation here.  We need to hold
   * a lock while traversing the list, but we don't want to hold the
   * lock while calling back into user code.
   *
   * Since we're not holding the lock while we call user code, we can't
   * render the list immutable.  We can, however, store a pointer to a
   * given suffix of the list and render that suffix immutable.
   *
   * Adds will never modify the suffix since adds always come in the
   * form of prepends.  We can also prevent removes from modifying the
   * suffix since removes only happen in response to the last reference
   * count dropping -- so just add a reference to everything in the
   * suffix.
   */
321
  g_static_mutex_lock (&backend->priv->lock);
322 323 324 325 326 327 328
  suffix = backend->priv->watches;
  for (watch = suffix; watch; watch = watch->next)
    g_object_ref (watch->target);
  g_static_mutex_unlock (&backend->priv->lock);

  /* The suffix is now immutable, so this is safe. */
  for (watch = suffix; watch; watch = next)
329 330 331 332 333
    {
      GSettingsBackendClosure *closure;

      closure = g_slice_new (GSettingsBackendClosure);
      closure->backend = g_object_ref (backend);
334
      closure->target = watch->target; /* we took our ref above */
335 336
      closure->function = G_STRUCT_MEMBER (void *, watch->vtable,
                                           function_offset);
337 338 339 340 341
      closure->name = g_strdup (name);
      closure->data1 = data1_copy (data1);
      closure->data1_free = data1_free;
      closure->data2 = data2;

342 343 344 345
      /* we do this here because 'watch' may not live to the end of this
       * iteration of the loop (since we may unref the target below).
       */
      next = watch->next;
346

347 348 349 350
      if (watch->context)
        g_main_context_invoke (watch->context,
                               g_settings_backend_invoke_closure,
                               closure);
351
      else
352
        g_settings_backend_invoke_closure (closure);
353
    }
354 355
}

Allison Karlitskaya's avatar
Allison Karlitskaya committed
356 357 358 359 360 361 362 363 364 365
/**
 * g_settings_backend_changed:
 * @backend: a #GSettingsBackend implementation
 * @key: the name of the key
 * @origin_tag: the origin tag
 *
 * Signals that a single key has possibly changed.  Backend
 * implementations should call this if a key has possibly changed its
 * value.
 *
366
 * @key must be a valid key (ie starting with a slash, not containing
Allison Karlitskaya's avatar
Allison Karlitskaya committed
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
 * '//', and not ending with a slash).
 *
 * The implementation must call this function during any call to
 * g_settings_backend_write(), before the call returns (except in the
 * case that no keys are actually changed and it cares to detect this
 * fact).  It may not rely on the existence of a mainloop for
 * dispatching the signal later.
 *
 * The implementation may call this function at any other time it likes
 * in response to other events (such as changes occuring outside of the
 * program).  These calls may originate from a mainloop or may originate
 * in response to any other action (including from calls to
 * g_settings_backend_write()).
 *
 * In the case that this call is in response to a call to
 * g_settings_backend_write() then @origin_tag must be set to the same
 * value that was passed to that call.
 *
 * Since: 2.26
 **/
void
g_settings_backend_changed (GSettingsBackend *backend,
                            const gchar      *key,
                            gpointer          origin_tag)
{
  g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
  g_return_if_fail (is_key (key));

395
  g_settings_backend_dispatch_signal (backend,
396
                                      G_STRUCT_OFFSET (GSettingsListenerVTable,
397 398
                                                       changed),
                                      key, origin_tag, NULL, NULL, NULL);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
399 400 401 402 403 404 405 406 407 408 409 410 411
}

/**
 * g_settings_backend_keys_changed:
 * @backend: a #GSettingsBackend implementation
 * @path: the path containing the changes
 * @items: the %NULL-terminated list of changed keys
 * @origin_tag: the origin tag
 *
 * Signals that a list of keys have possibly changed.  Backend
 * implementations should call this if keys have possibly changed their
 * values.
 *
412
 * @path must be a valid path (ie starting and ending with a slash and
Allison Karlitskaya's avatar
Allison Karlitskaya committed
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
 * not containing '//').  Each string in @items must form a valid key
 * name when @path is prefixed to it (ie: each item must not start or
 * end with '/' and must not contain '//').
 *
 * The meaning of this signal is that any of the key names resulting
 * from the contatenation of @path with each item in @items may have
 * changed.
 *
 * The same rules for when notifications must occur apply as per
 * g_settings_backend_changed().  These two calls can be used
 * interchangeably if exactly one item has changed (although in that
 * case g_settings_backend_changed() is definitely preferred).
 *
 * For efficiency reasons, the implementation should strive for @path to
 * be as long as possible (ie: the longest common prefix of all of the
 * keys that were changed) but this is not strictly required.
 *
 * Since: 2.26
 */
void
g_settings_backend_keys_changed (GSettingsBackend    *backend,
                                 const gchar         *path,
                                 gchar const * const *items,
                                 gpointer             origin_tag)
{
  g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
439 440 441
  g_return_if_fail (is_path (path));

  /* XXX: should do stricter checking (ie: inspect each item) */
Allison Karlitskaya's avatar
Allison Karlitskaya committed
442 443
  g_return_if_fail (items != NULL);

444
  g_settings_backend_dispatch_signal (backend,
445
                                      G_STRUCT_OFFSET (GSettingsListenerVTable,
446 447 448 449 450
                                                       keys_changed),
                                      path, (gpointer) items,
                                      (GBoxedCopyFunc) g_strdupv,
                                      (GBoxedFreeFunc) g_strfreev,
                                      origin_tag);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
451 452 453
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
454
 * g_settings_backend_path_changed:
Allison Karlitskaya's avatar
Allison Karlitskaya committed
455 456 457 458 459 460 461 462
 * @backend: a #GSettingsBackend implementation
 * @path: the path containing the changes
 * @origin_tag: the origin tag
 *
 * Signals that all keys below a given path may have possibly changed.
 * Backend implementations should call this if an entire path of keys
 * have possibly changed their values.
 *
463
 * @path must be a valid path (ie starting and ending with a slash and
Allison Karlitskaya's avatar
Allison Karlitskaya committed
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
 * not containing '//').
 *
 * The meaning of this signal is that any of the key which has a name
 * starting with @path may have changed.
 *
 * The same rules for when notifications must occur apply as per
 * g_settings_backend_changed().  This call might be an appropriate
 * reasponse to a 'reset' call but implementations are also free to
 * explicitly list the keys that were affected by that call if they can
 * easily do so.
 *
 * For efficiency reasons, the implementation should strive for @path to
 * be as long as possible (ie: the longest common prefix of all of the
 * keys that were changed) but this is not strictly required.  As an
 * example, if this function is called with the path of "/" then every
 * single key in the application will be notified of a possible change.
 *
 * Since: 2.26
 */
void
g_settings_backend_path_changed (GSettingsBackend *backend,
                                 const gchar      *path,
                                 gpointer          origin_tag)
{
  g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
  g_return_if_fail (is_path (path));

491
  g_settings_backend_dispatch_signal (backend,
492
                                      G_STRUCT_OFFSET (GSettingsListenerVTable,
493 494
                                                       path_changed),
                                      path, origin_tag, NULL, NULL, NULL);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
}

/**
 * g_settings_backend_writable_changed:
 * @backend: a #GSettingsBackend implementation
 * @key: the name of the key
 *
 * Signals that the writability of a single key has possibly changed.
 *
 * Since GSettings performs no locking operations for itself, this call
 * will always be made in response to external events.
 *
 * Since: 2.26
 **/
void
g_settings_backend_writable_changed (GSettingsBackend *backend,
                                     const gchar      *key)
{
  g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
  g_return_if_fail (is_key (key));

516
  g_settings_backend_dispatch_signal (backend,
517
                                      G_STRUCT_OFFSET (GSettingsListenerVTable,
518 519
                                                       writable_changed),
                                      key, NULL, NULL, NULL, NULL);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
520 521 522
}

/**
Matthias Clasen's avatar
Matthias Clasen committed
523
 * g_settings_backend_path_writable_changed:
Allison Karlitskaya's avatar
Allison Karlitskaya committed
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
 * @backend: a #GSettingsBackend implementation
 * @path: the name of the path
 *
 * Signals that the writability of all keys below a given path may have
 * changed.
 *
 * Since GSettings performs no locking operations for itself, this call
 * will always be made in response to external events.
 *
 * Since: 2.26
 **/
void
g_settings_backend_path_writable_changed (GSettingsBackend *backend,
                                          const gchar      *path)
{
  g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
  g_return_if_fail (is_path (path));

542
  g_settings_backend_dispatch_signal (backend,
543
                                      G_STRUCT_OFFSET (GSettingsListenerVTable,
544 545
                                                       path_writable_changed),
                                      path, NULL, NULL, NULL, NULL);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
546 547 548 549
}

typedef struct
{
550 551
  const gchar **keys;
  GVariant **values;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
552 553
  gint prefix_len;
  gchar *prefix;
554
} FlattenState;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
555 556

static gboolean
557 558 559
g_settings_backend_flatten_one (gpointer key,
                                gpointer value,
                                gpointer user_data)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
560
{
561
  FlattenState *state = user_data;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
  const gchar *skey = key;
  gint i;

  g_return_val_if_fail (is_key (key), TRUE);

  /* calculate longest common prefix */
  if (state->prefix == NULL)
    {
      gchar *last_byte;

      /* first key?  just take the prefix up to the last '/' */
      state->prefix = g_strdup (skey);
      last_byte = strrchr (state->prefix, '/') + 1;
      state->prefix_len = last_byte - state->prefix;
      *last_byte = '\0';
    }
  else
    {
      /* find the first character that does not match.  we will
       * definitely find one because the prefix ends in '/' and the key
       * does not.  also: no two keys in the tree are the same.
       */
      for (i = 0; state->prefix[i] == skey[i]; i++);

      /* check if we need to shorten the prefix */
      if (state->prefix[i] != '\0')
        {
          /* find the nearest '/', terminate after it */
          while (state->prefix[i - 1] != '/')
            i--;

          state->prefix[i] = '\0';
          state->prefix_len = i;
        }
    }


  /* save the entire item into the array.
   * the prefixes will be removed later.
   */
602 603 604 605
  *state->keys++ = key;

  if (state->values)
    *state->values++ = value;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
606 607 608 609

  return FALSE;
}

610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
/**
 * g_settings_backend_flatten_tree:
 * @tree: a #GTree containing the changes
 * @path: the location to save the path
 * @keys: the location to save the relative keys
 * @values: the location to save the values, or %NULL
 *
 * Calculate the longest common prefix of all keys in a tree and write
 * out an array of the key names relative to that prefix and,
 * optionally, the value to store at each of those keys.
 *
 * You must free the value returned in @path, @keys and @values using
 * g_free().  You should not attempt to free or unref the contents of
 * @keys or @values.
 *
 * Since: 2.26
 **/
void
g_settings_backend_flatten_tree (GTree         *tree,
                                 gchar        **path,
                                 const gchar ***keys,
                                 GVariant    ***values)
{
  FlattenState state = { 0, };
  gsize nnodes;

  nnodes = g_tree_nnodes (tree);

  *keys = state.keys = g_new (const gchar *, nnodes + 1);
  state.keys[nnodes] = NULL;

  if (values != NULL)
    {
      *values = state.values = g_new (GVariant *, nnodes + 1);
      state.values[nnodes] = NULL;
    }

  g_tree_foreach (tree, g_settings_backend_flatten_one, &state);
  g_return_if_fail (*keys + nnodes == state.keys);

  *path = state.prefix;
651 652
  while (nnodes--)
    *--state.keys += state.prefix_len;
653 654
}

Allison Karlitskaya's avatar
Allison Karlitskaya committed
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
/**
 * g_settings_backend_changed_tree:
 * @backend: a #GSettingsBackend implementation
 * @tree: a #GTree containing the changes
 * @origin_tag: the origin tag
 *
 * This call is a convenience wrapper.  It gets the list of changes from
 * @tree, computes the longest common prefix and calls
 * g_settings_backend_changed().
 *
 * Since: 2.26
 **/
void
g_settings_backend_changed_tree (GSettingsBackend *backend,
                                 GTree            *tree,
                                 gpointer          origin_tag)
{
  GSettingsBackendWatch *watch;
673 674
  const gchar **keys;
  gchar *path;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
675

676 677
  g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));

678
  g_settings_backend_flatten_tree (tree, &path, &keys, NULL);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
679

680 681 682 683 684 685 686 687 688 689 690 691
#ifdef DEBUG_CHANGES
  {
    gint i;

    g_print ("----\n");
    g_print ("changed_tree(): prefix %s\n", path);
    for (i = 0; keys[i]; i++)
      g_print ("  %s\n", keys[i]);
    g_print ("----\n");
  }
#endif

Allison Karlitskaya's avatar
Allison Karlitskaya committed
692
  for (watch = backend->priv->watches; watch; watch = watch->next)
693 694
    watch->vtable->keys_changed (watch->target, backend,
                                 path, keys, origin_tag);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
695

696 697
  g_free (path);
  g_free (keys);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
698 699 700 701 702 703
}

/*< private >
 * g_settings_backend_read:
 * @backend: a #GSettingsBackend implementation
 * @key: the key to read
704 705
 * @expected_type: a #GVariantType
 * @default_value: if the default value should be returned
Allison Karlitskaya's avatar
Allison Karlitskaya committed
706 707 708 709 710 711 712
 * @returns: the value that was read, or %NULL
 *
 * Reads a key. This call will never block.
 *
 * If the key exists, the value associated with it will be returned.
 * If the key does not exist, %NULL will be returned.
 *
713 714 715 716 717 718 719
 * The returned value will be of the type given in @expected_type.  If
 * the backend stored a value of a different type then %NULL will be
 * returned.
 *
 * If @default_value is %TRUE then this gets the default value from the
 * backend (ie: the one that the backend would contain if
 * g_settings_reset() were called).
Allison Karlitskaya's avatar
Allison Karlitskaya committed
720 721 722 723
 */
GVariant *
g_settings_backend_read (GSettingsBackend   *backend,
                         const gchar        *key,
724 725
                         const GVariantType *expected_type,
                         gboolean            default_value)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
726
{
727 728 729
  GVariant *value;

  value = G_SETTINGS_BACKEND_GET_CLASS (backend)
730
    ->read (backend, key, expected_type, default_value);
731 732 733 734 735 736 737 738

  if G_UNLIKELY (value && !g_variant_is_of_type (value, expected_type))
    {
      g_variant_unref (value);
      value = NULL;
    }

  return value;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
739 740 741 742 743 744 745 746
}

/*< private >
 * g_settings_backend_write:
 * @backend: a #GSettingsBackend implementation
 * @key: the name of the key
 * @value: a #GVariant value to write to this key
 * @origin_tag: the origin tag
Matthias Clasen's avatar
Matthias Clasen committed
747
 * @returns: %TRUE if the write succeeded, %FALSE if the key was not writable
Allison Karlitskaya's avatar
Allison Karlitskaya committed
748 749 750 751 752 753 754 755 756 757 758 759 760
 *
 * Writes exactly one key.
 *
 * This call does not fail.  During this call a
 * #GSettingsBackend::changed signal will be emitted if the value of the
 * key has changed.  The updated key value will be visible to any signal
 * callbacks.
 *
 * One possible method that an implementation might deal with failures is
 * to emit a second "changed" signal (either during this call, or later)
 * to indicate that the affected keys have suddenly "changed back" to their
 * old values.
 */
761
gboolean
Allison Karlitskaya's avatar
Allison Karlitskaya committed
762 763 764 765 766
g_settings_backend_write (GSettingsBackend *backend,
                          const gchar      *key,
                          GVariant         *value,
                          gpointer          origin_tag)
{
767
  return G_SETTINGS_BACKEND_GET_CLASS (backend)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
    ->write (backend, key, value, origin_tag);
}

/*< private >
 * g_settings_backend_write_keys:
 * @backend: a #GSettingsBackend implementation
 * @values: a #GTree containing key-value pairs to write
 * @origin_tag: the origin tag
 *
 * Writes one or more keys.  This call will never block.
 *
 * The key of each item in the tree is the key name to write to and the
 * value is a #GVariant to write.  The proper type of #GTree for this
 * call can be created with g_settings_backend_create_tree().  This call
 * might take a reference to the tree; you must not modified the #GTree
 * after passing it to this call.
 *
 * This call does not fail.  During this call a #GSettingsBackend::changed
 * signal will be emitted if any keys have been changed.  The new values of
 * all updated keys will be visible to any signal callbacks.
 *
 * One possible method that an implementation might deal with failures is
 * to emit a second "changed" signal (either during this call, or later)
 * to indicate that the affected keys have suddenly "changed back" to their
 * old values.
 */
794
gboolean
795
g_settings_backend_write_tree (GSettingsBackend *backend,
Allison Karlitskaya's avatar
Allison Karlitskaya committed
796 797 798
                               GTree            *tree,
                               gpointer          origin_tag)
{
799
  return G_SETTINGS_BACKEND_GET_CLASS (backend)
800
    ->write_tree (backend, tree, origin_tag);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
801 802 803 804 805
}

/*< private >
 * g_settings_backend_reset:
 * @backend: a #GSettingsBackend implementation
806
 * @key: the name of a key
Allison Karlitskaya's avatar
Allison Karlitskaya committed
807 808
 * @origin_tag: the origin tag
 *
809 810 811
 * "Resets" the named key to its "default" value (ie: after system-wide
 * defaults, mandatory keys, etc. have been taken into account) or possibly
 * unsets it.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
812 813 814
 */
void
g_settings_backend_reset (GSettingsBackend *backend,
815
                          const gchar      *key,
Allison Karlitskaya's avatar
Allison Karlitskaya committed
816 817 818
                          gpointer          origin_tag)
{
  G_SETTINGS_BACKEND_GET_CLASS (backend)
819 820 821
    ->reset (backend, key, origin_tag);
}

Allison Karlitskaya's avatar
Allison Karlitskaya committed
822 823 824
/*< private >
 * g_settings_backend_get_writable:
 * @backend: a #GSettingsBackend implementation
825
 * @key: the name of a key
Allison Karlitskaya's avatar
Allison Karlitskaya committed
826 827 828 829 830 831 832 833 834 835 836
 * @returns: %TRUE if the key is writable
 *
 * Finds out if a key is available for writing to.  This is the
 * interface through which 'lockdown' is implemented.  Locked down
 * keys will have %FALSE returned by this call.
 *
 * You should not write to locked-down keys, but if you do, the
 * implementation will deal with it.
 */
gboolean
g_settings_backend_get_writable (GSettingsBackend *backend,
837
                                 const gchar      *key)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
838 839
{
  return G_SETTINGS_BACKEND_GET_CLASS (backend)
840
    ->get_writable (backend, key);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
}

/*< private >
 * g_settings_backend_unsubscribe:
 * @backend: a #GSettingsBackend
 * @name: a key or path to subscribe to
 *
 * Reverses the effect of a previous call to
 * g_settings_backend_subscribe().
 */
void
g_settings_backend_unsubscribe (GSettingsBackend *backend,
                                const char       *name)
{
  G_SETTINGS_BACKEND_GET_CLASS (backend)
    ->unsubscribe (backend, name);
}

/*< private >
 * g_settings_backend_subscribe:
 * @backend: a #GSettingsBackend
 * @name: a key or path to subscribe to
 *
 * Requests that change signals be emitted for events on @name.
 */
void
g_settings_backend_subscribe (GSettingsBackend *backend,
                              const gchar      *name)
{
  G_SETTINGS_BACKEND_GET_CLASS (backend)
    ->subscribe (backend, name);
}

static void
g_settings_backend_finalize (GObject *object)
{
  GSettingsBackend *backend = G_SETTINGS_BACKEND (object);

879
  g_static_mutex_unlock (&backend->priv->lock);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
880

881 882
  G_OBJECT_CLASS (g_settings_backend_parent_class)
    ->finalize (object);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
883 884 885 886 887 888 889 890 891 892 893 894 895 896
}

static void
ignore_subscription (GSettingsBackend *backend,
                     const gchar      *key)
{
}

static void
g_settings_backend_init (GSettingsBackend *backend)
{
  backend->priv = G_TYPE_INSTANCE_GET_PRIVATE (backend,
                                               G_TYPE_SETTINGS_BACKEND,
                                               GSettingsBackendPrivate);
897
  g_static_mutex_init (&backend->priv->lock);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
}

static void
g_settings_backend_class_init (GSettingsBackendClass *class)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);

  class->subscribe = ignore_subscription;
  class->unsubscribe = ignore_subscription;

  gobject_class->finalize = g_settings_backend_finalize;

  g_type_class_add_private (class, sizeof (GSettingsBackendPrivate));
}

/*< private >
 * g_settings_backend_create_tree:
 * @returns: a new #GTree
 *
 * This is a convenience function for creating a tree that is compatible
 * with g_settings_backend_write().  It merely calls g_tree_new_full()
 * with strcmp(), g_free() and g_variant_unref().
 */
GTree *
g_settings_backend_create_tree (void)
{
  return g_tree_new_full ((GCompareDataFunc) strcmp, NULL,
                          g_free, (GDestroyNotify) g_variant_unref);
}

/*< private >
929
 * g_settings_backend_get_default:
Allison Karlitskaya's avatar
Allison Karlitskaya committed
930 931 932 933 934 935
 * @returns: the default #GSettingsBackend
 *
 * Returns the default #GSettingsBackend. It is possible to override
 * the default by setting the <envar>GSETTINGS_BACKEND</envar>
 * environment variable to the name of a settings backend.
 *
936
 * The user gets a reference to the backend.
Allison Karlitskaya's avatar
Allison Karlitskaya committed
937 938
 */
GSettingsBackend *
939
g_settings_backend_get_default (void)
Allison Karlitskaya's avatar
Allison Karlitskaya committed
940
{
941
  static gsize backend;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
942

943 944 945 946 947 948 949 950
  if (g_once_init_enter (&backend))
    {
      GSettingsBackend *instance;
      GIOExtensionPoint *point;
      GIOExtension *extension;
      GType extension_type;
      GList *extensions;
      const gchar *env;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
951

952
      _g_io_modules_ensure_loaded ();
Allison Karlitskaya's avatar
Allison Karlitskaya committed
953

954 955
      point = g_io_extension_point_lookup (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME);
      extension = NULL;
Allison Karlitskaya's avatar
Allison Karlitskaya committed
956

957 958 959
      if ((env = getenv ("GSETTINGS_BACKEND")))
        {
          extension = g_io_extension_point_get_extension_by_name (point, env);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
960

961 962 963 964
          if (extension == NULL)
            g_warning ("Can't find GSettings backend '%s' given in "
                       "GSETTINGS_BACKEND environment variable", env);
        }
Allison Karlitskaya's avatar
Allison Karlitskaya committed
965

966 967 968
      if (extension == NULL)
        {
          extensions = g_io_extension_point_get_extensions (point);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
969

970 971
          if (extensions == NULL)
            g_error ("No GSettingsBackend implementations exist.");
Allison Karlitskaya's avatar
Allison Karlitskaya committed
972

973
          extension = extensions->data;
974 975 976 977

          if (strcmp (g_io_extension_get_name (extension), "memory") == 0)
            g_message ("Using the 'memory' GSettings backend.  Your settings "
                       "will not be saved or shared with other applications.");
978
        }
979

980 981
      extension_type = g_io_extension_get_type (extension);
      instance = g_object_new (extension_type, NULL);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
982

983
      g_once_init_leave (&backend, (gsize) instance);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
984 985
    }

986
  return g_object_ref ((void *) backend);
Allison Karlitskaya's avatar
Allison Karlitskaya committed
987 988
}

989 990 991 992
/*< private >
 * g_settings_backend_get_permission:
 * @backend: a #GSettingsBackend
 * @path: a path
Matthias Clasen's avatar
Matthias Clasen committed
993
 * @returns: a non-%NULL #GPermission. Free with g_object_unref()
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
 *
 * Gets the permission object associated with writing to keys below
 * @path on @backend.
 *
 * If this is not implemented in the backend, then a %TRUE
 * #GSimplePermission is returned.
 */
GPermission *
g_settings_backend_get_permission (GSettingsBackend *backend,
                                   const gchar      *path)
{
  GSettingsBackendClass *class = G_SETTINGS_BACKEND_GET_CLASS (backend);

  if (class->get_permission)
    return class->get_permission (backend, path);

  return g_simple_permission_new (TRUE);
}

1013
/*< private >
1014
 * g_settings_backend_sync_default:
1015
 *
1016
 * Syncs the default backend.
1017 1018
 */
void
1019
g_settings_backend_sync_default (void)
1020
{
1021 1022 1023 1024 1025
  GSettingsBackendClass *class;
  GSettingsBackend *backend;

  backend = g_settings_backend_get_default ();
  class = G_SETTINGS_BACKEND_GET_CLASS (backend);
1026 1027 1028 1029

  if (class->sync)
    class->sync (backend);
}