* 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.
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Authors: Ryan Lortie <desrt@desrt.ca>
* Matthias Clasen <mclasen@redhat.com>
#include "config.h"
#include "gsettingsbackendinternal.h"
-#include "gnullsettingsbackend.h"
#include "gsimplepermission.h"
#include "giomodule-priv.h"
-#include "gio-marshal.h"
#include <string.h>
#include <stdlib.h>
#include <glib.h>
#include <glibintl.h>
-#include "gioalias.h"
-
-G_DEFINE_ABSTRACT_TYPE (GSettingsBackend, g_settings_backend, G_TYPE_OBJECT)
typedef struct _GSettingsBackendClosure GSettingsBackendClosure;
typedef struct _GSettingsBackendWatch GSettingsBackendWatch;
struct _GSettingsBackendPrivate
{
GSettingsBackendWatch *watches;
- GStaticMutex lock;
- gchar *context;
+ GMutex lock;
};
-enum
-{
- PROP_0,
- PROP_CONTEXT
-};
+G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GSettingsBackend, g_settings_backend, G_TYPE_OBJECT)
+
+/* For g_settings_backend_sync_default(), we only want to actually do
+ * the sync if the backend already exists. This avoids us creating an
+ * entire GSettingsBackend in order to call a do-nothing sync()
+ * operation on it. This variable lets us avoid that.
+ */
+static gboolean g_settings_has_backend;
/**
* SECTION:gsettingsbackend
* @title: GSettingsBackend
- * @short_description: an interface for settings backend implementations
+ * @short_description: Interface for settings backend implementations
* @include: gio/gsettingsbackend.h
* @see_also: #GSettings, #GIOExtensionPoint
*
* g_settings_backend_create_tree() is a convenience function to create
* suitable trees.
*
- * <note><para>
- * The #GSettingsBackend API is exported to allow third-party
+ * 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>
+ * C preprocessor symbol %G_SETTINGS_ENABLE_BACKEND before including
+ * `gio/gsettingsbackend.h`.
**/
static gboolean
return TRUE;
}
-GMainContext *
-g_settings_backend_get_active_context (void)
-{
- GMainContext *context;
- GSource *source;
-
- if ((source = g_main_current_source ()))
- context = g_source_get_context (source);
-
- else
- {
- context = g_main_context_get_thread_default ();
-
- if (context == NULL)
- context = g_main_context_default ();
- }
-
- return context;
-}
-
struct _GSettingsBackendWatch
{
- GObject *target;
- GMainContext *context;
- GSettingsBackendChangedFunc changed;
- GSettingsBackendPathChangedFunc path_changed;
- GSettingsBackendKeysChangedFunc keys_changed;
- GSettingsBackendWritableChangedFunc writable_changed;
- GSettingsBackendPathWritableChangedFunc path_writable_changed;
-
- GSettingsBackendWatch *next;
+ GObject *target;
+ const GSettingsListenerVTable *vtable;
+ GMainContext *context;
+ GSettingsBackendWatch *next;
};
struct _GSettingsBackendClosure
{
- void (*function) (GSettingsBackend *backend,
- GObject *target,
- const gchar *name,
- gpointer data1,
- gpointer data2);
-
- GSettingsBackend *backend;
- GObject *target;
- gchar *name;
- gpointer data1;
- GBoxedFreeFunc data1_free;
- gpointer data2;
+ void (*function) (GObject *target,
+ GSettingsBackend *backend,
+ const gchar *name,
+ gpointer origin_tag,
+ gchar **names);
+
+ GMainContext *context;
+ GObject *target;
+ GSettingsBackend *backend;
+ gchar *name;
+ gpointer origin_tag;
+ gchar **names;
};
static void
GSettingsBackendWatch **ptr;
/* search and remove */
- g_static_mutex_lock (&backend->priv->lock);
+ g_mutex_lock (&backend->priv->lock);
for (ptr = &backend->priv->watches; *ptr; ptr = &(*ptr)->next)
if ((*ptr)->target == where_the_object_was)
{
*ptr = tmp->next;
g_slice_free (GSettingsBackendWatch, tmp);
- g_static_mutex_unlock (&backend->priv->lock);
+ g_mutex_unlock (&backend->priv->lock);
return;
}
* g_settings_backend_watch:
* @backend: a #GSettingsBackend
* @target: the GObject (typically GSettings instance) to call back to
- * @context: a #GMainContext, or %NULL
+ * @context: (allow-none): a #GMainContext, or %NULL
* ...: callbacks...
*
* Registers a new watch on a #GSettingsBackend.
* 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
+ * occurred 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.
*
* value of @origin_tag given to any callbacks.
**/
void
-g_settings_backend_watch (GSettingsBackend *backend,
- GObject *target,
- GMainContext *context,
- GSettingsBackendChangedFunc changed,
- GSettingsBackendPathChangedFunc path_changed,
- GSettingsBackendKeysChangedFunc keys_changed,
- GSettingsBackendWritableChangedFunc writable_changed,
- GSettingsBackendPathWritableChangedFunc path_writable_changed)
+g_settings_backend_watch (GSettingsBackend *backend,
+ const GSettingsListenerVTable *vtable,
+ GObject *target,
+ GMainContext *context)
{
GSettingsBackendWatch *watch;
watch = g_slice_new (GSettingsBackendWatch);
watch->context = context;
+ watch->vtable = vtable;
watch->target = target;
g_object_weak_ref (target, g_settings_backend_watch_weak_notify, backend);
- watch->changed = changed;
- watch->path_changed = path_changed;
- watch->keys_changed = keys_changed;
- watch->writable_changed = writable_changed;
- watch->path_writable_changed = path_writable_changed;
-
/* linked list prepend */
- g_static_mutex_lock (&backend->priv->lock);
+ g_mutex_lock (&backend->priv->lock);
watch->next = backend->priv->watches;
backend->priv->watches = watch;
- g_static_mutex_unlock (&backend->priv->lock);
+ g_mutex_unlock (&backend->priv->lock);
}
void
{
GSettingsBackendClosure *closure = user_data;
- closure->function (closure->backend, closure->target, closure->name,
- closure->data1, closure->data2);
+ closure->function (closure->target, closure->backend, closure->name,
+ closure->origin_tag, closure->names);
- closure->data1_free (closure->data1);
g_object_unref (closure->backend);
g_object_unref (closure->target);
+ g_strfreev (closure->names);
g_free (closure->name);
g_slice_free (GSettingsBackendClosure, closure);
return FALSE;
}
-static gpointer
-pointer_id (gpointer a)
-{
- return a;
-}
-
-static void
-pointer_ignore (gpointer a)
-{
-}
-
static void
-g_settings_backend_dispatch_signal (GSettingsBackend *backend,
- gsize function_offset,
- const gchar *name,
- gpointer data1,
- GBoxedCopyFunc data1_copy,
- GBoxedFreeFunc data1_free,
- gpointer data2)
+g_settings_backend_dispatch_signal (GSettingsBackend *backend,
+ gsize function_offset,
+ const gchar *name,
+ gpointer origin_tag,
+ const gchar * const *names)
{
- GMainContext *context, *here_and_now;
GSettingsBackendWatch *watch;
+ GSList *closures = NULL;
- /* We need to hold the mutex here (to prevent a node from being
- * deleted as we are traversing the list). Since we should not
- * re-enter user code while holding this mutex, we create a
- * one-time-use GMainContext and populate it with the events that we
- * would have called directly. We dispatch these events after
- * releasing the lock. Note that the GObject reference is acquired on
- * the target while holding the mutex and the mutex needs to be held
- * as part of the destruction of any GSettings instance (via the weak
- * reference handling). This is the key to the safety of the whole
- * setup.
+ /* 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.
+ *
+ * We work around this by creating a bunch of GSettingsBackendClosure
+ * objects while holding the lock and dispatching them after. We
+ * never touch the list without holding the lock.
*/
-
- if (data1_copy == NULL)
- data1_copy = pointer_id;
-
- if (data1_free == NULL)
- data1_free = pointer_ignore;
-
- context = g_settings_backend_get_active_context ();
- here_and_now = g_main_context_new ();
-
- /* traverse the (immutable while holding lock) list */
- g_static_mutex_lock (&backend->priv->lock);
+ g_mutex_lock (&backend->priv->lock);
for (watch = backend->priv->watches; watch; watch = watch->next)
{
GSettingsBackendClosure *closure;
- GSource *source;
closure = g_slice_new (GSettingsBackendClosure);
+ closure->context = watch->context;
closure->backend = g_object_ref (backend);
closure->target = g_object_ref (watch->target);
- closure->function = G_STRUCT_MEMBER (void *, watch, function_offset);
+ closure->function = G_STRUCT_MEMBER (void *, watch->vtable,
+ function_offset);
closure->name = g_strdup (name);
- closure->data1 = data1_copy (data1);
- closure->data1_free = data1_free;
- closure->data2 = data2;
-
- source = g_idle_source_new ();
- g_source_set_priority (source, G_PRIORITY_DEFAULT);
- g_source_set_callback (source,
- g_settings_backend_invoke_closure,
- closure, NULL);
-
- if (watch->context && watch->context != context)
- g_source_attach (source, watch->context);
- else
- g_source_attach (source, here_and_now);
+ closure->origin_tag = origin_tag;
+ closure->names = g_strdupv ((gchar **) names);
- g_source_unref (source);
+ closures = g_slist_prepend (closures, closure);
}
- g_static_mutex_unlock (&backend->priv->lock);
+ g_mutex_unlock (&backend->priv->lock);
+
+ while (closures)
+ {
+ GSettingsBackendClosure *closure = closures->data;
+
+ if (closure->context)
+ g_main_context_invoke (closure->context,
+ g_settings_backend_invoke_closure,
+ closure);
+ else
+ g_settings_backend_invoke_closure (closure);
- while (g_main_context_iteration (here_and_now, FALSE));
- g_main_context_unref (here_and_now);
+ closures = g_slist_delete_link (closures, closures);
+ }
}
/**
* implementations should call this if a key has possibly changed its
* value.
*
- * @key must be a valid key (ie: starting with a slash, not containing
+ * @key must be a valid key (ie starting with a slash, not containing
* '//', and not ending with a slash).
*
* The implementation must call this function during any call to
* 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
+ * in response to other events (such as changes occurring 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()).
g_return_if_fail (is_key (key));
g_settings_backend_dispatch_signal (backend,
- G_STRUCT_OFFSET (GSettingsBackendWatch,
+ G_STRUCT_OFFSET (GSettingsListenerVTable,
changed),
- key, origin_tag, NULL, NULL, NULL);
+ key, origin_tag, NULL);
}
/**
* g_settings_backend_keys_changed:
* @backend: a #GSettingsBackend implementation
* @path: the path containing the changes
- * @items: the %NULL-terminated list of changed keys
+ * @items: (array zero-terminated=1): 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.
*
- * @path must be a valid path (ie: starting and ending with a slash and
+ * @path must be a valid path (ie starting and ending with a slash and
* 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 '//').
g_return_if_fail (items != NULL);
g_settings_backend_dispatch_signal (backend,
- G_STRUCT_OFFSET (GSettingsBackendWatch,
+ G_STRUCT_OFFSET (GSettingsListenerVTable,
keys_changed),
- path, (gpointer) items,
- (GBoxedCopyFunc) g_strdupv,
- (GBoxedFreeFunc) g_strfreev,
- origin_tag);
+ path, origin_tag, items);
}
/**
* Backend implementations should call this if an entire path of keys
* have possibly changed their values.
*
- * @path must be a valid path (ie: starting and ending with a slash and
+ * @path must be a valid path (ie starting and ending with a slash and
* not containing '//').
*
* The meaning of this signal is that any of the key which has a name
g_return_if_fail (is_path (path));
g_settings_backend_dispatch_signal (backend,
- G_STRUCT_OFFSET (GSettingsBackendWatch,
+ G_STRUCT_OFFSET (GSettingsListenerVTable,
path_changed),
- path, origin_tag, NULL, NULL, NULL);
+ path, origin_tag, NULL);
}
/**
g_return_if_fail (is_key (key));
g_settings_backend_dispatch_signal (backend,
- G_STRUCT_OFFSET (GSettingsBackendWatch,
+ G_STRUCT_OFFSET (GSettingsListenerVTable,
writable_changed),
- key, NULL, NULL, NULL, NULL);
+ key, NULL, NULL);
}
/**
g_return_if_fail (is_path (path));
g_settings_backend_dispatch_signal (backend,
- G_STRUCT_OFFSET (GSettingsBackendWatch,
+ G_STRUCT_OFFSET (GSettingsListenerVTable,
path_writable_changed),
- path, NULL, NULL, NULL, NULL);
+ path, NULL, NULL);
}
typedef struct
/**
* 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
+ * @path: (out): the location to save the path
+ * @keys: (out) (transfer container) (array zero-terminated=1): the
+ * location to save the relative keys
+ * @values: (out) (allow-none) (transfer container) (array zero-terminated=1):
+ * 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,
GTree *tree,
gpointer origin_tag)
{
- GSettingsBackendWatch *watch;
const gchar **keys;
gchar *path;
g_settings_backend_flatten_tree (tree, &path, &keys, NULL);
- for (watch = backend->priv->watches; watch; watch = watch->next)
- watch->keys_changed (backend, watch->target, path, keys, origin_tag);
+#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
+ g_settings_backend_keys_changed (backend, path, keys, origin_tag);
g_free (path);
g_free (keys);
}
* g_settings_backend_read:
* @backend: a #GSettingsBackend implementation
* @key: the key to read
- * @expected_type: a #GVariantType hint
- * @returns: the value that was read, or %NULL
+ * @expected_type: a #GVariantType
+ * @default_value: if the default value should be returned
*
* 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.
*
- * If @expected_type is given, it serves as a type hint to the backend.
- * If you expect a key of a certain type then you should give
- * @expected_type to increase your chances of getting it. Some backends
- * may ignore this argument and return values of a different type; it is
- * mostly used by backends that don't store strong type information.
+ * 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).
+ *
+ * Returns: the value that was read, or %NULL
*/
GVariant *
g_settings_backend_read (GSettingsBackend *backend,
const GVariantType *expected_type,
gboolean default_value)
{
- return G_SETTINGS_BACKEND_GET_CLASS (backend)
+ GVariant *value;
+
+ value = G_SETTINGS_BACKEND_GET_CLASS (backend)
->read (backend, key, expected_type, default_value);
+
+ if (value != NULL)
+ value = g_variant_take_ref (value);
+
+ if G_UNLIKELY (value && !g_variant_is_of_type (value, expected_type))
+ {
+ g_variant_unref (value);
+ value = NULL;
+ }
+
+ return value;
+}
+
+/*< private >
+ * g_settings_backend_read_user_value:
+ * @backend: a #GSettingsBackend implementation
+ * @key: the key to read
+ * @expected_type: a #GVariantType
+ *
+ * Reads the 'user value' of a key.
+ *
+ * This is the value of the key that the user has control over and has
+ * set for themselves. Put another way: if the user did not set the
+ * value for themselves, then this will return %NULL (even if the
+ * sysadmin has provided a default value).
+ *
+ * Returns: the value that was read, or %NULL
+ */
+GVariant *
+g_settings_backend_read_user_value (GSettingsBackend *backend,
+ const gchar *key,
+ const GVariantType *expected_type)
+{
+ GVariant *value;
+
+ value = G_SETTINGS_BACKEND_GET_CLASS (backend)
+ ->read_user_value (backend, key, expected_type);
+
+ if (value != NULL)
+ value = g_variant_take_ref (value);
+
+ if G_UNLIKELY (value && !g_variant_is_of_type (value, expected_type))
+ {
+ g_variant_unref (value);
+ value = NULL;
+ }
+
+ return value;
}
/*< private >
* @key: the name of the key
* @value: a #GVariant value to write to this key
* @origin_tag: the origin tag
- * @returns: %TRUE if the write succeeded, %FALSE if the key was not writable
*
* Writes exactly one key.
*
* 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.
+ *
+ * Returns: %TRUE if the write succeeded, %FALSE if the key was not writable
*/
gboolean
g_settings_backend_write (GSettingsBackend *backend,
GVariant *value,
gpointer origin_tag)
{
- return G_SETTINGS_BACKEND_GET_CLASS (backend)
+ gboolean success;
+
+ g_variant_ref_sink (value);
+ success = G_SETTINGS_BACKEND_GET_CLASS (backend)
->write (backend, key, value, origin_tag);
+ g_variant_unref (value);
+
+ return success;
}
/*< private >
- * g_settings_backend_write_keys:
+ * g_settings_backend_write_tree:
* @backend: a #GSettingsBackend implementation
- * @values: a #GTree containing key-value pairs to write
+ * @tree: a #GTree containing key-value pairs to write
* @origin_tag: the origin tag
*
* Writes one or more keys. This call will never block.
* old values.
*/
gboolean
-g_settings_backend_write_keys (GSettingsBackend *backend,
+g_settings_backend_write_tree (GSettingsBackend *backend,
GTree *tree,
gpointer origin_tag)
{
return G_SETTINGS_BACKEND_GET_CLASS (backend)
- ->write_keys (backend, tree, origin_tag);
+ ->write_tree (backend, tree, origin_tag);
}
/*< private >
}
/*< private >
- * g_settings_backend_reset_path:
- * @backend: a #GSettingsBackend implementation
- * @name: the name of a key or path
- * @origin_tag: the origin tag
- *
- * "Resets" the named path. This means that every key under the path is
- * reset.
- */
-void
-g_settings_backend_reset_path (GSettingsBackend *backend,
- const gchar *path,
- gpointer origin_tag)
-{
- G_SETTINGS_BACKEND_GET_CLASS (backend)
- ->reset_path (backend, path, origin_tag);
-}
-
-/*< private >
* g_settings_backend_get_writable:
* @backend: a #GSettingsBackend implementation
* @key: the name of a key
- * @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
*
* You should not write to locked-down keys, but if you do, the
* implementation will deal with it.
+ *
+ * Returns: %TRUE if the key is writable
*/
gboolean
g_settings_backend_get_writable (GSettingsBackend *backend,
}
static void
-g_settings_backend_set_property (GObject *object,
- guint prop_id,
- const GValue *value,
- GParamSpec *pspec)
-{
- GSettingsBackend *backend = G_SETTINGS_BACKEND (object);
-
- switch (prop_id)
- {
- case PROP_CONTEXT:
- backend->priv->context = g_value_dup_string (value);
- break;
-
- default:
- G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
- break;
- }
-}
-
-static void
-g_settings_backend_get_property (GObject *object,
- guint prop_id,
- GValue *value,
- GParamSpec *pspec)
-{
- GSettingsBackend *backend = G_SETTINGS_BACKEND (object);
-
- switch (prop_id)
- {
- case PROP_CONTEXT:
- g_value_set_string (value, backend->priv->context);
- break;
-
- default:
- G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
- break;
- }
-}
-
-static void
g_settings_backend_finalize (GObject *object)
{
GSettingsBackend *backend = G_SETTINGS_BACKEND (object);
- g_static_mutex_unlock (&backend->priv->lock);
- g_free (backend->priv->context);
+ g_mutex_clear (&backend->priv->lock);
G_OBJECT_CLASS (g_settings_backend_parent_class)
->finalize (object);
{
}
+static GVariant *
+g_settings_backend_real_read_user_value (GSettingsBackend *backend,
+ const gchar *key,
+ const GVariantType *expected_type)
+{
+ return g_settings_backend_read (backend, key, expected_type, FALSE);
+}
+
static void
g_settings_backend_init (GSettingsBackend *backend)
{
- backend->priv = G_TYPE_INSTANCE_GET_PRIVATE (backend,
- G_TYPE_SETTINGS_BACKEND,
- GSettingsBackendPrivate);
- g_static_mutex_init (&backend->priv->lock);
+ backend->priv = g_settings_backend_get_instance_private (backend);
+ g_mutex_init (&backend->priv->lock);
}
static void
class->subscribe = ignore_subscription;
class->unsubscribe = ignore_subscription;
- gobject_class->get_property = g_settings_backend_get_property;
- gobject_class->set_property = g_settings_backend_set_property;
- gobject_class->finalize = g_settings_backend_finalize;
-
- g_type_class_add_private (class, sizeof (GSettingsBackendPrivate));
+ class->read_user_value = g_settings_backend_real_read_user_value;
- /**
- * GSettingsBackend:context:
- *
- * The "context" property gives a hint to the backend as to
- * what storage to use. It is up to the implementation to make
- * use of this information.
- *
- * E.g. DConf supports "user", "system", "defaults" and "login"
- * contexts.
- *
- * If your backend supports different contexts, you should also
- * provide an implementation of the supports_context() class
- * function in #GSettingsBackendClass.
- */
- g_object_class_install_property (gobject_class, PROP_CONTEXT,
- g_param_spec_string ("context", P_("Context"),
- P_("An identifier to decide which storage to use"),
- "", G_PARAM_READWRITE |
- G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
+ gobject_class->finalize = g_settings_backend_finalize;
+}
+static void
+g_settings_backend_variant_unref0 (gpointer data)
+{
+ if (data != NULL)
+ g_variant_unref (data);
}
/*< 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().
+ *
+ * Returns: a new #GTree
*/
GTree *
g_settings_backend_create_tree (void)
{
return g_tree_new_full ((GCompareDataFunc) strcmp, NULL,
- g_free, (GDestroyNotify) g_variant_unref);
+ g_free, g_settings_backend_variant_unref0);
}
-
-static gpointer
-get_default_backend (const gchar *context)
+static gboolean
+g_settings_backend_verify (gpointer impl)
{
- GIOExtension *extension = NULL;
- GIOExtensionPoint *point;
- GList *extensions;
- const gchar *env;
- GType type;
-
- _g_io_modules_ensure_loaded ();
-
- point = g_io_extension_point_lookup (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME);
-
- if ((env = getenv ("GSETTINGS_BACKEND")))
- {
- extension = g_io_extension_point_get_extension_by_name (point, env);
-
- if (extension == NULL)
- g_warning ("Can't find GSettings backend '%s' given in "
- "GSETTINGS_BACKEND environment variable", env);
- }
-
- if (extension == NULL)
- {
- extensions = g_io_extension_point_get_extensions (point);
-
- if (extensions == NULL)
- g_error ("No GSettingsBackend implementations exist.");
-
- extension = extensions->data;
- }
+ GSettingsBackend *backend = impl;
- if (context[0] != '\0') /* (context != "") */
+ if (strcmp (G_OBJECT_TYPE_NAME (backend), "GMemorySettingsBackend") == 0 &&
+ g_strcmp0 (g_getenv ("GSETTINGS_BACKEND"), "memory") != 0)
{
- GSettingsBackendClass *backend_class;
- GTypeClass *class;
-
- class = g_io_extension_ref_class (extension);
- backend_class = G_SETTINGS_BACKEND_CLASS (class);
-
- if (backend_class->supports_context == NULL ||
- !backend_class->supports_context (context))
- {
- g_type_class_unref (class);
- return NULL;
- }
-
- g_type_class_unref (class);
+ g_message ("Using the 'memory' GSettings backend. Your settings "
+ "will not be saved or shared with other applications.");
}
- type = g_io_extension_get_type (extension);
-
- return g_object_new (type, "context", context, NULL);
+ g_settings_has_backend = TRUE;
+ return TRUE;
}
-static GHashTable *g_settings_backends;
-
-/*< private >
- * g_settings_backend_get_with_context:
- * @context: a context that might be used by the backend to determine
- * which storage to use, or %NULL to use the default storage
- * @returns: the default #GSettingsBackend
+/**
+ * g_settings_backend_get_default:
*
* 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.
+ * the default by setting the `GSETTINGS_BACKEND` environment variable
+ * to the name of a settings backend.
*
- * The @context parameter can be used to indicate that a different
- * than the default storage is desired. E.g. the DConf backend lets
- * you use "user", "system", "defaults" and "login" as contexts.
+ * The user gets a reference to the backend.
*
- * If @context is not supported by the implementation, this function
- * returns an instance of the #GSettingsMemoryBackend.
- * See g_settings_backend_supports_context(),
+ * Returns: (transfer full): the default #GSettingsBackend
*
- * The user does not own the return value and it must not be freed.
+ * Since: 2.28
*/
GSettingsBackend *
-g_settings_backend_get_with_context (const gchar *context)
+g_settings_backend_get_default (void)
{
GSettingsBackend *backend;
- g_return_val_if_fail (context != NULL, NULL);
-
- _g_io_modules_ensure_extension_points_registered ();
-
- if (g_settings_backends == NULL)
- g_settings_backends = g_hash_table_new (g_str_hash, g_str_equal);
-
- backend = g_hash_table_lookup (g_settings_backends, context);
-
- if (!backend)
- {
- backend = get_default_backend (context);
-
- if (!backend)
- backend = g_null_settings_backend_new ();
-
- g_hash_table_insert (g_settings_backends, g_strdup (context), backend);
- }
-
+ backend = _g_io_module_get_default (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME,
+ "GSETTINGS_BACKEND",
+ g_settings_backend_verify);
return g_object_ref (backend);
}
/*< private >
- * g_settings_backend_supports_context:
- * @context: a context string that might be passed to
- * g_settings_backend_new_with_context()
- * @returns: #TRUE if @context is supported
- *
- * Determines if the given context is supported by the default
- * GSettingsBackend implementation.
- */
-gboolean
-g_settings_backend_supports_context (const gchar *context)
-{
- GSettingsBackend *backend;
-
- g_return_val_if_fail (context != NULL, FALSE);
-
- backend = get_default_backend (context);
-
- if (backend)
- {
- g_object_unref (backend);
- return TRUE;
- }
-
- return FALSE;
-}
-
-/*< private >
* g_settings_backend_get_permission:
* @backend: a #GSettingsBackend
* @path: a path
- * @returns: a non-%NULL #GPermission
*
* 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.
+ *
+ * Returns: a non-%NULL #GPermission. Free with g_object_unref()
*/
GPermission *
g_settings_backend_get_permission (GSettingsBackend *backend,
return g_simple_permission_new (TRUE);
}
-/**
- * g_settings_backend_setup:
- * @context: a context string (not %NULL or "")
- * @backend: a #GSettingsBackend
- *
- * Sets up @backend for use with #GSettings.
- *
- * If you create a #GSettings with its context property set to @context
- * then it will use the backend given to this function. See
- * g_settings_new_with_context().
- *
- * The backend must be set up before any settings objects are created
- * for the named context.
- *
- * It is not possible to specify a backend for the default context.
- *
- * This function takes a reference on @backend and never releases it.
+/*< private >
+ * g_settings_backend_sync_default:
*
- * Since: 2.26
- **/
+ * Syncs the default backend.
+ */
void
-g_settings_backend_setup (const gchar *context,
- GSettingsBackend *backend)
+g_settings_backend_sync_default (void)
{
- g_return_if_fail (context[0] != '\0');
- g_return_if_fail (G_IS_SETTINGS_BACKEND (backend));
-
- if (g_settings_backends == NULL)
- g_settings_backends = g_hash_table_new (g_str_hash, g_str_equal);
+ if (g_settings_has_backend)
+ {
+ GSettingsBackendClass *class;
+ GSettingsBackend *backend;
- if (g_hash_table_lookup (g_settings_backends, context))
- g_error ("A GSettingsBackend already exists for context '%s'", context);
+ backend = g_settings_backend_get_default ();
+ class = G_SETTINGS_BACKEND_GET_CLASS (backend);
- g_hash_table_insert (g_settings_backends,
- g_strdup (context),
- g_object_ref (backend));
+ if (class->sync)
+ class->sync (backend);
+ }
}
-
-#define __G_SETTINGS_BACKEND_C__
-#include "gioaliasdef.c"