2 * Copyright © 2009, 2010 Codethink Limited
3 * Copyright © 2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the licence, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Authors: Ryan Lortie <desrt@desrt.ca>
21 * Matthias Clasen <mclasen@redhat.com>
26 #include "gsettingsbackendinternal.h"
27 #include "gsimplepermission.h"
28 #include "giomodule-priv.h"
36 G_DEFINE_ABSTRACT_TYPE (GSettingsBackend
, g_settings_backend
, G_TYPE_OBJECT
)
38 typedef struct _GSettingsBackendClosure GSettingsBackendClosure
;
39 typedef struct _GSettingsBackendWatch GSettingsBackendWatch
;
41 struct _GSettingsBackendPrivate
43 GSettingsBackendWatch
*watches
;
47 /* For g_settings_backend_sync_default(), we only want to actually do
48 * the sync if the backend already exists. This avoids us creating an
49 * entire GSettingsBackend in order to call a do-nothing sync()
50 * operation on it. This variable lets us avoid that.
52 static gboolean g_settings_has_backend
;
55 * SECTION:gsettingsbackend
56 * @title: GSettingsBackend
57 * @short_description: Interface for settings backend implementations
58 * @include: gio/gsettingsbackend.h
59 * @see_also: #GSettings, #GIOExtensionPoint
61 * The #GSettingsBackend interface defines a generic interface for
62 * non-strictly-typed data that is stored in a hierarchy. To implement
63 * an alternative storage backend for #GSettings, you need to implement
64 * the #GSettingsBackend interface and then make it implement the
65 * extension point #G_SETTINGS_BACKEND_EXTENSION_POINT_NAME.
67 * The interface defines methods for reading and writing values, a
68 * method for determining if writing of certain values will fail
69 * (lockdown) and a change notification mechanism.
71 * The semantics of the interface are very precisely defined and
72 * implementations must carefully adhere to the expectations of
73 * callers that are documented on each of the interface methods.
75 * Some of the GSettingsBackend functions accept or return a #GTree.
76 * These trees always have strings as keys and #GVariant as values.
77 * g_settings_backend_create_tree() is a convenience function to create
81 * The #GSettingsBackend API is exported to allow third-party
82 * implementations, but does not carry the same stability guarantees
83 * as the public GIO API. For this reason, you have to define the
84 * C preprocessor symbol #G_SETTINGS_ENABLE_BACKEND before including
85 * <filename>gio/gsettingsbackend.h</filename>
90 is_key (const gchar
*key
)
95 g_return_val_if_fail (key
!= NULL
, FALSE
);
96 g_return_val_if_fail (key
[0] == '/', FALSE
);
98 for (i
= 1; key
[i
]; i
++)
99 g_return_val_if_fail (key
[i
] != '/' || key
[i
+ 1] != '/', FALSE
);
103 g_return_val_if_fail (key
[length
- 1] != '/', FALSE
);
109 is_path (const gchar
*path
)
114 g_return_val_if_fail (path
!= NULL
, FALSE
);
115 g_return_val_if_fail (path
[0] == '/', FALSE
);
117 for (i
= 1; path
[i
]; i
++)
118 g_return_val_if_fail (path
[i
] != '/' || path
[i
+ 1] != '/', FALSE
);
122 g_return_val_if_fail (path
[length
- 1] == '/', FALSE
);
127 struct _GSettingsBackendWatch
130 const GSettingsListenerVTable
*vtable
;
131 GMainContext
*context
;
132 GSettingsBackendWatch
*next
;
135 struct _GSettingsBackendClosure
137 void (*function
) (GObject
*target
,
138 GSettingsBackend
*backend
,
143 GSettingsBackend
*backend
;
147 GBoxedFreeFunc data1_free
;
152 g_settings_backend_watch_weak_notify (gpointer data
,
153 GObject
*where_the_object_was
)
155 GSettingsBackend
*backend
= data
;
156 GSettingsBackendWatch
**ptr
;
158 /* search and remove */
159 g_mutex_lock (&backend
->priv
->lock
);
160 for (ptr
= &backend
->priv
->watches
; *ptr
; ptr
= &(*ptr
)->next
)
161 if ((*ptr
)->target
== where_the_object_was
)
163 GSettingsBackendWatch
*tmp
= *ptr
;
166 g_slice_free (GSettingsBackendWatch
, tmp
);
168 g_mutex_unlock (&backend
->priv
->lock
);
172 /* we didn't find it. that shouldn't happen. */
173 g_assert_not_reached ();
177 * g_settings_backend_watch:
178 * @backend: a #GSettingsBackend
179 * @target: the GObject (typically GSettings instance) to call back to
180 * @context: a #GMainContext, or %NULL
183 * Registers a new watch on a #GSettingsBackend.
185 * note: %NULL @context does not mean "default main context" but rather,
186 * "it is okay to dispatch in any context". If the default main context
187 * is specifically desired then it must be given.
189 * note also: if you want to get meaningful values for the @origin_tag
190 * that appears as an argument to some of the callbacks, you *must* have
191 * @context as %NULL. Otherwise, you are subject to cross-thread
192 * dispatching and whatever owned @origin_tag at the time that the event
193 * occurred may no longer own it. This is a problem if you consider that
194 * you may now be the new owner of that address and mistakenly think
195 * that the event in question originated from yourself.
197 * tl;dr: If you give a non-%NULL @context then you must ignore the
198 * value of @origin_tag given to any callbacks.
201 g_settings_backend_watch (GSettingsBackend
*backend
,
202 const GSettingsListenerVTable
*vtable
,
204 GMainContext
*context
)
206 GSettingsBackendWatch
*watch
;
208 /* For purposes of discussion, we assume that our target is a
209 * GSettings instance.
211 * Our strategy to defend against the final reference dropping on the
212 * GSettings object in a thread other than the one that is doing the
213 * dispatching is as follows:
215 * 1) hold a GObject reference on the GSettings during an outstanding
216 * dispatch. This ensures that the delivery is always possible.
218 * 2) hold a weak reference on the GSettings at other times. This
219 * allows us to receive early notification of pending destruction
220 * of the object. At this point, it is still safe to obtain a
221 * reference on the GObject to keep it alive, so #1 will work up
222 * to that point. After that point, we'll have been able to drop
223 * the watch from the list.
225 * Note, in particular, that it's not possible to simply have an
226 * "unwatch" function that gets called from the finalize function of
227 * the GSettings instance because, by that point it is no longer
228 * possible to keep the object alive using g_object_ref() and we would
229 * have no way of knowing this.
231 * Note also that we do not need to hold a reference on the main
232 * context here since the GSettings instance does that for us and we
233 * will receive the weak notify long before it is dropped. We don't
234 * even need to hold it during dispatches because our reference on the
235 * GSettings will prevent the finalize from running and dropping the
236 * ref on the context.
238 * All access to the list holds a mutex. We have some strategies to
239 * avoid some of the pain that would be associated with that.
242 watch
= g_slice_new (GSettingsBackendWatch
);
243 watch
->context
= context
;
244 watch
->vtable
= vtable
;
245 watch
->target
= target
;
246 g_object_weak_ref (target
, g_settings_backend_watch_weak_notify
, backend
);
248 /* linked list prepend */
249 g_mutex_lock (&backend
->priv
->lock
);
250 watch
->next
= backend
->priv
->watches
;
251 backend
->priv
->watches
= watch
;
252 g_mutex_unlock (&backend
->priv
->lock
);
256 g_settings_backend_unwatch (GSettingsBackend
*backend
,
259 /* Our caller surely owns a reference on 'target', so the order of
260 * these two calls is unimportant.
262 g_object_weak_unref (target
, g_settings_backend_watch_weak_notify
, backend
);
263 g_settings_backend_watch_weak_notify (backend
, target
);
267 g_settings_backend_invoke_closure (gpointer user_data
)
269 GSettingsBackendClosure
*closure
= user_data
;
271 closure
->function (closure
->target
, closure
->backend
, closure
->name
,
272 closure
->data1
, closure
->data2
);
274 closure
->data1_free (closure
->data1
);
275 g_object_unref (closure
->backend
);
276 g_object_unref (closure
->target
);
277 g_free (closure
->name
);
279 g_slice_free (GSettingsBackendClosure
, closure
);
285 pointer_id (gpointer a
)
291 pointer_ignore (gpointer a
)
296 g_settings_backend_dispatch_signal (GSettingsBackend
*backend
,
297 gsize function_offset
,
300 GBoxedCopyFunc data1_copy
,
301 GBoxedFreeFunc data1_free
,
304 GSettingsBackendWatch
*suffix
, *watch
, *next
;
306 if (data1_copy
== NULL
)
307 data1_copy
= pointer_id
;
309 if (data1_free
== NULL
)
310 data1_free
= pointer_ignore
;
312 /* We're in a little bit of a tricky situation here. We need to hold
313 * a lock while traversing the list, but we don't want to hold the
314 * lock while calling back into user code.
316 * Since we're not holding the lock while we call user code, we can't
317 * render the list immutable. We can, however, store a pointer to a
318 * given suffix of the list and render that suffix immutable.
320 * Adds will never modify the suffix since adds always come in the
321 * form of prepends. We can also prevent removes from modifying the
322 * suffix since removes only happen in response to the last reference
323 * count dropping -- so just add a reference to everything in the
326 g_mutex_lock (&backend
->priv
->lock
);
327 suffix
= backend
->priv
->watches
;
328 for (watch
= suffix
; watch
; watch
= watch
->next
)
329 g_object_ref (watch
->target
);
330 g_mutex_unlock (&backend
->priv
->lock
);
332 /* The suffix is now immutable, so this is safe. */
333 for (watch
= suffix
; watch
; watch
= next
)
335 GSettingsBackendClosure
*closure
;
337 closure
= g_slice_new (GSettingsBackendClosure
);
338 closure
->backend
= g_object_ref (backend
);
339 closure
->target
= watch
->target
; /* we took our ref above */
340 closure
->function
= G_STRUCT_MEMBER (void *, watch
->vtable
,
342 closure
->name
= g_strdup (name
);
343 closure
->data1
= data1_copy (data1
);
344 closure
->data1_free
= data1_free
;
345 closure
->data2
= data2
;
347 /* we do this here because 'watch' may not live to the end of this
348 * iteration of the loop (since we may unref the target below).
353 g_main_context_invoke (watch
->context
,
354 g_settings_backend_invoke_closure
,
357 g_settings_backend_invoke_closure (closure
);
362 * g_settings_backend_changed:
363 * @backend: a #GSettingsBackend implementation
364 * @key: the name of the key
365 * @origin_tag: the origin tag
367 * Signals that a single key has possibly changed. Backend
368 * implementations should call this if a key has possibly changed its
371 * @key must be a valid key (ie starting with a slash, not containing
372 * '//', and not ending with a slash).
374 * The implementation must call this function during any call to
375 * g_settings_backend_write(), before the call returns (except in the
376 * case that no keys are actually changed and it cares to detect this
377 * fact). It may not rely on the existence of a mainloop for
378 * dispatching the signal later.
380 * The implementation may call this function at any other time it likes
381 * in response to other events (such as changes occurring outside of the
382 * program). These calls may originate from a mainloop or may originate
383 * in response to any other action (including from calls to
384 * g_settings_backend_write()).
386 * In the case that this call is in response to a call to
387 * g_settings_backend_write() then @origin_tag must be set to the same
388 * value that was passed to that call.
393 g_settings_backend_changed (GSettingsBackend
*backend
,
397 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend
));
398 g_return_if_fail (is_key (key
));
400 g_settings_backend_dispatch_signal (backend
,
401 G_STRUCT_OFFSET (GSettingsListenerVTable
,
403 key
, origin_tag
, NULL
, NULL
, NULL
);
407 * g_settings_backend_keys_changed:
408 * @backend: a #GSettingsBackend implementation
409 * @path: the path containing the changes
410 * @items: (array zero-terminated=1): the %NULL-terminated list of changed keys
411 * @origin_tag: the origin tag
413 * Signals that a list of keys have possibly changed. Backend
414 * implementations should call this if keys have possibly changed their
417 * @path must be a valid path (ie starting and ending with a slash and
418 * not containing '//'). Each string in @items must form a valid key
419 * name when @path is prefixed to it (ie: each item must not start or
420 * end with '/' and must not contain '//').
422 * The meaning of this signal is that any of the key names resulting
423 * from the contatenation of @path with each item in @items may have
426 * The same rules for when notifications must occur apply as per
427 * g_settings_backend_changed(). These two calls can be used
428 * interchangeably if exactly one item has changed (although in that
429 * case g_settings_backend_changed() is definitely preferred).
431 * For efficiency reasons, the implementation should strive for @path to
432 * be as long as possible (ie: the longest common prefix of all of the
433 * keys that were changed) but this is not strictly required.
438 g_settings_backend_keys_changed (GSettingsBackend
*backend
,
440 gchar
const * const *items
,
443 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend
));
444 g_return_if_fail (is_path (path
));
446 /* XXX: should do stricter checking (ie: inspect each item) */
447 g_return_if_fail (items
!= NULL
);
449 g_settings_backend_dispatch_signal (backend
,
450 G_STRUCT_OFFSET (GSettingsListenerVTable
,
452 path
, (gpointer
) items
,
453 (GBoxedCopyFunc
) g_strdupv
,
454 (GBoxedFreeFunc
) g_strfreev
,
459 * g_settings_backend_path_changed:
460 * @backend: a #GSettingsBackend implementation
461 * @path: the path containing the changes
462 * @origin_tag: the origin tag
464 * Signals that all keys below a given path may have possibly changed.
465 * Backend implementations should call this if an entire path of keys
466 * have possibly changed their values.
468 * @path must be a valid path (ie starting and ending with a slash and
469 * not containing '//').
471 * The meaning of this signal is that any of the key which has a name
472 * starting with @path may have changed.
474 * The same rules for when notifications must occur apply as per
475 * g_settings_backend_changed(). This call might be an appropriate
476 * reasponse to a 'reset' call but implementations are also free to
477 * explicitly list the keys that were affected by that call if they can
480 * For efficiency reasons, the implementation should strive for @path to
481 * be as long as possible (ie: the longest common prefix of all of the
482 * keys that were changed) but this is not strictly required. As an
483 * example, if this function is called with the path of "/" then every
484 * single key in the application will be notified of a possible change.
489 g_settings_backend_path_changed (GSettingsBackend
*backend
,
493 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend
));
494 g_return_if_fail (is_path (path
));
496 g_settings_backend_dispatch_signal (backend
,
497 G_STRUCT_OFFSET (GSettingsListenerVTable
,
499 path
, origin_tag
, NULL
, NULL
, NULL
);
503 * g_settings_backend_writable_changed:
504 * @backend: a #GSettingsBackend implementation
505 * @key: the name of the key
507 * Signals that the writability of a single key has possibly changed.
509 * Since GSettings performs no locking operations for itself, this call
510 * will always be made in response to external events.
515 g_settings_backend_writable_changed (GSettingsBackend
*backend
,
518 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend
));
519 g_return_if_fail (is_key (key
));
521 g_settings_backend_dispatch_signal (backend
,
522 G_STRUCT_OFFSET (GSettingsListenerVTable
,
524 key
, NULL
, NULL
, NULL
, NULL
);
528 * g_settings_backend_path_writable_changed:
529 * @backend: a #GSettingsBackend implementation
530 * @path: the name of the path
532 * Signals that the writability of all keys below a given path may have
535 * Since GSettings performs no locking operations for itself, this call
536 * will always be made in response to external events.
541 g_settings_backend_path_writable_changed (GSettingsBackend
*backend
,
544 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend
));
545 g_return_if_fail (is_path (path
));
547 g_settings_backend_dispatch_signal (backend
,
548 G_STRUCT_OFFSET (GSettingsListenerVTable
,
549 path_writable_changed
),
550 path
, NULL
, NULL
, NULL
, NULL
);
562 g_settings_backend_flatten_one (gpointer key
,
566 FlattenState
*state
= user_data
;
567 const gchar
*skey
= key
;
570 g_return_val_if_fail (is_key (key
), TRUE
);
572 /* calculate longest common prefix */
573 if (state
->prefix
== NULL
)
577 /* first key? just take the prefix up to the last '/' */
578 state
->prefix
= g_strdup (skey
);
579 last_byte
= strrchr (state
->prefix
, '/') + 1;
580 state
->prefix_len
= last_byte
- state
->prefix
;
585 /* find the first character that does not match. we will
586 * definitely find one because the prefix ends in '/' and the key
587 * does not. also: no two keys in the tree are the same.
589 for (i
= 0; state
->prefix
[i
] == skey
[i
]; i
++);
591 /* check if we need to shorten the prefix */
592 if (state
->prefix
[i
] != '\0')
594 /* find the nearest '/', terminate after it */
595 while (state
->prefix
[i
- 1] != '/')
598 state
->prefix
[i
] = '\0';
599 state
->prefix_len
= i
;
604 /* save the entire item into the array.
605 * the prefixes will be removed later.
607 *state
->keys
++ = key
;
610 *state
->values
++ = value
;
616 * g_settings_backend_flatten_tree:
617 * @tree: a #GTree containing the changes
618 * @path: (out): the location to save the path
619 * @keys: (out) (transfer container) (array zero-terminated=1): the
620 * location to save the relative keys
621 * @values: (out) (allow-none) (transfer container) (array zero-terminated=1):
622 * the location to save the values, or %NULL
624 * Calculate the longest common prefix of all keys in a tree and write
625 * out an array of the key names relative to that prefix and,
626 * optionally, the value to store at each of those keys.
628 * You must free the value returned in @path, @keys and @values using
629 * g_free(). You should not attempt to free or unref the contents of
635 g_settings_backend_flatten_tree (GTree
*tree
,
640 FlattenState state
= { 0, };
643 nnodes
= g_tree_nnodes (tree
);
645 *keys
= state
.keys
= g_new (const gchar
*, nnodes
+ 1);
646 state
.keys
[nnodes
] = NULL
;
650 *values
= state
.values
= g_new (GVariant
*, nnodes
+ 1);
651 state
.values
[nnodes
] = NULL
;
654 g_tree_foreach (tree
, g_settings_backend_flatten_one
, &state
);
655 g_return_if_fail (*keys
+ nnodes
== state
.keys
);
657 *path
= state
.prefix
;
659 *--state
.keys
+= state
.prefix_len
;
663 * g_settings_backend_changed_tree:
664 * @backend: a #GSettingsBackend implementation
665 * @tree: a #GTree containing the changes
666 * @origin_tag: the origin tag
668 * This call is a convenience wrapper. It gets the list of changes from
669 * @tree, computes the longest common prefix and calls
670 * g_settings_backend_changed().
675 g_settings_backend_changed_tree (GSettingsBackend
*backend
,
682 g_return_if_fail (G_IS_SETTINGS_BACKEND (backend
));
684 g_settings_backend_flatten_tree (tree
, &path
, &keys
, NULL
);
691 g_print ("changed_tree(): prefix %s\n", path
);
692 for (i
= 0; keys
[i
]; i
++)
693 g_print (" %s\n", keys
[i
]);
698 g_settings_backend_keys_changed (backend
, path
, keys
, origin_tag
);
704 * g_settings_backend_read:
705 * @backend: a #GSettingsBackend implementation
706 * @key: the key to read
707 * @expected_type: a #GVariantType
708 * @default_value: if the default value should be returned
710 * Reads a key. This call will never block.
712 * If the key exists, the value associated with it will be returned.
713 * If the key does not exist, %NULL will be returned.
715 * The returned value will be of the type given in @expected_type. If
716 * the backend stored a value of a different type then %NULL will be
719 * If @default_value is %TRUE then this gets the default value from the
720 * backend (ie: the one that the backend would contain if
721 * g_settings_reset() were called).
723 * Returns: the value that was read, or %NULL
726 g_settings_backend_read (GSettingsBackend
*backend
,
728 const GVariantType
*expected_type
,
729 gboolean default_value
)
733 value
= G_SETTINGS_BACKEND_GET_CLASS (backend
)
734 ->read (backend
, key
, expected_type
, default_value
);
736 if G_UNLIKELY (value
&& !g_variant_is_of_type (value
, expected_type
))
738 g_variant_unref (value
);
746 * g_settings_backend_write:
747 * @backend: a #GSettingsBackend implementation
748 * @key: the name of the key
749 * @value: a #GVariant value to write to this key
750 * @origin_tag: the origin tag
752 * Writes exactly one key.
754 * This call does not fail. During this call a
755 * #GSettingsBackend::changed signal will be emitted if the value of the
756 * key has changed. The updated key value will be visible to any signal
759 * One possible method that an implementation might deal with failures is
760 * to emit a second "changed" signal (either during this call, or later)
761 * to indicate that the affected keys have suddenly "changed back" to their
764 * Returns: %TRUE if the write succeeded, %FALSE if the key was not writable
767 g_settings_backend_write (GSettingsBackend
*backend
,
772 return G_SETTINGS_BACKEND_GET_CLASS (backend
)
773 ->write (backend
, key
, value
, origin_tag
);
777 * g_settings_backend_write_keys:
778 * @backend: a #GSettingsBackend implementation
779 * @values: a #GTree containing key-value pairs to write
780 * @origin_tag: the origin tag
782 * Writes one or more keys. This call will never block.
784 * The key of each item in the tree is the key name to write to and the
785 * value is a #GVariant to write. The proper type of #GTree for this
786 * call can be created with g_settings_backend_create_tree(). This call
787 * might take a reference to the tree; you must not modified the #GTree
788 * after passing it to this call.
790 * This call does not fail. During this call a #GSettingsBackend::changed
791 * signal will be emitted if any keys have been changed. The new values of
792 * all updated keys will be visible to any signal callbacks.
794 * One possible method that an implementation might deal with failures is
795 * to emit a second "changed" signal (either during this call, or later)
796 * to indicate that the affected keys have suddenly "changed back" to their
800 g_settings_backend_write_tree (GSettingsBackend
*backend
,
804 return G_SETTINGS_BACKEND_GET_CLASS (backend
)
805 ->write_tree (backend
, tree
, origin_tag
);
809 * g_settings_backend_reset:
810 * @backend: a #GSettingsBackend implementation
811 * @key: the name of a key
812 * @origin_tag: the origin tag
814 * "Resets" the named key to its "default" value (ie: after system-wide
815 * defaults, mandatory keys, etc. have been taken into account) or possibly
819 g_settings_backend_reset (GSettingsBackend
*backend
,
823 G_SETTINGS_BACKEND_GET_CLASS (backend
)
824 ->reset (backend
, key
, origin_tag
);
828 * g_settings_backend_get_writable:
829 * @backend: a #GSettingsBackend implementation
830 * @key: the name of a key
832 * Finds out if a key is available for writing to. This is the
833 * interface through which 'lockdown' is implemented. Locked down
834 * keys will have %FALSE returned by this call.
836 * You should not write to locked-down keys, but if you do, the
837 * implementation will deal with it.
839 * Returns: %TRUE if the key is writable
842 g_settings_backend_get_writable (GSettingsBackend
*backend
,
845 return G_SETTINGS_BACKEND_GET_CLASS (backend
)
846 ->get_writable (backend
, key
);
850 * g_settings_backend_unsubscribe:
851 * @backend: a #GSettingsBackend
852 * @name: a key or path to subscribe to
854 * Reverses the effect of a previous call to
855 * g_settings_backend_subscribe().
858 g_settings_backend_unsubscribe (GSettingsBackend
*backend
,
861 G_SETTINGS_BACKEND_GET_CLASS (backend
)
862 ->unsubscribe (backend
, name
);
866 * g_settings_backend_subscribe:
867 * @backend: a #GSettingsBackend
868 * @name: a key or path to subscribe to
870 * Requests that change signals be emitted for events on @name.
873 g_settings_backend_subscribe (GSettingsBackend
*backend
,
876 G_SETTINGS_BACKEND_GET_CLASS (backend
)
877 ->subscribe (backend
, name
);
881 g_settings_backend_finalize (GObject
*object
)
883 GSettingsBackend
*backend
= G_SETTINGS_BACKEND (object
);
885 g_mutex_clear (&backend
->priv
->lock
);
887 G_OBJECT_CLASS (g_settings_backend_parent_class
)
892 ignore_subscription (GSettingsBackend
*backend
,
898 g_settings_backend_init (GSettingsBackend
*backend
)
900 backend
->priv
= G_TYPE_INSTANCE_GET_PRIVATE (backend
,
901 G_TYPE_SETTINGS_BACKEND
,
902 GSettingsBackendPrivate
);
903 g_mutex_init (&backend
->priv
->lock
);
907 g_settings_backend_class_init (GSettingsBackendClass
*class)
909 GObjectClass
*gobject_class
= G_OBJECT_CLASS (class);
911 class->subscribe
= ignore_subscription
;
912 class->unsubscribe
= ignore_subscription
;
914 gobject_class
->finalize
= g_settings_backend_finalize
;
916 g_type_class_add_private (class, sizeof (GSettingsBackendPrivate
));
920 g_settings_backend_variant_unref0 (gpointer data
)
923 g_variant_unref (data
);
927 * g_settings_backend_create_tree:
929 * This is a convenience function for creating a tree that is compatible
930 * with g_settings_backend_write(). It merely calls g_tree_new_full()
931 * with strcmp(), g_free() and g_variant_unref().
933 * Returns: a new #GTree
936 g_settings_backend_create_tree (void)
938 return g_tree_new_full ((GCompareDataFunc
) strcmp
, NULL
,
939 g_free
, g_settings_backend_variant_unref0
);
943 g_settings_backend_verify (gpointer impl
)
945 GSettingsBackend
*backend
= impl
;
947 if (strcmp (G_OBJECT_TYPE_NAME (backend
), "GMemorySettingsBackend") == 0 &&
948 g_strcmp0 (g_getenv ("GSETTINGS_BACKEND"), "memory") != 0)
950 g_message ("Using the 'memory' GSettings backend. Your settings "
951 "will not be saved or shared with other applications.");
954 g_settings_has_backend
= TRUE
;
959 * g_settings_backend_get_default:
961 * Returns the default #GSettingsBackend. It is possible to override
962 * the default by setting the <envar>GSETTINGS_BACKEND</envar>
963 * environment variable to the name of a settings backend.
965 * The user gets a reference to the backend.
967 * Returns: (transfer full): the default #GSettingsBackend
972 g_settings_backend_get_default (void)
974 GSettingsBackend
*backend
;
976 backend
= _g_io_module_get_default (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME
,
978 g_settings_backend_verify
);
979 return g_object_ref (backend
);
983 * g_settings_backend_get_permission:
984 * @backend: a #GSettingsBackend
987 * Gets the permission object associated with writing to keys below
990 * If this is not implemented in the backend, then a %TRUE
991 * #GSimplePermission is returned.
993 * Returns: a non-%NULL #GPermission. Free with g_object_unref()
996 g_settings_backend_get_permission (GSettingsBackend
*backend
,
999 GSettingsBackendClass
*class = G_SETTINGS_BACKEND_GET_CLASS (backend
);
1001 if (class->get_permission
)
1002 return class->get_permission (backend
, path
);
1004 return g_simple_permission_new (TRUE
);
1008 * g_settings_backend_sync_default:
1010 * Syncs the default backend.
1013 g_settings_backend_sync_default (void)
1015 if (g_settings_has_backend
)
1017 GSettingsBackendClass
*class;
1018 GSettingsBackend
*backend
;
1020 backend
= g_settings_backend_get_default ();
1021 class = G_SETTINGS_BACKEND_GET_CLASS (backend
);
1024 class->sync (backend
);