diff --git a/lib/wp/private/internal-comp-loader.c b/lib/wp/private/internal-comp-loader.c index 667b98f9..097265ad 100644 --- a/lib/wp/private/internal-comp-loader.c +++ b/lib/wp/private/internal-comp-loader.c @@ -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); } -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 load_settings_instance (GTask * task, WpCore * core, WpSpaJson * args) { g_autofree gchar *metadata_name = NULL; if (args) 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, - metadata_name); - - 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)); + WpSettings *settings = wp_settings_new (core, metadata_name); + g_task_return_pointer (task, settings, g_object_unref); } static const struct { diff --git a/lib/wp/settings.c b/lib/wp/settings.c index 8124d40b..c76d9f61 100644 --- a/lib/wp/settings.c +++ b/lib/wp/settings.c @@ -264,52 +264,52 @@ wp_settings_class_init (WpSettingsClass * klass) 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; - g_auto (GValue) value = G_VALUE_INIT; - g_object_get_property (G_OBJECT(g_object), "metadata-name", &value); - - return g_str_equal (g_value_get_string (&value), (gchar *)metadata_name); + return g_str_equal (((WpSettings *) g_object)->metadata_name, + (gchar *) metadata_name); } /*! - * \brief Returns the WpSettings instance that is associated with the - * given core. - * - * This method will also create the instance and register it with the core - * if it had not been created before. + * \brief Finds a registered WpSettings object by its metadata name * * \ingroup wpsettings - * \param core the core - * \param metadata_name (nullable): the name of the metadata with which this - * object is associated. `sm-settings` is the default value picked if - * NULL is supplied. - * \returns (transfer full): the WpSettings instance + * \param core the WpCore + * \param metadata_name (nullable): the name of the metadata object that the + * settings object is associated with; NULL means the default "sm-settings" + * \returns (transfer full) (nullable): the WpSettings object, or NULL if not + * found */ 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") ; - WpSettings *settings = wp_core_find_object (core, - (GEqualFunc) check_metadata_name, name); + g_return_val_if_fail (WP_IS_CORE (core), NULL); - if (G_UNLIKELY (!settings)) { - settings = g_object_new (WP_TYPE_SETTINGS, - "core", core, - "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; + GObject *s = wp_core_find_object (core, (GEqualFunc) find_settings_func, + metadata_name ? metadata_name : "sm-settings"); + return s ? WP_SETTINGS (s) : NULL; } /*! diff --git a/lib/wp/settings.h b/lib/wp/settings.h index 9ce3af5e..cf04e453 100644 --- a/lib/wp/settings.h +++ b/lib/wp/settings.h @@ -33,8 +33,10 @@ WP_API G_DECLARE_FINAL_TYPE (WpSettings, wp_settings, WP, SETTINGS, WpObject) WP_API -WpSettings * wp_settings_get_instance (WpCore * core, - const gchar *metadata_name); +WpSettings * wp_settings_new (WpCore * core, 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 diff --git a/modules/module-lua-scripting/api/api.c b/modules/module-lua-scripting/api/api.c index 12e2a485..a81b479d 100644 --- a/modules/module-lua-scripting/api/api.c +++ b/modules/module-lua-scripting/api/api.c @@ -1810,8 +1810,7 @@ settings_get (lua_State *L) { const char *setting = luaL_checkstring (L, 1); - g_autoptr (WpSettings) s = wp_settings_get_instance (get_wp_core (L), - "sm-settings"); + g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL); if (s) { WpSpaJson *j = wp_settings_get (s, setting); @@ -1828,8 +1827,7 @@ static int settings_subscribe (lua_State *L) { const gchar *pattern = luaL_checkstring (L, 1); - g_autoptr (WpSettings) s = wp_settings_get_instance (get_wp_core (L), - "sm-settings"); + g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL); guintptr sub_id = 0; @@ -1847,8 +1845,7 @@ settings_unsubscribe (lua_State *L) { guintptr sub_id = luaL_checkinteger (L, 1); gboolean ret = FALSE; - g_autoptr (WpSettings) s = wp_settings_get_instance (get_wp_core (L), - "sm-settings"); + g_autoptr (WpSettings) s = wp_settings_find (get_wp_core (L), NULL); if (s) ret = wp_settings_unsubscribe (s, sub_id); diff --git a/tests/wp/settings.c b/tests/wp/settings.c index b959b1bf..23357146 100644 --- a/tests/wp/settings.c +++ b/tests/wp/settings.c @@ -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)); + wp_core_register_object (self->base.core, g_object_ref (s)); + 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); { - 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_FEATURES_ALL, @@ -288,19 +290,19 @@ test_wpsettings (TestSettingsFixture *self, gconstpointer data) 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 = - wp_settings_get_instance (self->base.core, "test-settings"); + wp_settings_find (self->base.core, NULL); 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 = - 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_false (s1 == s3); - + g_assert_null (s3); } { @@ -376,14 +378,12 @@ test_wpsettings (TestSettingsFixture *self, gconstpointer data) } { - g_autoptr (WpSettings) s4 = - wp_settings_get_instance (self->base.core, NULL); + g_autoptr (WpSettings) s4 = wp_settings_find (self->base.core, NULL); g_auto (GValue) value = G_VALUE_INIT; g_object_get_property (G_OBJECT(s4), "metadata-name", &value); g_assert_cmpstr (g_value_get_string (&value), ==, "sm-settings"); - } }