Bug 621266 - GSettings "context" clarification

Remove the concept of "context" in favour of dealing with
GSettingsBackend directly.
This commit is contained in:
Ryan Lortie 2010-06-17 14:05:40 -04:00
parent b3cc28bc34
commit 6c3ae976e6
9 changed files with 126 additions and 290 deletions

View File

@ -1420,12 +1420,7 @@ g_file_descriptor_based_get_fd
#endif
#if IN_HEADER(__G_SETTINGS_BACKEND_H__)
#if IN_FILE(__G_KEYFILE_SETTINGS_BACKEND_C__)
g_settings_backend_setup_keyfile
#endif
#if IN_FILE(__G_SETTINGS_BACKEND_C__)
g_settings_backend_setup
g_settings_backend_get_type
g_settings_backend_changed
g_settings_backend_flatten_tree
@ -1451,8 +1446,8 @@ g_settings_get_type
g_settings_get_value
g_settings_is_writable
g_settings_new
g_settings_new_with_context
g_settings_new_with_context_and_path
g_settings_new_with_backend
g_settings_new_with_backend_and_path
g_settings_new_with_path
g_settings_revert
g_settings_set

View File

@ -515,7 +515,7 @@ g_keyfile_settings_backend_class_init (GKeyfileSettingsBackendClass *class)
g_type_class_add_private (class, sizeof (GKeyfileSettingsBackendPrivate));
}
static GKeyfileSettingsBackend *
GSettingsBackend *
g_keyfile_settings_backend_new (const gchar *filename)
{
GKeyfileSettingsBackend *kf_backend;
@ -536,42 +536,7 @@ g_keyfile_settings_backend_new (const gchar *filename)
g_keyfile_settings_backend_keyfile_reload (kf_backend);
return kf_backend;
}
/**
* g_settings_backend_setup_keyfile:
* @context: a context string (not %NULL or "")
* @filename: a filename
*
* Sets up a keyfile for use with #GSettings.
*
* If you create a #GSettings with its context property set to @context
* then the settings will be stored in the keyfile at @filename. See
* g_settings_new_with_context().
*
* The keyfile must be setup before any settings objects are created
* for the named context.
*
* It is not possible to specify a keyfile for the default context.
*
* If the path leading up to @filename does not exist, it will be
* recursively created with user-only permissions. If the keyfile is
* not writable, any #GSettings objects created using @context will
* return %FALSE for any calls to g_settings_is_writable() and any
* attempts to write will fail.
*
* Since: 2.26
*/
void
g_settings_backend_setup_keyfile (const gchar *context,
const gchar *filename)
{
GKeyfileSettingsBackend *kf_backend;
kf_backend = g_keyfile_settings_backend_new (filename);
g_settings_backend_setup (context, G_SETTINGS_BACKEND (kf_backend));
g_object_unref (kf_backend);
return G_SETTINGS_BACKEND (kf_backend);
}
#define __G_KEYFILE_SETTINGS_BACKEND_C__

View File

