lib/settings: make the WpSettings object a non-singleton

This doesn't need to be a singleton, since we have the core registration
API available publicly nowadays. Makes things more clean for the API,
following the pattern of WpPlugin and WpSiFactory and simplifies the
built-in settings component in the internal component loader :)
This commit is contained in:
George Kiagiadakis
2024-02-10 17:48:23 +02:00
parent d61bf89969
commit 475ec4944d
5 changed files with 55 additions and 77 deletions

View File

@@ -641,39 +641,18 @@ load_export_core (GTask * task, WpCore * core, WpSpaJson * args)
g_task_return_pointer (task, g_steal_pointer (&export_core), g_object_unref); g_task_return_pointer (task, g_steal_pointer (&export_core), g_object_unref);
} }
static void
on_settings_ready (WpSettings *s, GAsyncResult *res, gpointer data)
{
GTask *task = G_TASK (data);
g_autoptr (GError) error = NULL;
if (!wp_object_activate_finish (WP_OBJECT (s), res, &error)) {
g_task_return_new_error (task,
WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED,
"failed to activate settings instance: %s", error->message);
return;
}
g_task_return_pointer (task, NULL, NULL);
}
static void static void
load_settings_instance (GTask * task, WpCore * core, WpSpaJson * args) load_settings_instance (GTask * task, WpCore * core, WpSpaJson * args)
{ {
g_autofree gchar *metadata_name = NULL; g_autofree gchar *metadata_name = NULL;
if (args) if (args)
wp_spa_json_object_get (args, "metadata.name", "s", &metadata_name, NULL); wp_spa_json_object_get (args, "metadata.name", "s", &metadata_name, NULL);
if (!metadata_name)
metadata_name = g_strdup ("sm-settings");
wp_info_object (core, "loading settings instance '%s'...", metadata_name); wp_info_object (core, "loading settings instance '%s'...",
metadata_name ? metadata_name : "(default: sm-settings)");
g_autoptr (WpSettings) settings = wp_settings_get_instance (core, WpSettings *settings = wp_settings_new (core, metadata_name);
metadata_name); g_task_return_pointer (task, settings, g_object_unref);
wp_object_activate_closure (WP_OBJECT (settings), WP_OBJECT_FEATURES_ALL, NULL,
g_cclosure_new (G_CALLBACK (on_settings_ready), g_object_ref (task),
(GClosureNotify) g_object_unref));
} }
static const struct { static const struct {

View File

@@ -264,52 +264,52 @@ wp_settings_class_init (WpSettingsClass * klass)
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS)); G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
} }
static gboolean /*!
check_metadata_name (gpointer g_object, gpointer metadata_name) * \brief Creates a new WpSettings object
*
* \ingroup wpsettings
* \param core the WpCore
* \param metadata_name (nullable): the name of the metadata object to
* associate with the settings object; NULL means the default "sm-settings"
* \returns (transfer full): a new WpSettings object
*/
WpSettings *
wp_settings_new (WpCore * core, const gchar * metadata_name)
{ {
if (!WP_IS_SETTINGS(g_object)) return g_object_new (WP_TYPE_SETTINGS,
"core", core,
"metadata-name", metadata_name ? metadata_name : "sm-settings",
NULL);
}
static gboolean
find_settings_func (gpointer g_object, gpointer metadata_name)
{
if (!WP_IS_SETTINGS (g_object))
return FALSE; return FALSE;
g_auto (GValue) value = G_VALUE_INIT; return g_str_equal (((WpSettings *) g_object)->metadata_name,
g_object_get_property (G_OBJECT(g_object), "metadata-name", &value); (gchar *) metadata_name);
return g_str_equal (g_value_get_string (&value), (gchar *)metadata_name);
} }
/*! /*!
* \brief Returns the WpSettings instance that is associated with the * \brief Finds a registered WpSettings object by its metadata name
* given core.
*
* This method will also create the instance and register it with the core
* if it had not been created before.
* *
* \ingroup wpsettings * \ingroup wpsettings
* \param core the core * \param core the WpCore
* \param metadata_name (nullable): the name of the metadata with which this * \param metadata_name (nullable): the name of the metadata object that the
* object is associated. `sm-settings` is the default value picked if * settings object is associated with; NULL means the default "sm-settings"
* NULL is supplied. * \returns (transfer full) (nullable): the WpSettings object, or NULL if not
* \returns (transfer full): the WpSettings instance * found
*/ */
WpSettings * WpSettings *
wp_settings_get_instance (WpCore *core, const gchar *metadata_name) wp_settings_find (WpCore * core, const gchar * metadata_name)
{ {
const gchar *name = (metadata_name ? metadata_name : "sm-settings") ; g_return_val_if_fail (WP_IS_CORE (core), NULL);
WpSettings *settings = wp_core_find_object (core,
(GEqualFunc) check_metadata_name, name);
if (G_UNLIKELY (!settings)) { GObject *s = wp_core_find_object (core, (GEqualFunc) find_settings_func,
settings = g_object_new (WP_TYPE_SETTINGS, metadata_name ? metadata_name : "sm-settings");
"core", core, return s ? WP_SETTINGS (s) : NULL;
"metadata-name", name,
NULL);
wp_core_register_object (core, g_object_ref (settings));
wp_info_object (settings, "created wpsettings object for metadata"
" name \"%s\"", name);
}
return settings;
} }
/*! /*!

View File

@@ -33,8 +33,10 @@ WP_API
G_DECLARE_FINAL_TYPE (WpSettings, wp_settings, WP, SETTINGS, WpObject) G_DECLARE_FINAL_TYPE (WpSettings, wp_settings, WP, SETTINGS, WpObject)
WP_API WP_API
WpSettings * wp_settings_get_instance (WpCore * core, WpSettings * wp_settings_new (WpCore * core, const gchar * metadata_name);
const gchar *metadata_name);
WP_API
WpSettings * wp_settings_find (WpCore * core, const gchar * metadata_name);
/*! /*!
* \brief callback conveying the changed setting and its json value * \brief callback conveying the changed setting and its json value

View File

@@ -1810,8 +1810,7 @@ settings_get (lua_State *L)
{ {
const char *setting = luaL_checkstring (L, 1); const char *setting = luaL_checkstring (L, 1);
g_autoptr (WpSettings) s = wp_settings_get_instance (get_wp_core (L), g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
"sm-settings");
if (s) { if (s) {
WpSpaJson *j = wp_settings_get (s, setting); WpSpaJson *j = wp_settings_get (s, setting);
@@ -1828,8 +1827,7 @@ static int
settings_subscribe (lua_State *L) settings_subscribe (lua_State *L)
{ {
const gchar *pattern = luaL_checkstring (L, 1); const gchar *pattern = luaL_checkstring (L, 1);
g_autoptr (WpSettings) s = wp_settings_get_instance (get_wp_core (L), g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
"sm-settings");
guintptr sub_id = 0; guintptr sub_id = 0;
@@ -1847,8 +1845,7 @@ settings_unsubscribe (lua_State *L)
{ {
guintptr sub_id = luaL_checkinteger (L, 1); guintptr sub_id = luaL_checkinteger (L, 1);
gboolean ret = FALSE; gboolean ret = FALSE;
g_autoptr (WpSettings) s = wp_settings_get_instance (get_wp_core (L), g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL);
"sm-settings");
if (s) if (s)
ret = wp_settings_unsubscribe (s, sub_id); ret = wp_settings_unsubscribe (s, sub_id);

View File

@@ -188,6 +188,8 @@ on_settings_ready (WpSettings *s, GAsyncResult *res, gpointer data)
g_assert_true(wp_object_activate_finish (WP_OBJECT (s), res, NULL)); g_assert_true(wp_object_activate_finish (WP_OBJECT (s), res, NULL));
wp_core_register_object (self->base.core, g_object_ref (s));
g_main_loop_quit(self->base.loop); g_main_loop_quit(self->base.loop);
} }
@@ -197,7 +199,7 @@ test_wpsettings_setup (TestSettingsFixture *self, gconstpointer user_data)
test_metadata_setup (self, user_data); test_metadata_setup (self, user_data);
{ {
self->s = wp_settings_get_instance (self->base.core, "sm-settings"); self->s = wp_settings_new (self->base.core, "sm-settings");
wp_object_activate (WP_OBJECT (self->s), wp_object_activate (WP_OBJECT (self->s),
WP_OBJECT_FEATURES_ALL, WP_OBJECT_FEATURES_ALL,
@@ -288,19 +290,19 @@ test_wpsettings (TestSettingsFixture *self, gconstpointer data)
g_assert_cmpfloat_with_epsilon (value, 0.4, 0.001); g_assert_cmpfloat_with_epsilon (value, 0.4, 0.001);
} }
/* test the wp_settings_get_instance () API */ /* test the wp_settings_find () API */
{ {
g_autoptr (WpSettings) s1 = g_autoptr (WpSettings) s1 =
wp_settings_get_instance (self->base.core, "test-settings"); wp_settings_find (self->base.core, NULL);
g_autoptr (WpSettings) s2 = g_autoptr (WpSettings) s2 =
wp_settings_get_instance (self->base.core, "test-settings"); wp_settings_find (self->base.core, "sm-settings");
g_autoptr (WpSettings) s3 = g_autoptr (WpSettings) s3 =
wp_settings_get_instance (self->base.core, "blah-blah"); wp_settings_find (self->base.core, "blah-blah");
g_assert_false (s == s1); g_assert_true (s == s1);
g_assert_true (s1 == s2); g_assert_true (s1 == s2);
g_assert_false (s1 == s3); g_assert_false (s1 == s3);
g_assert_null (s3);
} }
{ {
@@ -376,14 +378,12 @@ test_wpsettings (TestSettingsFixture *self, gconstpointer data)
} }
{ {
g_autoptr (WpSettings) s4 = g_autoptr (WpSettings) s4 = wp_settings_find (self->base.core, NULL);
wp_settings_get_instance (self->base.core, NULL);
g_auto (GValue) value = G_VALUE_INIT; g_auto (GValue) value = G_VALUE_INIT;
g_object_get_property (G_OBJECT(s4), "metadata-name", &value); g_object_get_property (G_OBJECT(s4), "metadata-name", &value);
g_assert_cmpstr (g_value_get_string (&value), ==, "sm-settings"); g_assert_cmpstr (g_value_get_string (&value), ==, "sm-settings");
} }
} }