2022-03-22 03:38:35 +05:30
|
|
|
/* WirePlumber
|
|
|
|
|
*
|
|
|
|
|
* Copyright © 2022 Collabora Ltd.
|
|
|
|
|
* @author Ashok Sidipotu <ashok.sidipotu@collabora.com>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
|
*/
|
|
|
|
|
|
2023-01-10 09:11:15 -05:00
|
|
|
#include "core.h"
|
2022-03-22 03:38:35 +05:30
|
|
|
#include "settings.h"
|
|
|
|
|
#include "metadata.h"
|
|
|
|
|
#include "log.h"
|
2023-01-10 09:11:15 -05:00
|
|
|
#include "object-manager.h"
|
2022-03-22 03:38:35 +05:30
|
|
|
|
log: implement a log topics system, like pipewire
The intention is to make checks for enabled log topics faster.
Every topic has its own structure that is statically defined in the file
where the logs are printed from. The structure is initialized transparently
when it is first used and it contains all the log level flags for the levels
that this topic should print messages. It is then checked on the wp_log()
macro before printing the message.
Topics from SPA/PipeWire are also handled natively, so messages are printed
directly without checking if the topic is enabled, since the PipeWire and SPA
macros do the checking themselves.
Messages coming from GLib are checked inside the handler.
An internal WpLogFields object is used to manage the state of each log
message, populating all the fields appropriately from the place they
are coming from (wp_log, spa_log, glib log), formatting the message and
then printing it. For printing to the journald, we still use the glib
message handler, converting all the needed fields to GLogField on demand.
That message handler does not do any checks for the topic or the level, so
we can just call it to send the message.
2023-05-16 11:51:29 +03:00
|
|
|
WP_DEFINE_LOCAL_LOG_TOPIC ("wp-settings")
|
|
|
|
|
|
2022-05-04 14:45:55 +03:00
|
|
|
/*! \defgroup wpsettings WpSettings */
|
2024-02-15 11:19:14 -05:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \struct WpSettingsSpec
|
|
|
|
|
*
|
|
|
|
|
* WpSettingSpec holds the specification of a setting.
|
|
|
|
|
*/
|
|
|
|
|
struct _WpSettingsSpec {
|
|
|
|
|
grefcount ref;
|
2025-03-30 23:51:52 +03:00
|
|
|
gchar *name;
|
2024-02-15 11:19:14 -05:00
|
|
|
gchar *desc;
|
|
|
|
|
WpSettingsSpecType type;
|
|
|
|
|
WpSpaJson *def_value;
|
|
|
|
|
WpSpaJson *min_value;
|
|
|
|
|
WpSpaJson *max_value;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
G_DEFINE_BOXED_TYPE (WpSettingsSpec, wp_settings_spec, wp_settings_spec_ref,
|
|
|
|
|
wp_settings_spec_unref)
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Increases the reference count of a settings spec object
|
|
|
|
|
* \ingroup wpsettings
|
|
|
|
|
* \param self a settings spec object
|
|
|
|
|
* \returns (transfer full): \a self with an additional reference count on it
|
|
|
|
|
*/
|
|
|
|
|
WpSettingsSpec *
|
|
|
|
|
wp_settings_spec_ref (WpSettingsSpec * self)
|
|
|
|
|
{
|
|
|
|
|
g_ref_count_inc (&self->ref);
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
wp_settings_spec_free (WpSettingsSpec * self)
|
|
|
|
|
{
|
2025-03-30 23:51:52 +03:00
|
|
|
g_clear_pointer (&self->name, g_free);
|
2024-02-15 11:19:14 -05:00
|
|
|
g_clear_pointer (&self->desc, g_free);
|
|
|
|
|
g_clear_pointer (&self->def_value, wp_spa_json_unref);
|
|
|
|
|
g_clear_pointer (&self->min_value, wp_spa_json_unref);
|
|
|
|
|
g_clear_pointer (&self->max_value, wp_spa_json_unref);
|
|
|
|
|
g_slice_free (WpSettingsSpec, self);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Decreases the reference count on \a self and frees it when the ref
|
|
|
|
|
* count reaches zero.
|
|
|
|
|
* \ingroup wpsettings
|
|
|
|
|
* \param self (transfer full): a settings spec object
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
wp_settings_spec_unref (WpSettingsSpec * self)
|
|
|
|
|
{
|
|
|
|
|
if (g_ref_count_dec (&self->ref))
|
|
|
|
|
wp_settings_spec_free (self);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static WpSettingsSpec *
|
|
|
|
|
wp_settings_spec_new (WpSpaJson * spec_json)
|
|
|
|
|
{
|
|
|
|
|
WpSettingsSpec *self;
|
2025-03-30 23:51:52 +03:00
|
|
|
g_autofree gchar *name = NULL;
|
2024-02-15 11:19:14 -05:00
|
|
|
g_autofree gchar *desc = NULL;
|
|
|
|
|
g_autofree gchar *type_str = NULL;
|
|
|
|
|
WpSettingsSpecType type = WP_SETTINGS_SPEC_TYPE_UNKNOWN;
|
|
|
|
|
g_autoptr (WpSpaJson) def_value = NULL;
|
|
|
|
|
g_autoptr (WpSpaJson) min_value = NULL;
|
|
|
|
|
g_autoptr (WpSpaJson) max_value = NULL;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (spec_json, NULL);
|
|
|
|
|
|
|
|
|
|
if (!wp_spa_json_is_object (spec_json))
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
/* Parse mandatory fields */
|
|
|
|
|
if (!wp_spa_json_object_get (spec_json,
|
|
|
|
|
"description", "s", &desc,
|
|
|
|
|
"type", "s", &type_str,
|
|
|
|
|
"default", "J", &def_value,
|
|
|
|
|
NULL))
|
|
|
|
|
return NULL;
|
|
|
|
|
|
2025-07-24 12:00:21 +03:00
|
|
|
/* Parse optional fields */
|
|
|
|
|
wp_spa_json_object_get (spec_json, "name", "s", &name, NULL);
|
|
|
|
|
|
2024-02-15 11:19:14 -05:00
|
|
|
/* Parse type and check if values are correct */
|
|
|
|
|
if (g_str_equal (type_str, "bool")) {
|
|
|
|
|
type = WP_SETTINGS_SPEC_TYPE_BOOL;
|
|
|
|
|
if (!wp_spa_json_is_boolean (def_value))
|
|
|
|
|
return NULL;
|
|
|
|
|
} else if (g_str_equal (type_str, "int")) {
|
|
|
|
|
type = WP_SETTINGS_SPEC_TYPE_INT;
|
2024-02-29 07:23:58 -05:00
|
|
|
if (!wp_spa_json_object_get (spec_json,
|
|
|
|
|
"min", "J", &min_value,
|
|
|
|
|
"max", "J", &max_value,
|
|
|
|
|
NULL))
|
|
|
|
|
return NULL;
|
2024-02-15 11:19:14 -05:00
|
|
|
if (!wp_spa_json_is_int (def_value) ||
|
|
|
|
|
!min_value || !wp_spa_json_is_int (min_value) ||
|
|
|
|
|
!max_value || !wp_spa_json_is_int (max_value))
|
|
|
|
|
return NULL;
|
|
|
|
|
} else if (g_str_equal (type_str, "float")) {
|
|
|
|
|
type = WP_SETTINGS_SPEC_TYPE_FLOAT;
|
2024-02-29 07:23:58 -05:00
|
|
|
if (!wp_spa_json_object_get (spec_json,
|
|
|
|
|
"min", "J", &min_value,
|
|
|
|
|
"max", "J", &max_value,
|
|
|
|
|
NULL))
|
|
|
|
|
return NULL;
|
2024-02-15 11:19:14 -05:00
|
|
|
if (!wp_spa_json_is_float (def_value) ||
|
|
|
|
|
!min_value || !wp_spa_json_is_float (min_value) ||
|
|
|
|
|
!max_value || !wp_spa_json_is_float (max_value))
|
|
|
|
|
return NULL;
|
|
|
|
|
} else if (g_str_equal (type_str, "string")) {
|
|
|
|
|
type = WP_SETTINGS_SPEC_TYPE_STRING;
|
|
|
|
|
} else if (g_str_equal (type_str, "array")) {
|
|
|
|
|
type = WP_SETTINGS_SPEC_TYPE_ARRAY;
|
|
|
|
|
if (!wp_spa_json_is_array (def_value))
|
|
|
|
|
return NULL;
|
|
|
|
|
} else if (g_str_equal (type_str, "object")) {
|
|
|
|
|
type = WP_SETTINGS_SPEC_TYPE_OBJECT;
|
|
|
|
|
if (!wp_spa_json_is_object (def_value))
|
|
|
|
|
return NULL;
|
|
|
|
|
} else {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self = g_slice_new0 (WpSettingsSpec);
|
|
|
|
|
g_ref_count_init (&self->ref);
|
2025-03-30 23:51:52 +03:00
|
|
|
self->name = g_steal_pointer (&name);
|
2024-02-15 11:19:14 -05:00
|
|
|
self->desc = g_steal_pointer (&desc);
|
|
|
|
|
self->type = type;
|
|
|
|
|
self->def_value = g_steal_pointer (&def_value);
|
|
|
|
|
self->min_value = g_steal_pointer (&min_value);
|
|
|
|
|
self->max_value = g_steal_pointer (&max_value);
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-30 23:51:52 +03:00
|
|
|
/*!
|
|
|
|
|
* \brief Gets the human-readable name of a settings spec
|
|
|
|
|
* \ingroup wpsettings
|
|
|
|
|
* \param self the settings spec object
|
2025-07-24 12:00:21 +03:00
|
|
|
* \returns (nullable): the human-readable name of the settings spec,
|
|
|
|
|
* or NULL if none
|
2025-03-30 23:51:52 +03:00
|
|
|
*/
|
|
|
|
|
const gchar *
|
|
|
|
|
wp_settings_spec_get_name (WpSettingsSpec * self)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (self, NULL);
|
|
|
|
|
return self->name;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-15 11:19:14 -05:00
|
|
|
/*!
|
|
|
|
|
* \brief Gets the description of a settings spec
|
|
|
|
|
* \ingroup wpsettings
|
|
|
|
|
* \param self the settings spec object
|
|
|
|
|
* \returns the description of the settings spec
|
|
|
|
|
*/
|
|
|
|
|
const gchar *
|
|
|
|
|
wp_settings_spec_get_description (WpSettingsSpec * self)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (self, NULL);
|
|
|
|
|
return self->desc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Gets the type of a settings spec
|
|
|
|
|
* \ingroup wpsettings
|
|
|
|
|
* \param self the settings spec object
|
|
|
|
|
* \returns the type of the settings spec
|
|
|
|
|
*/
|
|
|
|
|
WpSettingsSpecType
|
|
|
|
|
wp_settings_spec_get_value_type (WpSettingsSpec * self)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (self, WP_SETTINGS_SPEC_TYPE_UNKNOWN);
|
|
|
|
|
return self->type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Gets the default value of a settings spec
|
|
|
|
|
* \ingroup wpsettings
|
|
|
|
|
* \param self the settings spec object
|
|
|
|
|
* \returns (transfer full): the default value of the settings spec
|
|
|
|
|
*/
|
|
|
|
|
WpSpaJson *
|
|
|
|
|
wp_settings_spec_get_default_value (WpSettingsSpec * self)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (self, NULL);
|
|
|
|
|
g_return_val_if_fail (self->def_value, NULL);
|
|
|
|
|
return wp_spa_json_ref (self->def_value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Gets the minimum value of a settings spec.
|
|
|
|
|
* \ingroup wpsettings
|
|
|
|
|
* \param self the settings spec object
|
|
|
|
|
* \returns (transfer full)(nullable): the minimum value of the settings spec,
|
|
|
|
|
* or NULL if the spec type is not WP_SETTINGS_SPEC_TYPE_INT or
|
|
|
|
|
* WP_SETTINGS_SPEC_TYPE_FLOAT
|
|
|
|
|
*/
|
|
|
|
|
WpSpaJson *
|
|
|
|
|
wp_settings_spec_get_min_value (WpSettingsSpec * self)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (self, NULL);
|
|
|
|
|
return self->min_value ? wp_spa_json_ref (self->min_value) : NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Gets the maximum value of a settings spec.
|
|
|
|
|
* \ingroup wpsettings
|
|
|
|
|
* \param self the settings spec object
|
|
|
|
|
* \returns (transfer full)(nullable): the maximum value of the settings spec,
|
|
|
|
|
* or NULL if the spec type is not WP_SETTINGS_SPEC_TYPE_INT or
|
|
|
|
|
* WP_SETTINGS_SPEC_TYPE_FLOAT
|
|
|
|
|
*/
|
|
|
|
|
WpSpaJson *
|
|
|
|
|
wp_settings_spec_get_max_value (WpSettingsSpec * self)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (self, NULL);
|
|
|
|
|
return self->max_value ? wp_spa_json_ref (self->max_value) : NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Checks whether a value is compatible with the spec or not
|
|
|
|
|
* \ingroup wpsettings
|
|
|
|
|
* \param self the settings spec object
|
|
|
|
|
* \param value (transfer none): the value to check
|
|
|
|
|
* \returns TRUE if the value is compatible with the spec, FALSE otherwise
|
|
|
|
|
*/
|
|
|
|
|
gboolean
|
|
|
|
|
wp_settings_spec_check_value (WpSettingsSpec * self, WpSpaJson *value)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (self, FALSE);
|
|
|
|
|
g_return_val_if_fail (value, FALSE);
|
|
|
|
|
|
|
|
|
|
switch (self->type) {
|
|
|
|
|
case WP_SETTINGS_SPEC_TYPE_BOOL:
|
|
|
|
|
return wp_spa_json_is_boolean (value);
|
|
|
|
|
case WP_SETTINGS_SPEC_TYPE_INT: {
|
|
|
|
|
gint val = 0, min = 0, max = 0;
|
|
|
|
|
if (!wp_spa_json_is_int (value) || !wp_spa_json_parse_int (value, &val))
|
|
|
|
|
return FALSE;
|
2024-02-29 07:23:58 -05:00
|
|
|
if (!wp_spa_json_parse_int (self->min_value, &min) ||
|
|
|
|
|
!wp_spa_json_parse_int (self->max_value, &max))
|
|
|
|
|
return FALSE;
|
2024-02-15 11:19:14 -05:00
|
|
|
return val >= min && val <= max;
|
|
|
|
|
}
|
|
|
|
|
case WP_SETTINGS_SPEC_TYPE_FLOAT: {
|
|
|
|
|
float val = 0.0, min = 0.0, max = 0.0;
|
|
|
|
|
if (wp_spa_json_is_int (value) || !wp_spa_json_is_float (value) ||
|
|
|
|
|
!wp_spa_json_parse_float (value, &val))
|
|
|
|
|
return FALSE;
|
2024-02-29 07:23:58 -05:00
|
|
|
if (!wp_spa_json_parse_float (self->min_value, &min) ||
|
|
|
|
|
!wp_spa_json_parse_float (self->max_value, &max))
|
|
|
|
|
return FALSE;
|
2024-02-15 11:19:14 -05:00
|
|
|
return val >= min && val <= max;
|
|
|
|
|
}
|
|
|
|
|
case WP_SETTINGS_SPEC_TYPE_STRING:
|
|
|
|
|
/* We also accept strings without quotes, which is why we dont use
|
|
|
|
|
* wp_spa_json_is_string() */
|
|
|
|
|
return !wp_spa_json_is_boolean (value) && !wp_spa_json_is_int (value) &&
|
|
|
|
|
!wp_spa_json_is_float (value) && !wp_spa_json_is_array (value) &&
|
|
|
|
|
!wp_spa_json_is_object (value);
|
|
|
|
|
case WP_SETTINGS_SPEC_TYPE_ARRAY:
|
|
|
|
|
return wp_spa_json_is_array (value);
|
|
|
|
|
case WP_SETTINGS_SPEC_TYPE_OBJECT:
|
|
|
|
|
return wp_spa_json_is_object (value);
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \struct WpSettingsItem
|
|
|
|
|
*
|
|
|
|
|
* WpSettingsItem holds the key and value of a setting
|
|
|
|
|
*/
|
|
|
|
|
struct _WpSettingsItem
|
|
|
|
|
{
|
2025-04-14 11:09:56 -04:00
|
|
|
gchar *key;
|
2024-02-15 11:19:14 -05:00
|
|
|
WpSpaJson *value;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
G_DEFINE_BOXED_TYPE (WpSettingsItem, wp_settings_item,
|
|
|
|
|
wp_settings_item_ref, wp_settings_item_unref)
|
|
|
|
|
|
|
|
|
|
static WpSettingsItem *
|
2025-04-14 11:09:56 -04:00
|
|
|
wp_settings_item_new (const gchar *key, const gchar *value)
|
2024-02-15 11:19:14 -05:00
|
|
|
{
|
|
|
|
|
WpSettingsItem *self = g_rc_box_new0 (WpSettingsItem);
|
2025-04-14 11:09:56 -04:00
|
|
|
self->key = g_strdup (key);
|
2024-02-15 11:19:14 -05:00
|
|
|
self->value = wp_spa_json_new_from_string (value);
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
wp_settings_item_free (gpointer p)
|
|
|
|
|
{
|
|
|
|
|
WpSettingsItem *self = p;
|
2025-04-14 11:09:56 -04:00
|
|
|
g_clear_pointer (&self->key, g_free);
|
2024-02-15 11:19:14 -05:00
|
|
|
g_clear_pointer (&self->value, wp_spa_json_unref);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Increases the reference count of a settings item object
|
|
|
|
|
* \ingroup wpsettings
|
|
|
|
|
* \param self a settings item object
|
|
|
|
|
* \returns (transfer full): \a self with an additional reference count on it
|
|
|
|
|
*/
|
|
|
|
|
WpSettingsItem *
|
|
|
|
|
wp_settings_item_ref (WpSettingsItem *self)
|
|
|
|
|
{
|
|
|
|
|
return g_rc_box_acquire (self);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Decreases the reference count on \a self and frees it when the ref
|
|
|
|
|
* count reaches zero.
|
|
|
|
|
* \ingroup wpsettings
|
|
|
|
|
* \param self (transfer full): a settings item object
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
wp_settings_item_unref (WpSettingsItem *self)
|
|
|
|
|
{
|
|
|
|
|
g_rc_box_release_full (self, wp_settings_item_free);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Gets the key from a settings item
|
|
|
|
|
*
|
|
|
|
|
* \ingroup wpsettings
|
|
|
|
|
* \param self the item held by the GValue that was returned from the WpIterator
|
|
|
|
|
* of wp_settings_new_iterator()
|
|
|
|
|
* \returns (transfer none): the settings key of the \a item
|
|
|
|
|
*/
|
|
|
|
|
const gchar *
|
|
|
|
|
wp_settings_item_get_key (WpSettingsItem * self)
|
|
|
|
|
{
|
|
|
|
|
return self->key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Gets the value from a settings item
|
|
|
|
|
*
|
|
|
|
|
* \ingroup wpsettings
|
|
|
|
|
* \param self the item held by the GValue that was returned from the WpIterator
|
|
|
|
|
* of wp_settings_new_iterator()
|
|
|
|
|
* \returns (transfer full): the settings value of the \a item
|
|
|
|
|
*/
|
|
|
|
|
WpSpaJson *
|
|
|
|
|
wp_settings_item_get_value (WpSettingsItem * self)
|
|
|
|
|
{
|
|
|
|
|
return wp_spa_json_ref (self->value);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-22 03:38:35 +05:30
|
|
|
/*!
|
|
|
|
|
* \struct WpSettings
|
|
|
|
|
*
|
2022-05-04 14:45:55 +03:00
|
|
|
* WpSettings loads and parses the "sm-settings" (default value) metadata, which
|
2023-09-26 10:00:43 +03:00
|
|
|
* contains wireplumber settings, and provides APIs to its clients (modules, lua
|
|
|
|
|
* scripts etc) to access them.
|
2022-03-22 03:38:35 +05:30
|
|
|
*
|
|
|
|
|
* Being a WpObject subclass, the settings inherits WpObject's activation
|
|
|
|
|
* system.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
struct _WpSettings
|
|
|
|
|
{
|
|
|
|
|
WpObject parent;
|
|
|
|
|
|
2022-05-26 13:15:22 +05:30
|
|
|
/* element-type: Callback* */
|
|
|
|
|
GPtrArray *callbacks;
|
|
|
|
|
|
2022-04-12 11:20:57 +05:30
|
|
|
gchar *metadata_name;
|
2024-02-15 11:19:14 -05:00
|
|
|
gchar *metadata_schema_name;
|
|
|
|
|
gchar *metadata_persistent_name;
|
|
|
|
|
|
2022-04-12 11:20:57 +05:30
|
|
|
WpObjectManager *metadata_om;
|
2024-02-15 11:19:14 -05:00
|
|
|
GWeakRef metadata;
|
|
|
|
|
GWeakRef metadata_schema;
|
|
|
|
|
GWeakRef metadata_persistent;
|
2025-04-14 11:09:56 -04:00
|
|
|
|
|
|
|
|
/* We keep a hash table with all the settings info to make sure their values
|
|
|
|
|
* are always updated when using the settings API. This avoids syncing issues
|
|
|
|
|
* if pipewire has not finished syncing the metadata objects yet */
|
2024-02-15 11:19:14 -05:00
|
|
|
GHashTable *schema;
|
2025-04-14 11:09:56 -04:00
|
|
|
GHashTable *settings;
|
|
|
|
|
GHashTable *saved_settings;
|
2022-03-31 12:27:22 +05:30
|
|
|
};
|
|
|
|
|
|
2022-05-26 13:15:22 +05:30
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
GClosure *closure;
|
|
|
|
|
gchar *pattern;
|
|
|
|
|
} Callback;
|
|
|
|
|
|
2022-03-31 12:27:22 +05:30
|
|
|
enum {
|
|
|
|
|
PROP_0,
|
|
|
|
|
PROP_METADATA_NAME,
|
|
|
|
|
PROP_PROPERTIES,
|
2022-03-22 03:38:35 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (WpSettings, wp_settings, WP_TYPE_OBJECT)
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
wp_settings_init (WpSettings * self)
|
|
|
|
|
{
|
2024-02-15 11:19:14 -05:00
|
|
|
g_weak_ref_init (&self->metadata, NULL);
|
|
|
|
|
g_weak_ref_init (&self->metadata_schema, NULL);
|
|
|
|
|
g_weak_ref_init (&self->metadata_persistent, NULL);
|
|
|
|
|
|
|
|
|
|
self->schema = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
|
|
|
|
|
(GDestroyNotify) wp_settings_spec_unref);
|
2025-04-14 11:09:56 -04:00
|
|
|
self->settings = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
|
|
|
|
|
(GDestroyNotify) g_free);
|
|
|
|
|
self->saved_settings = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
|
|
|
|
|
(GDestroyNotify) g_free);
|
2022-03-22 03:38:35 +05:30
|
|
|
}
|
|
|
|
|
|
2024-02-10 16:42:01 +02:00
|
|
|
static void
|
|
|
|
|
wp_settings_set_property (GObject * object, guint property_id,
|
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
2022-05-26 13:15:22 +05:30
|
|
|
{
|
2024-02-10 16:42:01 +02:00
|
|
|
WpSettings *self = WP_SETTINGS (object);
|
2022-05-26 13:15:22 +05:30
|
|
|
|
2024-02-10 16:42:01 +02:00
|
|
|
switch (property_id) {
|
|
|
|
|
case PROP_METADATA_NAME:
|
|
|
|
|
self->metadata_name = g_value_dup_string (value);
|
2024-02-15 11:19:14 -05:00
|
|
|
self->metadata_schema_name = g_strdup_printf (
|
|
|
|
|
WP_SETTINGS_SCHEMA_METADATA_NAME_PREFIX "%s", self->metadata_name);
|
|
|
|
|
self->metadata_persistent_name = g_strdup_printf (
|
|
|
|
|
WP_SETTINGS_PERSISTENT_METADATA_NAME_PREFIX "%s", self->metadata_name);
|
2024-02-10 16:42:01 +02:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-05-26 13:15:22 +05:30
|
|
|
}
|
2022-03-22 03:38:35 +05:30
|
|
|
|
2024-02-10 16:42:01 +02:00
|
|
|
static void
|
|
|
|
|
wp_settings_get_property (GObject * object, guint property_id,
|
|
|
|
|
GValue * value, GParamSpec * pspec)
|
2022-08-11 11:30:10 -04:00
|
|
|
{
|
2024-02-10 16:42:01 +02:00
|
|
|
WpSettings *self = WP_SETTINGS (object);
|
2022-05-09 12:43:36 +05:30
|
|
|
|
2024-02-10 16:42:01 +02:00
|
|
|
switch (property_id) {
|
|
|
|
|
case PROP_METADATA_NAME:
|
|
|
|
|
g_value_set_string (value, self->metadata_name);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-05-09 12:43:36 +05:30
|
|
|
}
|
|
|
|
|
|
2022-03-22 03:38:35 +05:30
|
|
|
enum {
|
|
|
|
|
STEP_LOAD = WP_TRANSITION_STEP_CUSTOM_START,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static WpObjectFeatures
|
|
|
|
|
wp_settings_get_supported_features (WpObject * self)
|
|
|
|
|
{
|
|
|
|
|
return WP_SETTINGS_LOADED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static guint
|
|
|
|
|
wp_settings_activate_get_next_step (WpObject * object,
|
|
|
|
|
WpFeatureActivationTransition * transition, guint step,
|
|
|
|
|
WpObjectFeatures missing)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (missing == WP_SETTINGS_LOADED,
|
|
|
|
|
WP_TRANSITION_STEP_ERROR);
|
|
|
|
|
|
|
|
|
|
return STEP_LOAD;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 13:15:22 +05:30
|
|
|
static void
|
2025-04-14 11:09:56 -04:00
|
|
|
settings_notify_changed (WpSettings *self, const gchar *key, const gchar *value)
|
2022-05-26 13:15:22 +05:30
|
|
|
{
|
|
|
|
|
for (guint i = 0; i < self->callbacks->len; i++) {
|
|
|
|
|
Callback *cb = g_ptr_array_index (self->callbacks, i);
|
|
|
|
|
|
2024-02-15 11:19:14 -05:00
|
|
|
if (g_pattern_match_simple (cb->pattern, key)) {
|
2022-09-01 08:22:56 -04:00
|
|
|
g_autoptr (WpSpaJson) json = NULL;
|
2022-05-26 13:15:22 +05:30
|
|
|
GValue values[3] = { G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT };
|
2022-09-01 08:22:56 -04:00
|
|
|
|
2022-05-26 13:15:22 +05:30
|
|
|
g_value_init (&values[0], G_TYPE_OBJECT);
|
|
|
|
|
g_value_init (&values[1], G_TYPE_STRING);
|
2022-09-01 08:22:56 -04:00
|
|
|
g_value_init (&values[2], WP_TYPE_SPA_JSON);
|
2022-05-26 13:15:22 +05:30
|
|
|
|
|
|
|
|
g_value_set_object (&values[0], self);
|
2024-02-15 11:19:14 -05:00
|
|
|
g_value_set_string (&values[1], key);
|
|
|
|
|
json = value ? wp_spa_json_new_wrap_string (value) : NULL;
|
2022-09-01 08:22:56 -04:00
|
|
|
g_value_set_boxed (&values[2], json);
|
2022-05-26 13:15:22 +05:30
|
|
|
|
|
|
|
|
g_closure_invoke (cb->closure, NULL, 3, values, NULL);
|
|
|
|
|
|
|
|
|
|
g_value_unset (&values[0]);
|
|
|
|
|
g_value_unset (&values[1]);
|
|
|
|
|
g_value_unset (&values[2]);
|
2025-04-14 11:09:56 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
settings_notify_changed_cb (gpointer key, gpointer value, gpointer data)
|
|
|
|
|
{
|
|
|
|
|
WpSettings *self = WP_SETTINGS (data);
|
|
|
|
|
const gchar *k = key;
|
|
|
|
|
const gchar *v = value;
|
|
|
|
|
|
|
|
|
|
settings_notify_changed (self, k, v);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
on_metadata_changed (WpMetadata *m, guint32 subject,
|
|
|
|
|
const gchar *key, const gchar *type, const gchar *value, gpointer d)
|
|
|
|
|
{
|
|
|
|
|
WpSettings *self = WP_SETTINGS (d);
|
|
|
|
|
|
|
|
|
|
if (key) {
|
|
|
|
|
if (value) {
|
|
|
|
|
g_hash_table_insert (self->settings, g_strdup (key), g_strdup (value));
|
|
|
|
|
wp_info_object (self, "setting \"%s\" changed to \"%s\"", key, value);
|
|
|
|
|
} else {
|
|
|
|
|
g_hash_table_remove (self->settings, key);
|
|
|
|
|
wp_info_object (self, "setting \"%s\" removed", key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
settings_notify_changed (self, key, value);
|
|
|
|
|
} else {
|
|
|
|
|
wp_info_object (self, "all settings removed");
|
|
|
|
|
g_hash_table_foreach (self->settings, settings_notify_changed_cb, self);
|
|
|
|
|
g_hash_table_remove_all (self->settings);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-26 13:15:22 +05:30
|
|
|
|
2025-04-14 11:09:56 -04:00
|
|
|
static void
|
|
|
|
|
on_metadata_persistent_changed (WpMetadata *m, guint32 subject,
|
|
|
|
|
const gchar *key, const gchar *type, const gchar *value, gpointer d)
|
|
|
|
|
{
|
|
|
|
|
WpSettings *self = WP_SETTINGS (d);
|
|
|
|
|
|
|
|
|
|
if (key) {
|
|
|
|
|
if (value) {
|
|
|
|
|
g_hash_table_insert (self->saved_settings, g_strdup (key),
|
|
|
|
|
g_strdup (value));
|
|
|
|
|
wp_info_object (self, "saved setting \"%s\" changed to \"%s\"", key,
|
|
|
|
|
value);
|
|
|
|
|
} else {
|
|
|
|
|
g_hash_table_remove (self->saved_settings, key);
|
|
|
|
|
wp_info_object (self, "saved setting \"%s\" removed", key);
|
2022-05-26 13:15:22 +05:30
|
|
|
}
|
2025-04-14 11:09:56 -04:00
|
|
|
} else {
|
|
|
|
|
wp_info_object (self, "all saved settings removed");
|
|
|
|
|
g_hash_table_remove_all (self->saved_settings);
|
2022-05-26 13:15:22 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-22 03:38:35 +05:30
|
|
|
static void
|
|
|
|
|
on_metadata_added (WpObjectManager *om, WpMetadata *m, gpointer d)
|
|
|
|
|
{
|
|
|
|
|
WpTransition * transition = WP_TRANSITION (d);
|
|
|
|
|
WpSettings * self = wp_transition_get_source_object (transition);
|
2024-02-15 11:19:14 -05:00
|
|
|
g_autoptr (WpProperties) props = NULL;
|
|
|
|
|
const gchar *metadata_name = NULL;
|
|
|
|
|
g_autoptr (WpMetadata) metadata = NULL;
|
|
|
|
|
g_autoptr (WpMetadata) metadata_schema = NULL;
|
|
|
|
|
g_autoptr (WpMetadata) metadata_persistent = NULL;
|
|
|
|
|
|
|
|
|
|
/* make sure the metadata has a name */
|
|
|
|
|
props = wp_global_proxy_get_global_properties (WP_GLOBAL_PROXY (m));
|
|
|
|
|
if (props)
|
|
|
|
|
metadata_name = wp_properties_get (props, "metadata.name");
|
|
|
|
|
if (!metadata_name)
|
|
|
|
|
return;
|
2022-03-22 03:38:35 +05:30
|
|
|
|
2024-02-15 11:19:14 -05:00
|
|
|
/* sm-settings */
|
|
|
|
|
if (g_str_equal (metadata_name, self->metadata_name)) {
|
2025-04-14 11:09:56 -04:00
|
|
|
g_autoptr (WpIterator) it = NULL;
|
|
|
|
|
g_auto (GValue) item = G_VALUE_INIT;
|
|
|
|
|
it = wp_metadata_new_iterator (m, 0);
|
|
|
|
|
for (; wp_iterator_next (it, &item); g_value_unset (&item)) {
|
|
|
|
|
WpMetadataItem *mi = g_value_get_boxed (&item);
|
|
|
|
|
const gchar *key = wp_metadata_item_get_key (mi);
|
|
|
|
|
const gchar *value = wp_metadata_item_get_value (mi);
|
|
|
|
|
g_hash_table_insert (self->settings, g_strdup (key), g_strdup (value));
|
|
|
|
|
}
|
2024-02-15 11:19:14 -05:00
|
|
|
g_signal_connect_object (m, "changed", G_CALLBACK (on_metadata_changed),
|
|
|
|
|
self, 0);
|
|
|
|
|
g_weak_ref_set (&self->metadata, m);
|
|
|
|
|
}
|
2022-05-26 13:15:22 +05:30
|
|
|
|
2024-02-15 11:19:14 -05:00
|
|
|
/* schema-sm-settings */
|
|
|
|
|
else if (g_str_equal (metadata_name, self->metadata_schema_name)) {
|
|
|
|
|
g_autoptr (WpIterator) it = NULL;
|
|
|
|
|
g_auto (GValue) item = G_VALUE_INIT;
|
|
|
|
|
it = wp_metadata_new_iterator (m, 0);
|
|
|
|
|
for (; wp_iterator_next (it, &item); g_value_unset (&item)) {
|
|
|
|
|
WpMetadataItem *mi = g_value_get_boxed (&item);
|
|
|
|
|
const gchar *key = wp_metadata_item_get_key (mi);
|
|
|
|
|
const gchar *value = wp_metadata_item_get_value (mi);
|
|
|
|
|
g_autoptr (WpSpaJson) spec_json = NULL;
|
|
|
|
|
g_autoptr (WpSettingsSpec) spec = NULL;
|
|
|
|
|
spec_json = wp_spa_json_new_from_string (value);
|
|
|
|
|
spec = wp_settings_spec_new (spec_json);
|
|
|
|
|
if (spec)
|
|
|
|
|
g_hash_table_insert (self->schema, g_strdup (key),
|
|
|
|
|
g_steal_pointer (&spec));
|
|
|
|
|
else
|
|
|
|
|
wp_warning_object (self, "malformed setting spec: %s", value);
|
|
|
|
|
}
|
|
|
|
|
g_weak_ref_set (&self->metadata_schema, m);
|
2022-03-22 03:38:35 +05:30
|
|
|
}
|
|
|
|
|
|
2024-02-15 11:19:14 -05:00
|
|
|
/* presistent-sm-settings */
|
|
|
|
|
else if (g_str_equal (metadata_name, self->metadata_persistent_name)) {
|
2025-04-14 11:09:56 -04:00
|
|
|
g_autoptr (WpIterator) it = NULL;
|
|
|
|
|
g_auto (GValue) item = G_VALUE_INIT;
|
|
|
|
|
it = wp_metadata_new_iterator (m, 0);
|
|
|
|
|
for (; wp_iterator_next (it, &item); g_value_unset (&item)) {
|
|
|
|
|
WpMetadataItem *mi = g_value_get_boxed (&item);
|
|
|
|
|
const gchar *key = wp_metadata_item_get_key (mi);
|
|
|
|
|
const gchar *value = wp_metadata_item_get_value (mi);
|
|
|
|
|
g_hash_table_insert (self->saved_settings, g_strdup (key),
|
|
|
|
|
g_strdup (value));
|
|
|
|
|
}
|
|
|
|
|
g_signal_connect_object (m, "changed",
|
|
|
|
|
G_CALLBACK (on_metadata_persistent_changed), self, 0);
|
2024-02-15 11:19:14 -05:00
|
|
|
g_weak_ref_set (&self->metadata_persistent, m);
|
|
|
|
|
}
|
2022-03-22 03:38:35 +05:30
|
|
|
|
2024-02-15 11:19:14 -05:00
|
|
|
/* Finish loading when all metadatas are found */
|
|
|
|
|
metadata = g_weak_ref_get (&self->metadata);
|
|
|
|
|
metadata_schema = g_weak_ref_get (&self->metadata_schema);
|
|
|
|
|
metadata_persistent = g_weak_ref_get (&self->metadata_persistent);
|
|
|
|
|
if (metadata && metadata_schema && metadata_persistent)
|
|
|
|
|
wp_object_update_features (WP_OBJECT (self), WP_SETTINGS_LOADED, 0);
|
2022-03-22 03:38:35 +05:30
|
|
|
}
|
|
|
|
|
|
2022-05-26 13:15:22 +05:30
|
|
|
static void
|
|
|
|
|
callback_unref (Callback * self)
|
|
|
|
|
{
|
|
|
|
|
g_free (self->pattern);
|
|
|
|
|
g_clear_pointer (&self->closure, g_closure_unref);
|
2022-08-15 09:55:46 -04:00
|
|
|
g_slice_free (Callback, self);
|
2022-05-26 13:15:22 +05:30
|
|
|
}
|
|
|
|
|
|
2022-03-22 03:38:35 +05:30
|
|
|
static void
|
|
|
|
|
wp_settings_activate_execute_step (WpObject * object,
|
|
|
|
|
WpFeatureActivationTransition * transition, guint step,
|
|
|
|
|
WpObjectFeatures missing)
|
|
|
|
|
{
|
|
|
|
|
WpSettings * self = WP_SETTINGS (object);
|
|
|
|
|
g_autoptr (WpCore) core = wp_object_get_core (object);
|
|
|
|
|
|
|
|
|
|
switch (step) {
|
|
|
|
|
case STEP_LOAD: {
|
2022-05-26 13:15:22 +05:30
|
|
|
self->callbacks = g_ptr_array_new_with_free_func
|
|
|
|
|
((GDestroyNotify) callback_unref);
|
|
|
|
|
|
2022-03-22 03:38:35 +05:30
|
|
|
self->metadata_om = wp_object_manager_new ();
|
|
|
|
|
wp_object_manager_add_interest (self->metadata_om, WP_TYPE_METADATA,
|
|
|
|
|
WP_CONSTRAINT_TYPE_PW_GLOBAL_PROPERTY, "metadata.name", "=s",
|
2022-03-31 12:27:22 +05:30
|
|
|
self->metadata_name, NULL);
|
2024-02-15 11:19:14 -05:00
|
|
|
wp_object_manager_add_interest (self->metadata_om, WP_TYPE_METADATA,
|
|
|
|
|
WP_CONSTRAINT_TYPE_PW_GLOBAL_PROPERTY, "metadata.name", "=s",
|
|
|
|
|
self->metadata_schema_name, NULL);
|
|
|
|
|
wp_object_manager_add_interest (self->metadata_om, WP_TYPE_METADATA,
|
|
|
|
|
WP_CONSTRAINT_TYPE_PW_GLOBAL_PROPERTY, "metadata.name", "=s",
|
|
|
|
|
self->metadata_persistent_name, NULL);
|
2022-03-22 03:38:35 +05:30
|
|
|
wp_object_manager_request_object_features (self->metadata_om,
|
|
|
|
|
WP_TYPE_METADATA, WP_OBJECT_FEATURES_ALL);
|
|
|
|
|
g_signal_connect_object (self->metadata_om, "object-added",
|
|
|
|
|
G_CALLBACK (on_metadata_added), transition, 0);
|
|
|
|
|
wp_core_install_object_manager (core, self->metadata_om);
|
|
|
|
|
|
2022-04-25 10:45:34 +05:30
|
|
|
wp_info_object (self, "looking for metadata object named %s",
|
2022-03-31 12:27:22 +05:30
|
|
|
self->metadata_name);
|
2022-03-22 03:38:35 +05:30
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case WP_TRANSITION_STEP_ERROR:
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
g_assert_not_reached ();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
wp_settings_deactivate (WpObject * object, WpObjectFeatures features)
|
|
|
|
|
{
|
|
|
|
|
WpSettings *self = WP_SETTINGS (object);
|
|
|
|
|
|
|
|
|
|
g_clear_object (&self->metadata_om);
|
2022-05-26 13:15:22 +05:30
|
|
|
g_clear_pointer (&self->callbacks, g_ptr_array_unref);
|
2022-03-22 03:38:35 +05:30
|
|
|
|
|
|
|
|
wp_object_update_features (WP_OBJECT (self), 0, WP_OBJECT_FEATURES_ALL);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-15 11:19:14 -05:00
|
|
|
static void
|
|
|
|
|
wp_settings_finalize (GObject * object)
|
|
|
|
|
{
|
|
|
|
|
WpSettings *self = WP_SETTINGS (object);
|
|
|
|
|
|
|
|
|
|
g_clear_pointer (&self->metadata_name, g_free);
|
|
|
|
|
g_clear_pointer (&self->metadata_schema_name, g_free);
|
|
|
|
|
g_clear_pointer (&self->metadata_persistent_name, g_free);
|
|
|
|
|
|
|
|
|
|
g_clear_pointer (&self->schema, g_hash_table_unref);
|
2025-04-14 11:09:56 -04:00
|
|
|
g_clear_pointer (&self->settings, g_hash_table_unref);
|
|
|
|
|
g_clear_pointer (&self->saved_settings, g_hash_table_unref);
|
2024-02-15 11:19:14 -05:00
|
|
|
|
|
|
|
|
g_weak_ref_clear (&self->metadata);
|
|
|
|
|
g_weak_ref_clear (&self->metadata_schema);
|
|
|
|
|
g_weak_ref_clear (&self->metadata_persistent);
|
|
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (wp_settings_parent_class)->finalize (object);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-22 03:38:35 +05:30
|
|
|
static void
|
|
|
|
|
wp_settings_class_init (WpSettingsClass * klass)
|
|
|
|
|
{
|
2022-03-31 12:27:22 +05:30
|
|
|
GObjectClass * object_class = (GObjectClass *) klass;
|
2022-03-22 03:38:35 +05:30
|
|
|
WpObjectClass * wpobject_class = (WpObjectClass *) klass;
|
|
|
|
|
|
2024-02-15 11:19:14 -05:00
|
|
|
object_class->finalize = wp_settings_finalize;
|
2022-03-31 12:27:22 +05:30
|
|
|
object_class->set_property = wp_settings_set_property;
|
|
|
|
|
object_class->get_property = wp_settings_get_property;
|
|
|
|
|
|
2024-02-10 16:42:01 +02:00
|
|
|
wpobject_class->get_supported_features = wp_settings_get_supported_features;
|
2022-03-22 03:38:35 +05:30
|
|
|
wpobject_class->activate_get_next_step = wp_settings_activate_get_next_step;
|
|
|
|
|
wpobject_class->activate_execute_step = wp_settings_activate_execute_step;
|
|
|
|
|
wpobject_class->deactivate = wp_settings_deactivate;
|
2022-03-31 12:27:22 +05:30
|
|
|
|
|
|
|
|
g_object_class_install_property (object_class, PROP_METADATA_NAME,
|
|
|
|
|
g_param_spec_string ("metadata-name", "metadata-name",
|
|
|
|
|
"The metadata object to look after", NULL,
|
|
|
|
|
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
|
2022-03-22 03:38:35 +05:30
|
|
|
}
|
2024-02-10 16:42:01 +02:00
|
|
|
|
2024-02-10 17:48:23 +02:00
|
|
|
/*!
|
|
|
|
|
* \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)
|
|
|
|
|
{
|
|
|
|
|
return g_object_new (WP_TYPE_SETTINGS,
|
|
|
|
|
"core", core,
|
|
|
|
|
"metadata-name", metadata_name ? metadata_name : "sm-settings",
|
|
|
|
|
NULL);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-10 16:42:01 +02:00
|
|
|
static gboolean
|
2024-02-10 17:48:23 +02:00
|
|
|
find_settings_func (gpointer g_object, gpointer metadata_name)
|
2024-02-10 16:42:01 +02:00
|
|
|
{
|
2024-02-10 17:48:23 +02:00
|
|
|
if (!WP_IS_SETTINGS (g_object))
|
2024-02-10 16:42:01 +02:00
|
|
|
return FALSE;
|
|
|
|
|
|
2024-08-29 16:58:14 +03:00
|
|
|
if (metadata_name)
|
|
|
|
|
return g_str_equal (((WpSettings *) g_object)->metadata_name,
|
|
|
|
|
(gchar *) metadata_name);
|
|
|
|
|
else
|
|
|
|
|
return TRUE;
|
2024-02-10 16:42:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
2024-02-10 17:48:23 +02:00
|
|
|
* \brief Finds a registered WpSettings object by its metadata name
|
2024-02-10 16:42:01 +02:00
|
|
|
*
|
|
|
|
|
* \ingroup wpsettings
|
2024-02-10 17:48:23 +02:00
|
|
|
* \param core the WpCore
|
|
|
|
|
* \param metadata_name (nullable): the name of the metadata object that the
|
2024-08-29 16:58:14 +03:00
|
|
|
* settings object is associated with; NULL returns the first settings object
|
|
|
|
|
* that is found
|
2024-02-10 17:48:23 +02:00
|
|
|
* \returns (transfer full) (nullable): the WpSettings object, or NULL if not
|
|
|
|
|
* found
|
2024-02-10 16:42:01 +02:00
|
|
|
*/
|
|
|
|
|
WpSettings *
|
2024-02-10 17:48:23 +02:00
|
|
|
wp_settings_find (WpCore * core, const gchar * metadata_name)
|
2024-02-10 16:42:01 +02:00
|
|
|
{
|
2024-02-10 17:48:23 +02:00
|
|
|
g_return_val_if_fail (WP_IS_CORE (core), NULL);
|
2024-02-10 16:42:01 +02:00
|
|
|
|
2024-02-10 17:48:23 +02:00
|
|
|
GObject *s = wp_core_find_object (core, (GEqualFunc) find_settings_func,
|
2024-08-29 16:58:14 +03:00
|
|
|
metadata_name);
|
2024-02-10 17:48:23 +02:00
|
|
|
return s ? WP_SETTINGS (s) : NULL;
|
2024-02-10 16:42:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Subscribes callback for a given setting pattern(a glob-style pattern
|
|
|
|
|
* matched using g_pattern_match_simple), this allows clients to look
|
|
|
|
|
* for any changes made in settings through metadata.
|
|
|
|
|
*
|
|
|
|
|
* \ingroup wpsettings
|
|
|
|
|
* \param self the settings object
|
|
|
|
|
* \param pattern name of the pattern to match the settings with
|
|
|
|
|
* \param callback (scope async): the callback triggered when the settings
|
|
|
|
|
* change.
|
|
|
|
|
* \param user_data data to pass to \a callback
|
|
|
|
|
* \returns the subscription ID (always greater than 0 for successful
|
|
|
|
|
* subscriptions)
|
|
|
|
|
*/
|
|
|
|
|
guintptr
|
|
|
|
|
wp_settings_subscribe (WpSettings *self,
|
|
|
|
|
const gchar *pattern, WpSettingsChangedCallback callback,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
return wp_settings_subscribe_closure (self, pattern,
|
|
|
|
|
g_cclosure_new (G_CALLBACK (callback), user_data, NULL));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Subscribes callback for a given setting pattern(a glob-style pattern
|
|
|
|
|
* matched using g_pattern_match_simple), this allows clients to look
|
|
|
|
|
* for any changes made in settings through metadata.
|
|
|
|
|
*
|
|
|
|
|
* \ingroup wpsettings
|
|
|
|
|
* \param self the settings object
|
|
|
|
|
* \param pattern name of the pattern to match the settings with
|
|
|
|
|
* \param closure (nullable): a GAsyncReadyCallback wrapped in a GClosure
|
|
|
|
|
* \returns the subscription ID (always greater than 0 for success)
|
|
|
|
|
*/
|
|
|
|
|
guintptr
|
|
|
|
|
wp_settings_subscribe_closure (WpSettings *self, const gchar *pattern,
|
|
|
|
|
GClosure *closure)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (WP_IS_SETTINGS (self), 0);
|
|
|
|
|
g_return_val_if_fail (pattern, 0);
|
|
|
|
|
g_return_val_if_fail (closure, 0);
|
|
|
|
|
|
|
|
|
|
Callback *cb = g_slice_new0 (Callback);
|
|
|
|
|
g_return_val_if_fail (cb, 0);
|
|
|
|
|
|
|
|
|
|
cb->closure = g_closure_ref (closure);
|
|
|
|
|
g_closure_sink (closure);
|
|
|
|
|
if (G_CLOSURE_NEEDS_MARSHAL (closure))
|
|
|
|
|
g_closure_set_marshal (closure, g_cclosure_marshal_generic);
|
|
|
|
|
|
|
|
|
|
cb->pattern = g_strdup (pattern);
|
|
|
|
|
|
|
|
|
|
g_ptr_array_add (self->callbacks, cb);
|
|
|
|
|
|
|
|
|
|
wp_debug_object (self, "callback(%p) subscribed for pattern(%s)",
|
|
|
|
|
(void *) cb, pattern);
|
|
|
|
|
|
|
|
|
|
return (guintptr) cb;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Unsubscribes callback for a given subscription_id.
|
|
|
|
|
*
|
|
|
|
|
* \ingroup wpsettings
|
|
|
|
|
* \param self the settings object
|
|
|
|
|
* \param subscription_id identifies the callback
|
|
|
|
|
* \returns TRUE if success, FALSE otherwise
|
|
|
|
|
*/
|
|
|
|
|
gboolean
|
|
|
|
|
wp_settings_unsubscribe (WpSettings *self, guintptr subscription_id)
|
|
|
|
|
{
|
|
|
|
|
gboolean ret = FALSE;
|
|
|
|
|
g_return_val_if_fail (WP_IS_SETTINGS (self), FALSE);
|
|
|
|
|
g_return_val_if_fail (subscription_id, FALSE);
|
|
|
|
|
|
|
|
|
|
Callback *cb = (Callback *) subscription_id;
|
|
|
|
|
|
|
|
|
|
ret = g_ptr_array_remove (self->callbacks, cb);
|
|
|
|
|
|
|
|
|
|
wp_debug_object (self, "callback(%p) unsubscription %s", (void *) cb,
|
|
|
|
|
(ret)? "succeeded": "failed");
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
2024-02-15 11:19:14 -05:00
|
|
|
* \brief Gets the WpSpaJson value of a setting
|
2024-02-10 16:42:01 +02:00
|
|
|
* \ingroup wpsettings
|
|
|
|
|
* \param self the settings object
|
2024-02-15 11:19:14 -05:00
|
|
|
* \param name the name of the setting
|
|
|
|
|
* \returns (transfer full) (nullable): The WpSpaJson value of the setting, or
|
|
|
|
|
* NULL if the setting does not exist
|
2024-02-10 16:42:01 +02:00
|
|
|
*/
|
|
|
|
|
WpSpaJson *
|
2024-02-15 11:19:14 -05:00
|
|
|
wp_settings_get (WpSettings *self, const gchar *name)
|
2024-02-10 16:42:01 +02:00
|
|
|
{
|
|
|
|
|
const gchar *value;
|
2024-02-15 11:19:14 -05:00
|
|
|
g_autoptr (WpSettingsSpec) spec = NULL;
|
2024-02-10 16:42:01 +02:00
|
|
|
|
|
|
|
|
g_return_val_if_fail (WP_IS_SETTINGS (self), NULL);
|
2024-02-15 11:19:14 -05:00
|
|
|
g_return_val_if_fail (name, NULL);
|
2024-02-10 16:42:01 +02:00
|
|
|
|
2024-02-15 11:19:14 -05:00
|
|
|
spec = wp_settings_get_spec (self, name);
|
2024-03-04 07:51:37 -05:00
|
|
|
if (!spec) {
|
|
|
|
|
wp_warning ("Setting '%s' does not exist in the settings schema", name);
|
2024-02-10 16:42:01 +02:00
|
|
|
return NULL;
|
2024-03-04 07:51:37 -05:00
|
|
|
}
|
2024-02-10 16:42:01 +02:00
|
|
|
|
2025-04-14 11:09:56 -04:00
|
|
|
value = g_hash_table_lookup (self->settings, name);
|
2024-02-15 11:19:14 -05:00
|
|
|
return value ? wp_spa_json_new_wrap_string (value) :
|
|
|
|
|
wp_settings_spec_get_default_value (spec);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Gets the WpSpaJson saved value of a setting
|
|
|
|
|
* \ingroup wpsettings
|
|
|
|
|
* \param self the settings object
|
|
|
|
|
* \param name the name of the setting
|
|
|
|
|
* \returns (transfer full) (nullable): The WpSpaJson saved value of the
|
|
|
|
|
* setting, or NULL if the setting does not exist
|
|
|
|
|
*/
|
|
|
|
|
WpSpaJson *
|
|
|
|
|
wp_settings_get_saved (WpSettings *self, const gchar *name)
|
|
|
|
|
{
|
|
|
|
|
const gchar *value;
|
2024-03-04 07:51:37 -05:00
|
|
|
g_autoptr (WpSettingsSpec) spec = NULL;
|
2024-02-15 11:19:14 -05:00
|
|
|
|
|
|
|
|
g_return_val_if_fail (WP_IS_SETTINGS (self), NULL);
|
|
|
|
|
g_return_val_if_fail (name, NULL);
|
|
|
|
|
|
2024-03-04 07:51:37 -05:00
|
|
|
spec = wp_settings_get_spec (self, name);
|
|
|
|
|
if (!spec) {
|
|
|
|
|
wp_warning ("Setting '%s' does not exist in the settings schema", name);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-14 11:09:56 -04:00
|
|
|
value = g_hash_table_lookup (self->saved_settings, name);
|
2024-02-10 16:42:01 +02:00
|
|
|
return value ? wp_spa_json_new_wrap_string (value) : NULL;
|
|
|
|
|
}
|
2024-02-15 11:19:14 -05:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Gets the specification of a setting
|
|
|
|
|
* \ingroup wpsettings
|
|
|
|
|
* \param self the settings object
|
|
|
|
|
* \param name the name of the setting
|
|
|
|
|
* \returns (transfer full) (nullable): the specification of the setting
|
|
|
|
|
*/
|
|
|
|
|
WpSettingsSpec *
|
|
|
|
|
wp_settings_get_spec (WpSettings *self, const gchar *name)
|
|
|
|
|
{
|
|
|
|
|
WpSettingsSpec *spec;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (WP_IS_SETTINGS (self), NULL);
|
|
|
|
|
g_return_val_if_fail (name, NULL);
|
|
|
|
|
|
|
|
|
|
spec = g_hash_table_lookup (self->schema, name);
|
|
|
|
|
return spec ? wp_settings_spec_ref (spec) : NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Sets a new setting value
|
|
|
|
|
* \ingroup wpsettings
|
|
|
|
|
* \param self the settings object
|
|
|
|
|
* \param name the name of the setting
|
|
|
|
|
* \param value (transfer none): the JSON value of the setting
|
|
|
|
|
* \returns TRUE if the setting could be set, FALSE otherwise
|
|
|
|
|
*/
|
|
|
|
|
gboolean
|
|
|
|
|
wp_settings_set (WpSettings *self, const gchar *name, WpSpaJson *value)
|
|
|
|
|
{
|
|
|
|
|
g_autoptr (WpMetadata) m = NULL;
|
|
|
|
|
g_autoptr (WpSettingsSpec) spec = NULL;
|
|
|
|
|
g_autofree gchar *value_str = NULL;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (WP_IS_SETTINGS (self), FALSE);
|
|
|
|
|
g_return_val_if_fail (name, FALSE);
|
|
|
|
|
g_return_val_if_fail (value, FALSE);
|
|
|
|
|
|
|
|
|
|
m = g_weak_ref_get (&self->metadata);
|
|
|
|
|
if (!m)
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
spec = wp_settings_get_spec (self, name);
|
2024-03-04 07:51:37 -05:00
|
|
|
if (!spec) {
|
|
|
|
|
wp_warning ("Setting '%s' does not exist in the settings schema", name);
|
2024-02-15 11:19:14 -05:00
|
|
|
return FALSE;
|
2024-03-04 07:51:37 -05:00
|
|
|
}
|
2024-02-15 11:19:14 -05:00
|
|
|
|
2024-03-04 07:51:37 -05:00
|
|
|
value_str = wp_spa_json_to_string (value);
|
|
|
|
|
if (!wp_settings_spec_check_value (spec, value)) {
|
|
|
|
|
wp_warning ("Cannot set setting '%s' with value: %s", name, value_str);
|
2024-02-15 11:19:14 -05:00
|
|
|
return FALSE;
|
2024-03-04 07:51:37 -05:00
|
|
|
}
|
2024-02-15 11:19:14 -05:00
|
|
|
|
2025-04-14 11:09:56 -04:00
|
|
|
g_hash_table_insert (self->settings, g_strdup (name), g_strdup (value_str));
|
2024-02-15 11:19:14 -05:00
|
|
|
wp_metadata_set (m, 0, name, "Spa:String:JSON", value_str);
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Resets the setting to its default value
|
|
|
|
|
* \ingroup wpsettings
|
|
|
|
|
* \param self the settings object
|
|
|
|
|
* \param name the name of the setting to reset
|
|
|
|
|
* \returns TRUE if the setting could be reset, FALSE otherwise
|
|
|
|
|
*/
|
|
|
|
|
gboolean
|
|
|
|
|
wp_settings_reset (WpSettings *self, const char *name)
|
|
|
|
|
{
|
|
|
|
|
g_autoptr (WpSettingsSpec) spec = NULL;
|
|
|
|
|
g_autoptr (WpSpaJson) def_value = NULL;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (WP_IS_SETTINGS (self), FALSE);
|
|
|
|
|
g_return_val_if_fail (name, FALSE);
|
|
|
|
|
|
|
|
|
|
spec = wp_settings_get_spec (self, name);
|
2024-03-04 07:51:37 -05:00
|
|
|
if (!spec) {
|
|
|
|
|
wp_warning ("Setting '%s' does not exist in the settings schema", name);
|
2024-02-15 11:19:14 -05:00
|
|
|
return FALSE;
|
2024-03-04 07:51:37 -05:00
|
|
|
}
|
2024-02-15 11:19:14 -05:00
|
|
|
|
|
|
|
|
def_value = wp_settings_spec_get_default_value (spec);
|
|
|
|
|
return wp_settings_set (self, name, def_value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Saves a setting to make it persistent after reboot
|
|
|
|
|
* \ingroup wpsettings
|
|
|
|
|
* \param self the settings object
|
|
|
|
|
* \param name the name of the setting to be saved
|
|
|
|
|
* \returns TRUE if the setting could be saved, FALSE otherwise
|
|
|
|
|
*/
|
|
|
|
|
gboolean
|
|
|
|
|
wp_settings_save (WpSettings *self, const char *name)
|
|
|
|
|
{
|
|
|
|
|
g_autoptr (WpMetadata) mp = NULL;
|
|
|
|
|
g_autoptr (WpSpaJson) value = NULL;
|
|
|
|
|
g_autofree gchar *value_str = NULL;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (WP_IS_SETTINGS (self), FALSE);
|
|
|
|
|
g_return_val_if_fail (name, FALSE);
|
|
|
|
|
|
|
|
|
|
mp = g_weak_ref_get (&self->metadata_persistent);
|
|
|
|
|
if (!mp)
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
value = wp_settings_get (self, name);
|
|
|
|
|
if (!value)
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
value_str = wp_spa_json_to_string (value);
|
2025-04-14 11:09:56 -04:00
|
|
|
g_hash_table_insert (self->saved_settings, g_strdup (name),
|
|
|
|
|
g_strdup (value_str));
|
2024-02-15 11:19:14 -05:00
|
|
|
wp_metadata_set (mp, 0, name, "Spa:String:JSON", value_str);
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Deletes a saved setting to not make it persistent after reboot
|
|
|
|
|
* \ingroup wpsettings
|
|
|
|
|
* \param self the settings object
|
|
|
|
|
* \param name the name of the saved setting to be deleted
|
|
|
|
|
* \returns TRUE if the setting could be deleted, FALSE otherwise
|
|
|
|
|
*/
|
|
|
|
|
gboolean
|
|
|
|
|
wp_settings_delete (WpSettings *self, const char *name)
|
|
|
|
|
{
|
|
|
|
|
g_autoptr (WpMetadata) mp = NULL;
|
2024-03-04 07:51:37 -05:00
|
|
|
g_autoptr (WpSettingsSpec) spec = NULL;
|
2024-02-15 11:19:14 -05:00
|
|
|
|
|
|
|
|
g_return_val_if_fail (WP_IS_SETTINGS (self), FALSE);
|
|
|
|
|
g_return_val_if_fail (name, FALSE);
|
|
|
|
|
|
2024-03-04 07:51:37 -05:00
|
|
|
spec = wp_settings_get_spec (self, name);
|
|
|
|
|
if (!spec) {
|
|
|
|
|
wp_warning ("Setting '%s' does not exist in the settings schema", name);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-15 11:19:14 -05:00
|
|
|
mp = g_weak_ref_get (&self->metadata_persistent);
|
|
|
|
|
if (!mp)
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
2025-04-14 11:09:56 -04:00
|
|
|
g_hash_table_remove (self->saved_settings, name);
|
2024-02-15 11:19:14 -05:00
|
|
|
wp_metadata_set (mp, 0, name, NULL, NULL);
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-14 11:09:56 -04:00
|
|
|
static void
|
|
|
|
|
settings_reset_cb (gpointer key, gpointer value, gpointer data)
|
|
|
|
|
{
|
|
|
|
|
WpSettings *self = WP_SETTINGS (data);
|
|
|
|
|
const gchar *k = key;
|
|
|
|
|
|
|
|
|
|
if (!wp_settings_reset (self, k))
|
|
|
|
|
wp_warning_object (self, "Failed to reset setting %s", k);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-15 11:19:14 -05:00
|
|
|
/*!
|
|
|
|
|
* \brief Resets all the settings to their default value
|
|
|
|
|
* \ingroup wpsettings
|
|
|
|
|
* \param self the settings object
|
|
|
|
|
*/
|
|
|
|
|
void wp_settings_reset_all (WpSettings *self)
|
|
|
|
|
{
|
|
|
|
|
g_return_if_fail (WP_IS_SETTINGS (self));
|
|
|
|
|
|
2025-04-14 11:09:56 -04:00
|
|
|
g_hash_table_foreach (self->settings, settings_reset_cb, self);
|
|
|
|
|
}
|
2024-02-15 11:19:14 -05:00
|
|
|
|
2025-04-14 11:09:56 -04:00
|
|
|
static void
|
|
|
|
|
settings_save_cb (gpointer key, gpointer value, gpointer data)
|
|
|
|
|
{
|
|
|
|
|
WpSettings *self = WP_SETTINGS (data);
|
|
|
|
|
const gchar *k = key;
|
|
|
|
|
|
|
|
|
|
if (!wp_settings_save (self, k))
|
|
|
|
|
wp_warning_object (self, "Failed to save setting %s", k);
|
2024-02-15 11:19:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Saves all the settings to make them persistent after reboot
|
|
|
|
|
* \ingroup wpsettings
|
|
|
|
|
* \param self the settings object
|
|
|
|
|
*/
|
|
|
|
|
void wp_settings_save_all (WpSettings *self)
|
|
|
|
|
{
|
|
|
|
|
g_return_if_fail (WP_IS_SETTINGS (self));
|
|
|
|
|
|
2025-04-14 11:09:56 -04:00
|
|
|
g_hash_table_foreach (self->settings, settings_save_cb, self);
|
2024-02-15 11:19:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Deletes all saved setting to not make them persistent after reboot
|
|
|
|
|
* \ingroup wpsettings
|
|
|
|
|
* \param self the settings object
|
|
|
|
|
*/
|
|
|
|
|
void wp_settings_delete_all (WpSettings *self)
|
|
|
|
|
{
|
|
|
|
|
g_autoptr (WpMetadata) mp = NULL;
|
|
|
|
|
|
|
|
|
|
g_return_if_fail (WP_IS_SETTINGS (self));
|
|
|
|
|
|
2025-04-14 11:09:56 -04:00
|
|
|
g_hash_table_remove_all (self->saved_settings);
|
2024-02-15 11:19:14 -05:00
|
|
|
mp = g_weak_ref_get (&self->metadata_persistent);
|
2025-04-14 11:09:56 -04:00
|
|
|
if (mp)
|
|
|
|
|
wp_metadata_clear (mp);
|
2024-02-15 11:19:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct settings_iterator_data
|
|
|
|
|
{
|
|
|
|
|
WpSettings *settings;
|
2025-04-14 11:09:56 -04:00
|
|
|
const gchar **keys;
|
|
|
|
|
guint len;
|
|
|
|
|
guint curr;
|
2024-02-15 11:19:14 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
settings_iterator_reset (WpIterator *it)
|
|
|
|
|
{
|
|
|
|
|
struct settings_iterator_data *it_data = wp_iterator_get_user_data (it);
|
2025-04-14 11:09:56 -04:00
|
|
|
it_data->curr = 0;
|
2024-02-15 11:19:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
settings_iterator_next (WpIterator *it, GValue *item)
|
|
|
|
|
{
|
|
|
|
|
struct settings_iterator_data *it_data = wp_iterator_get_user_data (it);
|
|
|
|
|
g_autoptr (WpSettingsItem) si = NULL;
|
|
|
|
|
const gchar *key, *value;
|
|
|
|
|
|
2025-04-14 11:09:56 -04:00
|
|
|
if (it_data->curr < it_data->len) {
|
|
|
|
|
key = it_data->keys [it_data->curr++];
|
|
|
|
|
value = g_hash_table_lookup (it_data->settings->settings, key);
|
|
|
|
|
g_return_val_if_fail (value, FALSE);
|
|
|
|
|
} else {
|
2024-02-15 11:19:14 -05:00
|
|
|
return FALSE;
|
2025-04-14 11:09:56 -04:00
|
|
|
}
|
2024-02-15 11:19:14 -05:00
|
|
|
|
2025-04-14 11:09:56 -04:00
|
|
|
si = wp_settings_item_new (key, value);
|
2024-02-15 11:19:14 -05:00
|
|
|
g_value_init (item, WP_TYPE_SETTINGS_ITEM);
|
|
|
|
|
g_value_take_boxed (item, g_steal_pointer (&si));
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
settings_iterator_finalize (WpIterator *it)
|
|
|
|
|
{
|
|
|
|
|
struct settings_iterator_data *it_data = wp_iterator_get_user_data (it);
|
2025-04-14 11:09:56 -04:00
|
|
|
g_clear_pointer (&it_data->keys, g_free);
|
2024-02-15 11:19:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const WpIteratorMethods settings_iterator_methods = {
|
|
|
|
|
.version = WP_ITERATOR_METHODS_VERSION,
|
|
|
|
|
.reset = settings_iterator_reset,
|
|
|
|
|
.next = settings_iterator_next,
|
|
|
|
|
.fold = NULL,
|
|
|
|
|
.foreach = NULL,
|
|
|
|
|
.finalize = settings_iterator_finalize,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Iterates over settings
|
|
|
|
|
* \ingroup wpsettings
|
|
|
|
|
* \param self the settings object
|
|
|
|
|
* \returns (transfer full): an iterator that iterates over the settings.
|
|
|
|
|
*/
|
|
|
|
|
WpIterator *
|
|
|
|
|
wp_settings_new_iterator (WpSettings *self)
|
|
|
|
|
{
|
|
|
|
|
g_autoptr (WpIterator) it = NULL;
|
|
|
|
|
struct settings_iterator_data *it_data;
|
|
|
|
|
g_autoptr (WpMetadata) m = NULL;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (WP_IS_SETTINGS (self), NULL);
|
|
|
|
|
|
|
|
|
|
m = g_weak_ref_get (&self->metadata);
|
|
|
|
|
if (!m)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
it = wp_iterator_new (&settings_iterator_methods,
|
|
|
|
|
sizeof (struct settings_iterator_data));
|
|
|
|
|
it_data = wp_iterator_get_user_data (it);
|
|
|
|
|
it_data->settings = g_object_ref (self);
|
2025-04-14 11:09:56 -04:00
|
|
|
it_data->keys = (const gchar **)g_hash_table_get_keys_as_array (
|
|
|
|
|
self->settings, &it_data->len);
|
|
|
|
|
it_data->curr = 0;
|
2024-02-15 11:19:14 -05:00
|
|
|
return g_steal_pointer (&it);
|
|
|
|
|
}
|