@ -236,7 +236,6 @@ struct _GSettingsPrivate
GSettingsBackend *backend;
GSettingsSchema *schema;
gchar *schema_name;
gchar *context;
gchar *path;
GDelayedSettingsBackend *delayed;
@ -245,9 +244,8 @@ struct _GSettingsPrivate
enum
{
PROP_0,
PROP_BACKEND,
PROP_SCHEMA,
PROP_CONTEXT,
PROP_BACKEND,
PROP_PATH,
PROP_HAS_UNAPPLIED,
};
@ -446,8 +444,8 @@ g_settings_set_property (GObject *object,
settings->priv->path = g_value_dup_string (value);
break;
case PROP_CONTEXT:
settings->priv->context = g_value_dup_string (value);
case PROP_BACKEND:
settings->priv->backend = g_value_dup_object (value);
break;
default:
@ -501,7 +499,9 @@ g_settings_constructed (GObject *object)
settings->priv->path = g_strdup (schema_path);
}
settings->priv->backend = g_settings_backend_get_with_context (settings->priv->context);
if (settings->priv->backend == NULL)
settings->priv->backend = g_settings_backend_get_default ();
g_settings_backend_watch (settings->priv->backend, G_OBJECT (settings),
settings->priv->main_context,
settings_backend_changed,
@ -524,7 +524,6 @@ g_settings_finalize (GObject *object)
g_object_unref (settings->priv->backend);
g_object_unref (settings->priv->schema);
g_free (settings->priv->schema_name);
g_free (settings->priv->context);
g_free (settings->priv->path);
}
@ -668,11 +667,11 @@ g_settings_class_init (GSettingsClass *class)
*
* The name of the context that the settings are stored in.
*/
g_object_class_install_property (object_class, PROP_CONTEXT,
g_param_spec_string ("context",
P_("Context name"),
P_("The name of the context for this settings object"),
"", G_PARAM_CONSTRUCT_ONLY |
g_object_class_install_property (object_class, PROP_BACKEND,
g_param_spec_object ("backend",
P_("GSettingsBackend"),
P_("The GSettingsBackend for this settings object"),
G_TYPE_SETTINGS_BACKEND, G_PARAM_CONSTRUCT_ONLY |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
/**
@ -773,66 +772,63 @@ g_settings_new_with_path (const gchar *schema,
}
/**
* g_settings_new_with_context:
* g_settings_new_with_backend:
* @schema: the name of the schema
* @context: the context to use
* @backend: the #GSettingsBackend to use
* @returns: a new #GSettings object
*
* Creates a new #GSettings object with a given schema and context.
* Creates a new #GSettings object with a given schema and backend.
*
* Creating settings objects with a context allow accessing settings
* Creating settings objects with an different backend allows accessing settings
* from a database other than the usual one. For example, it may make
* sense to specify "defaults" in order to get a settings object that
* modifies the system default settings instead of the settings for this
* user.
*
* It is a programmer error to call this function for an unsupported
* context. Use g_settings_supports_context() to determine if a context
* is supported if you are unsure.
* sense to pass a backend corresponding to teh "defaults" settings database on
* the system to get a settings object that modifies the system default
* settings instead of the settings for this user.
*
* Since: 2.26
*/
GSettings *
g_settings_new_with_context (const gchar *schema,
const gchar *context)
g_settings_new_with_backend (const gchar *schema,
GSettingsBackend *backend)
{
g_return_val_if_fail (schema != NULL, NULL);
g_return_val_if_fail (context != NULL, NULL);
g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
return g_object_new (G_TYPE_SETTINGS,
"schema", schema,
"context", context,
"backend", backend,
NULL);
}
/**
* g_settings_new_with_context_and_path:
* g_settings_new_with_backend_and_path:
* @schema: the name of the schema
* @backend: the #GSettingsBackend to use
* @path: the path to use
* @returns: a new #GSettings object
*
* Creates a new #GSettings object with a given schema, context and
* Creates a new #GSettings object with a given schema, backend and
* path.
*
* This is a mix of g_settings_new_with_context() and
* This is a mix of g_settings_new_with_backend() and
* g_settings_new_with_path().
*
* Since: 2.26
*/
GSettings *
g_settings_new_with_context_and_path (const gchar *schema,
const gchar *context,
const gchar *path)
g_settings_new_with_backend_and_path (const gchar *schema,
GSettingsBackend *backend,
const gchar *path)
{
g_return_val_if_fail (schema != NULL, NULL);
g_return_val_if_fail (context != NULL, NULL);
g_return_val_if_fail (G_IS_SETTINGS_BACKEND (backend), NULL);
g_return_val_if_fail (path != NULL, NULL);
return g_object_new (G_TYPE_SETTINGS,
"schema", schema,
"context", context,
"path", path,
NULL);
"backend", backend,
"path", path,
NULL);
}
/* Internal read/write utilities, enum conversion, validation {{{1 */
@ -1784,15 +1780,9 @@ g_settings_get_has_unapplied (GSettings *settings)
* time the call is done).
**/
void
g_settings_sync (const gchar *context)
g_settings_sync (void)
{
GSettingsBackend *backend;
if (context == NULL)
context = "";
backend = g_settings_backend_get_with_context (context);
g_settings_backend_sync (backend);
g_settings_backend_sync_default ();
}
/**

View File

@ -73,11 +73,10 @@ GType g_settings_get_type (void);
GSettings * g_settings_new (const gchar *schema);
GSettings * g_settings_new_with_path (const gchar *schema,
const gchar *path);
gboolean g_settings_supports_context (const gchar *context);
GSettings * g_settings_new_with_context (const gchar *schema,
const gchar *context);
GSettings * g_settings_new_with_context_and_path (const gchar *schema,
const gchar *context,
GSettings * g_settings_new_with_backend (const gchar *schema,
GSettingsBackend *backend);
GSettings * g_settings_new_with_backend_and_path (const gchar *schema,
GSettingsBackend *backend,
const gchar *path);
gboolean g_settings_set_value (GSettings *settings,
@ -135,7 +134,7 @@ void g_settings_delay (GSettin
void g_settings_apply (GSettings *settings);
void g_settings_revert (GSettings *settings);
gboolean g_settings_get_has_unapplied (GSettings *settings);
void g_settings_sync (const gchar *context);
void g_settings_sync ();
/**
* GSettingsBindSetMapping:

View File

@ -1038,136 +1038,61 @@ g_settings_backend_create_tree (void)
g_free, (GDestroyNotify) g_variant_unref);
}
static gpointer
get_default_backend (const gchar *context)
{
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;
}
if (context[0] != '\0') /* (context != "") */
{
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);
}
type = g_io_extension_get_type (extension);
return g_object_new (type, "context", context, NULL);
}
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
* g_settings_backend_get_default:
* @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.
*
* 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.
*
* If @context is not supported by the implementation, this function
* returns an instance of the #GSettingsMemoryBackend.
* See g_settings_backend_supports_context(),
*
* The user does not own the return value and it must not be freed.
* The user gets a reference to the backend.
*/
GSettingsBackend *
g_settings_backend_get_with_context (const gchar *context)
g_settings_backend_get_default (void)
{
GSettingsBackend *backend;
static gsize 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)
if (g_once_init_enter (&backend))
{
backend = get_default_backend (context);
GSettingsBackend *instance;
GIOExtensionPoint *point;
GIOExtension *extension;
GType extension_type;
GList *extensions;
const gchar *env;
if (!backend)
backend = g_null_settings_backend_new ();
_g_io_modules_ensure_loaded ();
g_hash_table_insert (g_settings_backends, g_strdup (context), backend);
point = g_io_extension_point_lookup (G_SETTINGS_BACKEND_EXTENSION_POINT_NAME);
extension = NULL;
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;
}
extension_type = g_io_extension_get_type (extension);
instance = g_object_new (extension_type, NULL);
g_once_init_leave (&backend, (gsize) instance);
}
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;
return g_object_ref ((void *) backend);
}
/*< private >
@ -1194,54 +1119,19 @@ 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.
*
* Since: 2.26
**/
void
g_settings_backend_setup (const gchar *context,
GSettingsBackend *backend)
{
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_hash_table_lookup (g_settings_backends, context))
g_error ("A GSettingsBackend already exists for context '%s'", context);
g_hash_table_insert (g_settings_backends,
g_strdup (context),
g_object_ref (backend));
}
/*< private >
* g_settings_backend_sync:
* @backend: a #GSettingsBackend
* g_settings_backend_sync_default:
*
* Syncs the backend.
* Syncs the default backend.
*/
void
g_settings_backend_sync (GSettingsBackend *backend)
g_settings_backend_sync_default (void)
{
GSettingsBackendClass *class = G_SETTINGS_BACKEND_GET_CLASS (backend);
GSettingsBackendClass *class;
GSettingsBackend *backend;
backend = g_settings_backend_get_default ();
class = G_SETTINGS_BACKEND_GET_CLASS (backend);
if (class->sync)
class->sync (backend);

View File

@ -111,36 +111,33 @@ struct _GSettingsBackend
GSettingsBackendPrivate *priv;
};
GType g_settings_backend_get_type (void);
GType g_settings_backend_get_type (void);
void g_settings_backend_setup (const gchar *context,
GSettingsBackend *backend);
void g_settings_backend_setup_keyfile (const gchar *context,
const gchar *filename);
void g_settings_backend_changed (GSettingsBackend *backend,
void g_settings_backend_changed (GSettingsBackend *backend,
const gchar *key,
gpointer origin_tag);
void g_settings_backend_path_changed (GSettingsBackend *backend,
void g_settings_backend_path_changed (GSettingsBackend *backend,
const gchar *path,
gpointer origin_tag);
void g_settings_backend_flatten_tree (GTree *tree,
void g_settings_backend_flatten_tree (GTree *tree,
gchar **path,
const gchar ***keys,
GVariant ***values);
void g_settings_backend_keys_changed (GSettingsBackend *backend,
void g_settings_backend_keys_changed (GSettingsBackend *backend,
const gchar *path,
gchar const * const *items,
gpointer origin_tag);
void g_settings_backend_path_writable_changed(GSettingsBackend *backend,
void g_settings_backend_path_writable_changed (GSettingsBackend *backend,
const gchar *path);
void g_settings_backend_writable_changed (GSettingsBackend *backend,
void g_settings_backend_writable_changed (GSettingsBackend *backend,
const gchar *key);
void g_settings_backend_changed_tree (GSettingsBackend *backend,
void g_settings_backend_changed_tree (GSettingsBackend *backend,
GTree *tree,
gpointer origin_tag);
GSettingsBackend * g_keyfile_settings_backend_new (const gchar *filename);
G_END_DECLS
#endif /* __G_SETTINGS_BACKEND_H__ */

View File

@ -60,50 +60,48 @@ void g_settings_backend_unwatch (GSettin
GObject *target);
G_GNUC_INTERNAL
gboolean g_settings_backend_supports_context (const gchar *context);
GTree * g_settings_backend_create_tree (void);
G_GNUC_INTERNAL
GSettingsBackend * g_settings_backend_get_with_context (const gchar *context);
G_GNUC_INTERNAL
GTree * g_settings_backend_create_tree (void);
G_GNUC_INTERNAL
GVariant * g_settings_backend_read (GSettingsBackend *backend,
GVariant * g_settings_backend_read (GSettingsBackend *backend,
const gchar *key,
const GVariantType *expected_type,
gboolean default_value);
G_GNUC_INTERNAL
gboolean g_settings_backend_write (GSettingsBackend *backend,
gboolean g_settings_backend_write (GSettingsBackend *backend,
const gchar *key,
GVariant *value,
gpointer origin_tag);
G_GNUC_INTERNAL
gboolean g_settings_backend_write_keys (GSettingsBackend *backend,
gboolean g_settings_backend_write_keys (GSettingsBackend *backend,
GTree *tree,
gpointer origin_tag);
G_GNUC_INTERNAL
void g_settings_backend_reset (GSettingsBackend *backend,
void g_settings_backend_reset (GSettingsBackend *backend,
const gchar *key,
gpointer origin_tag);
G_GNUC_INTERNAL
void g_settings_backend_reset_path (GSettingsBackend *backend,
void g_settings_backend_reset_path (GSettingsBackend *backend,
const gchar *path,
gpointer origin_tag);
G_GNUC_INTERNAL
gboolean g_settings_backend_get_writable (GSettingsBackend *backend,
gboolean g_settings_backend_get_writable (GSettingsBackend *backend,
const char *key);
G_GNUC_INTERNAL
void g_settings_backend_unsubscribe (GSettingsBackend *backend,
void g_settings_backend_unsubscribe (GSettingsBackend *backend,
const char *name);
G_GNUC_INTERNAL
void g_settings_backend_subscribe (GSettingsBackend *backend,
void g_settings_backend_subscribe (GSettingsBackend *backend,
const char *name);
G_GNUC_INTERNAL
GPermission * g_settings_backend_get_permission (GSettingsBackend *backend,
GPermission * g_settings_backend_get_permission (GSettingsBackend *backend,
const gchar *path);
G_GNUC_INTERNAL
GMainContext * g_settings_backend_get_active_context (void);
GMainContext * g_settings_backend_get_active_context (void);
G_GNUC_INTERNAL
void g_settings_backend_sync (GSettingsBackend *backend);
GSettingsBackend * g_settings_backend_get_default (void);
G_GNUC_INTERNAL
void g_settings_backend_sync_default (void);
#endif /* __G_SETTINGS_BACKEND_INTERNAL_H__ */

View File

@ -344,9 +344,10 @@ else
glib_compile_schemas=$(top_builddir)/gio/glib-compile-schemas
endif
DISTCLEANFILES = \
applications/mimeinfo.cache \
de/LC_MESSAGES/test.mo \
test.mo \
gsettings.store \
DISTCLEANFILES = \
applications/mimeinfo.cache \
org.gtk.test.enums.xml \
de/LC_MESSAGES/test.mo \
test.mo \
gsettings.store \
gschemas.compiled

View File

@ -1120,15 +1120,16 @@ test_no_write_binding (void)
static void
test_keyfile (void)
{
GSettingsBackend *kf_backend;
GSettings *settings;
GKeyFile *keyfile;
gchar *str;
g_remove ("gsettings.store");
g_settings_backend_setup_keyfile ("blah", "gsettings.store");
settings = g_settings_new_with_context ("org.gtk.test", "blah");
kf_backend = g_keyfile_settings_backend_new ("gsettings.store");
settings = g_settings_new_with_backend ("org.gtk.test", kf_backend);
g_object_unref (kf_backend);
g_settings_set (settings, "greeting", "s", "see if this works");
@ -1390,7 +1391,7 @@ main (int argc, char *argv[])
result = g_test_run ();
g_settings_sync (NULL);
g_settings_sync ();
return result;
}