| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2022 Collabora Ltd. |
| 4 |
* @author Ashok Sidipotu <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "core.h" |
| 10 |
#include "settings.h" |
| 11 |
#include "metadata.h" |
| 12 |
#include "log.h" |
| 13 |
#include "object-manager.h" |
| 14 |
|
| 15 |
WP_DEFINE_LOCAL_LOG_TOPIC ("wp-settings") |
| 16 |
|
| 17 |
/*! \defgroup wpsettings WpSettings */ |
| 18 |
|
| 19 |
/*! |
| 20 |
* \struct WpSettingsSpec |
| 21 |
* |
| 22 |
* WpSettingSpec holds the specification of a setting. |
| 23 |
*/ |
| 24 |
struct _WpSettingsSpec { |
| 25 |
grefcount ref; |
| 26 |
gchar *name; |
| 27 |
gchar *desc; |
| 28 |
WpSettingsSpecType type; |
| 29 |
WpSpaJson *def_value; |
| 30 |
WpSpaJson *min_value; |
| 31 |
WpSpaJson *max_value; |
| 32 |
}; |
| 33 |
|
| 34 |
G_DEFINE_BOXED_TYPE (WpSettingsSpec, wp_settings_spec, wp_settings_spec_ref, |
| 35 |
wp_settings_spec_unref) |
| 36 |
|
| 37 |
/*! |
| 38 |
* \brief Increases the reference count of a settings spec object |
| 39 |
* \ingroup wpsettings |
| 40 |
* \param self a settings spec object |
| 41 |
* \returns (transfer full): \a self with an additional reference count on it |
| 42 |
*/ |
| 43 |
WpSettingsSpec * |
| 44 |
wp_settings_spec_ref (WpSettingsSpec * self) |
| 45 |
{ |
| 46 |
g_ref_count_inc (&self->ref); |
| 47 |
return self; |
| 48 |
} |
| 49 |
|
| 50 |
static void |
| 51 |
wp_settings_spec_free (WpSettingsSpec * self) |
| 52 |
{ |
| 53 |
g_clear_pointer (&self->name, g_free); |
| 54 |
g_clear_pointer (&self->desc, g_free); |
| 55 |
g_clear_pointer (&self->def_value, wp_spa_json_unref); |
| 56 |
g_clear_pointer (&self->min_value, wp_spa_json_unref); |
| 57 |
g_clear_pointer (&self->max_value, wp_spa_json_unref); |
| 58 |
g_slice_free (WpSettingsSpec, self); |
| 59 |
} |
| 60 |
|
| 61 |
/*! |
| 62 |
* \brief Decreases the reference count on \a self and frees it when the ref |
| 63 |
* count reaches zero. |
| 64 |
* \ingroup wpsettings |
| 65 |
* \param self (transfer full): a settings spec object |
| 66 |
*/ |
| 67 |
void |
| 68 |
wp_settings_spec_unref (WpSettingsSpec * self) |
| 69 |
{ |
| 70 |
if (g_ref_count_dec (&self->ref)) |
| 71 |
wp_settings_spec_free (self); |
| 72 |
} |
| 73 |
|
| 74 |
static WpSettingsSpec * |
| 75 |
wp_settings_spec_new (WpSpaJson * spec_json) |
| 76 |
{ |
| 77 |
WpSettingsSpec *self; |
| 78 |
g_autofree gchar *name = NULL; |
| 79 |
g_autofree gchar *desc = NULL; |
| 80 |
g_autofree gchar *type_str = NULL; |
| 81 |
WpSettingsSpecType type = WP_SETTINGS_SPEC_TYPE_UNKNOWN; |
| 82 |
g_autoptr (WpSpaJson) def_value = NULL; |
| 83 |
g_autoptr (WpSpaJson) min_value = NULL; |
| 84 |
g_autoptr (WpSpaJson) max_value = NULL; |
| 85 |
|
| 86 |
g_return_val_if_fail (spec_json, NULL); |
| 87 |
|
| 88 |
if (!wp_spa_json_is_object (spec_json)) |
| 89 |
return NULL; |
| 90 |
|
| 91 |
/* Parse mandatory fields */ |
| 92 |
if (!wp_spa_json_object_get (spec_json, |
| 93 |
"description", "s", &desc, |
| 94 |
"type", "s", &type_str, |
| 95 |
"default", "J", &def_value, |
| 96 |
NULL)) |
| 97 |
return NULL; |
| 98 |
|
| 99 |
/* Parse optional fields */ |
| 100 |
wp_spa_json_object_get (spec_json, "name", "s", &name, NULL); |
| 101 |
|
| 102 |
/* Parse type and check if values are correct */ |
| 103 |
if (g_str_equal (type_str, "bool")) { |
| 104 |
type = WP_SETTINGS_SPEC_TYPE_BOOL; |
| 105 |
if (!wp_spa_json_is_boolean (def_value)) |
| 106 |
return NULL; |
| 107 |
} else if (g_str_equal (type_str, "int")) { |
| 108 |
type = WP_SETTINGS_SPEC_TYPE_INT; |
| 109 |
if (!wp_spa_json_object_get (spec_json, |
| 110 |
"min", "J", &min_value, |
| 111 |
"max", "J", &max_value, |
| 112 |
NULL)) |
| 113 |
return NULL; |
| 114 |
if (!wp_spa_json_is_int (def_value) || |
| 115 |
!min_value || !wp_spa_json_is_int (min_value) || |
| 116 |
!max_value || !wp_spa_json_is_int (max_value)) |
| 117 |
return NULL; |
| 118 |
} else if (g_str_equal (type_str, "float")) { |
| 119 |
type = WP_SETTINGS_SPEC_TYPE_FLOAT; |
| 120 |
if (!wp_spa_json_object_get (spec_json, |
| 121 |
"min", "J", &min_value, |
| 122 |
"max", "J", &max_value, |
| 123 |
NULL)) |
| 124 |
return NULL; |
| 125 |
if (!wp_spa_json_is_float (def_value) || |
| 126 |
!min_value || !wp_spa_json_is_float (min_value) || |
| 127 |
!max_value || !wp_spa_json_is_float (max_value)) |
| 128 |
return NULL; |
| 129 |
} else if (g_str_equal (type_str, "string")) { |
| 130 |
type = WP_SETTINGS_SPEC_TYPE_STRING; |
| 131 |
} else if (g_str_equal (type_str, "array")) { |
| 132 |
type = WP_SETTINGS_SPEC_TYPE_ARRAY; |
| 133 |
if (!wp_spa_json_is_array (def_value)) |
| 134 |
return NULL; |
| 135 |
} else if (g_str_equal (type_str, "object")) { |
| 136 |
type = WP_SETTINGS_SPEC_TYPE_OBJECT; |
| 137 |
if (!wp_spa_json_is_object (def_value)) |
| 138 |
return NULL; |
| 139 |
} else { |
| 140 |
return NULL; |
| 141 |
} |
| 142 |
|
| 143 |
self = g_slice_new0 (WpSettingsSpec); |
| 144 |
g_ref_count_init (&self->ref); |
| 145 |
self->name = g_steal_pointer (&name); |
| 146 |
self->desc = g_steal_pointer (&desc); |
| 147 |
self->type = type; |
| 148 |
self->def_value = g_steal_pointer (&def_value); |
| 149 |
self->min_value = g_steal_pointer (&min_value); |
| 150 |
self->max_value = g_steal_pointer (&max_value); |
| 151 |
return self; |
| 152 |
} |
| 153 |
|
| 154 |
/*! |
| 155 |
* \brief Gets the human-readable name of a settings spec |
| 156 |
* \ingroup wpsettings |
| 157 |
* \param self the settings spec object |
| 158 |
* \returns (nullable): the human-readable name of the settings spec, |
| 159 |
* or NULL if none |
| 160 |
*/ |
| 161 |
const gchar * |
| 162 |
wp_settings_spec_get_name (WpSettingsSpec * self) |
| 163 |
{ |
| 164 |
g_return_val_if_fail (self, NULL); |
| 165 |
return self->name; |
| 166 |
} |
| 167 |
|
| 168 |
/*! |
| 169 |
* \brief Gets the description of a settings spec |
| 170 |
* \ingroup wpsettings |
| 171 |
* \param self the settings spec object |
| 172 |
* \returns the description of the settings spec |
| 173 |
*/ |
| 174 |
const gchar * |
| 175 |
wp_settings_spec_get_description (WpSettingsSpec * self) |
| 176 |
{ |
| 177 |
g_return_val_if_fail (self, NULL); |
| 178 |
return self->desc; |
| 179 |
} |
| 180 |
|
| 181 |
/*! |
| 182 |
* \brief Gets the type of a settings spec |
| 183 |
* \ingroup wpsettings |
| 184 |
* \param self the settings spec object |
| 185 |
* \returns the type of the settings spec |
| 186 |
*/ |
| 187 |
WpSettingsSpecType |
| 188 |
wp_settings_spec_get_value_type (WpSettingsSpec * self) |
| 189 |
{ |
| 190 |
g_return_val_if_fail (self, WP_SETTINGS_SPEC_TYPE_UNKNOWN); |
| 191 |
return self->type; |
| 192 |
} |
| 193 |
|
| 194 |
/*! |
| 195 |
* \brief Gets the default value of a settings spec |
| 196 |
* \ingroup wpsettings |
| 197 |
* \param self the settings spec object |
| 198 |
* \returns (transfer full): the default value of the settings spec |
| 199 |
*/ |
| 200 |
WpSpaJson * |
| 201 |
wp_settings_spec_get_default_value (WpSettingsSpec * self) |
| 202 |
{ |
| 203 |
g_return_val_if_fail (self, NULL); |
| 204 |
g_return_val_if_fail (self->def_value, NULL); |
| 205 |
return wp_spa_json_ref (self->def_value); |
| 206 |
} |
| 207 |
|
| 208 |
/*! |
| 209 |
* \brief Gets the minimum value of a settings spec. |
| 210 |
* \ingroup wpsettings |
| 211 |
* \param self the settings spec object |
| 212 |
* \returns (transfer full)(nullable): the minimum value of the settings spec, |
| 213 |
* or NULL if the spec type is not WP_SETTINGS_SPEC_TYPE_INT or |
| 214 |
* WP_SETTINGS_SPEC_TYPE_FLOAT |
| 215 |
*/ |
| 216 |
WpSpaJson * |
| 217 |
wp_settings_spec_get_min_value (WpSettingsSpec * self) |
| 218 |
{ |
| 219 |
g_return_val_if_fail (self, NULL); |
| 220 |
return self->min_value ? wp_spa_json_ref (self->min_value) : NULL; |
| 221 |
} |
| 222 |
|
| 223 |
/*! |
| 224 |
* \brief Gets the maximum value of a settings spec. |
| 225 |
* \ingroup wpsettings |
| 226 |
* \param self the settings spec object |
| 227 |
* \returns (transfer full)(nullable): the maximum value of the settings spec, |
| 228 |
* or NULL if the spec type is not WP_SETTINGS_SPEC_TYPE_INT or |
| 229 |
* WP_SETTINGS_SPEC_TYPE_FLOAT |
| 230 |
*/ |
| 231 |
WpSpaJson * |
| 232 |
wp_settings_spec_get_max_value (WpSettingsSpec * self) |
| 233 |
{ |
| 234 |
g_return_val_if_fail (self, NULL); |
| 235 |
return self->max_value ? wp_spa_json_ref (self->max_value) : NULL; |
| 236 |
} |
| 237 |
|
| 238 |
/*! |
| 239 |
* \brief Checks whether a value is compatible with the spec or not |
| 240 |
* \ingroup wpsettings |
| 241 |
* \param self the settings spec object |
| 242 |
* \param value (transfer none): the value to check |
| 243 |
* \returns TRUE if the value is compatible with the spec, FALSE otherwise |
| 244 |
*/ |
| 245 |
gboolean |
| 246 |
wp_settings_spec_check_value (WpSettingsSpec * self, WpSpaJson *value) |
| 247 |
{ |
| 248 |
g_return_val_if_fail (self, FALSE); |
| 249 |
g_return_val_if_fail (value, FALSE); |
| 250 |
|
| 251 |
switch (self->type) { |
| 252 |
case WP_SETTINGS_SPEC_TYPE_BOOL: |
| 253 |
return wp_spa_json_is_boolean (value); |
| 254 |
case WP_SETTINGS_SPEC_TYPE_INT: { |
| 255 |
gint val = 0, min = 0, max = 0; |
| 256 |
if (!wp_spa_json_is_int (value) || !wp_spa_json_parse_int (value, &val)) |
| 257 |
return FALSE; |
| 258 |
if (!wp_spa_json_parse_int (self->min_value, &min) || |
| 259 |
!wp_spa_json_parse_int (self->max_value, &max)) |
| 260 |
return FALSE; |
| 261 |
return val >= min && val <= max; |
| 262 |
} |
| 263 |
case WP_SETTINGS_SPEC_TYPE_FLOAT: { |
| 264 |
float val = 0.0, min = 0.0, max = 0.0; |
| 265 |
if (wp_spa_json_is_int (value) || !wp_spa_json_is_float (value) || |
| 266 |
!wp_spa_json_parse_float (value, &val)) |
| 267 |
return FALSE; |
| 268 |
if (!wp_spa_json_parse_float (self->min_value, &min) || |
| 269 |
!wp_spa_json_parse_float (self->max_value, &max)) |
| 270 |
return FALSE; |
| 271 |
return val >= min && val <= max; |
| 272 |
} |
| 273 |
case WP_SETTINGS_SPEC_TYPE_STRING: |
| 274 |
/* We also accept strings without quotes, which is why we dont use |
| 275 |
* wp_spa_json_is_string() */ |
| 276 |
return !wp_spa_json_is_boolean (value) && !wp_spa_json_is_int (value) && |
| 277 |
!wp_spa_json_is_float (value) && !wp_spa_json_is_array (value) && |
| 278 |
!wp_spa_json_is_object (value); |
| 279 |
case WP_SETTINGS_SPEC_TYPE_ARRAY: |
| 280 |
return wp_spa_json_is_array (value); |
| 281 |
case WP_SETTINGS_SPEC_TYPE_OBJECT: |
| 282 |
return wp_spa_json_is_object (value); |
| 283 |
default: |
| 284 |
break; |
| 285 |
} |
| 286 |
|
| 287 |
return FALSE; |
| 288 |
} |
| 289 |
|
| 290 |
/*! |
| 291 |
* \struct WpSettingsItem |
| 292 |
* |
| 293 |
* WpSettingsItem holds the key and value of a setting |
| 294 |
*/ |
| 295 |
struct _WpSettingsItem |
| 296 |
{ |
| 297 |
gchar *key; |
| 298 |
WpSpaJson *value; |
| 299 |
}; |
| 300 |
|
| 301 |
G_DEFINE_BOXED_TYPE (WpSettingsItem, wp_settings_item, |
| 302 |
wp_settings_item_ref, wp_settings_item_unref) |
| 303 |
|
| 304 |
static WpSettingsItem * |
| 305 |
wp_settings_item_new (const gchar *key, const gchar *value) |
| 306 |
{ |
| 307 |
WpSettingsItem *self = g_rc_box_new0 (WpSettingsItem); |
| 308 |
self->key = g_strdup (key); |
| 309 |
self->value = wp_spa_json_new_from_string (value); |
| 310 |
return self; |
| 311 |
} |
| 312 |
|
| 313 |
static void |
| 314 |
wp_settings_item_free (gpointer p) |
| 315 |
{ |
| 316 |
WpSettingsItem *self = p; |
| 317 |
g_clear_pointer (&self->key, g_free); |
| 318 |
g_clear_pointer (&self->value, wp_spa_json_unref); |
| 319 |
} |
| 320 |
|
| 321 |
/*! |
| 322 |
* \brief Increases the reference count of a settings item object |
| 323 |
* \ingroup wpsettings |
| 324 |
* \param self a settings item object |
| 325 |
* \returns (transfer full): \a self with an additional reference count on it |
| 326 |
*/ |
| 327 |
WpSettingsItem * |
| 328 |
wp_settings_item_ref (WpSettingsItem *self) |
| 329 |
{ |
| 330 |
return g_rc_box_acquire (self); |
| 331 |
} |
| 332 |
|
| 333 |
/*! |
| 334 |
* \brief Decreases the reference count on \a self and frees it when the ref |
| 335 |
* count reaches zero. |
| 336 |
* \ingroup wpsettings |
| 337 |
* \param self (transfer full): a settings item object |
| 338 |
*/ |
| 339 |
void |
| 340 |
wp_settings_item_unref (WpSettingsItem *self) |
| 341 |
{ |
| 342 |
g_rc_box_release_full (self, wp_settings_item_free); |
| 343 |
} |
| 344 |
|
| 345 |
/*! |
| 346 |
* \brief Gets the key from a settings item |
| 347 |
* |
| 348 |
* \ingroup wpsettings |
| 349 |
* \param self the item held by the GValue that was returned from the WpIterator |
| 350 |
* of wp_settings_new_iterator() |
| 351 |
* \returns (transfer none): the settings key of the \a item |
| 352 |
*/ |
| 353 |
const gchar * |
| 354 |
wp_settings_item_get_key (WpSettingsItem * self) |
| 355 |
{ |
| 356 |
return self->key; |
| 357 |
} |
| 358 |
|
| 359 |
/*! |
| 360 |
* \brief Gets the value from a settings item |
| 361 |
* |
| 362 |
* \ingroup wpsettings |
| 363 |
* \param self the item held by the GValue that was returned from the WpIterator |
| 364 |
* of wp_settings_new_iterator() |
| 365 |
* \returns (transfer full): the settings value of the \a item |
| 366 |
*/ |
| 367 |
WpSpaJson * |
| 368 |
wp_settings_item_get_value (WpSettingsItem * self) |
| 369 |
{ |
| 370 |
return wp_spa_json_ref (self->value); |
| 371 |
} |
| 372 |
|
| 373 |
/*! |
| 374 |
* \struct WpSettings |
| 375 |
* |
| 376 |
* WpSettings loads and parses the "sm-settings" (default value) metadata, which |
| 377 |
* contains wireplumber settings, and provides APIs to its clients (modules, lua |
| 378 |
* scripts etc) to access them. |
| 379 |
* |
| 380 |
* Being a WpObject subclass, the settings inherits WpObject's activation |
| 381 |
* system. |
| 382 |
*/ |
| 383 |
|
| 384 |
struct _WpSettings |
| 385 |
{ |
| 386 |
WpObject parent; |
| 387 |
|
| 388 |
/* element-type: Callback* */ |
| 389 |
GPtrArray *callbacks; |
| 390 |
|
| 391 |
gchar *metadata_name; |
| 392 |
gchar *metadata_schema_name; |
| 393 |
gchar *metadata_persistent_name; |
| 394 |
|
| 395 |
WpObjectManager *metadata_om; |
| 396 |
GWeakRef metadata; |
| 397 |
GWeakRef metadata_schema; |
| 398 |
GWeakRef metadata_persistent; |
| 399 |
|
| 400 |
/* We keep a hash table with all the settings info to make sure their values |
| 401 |
* are always updated when using the settings API. This avoids syncing issues |
| 402 |
* if pipewire has not finished syncing the metadata objects yet */ |
| 403 |
GHashTable *schema; |
| 404 |
GHashTable *settings; |
| 405 |
GHashTable *saved_settings; |
| 406 |
}; |
| 407 |
|
| 408 |
typedef struct |
| 409 |
{ |
| 410 |
GClosure *closure; |
| 411 |
gchar *pattern; |
| 412 |
} Callback; |
| 413 |
|
| 414 |
enum { |
| 415 |
PROP_0, |
| 416 |
PROP_METADATA_NAME, |
| 417 |
PROP_PROPERTIES, |
| 418 |
}; |
| 419 |
|
| 420 |
G_DEFINE_TYPE (WpSettings, wp_settings, WP_TYPE_OBJECT) |
| 421 |
|
| 422 |
static void |
| 423 |
wp_settings_init (WpSettings * self) |
| 424 |
{ |
| 425 |
g_weak_ref_init (&self->metadata, NULL); |
| 426 |
g_weak_ref_init (&self->metadata_schema, NULL); |
| 427 |
g_weak_ref_init (&self->metadata_persistent, NULL); |
| 428 |
|
| 429 |
self->schema = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, |
| 430 |
(GDestroyNotify) wp_settings_spec_unref); |
| 431 |
self->settings = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, |
| 432 |
(GDestroyNotify) g_free); |
| 433 |
self->saved_settings = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, |
| 434 |
(GDestroyNotify) g_free); |
| 435 |
} |
| 436 |
|
| 437 |
static void |
| 438 |
wp_settings_set_property (GObject * object, guint property_id, |
| 439 |
const GValue * value, GParamSpec * pspec) |
| 440 |
{ |
| 441 |
WpSettings *self = WP_SETTINGS (object); |
| 442 |
|
| 443 |
switch (property_id) { |
| 444 |
case PROP_METADATA_NAME: |
| 445 |
self->metadata_name = g_value_dup_string (value); |
| 446 |
self->metadata_schema_name = g_strdup_printf ( |
| 447 |
WP_SETTINGS_SCHEMA_METADATA_NAME_PREFIX "%s", self->metadata_name); |
| 448 |
self->metadata_persistent_name = g_strdup_printf ( |
| 449 |
WP_SETTINGS_PERSISTENT_METADATA_NAME_PREFIX "%s", self->metadata_name); |
| 450 |
break; |
| 451 |
default: |
| 452 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); |
| 453 |
break; |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
static void |
| 458 |
wp_settings_get_property (GObject * object, guint property_id, |
| 459 |
GValue * value, GParamSpec * pspec) |
| 460 |
{ |
| 461 |
WpSettings *self = WP_SETTINGS (object); |
| 462 |
|
| 463 |
switch (property_id) { |
| 464 |
case PROP_METADATA_NAME: |
| 465 |
g_value_set_string (value, self->metadata_name); |
| 466 |
break; |
| 467 |
default: |
| 468 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); |
| 469 |
break; |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
enum { |
| 474 |
STEP_LOAD = WP_TRANSITION_STEP_CUSTOM_START, |
| 475 |
}; |
| 476 |
|
| 477 |
static WpObjectFeatures |
| 478 |
wp_settings_get_supported_features (WpObject * self) |
| 479 |
{ |
| 480 |
return WP_SETTINGS_LOADED; |
| 481 |
} |
| 482 |
|
| 483 |
static guint |
| 484 |
wp_settings_activate_get_next_step (WpObject * object, |
| 485 |
WpFeatureActivationTransition * transition, guint step, |
| 486 |
WpObjectFeatures missing) |
| 487 |
{ |
| 488 |
g_return_val_if_fail (missing == WP_SETTINGS_LOADED, |
| 489 |
WP_TRANSITION_STEP_ERROR); |
| 490 |
|
| 491 |
return STEP_LOAD; |
| 492 |
} |
| 493 |
|
| 494 |
static void |
| 495 |
settings_notify_changed (WpSettings *self, const gchar *key, const gchar *value) |
| 496 |
{ |
| 497 |
for (guint i = 0; i < self->callbacks->len; i++) { |
| 498 |
Callback *cb = g_ptr_array_index (self->callbacks, i); |
| 499 |
|
| 500 |
if (g_pattern_match_simple (cb->pattern, key)) { |
| 501 |
g_autoptr (WpSpaJson) json = NULL; |
| 502 |
GValue values[3] = { G_VALUE_INIT, G_VALUE_INIT, G_VALUE_INIT }; |
| 503 |
|
| 504 |
g_value_init (&values[0], G_TYPE_OBJECT); |
| 505 |
g_value_init (&values[1], G_TYPE_STRING); |
| 506 |
g_value_init (&values[2], WP_TYPE_SPA_JSON); |
| 507 |
|
| 508 |
g_value_set_object (&values[0], self); |
| 509 |
g_value_set_string (&values[1], key); |
| 510 |
json = value ? wp_spa_json_new_wrap_string (value) : NULL; |
| 511 |
g_value_set_boxed (&values[2], json); |
| 512 |
|
| 513 |
g_closure_invoke (cb->closure, NULL, 3, values, NULL); |
| 514 |
|
| 515 |
g_value_unset (&values[0]); |
| 516 |
g_value_unset (&values[1]); |
| 517 |
g_value_unset (&values[2]); |
| 518 |
} |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
static void |
| 523 |
settings_notify_changed_cb (gpointer key, gpointer value, gpointer data) |
| 524 |
{ |
| 525 |
WpSettings *self = WP_SETTINGS (data); |
| 526 |
const gchar *k = key; |
| 527 |
const gchar *v = value; |
| 528 |
|
| 529 |
settings_notify_changed (self, k, v); |
| 530 |
} |
| 531 |
|
| 532 |
static void |
| 533 |
on_metadata_changed (WpMetadata *m, guint32 subject, |
| 534 |
const gchar *key, const gchar *type, const gchar *value, gpointer d) |
| 535 |
{ |
| 536 |
WpSettings *self = WP_SETTINGS (d); |
| 537 |
|
| 538 |
if (key) { |
| 539 |
if (value) { |
| 540 |
g_hash_table_insert (self->settings, g_strdup (key), g_strdup (value)); |
| 541 |
wp_info_object (self, "setting \"%s\" changed to \"%s\"", key, value); |
| 542 |
} else { |
| 543 |
g_hash_table_remove (self->settings, key); |
| 544 |
wp_info_object (self, "setting \"%s\" removed", key); |
| 545 |
} |
| 546 |
|
| 547 |
settings_notify_changed (self, key, value); |
| 548 |
} else { |
| 549 |
wp_info_object (self, "all settings removed"); |
| 550 |
g_hash_table_foreach (self->settings, settings_notify_changed_cb, self); |
| 551 |
g_hash_table_remove_all (self->settings); |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
static void |
| 556 |
on_metadata_persistent_changed (WpMetadata *m, guint32 subject, |
| 557 |
const gchar *key, const gchar *type, const gchar *value, gpointer d) |
| 558 |
{ |
| 559 |
WpSettings *self = WP_SETTINGS (d); |
| 560 |
|
| 561 |
if (key) { |
| 562 |
if (value) { |
| 563 |
g_hash_table_insert (self->saved_settings, g_strdup (key), |
| 564 |
g_strdup (value)); |
| 565 |
wp_info_object (self, "saved setting \"%s\" changed to \"%s\"", key, |
| 566 |
value); |
| 567 |
} else { |
| 568 |
g_hash_table_remove (self->saved_settings, key); |
| 569 |
wp_info_object (self, "saved setting \"%s\" removed", key); |
| 570 |
} |
| 571 |
} else { |
| 572 |
wp_info_object (self, "all saved settings removed"); |
| 573 |
g_hash_table_remove_all (self->saved_settings); |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
static void |
| 578 |
on_metadata_added (WpObjectManager *om, WpMetadata *m, gpointer d) |
| 579 |
{ |
| 580 |
WpTransition * transition = WP_TRANSITION (d); |
| 581 |
WpSettings * self = wp_transition_get_source_object (transition); |
| 582 |
g_autoptr (WpProperties) props = NULL; |
| 583 |
const gchar *metadata_name = NULL; |
| 584 |
g_autoptr (WpMetadata) metadata = NULL; |
| 585 |
g_autoptr (WpMetadata) metadata_schema = NULL; |
| 586 |
g_autoptr (WpMetadata) metadata_persistent = NULL; |
| 587 |
|
| 588 |
/* make sure the metadata has a name */ |
| 589 |
props = wp_global_proxy_get_global_properties (WP_GLOBAL_PROXY (m)); |
| 590 |
if (props) |
| 591 |
metadata_name = wp_properties_get (props, "metadata.name"); |
| 592 |
if (!metadata_name) |
| 593 |
return; |
| 594 |
|
| 595 |
/* sm-settings */ |
| 596 |
if (g_str_equal (metadata_name, self->metadata_name)) { |
| 597 |
g_autoptr (WpIterator) it = NULL; |
| 598 |
g_auto (GValue) item = G_VALUE_INIT; |
| 599 |
it = wp_metadata_new_iterator (m, 0); |
| 600 |
for (; wp_iterator_next (it, &item); g_value_unset (&item)) { |
| 601 |
WpMetadataItem *mi = g_value_get_boxed (&item); |
| 602 |
const gchar *key = wp_metadata_item_get_key (mi); |
| 603 |
const gchar *value = wp_metadata_item_get_value (mi); |
| 604 |
g_hash_table_insert (self->settings, g_strdup (key), g_strdup (value)); |
| 605 |
} |
| 606 |
g_signal_connect_object (m, "changed", G_CALLBACK (on_metadata_changed), |
| 607 |
self, 0); |
| 608 |
g_weak_ref_set (&self->metadata, m); |
| 609 |
} |
| 610 |
|
| 611 |
/* schema-sm-settings */ |
| 612 |
else if (g_str_equal (metadata_name, self->metadata_schema_name)) { |
| 613 |
g_autoptr (WpIterator) it = NULL; |
| 614 |
g_auto (GValue) item = G_VALUE_INIT; |
| 615 |
it = wp_metadata_new_iterator (m, 0); |
| 616 |
for (; wp_iterator_next (it, &item); g_value_unset (&item)) { |
| 617 |
WpMetadataItem *mi = g_value_get_boxed (&item); |
| 618 |
const gchar *key = wp_metadata_item_get_key (mi); |
| 619 |
const gchar *value = wp_metadata_item_get_value (mi); |
| 620 |
g_autoptr (WpSpaJson) spec_json = NULL; |
| 621 |
g_autoptr (WpSettingsSpec) spec = NULL; |
| 622 |
spec_json = wp_spa_json_new_from_string (value); |
| 623 |
spec = wp_settings_spec_new (spec_json); |
| 624 |
if (spec) |
| 625 |
g_hash_table_insert (self->schema, g_strdup (key), |
| 626 |
g_steal_pointer (&spec)); |
| 627 |
else |
| 628 |
wp_warning_object (self, "malformed setting spec: %s", value); |
| 629 |
} |
| 630 |
g_weak_ref_set (&self->metadata_schema, m); |
| 631 |
} |
| 632 |
|
| 633 |
/* presistent-sm-settings */ |
| 634 |
else if (g_str_equal (metadata_name, self->metadata_persistent_name)) { |
| 635 |
g_autoptr (WpIterator) it = NULL; |
| 636 |
g_auto (GValue) item = G_VALUE_INIT; |
| 637 |
it = wp_metadata_new_iterator (m, 0); |
| 638 |
for (; wp_iterator_next (it, &item); g_value_unset (&item)) { |
| 639 |
WpMetadataItem *mi = g_value_get_boxed (&item); |
| 640 |
const gchar *key = wp_metadata_item_get_key (mi); |
| 641 |
const gchar *value = wp_metadata_item_get_value (mi); |
| 642 |
g_hash_table_insert (self->saved_settings, g_strdup (key), |
| 643 |
g_strdup (value)); |
| 644 |
} |
| 645 |
g_signal_connect_object (m, "changed", |
| 646 |
G_CALLBACK (on_metadata_persistent_changed), self, 0); |
| 647 |
g_weak_ref_set (&self->metadata_persistent, m); |
| 648 |
} |
| 649 |
|
| 650 |
/* Finish loading when all metadatas are found */ |
| 651 |
metadata = g_weak_ref_get (&self->metadata); |
| 652 |
metadata_schema = g_weak_ref_get (&self->metadata_schema); |
| 653 |
metadata_persistent = g_weak_ref_get (&self->metadata_persistent); |
| 654 |
if (metadata && metadata_schema && metadata_persistent) |
| 655 |
wp_object_update_features (WP_OBJECT (self), WP_SETTINGS_LOADED, 0); |
| 656 |
} |
| 657 |
|
| 658 |
static void |
| 659 |
callback_unref (Callback * self) |
| 660 |
{ |
| 661 |
g_free (self->pattern); |
| 662 |
g_clear_pointer (&self->closure, g_closure_unref); |
| 663 |
g_slice_free (Callback, self); |
| 664 |
} |
| 665 |
|
| 666 |
static void |
| 667 |
wp_settings_activate_execute_step (WpObject * object, |
| 668 |
WpFeatureActivationTransition * transition, guint step, |
| 669 |
WpObjectFeatures missing) |
| 670 |
{ |
| 671 |
WpSettings * self = WP_SETTINGS (object); |
| 672 |
g_autoptr (WpCore) core = wp_object_get_core (object); |
| 673 |
|
| 674 |
switch (step) { |
| 675 |
case STEP_LOAD: { |
| 676 |
self->callbacks = g_ptr_array_new_with_free_func |
| 677 |
((GDestroyNotify) callback_unref); |
| 678 |
|
| 679 |
self->metadata_om = wp_object_manager_new (); |
| 680 |
wp_object_manager_add_interest (self->metadata_om, WP_TYPE_METADATA, |
| 681 |
WP_CONSTRAINT_TYPE_PW_GLOBAL_PROPERTY, "metadata.name", "=s", |
| 682 |
self->metadata_name, NULL); |
| 683 |
wp_object_manager_add_interest (self->metadata_om, WP_TYPE_METADATA, |
| 684 |
WP_CONSTRAINT_TYPE_PW_GLOBAL_PROPERTY, "metadata.name", "=s", |
| 685 |
self->metadata_schema_name, NULL); |
| 686 |
wp_object_manager_add_interest (self->metadata_om, WP_TYPE_METADATA, |
| 687 |
WP_CONSTRAINT_TYPE_PW_GLOBAL_PROPERTY, "metadata.name", "=s", |
| 688 |
self->metadata_persistent_name, NULL); |
| 689 |
wp_object_manager_request_object_features (self->metadata_om, |
| 690 |
WP_TYPE_METADATA, WP_OBJECT_FEATURES_ALL); |
| 691 |
g_signal_connect_object (self->metadata_om, "object-added", |
| 692 |
G_CALLBACK (on_metadata_added), transition, 0); |
| 693 |
wp_core_install_object_manager (core, self->metadata_om); |
| 694 |
|
| 695 |
wp_info_object (self, "looking for metadata object named %s", |
| 696 |
self->metadata_name); |
| 697 |
break; |
| 698 |
} |
| 699 |
case WP_TRANSITION_STEP_ERROR: |
| 700 |
break; |
| 701 |
default: |
| 702 |
g_assert_not_reached (); |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
static void |
| 707 |
wp_settings_deactivate (WpObject * object, WpObjectFeatures features) |
| 708 |
{ |
| 709 |
WpSettings *self = WP_SETTINGS (object); |
| 710 |
|
| 711 |
g_clear_object (&self->metadata_om); |
| 712 |
g_clear_pointer (&self->callbacks, g_ptr_array_unref); |
| 713 |
|
| 714 |
wp_object_update_features (WP_OBJECT (self), 0, WP_OBJECT_FEATURES_ALL); |
| 715 |
} |
| 716 |
|
| 717 |
static void |
| 718 |
wp_settings_finalize (GObject * object) |
| 719 |
{ |
| 720 |
WpSettings *self = WP_SETTINGS (object); |
| 721 |
|
| 722 |
g_clear_pointer (&self->metadata_name, g_free); |
| 723 |
g_clear_pointer (&self->metadata_schema_name, g_free); |
| 724 |
g_clear_pointer (&self->metadata_persistent_name, g_free); |
| 725 |
|
| 726 |
g_clear_pointer (&self->schema, g_hash_table_unref); |
| 727 |
g_clear_pointer (&self->settings, g_hash_table_unref); |
| 728 |
g_clear_pointer (&self->saved_settings, g_hash_table_unref); |
| 729 |
|
| 730 |
g_weak_ref_clear (&self->metadata); |
| 731 |
g_weak_ref_clear (&self->metadata_schema); |
| 732 |
g_weak_ref_clear (&self->metadata_persistent); |
| 733 |
|
| 734 |
G_OBJECT_CLASS (wp_settings_parent_class)->finalize (object); |
| 735 |
} |
| 736 |
|
| 737 |
static void |
| 738 |
wp_settings_class_init (WpSettingsClass * klass) |
| 739 |
{ |
| 740 |
GObjectClass * object_class = (GObjectClass *) klass; |
| 741 |
WpObjectClass * wpobject_class = (WpObjectClass *) klass; |
| 742 |
|
| 743 |
object_class->finalize = wp_settings_finalize; |
| 744 |
object_class->set_property = wp_settings_set_property; |
| 745 |
object_class->get_property = wp_settings_get_property; |
| 746 |
|
| 747 |
wpobject_class->get_supported_features = wp_settings_get_supported_features; |
| 748 |
wpobject_class->activate_get_next_step = wp_settings_activate_get_next_step; |
| 749 |
wpobject_class->activate_execute_step = wp_settings_activate_execute_step; |
| 750 |
wpobject_class->deactivate = wp_settings_deactivate; |
| 751 |
|
| 752 |
g_object_class_install_property (object_class, PROP_METADATA_NAME, |
| 753 |
g_param_spec_string ("metadata-name", "metadata-name", |
| 754 |
"The metadata object to look after", NULL, |
| 755 |
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS)); |
| 756 |
} |
| 757 |
|
| 758 |
/*! |
| 759 |
* \brief Creates a new WpSettings object |
| 760 |
* |
| 761 |
* \ingroup wpsettings |
| 762 |
* \param core the WpCore |
| 763 |
* \param metadata_name (nullable): the name of the metadata object to |
| 764 |
* associate with the settings object; NULL means the default "sm-settings" |
| 765 |
* \returns (transfer full): a new WpSettings object |
| 766 |
*/ |
| 767 |
WpSettings * |
| 768 |
wp_settings_new (WpCore * core, const gchar * metadata_name) |
| 769 |
{ |
| 770 |
return g_object_new (WP_TYPE_SETTINGS, |
| 771 |
"core", core, |
| 772 |
"metadata-name", metadata_name ? metadata_name : "sm-settings", |
| 773 |
NULL); |
| 774 |
} |
| 775 |
|
| 776 |
static gboolean |
| 777 |
find_settings_func (gpointer g_object, gpointer metadata_name) |
| 778 |
{ |
| 779 |
if (!WP_IS_SETTINGS (g_object)) |
| 780 |
return FALSE; |
| 781 |
|
| 782 |
if (metadata_name) |
| 783 |
return g_str_equal (((WpSettings *) g_object)->metadata_name, |
| 784 |
(gchar *) metadata_name); |
| 785 |
else |
| 786 |
return TRUE; |
| 787 |
} |
| 788 |
|
| 789 |
/*! |
| 790 |
* \brief Finds a registered WpSettings object by its metadata name |
| 791 |
* |
| 792 |
* \ingroup wpsettings |
| 793 |
* \param core the WpCore |
| 794 |
* \param metadata_name (nullable): the name of the metadata object that the |
| 795 |
* settings object is associated with; NULL returns the first settings object |
| 796 |
* that is found |
| 797 |
* \returns (transfer full) (nullable): the WpSettings object, or NULL if not |
| 798 |
* found |
| 799 |
*/ |
| 800 |
WpSettings * |
| 801 |
wp_settings_find (WpCore * core, const gchar * metadata_name) |
| 802 |
{ |
| 803 |
g_return_val_if_fail (WP_IS_CORE (core), NULL); |
| 804 |
|
| 805 |
GObject *s = wp_core_find_object (core, (GEqualFunc) find_settings_func, |
| 806 |
metadata_name); |
| 807 |
return s ? WP_SETTINGS (s) : NULL; |
| 808 |
} |
| 809 |
|
| 810 |
/*! |
| 811 |
* \brief Subscribes callback for a given setting pattern(a glob-style pattern |
| 812 |
* matched using g_pattern_match_simple), this allows clients to look |
| 813 |
* for any changes made in settings through metadata. |
| 814 |
* |
| 815 |
* \ingroup wpsettings |
| 816 |
* \param self the settings object |
| 817 |
* \param pattern name of the pattern to match the settings with |
| 818 |
* \param callback (scope async): the callback triggered when the settings |
| 819 |
* change. |
| 820 |
* \param user_data data to pass to \a callback |
| 821 |
* \returns the subscription ID (always greater than 0 for successful |
| 822 |
* subscriptions) |
| 823 |
*/ |
| 824 |
guintptr |
| 825 |
wp_settings_subscribe (WpSettings *self, |
| 826 |
const gchar *pattern, WpSettingsChangedCallback callback, |
| 827 |
gpointer user_data) |
| 828 |
{ |
| 829 |
return wp_settings_subscribe_closure (self, pattern, |
| 830 |
g_cclosure_new (G_CALLBACK (callback), user_data, NULL)); |
| 831 |
} |
| 832 |
|
| 833 |
/*! |
| 834 |
* \brief Subscribes callback for a given setting pattern(a glob-style pattern |
| 835 |
* matched using g_pattern_match_simple), this allows clients to look |
| 836 |
* for any changes made in settings through metadata. |
| 837 |
* |
| 838 |
* \ingroup wpsettings |
| 839 |
* \param self the settings object |
| 840 |
* \param pattern name of the pattern to match the settings with |
| 841 |
* \param closure (nullable): a GAsyncReadyCallback wrapped in a GClosure |
| 842 |
* \returns the subscription ID (always greater than 0 for success) |
| 843 |
*/ |
| 844 |
guintptr |
| 845 |
wp_settings_subscribe_closure (WpSettings *self, const gchar *pattern, |
| 846 |
GClosure *closure) |
| 847 |
{ |
| 848 |
g_return_val_if_fail (WP_IS_SETTINGS (self), 0); |
| 849 |
g_return_val_if_fail (pattern, 0); |
| 850 |
g_return_val_if_fail (closure, 0); |
| 851 |
|
| 852 |
Callback *cb = g_slice_new0 (Callback); |
| 853 |
g_return_val_if_fail (cb, 0); |
| 854 |
|
| 855 |
cb->closure = g_closure_ref (closure); |
| 856 |
g_closure_sink (closure); |
| 857 |
if (G_CLOSURE_NEEDS_MARSHAL (closure)) |
| 858 |
g_closure_set_marshal (closure, g_cclosure_marshal_generic); |
| 859 |
|
| 860 |
cb->pattern = g_strdup (pattern); |
| 861 |
|
| 862 |
g_ptr_array_add (self->callbacks, cb); |
| 863 |
|
| 864 |
wp_debug_object (self, "callback(%p) subscribed for pattern(%s)", |
| 865 |
(void *) cb, pattern); |
| 866 |
|
| 867 |
return (guintptr) cb; |
| 868 |
} |
| 869 |
|
| 870 |
/*! |
| 871 |
* \brief Unsubscribes callback for a given subscription_id. |
| 872 |
* |
| 873 |
* \ingroup wpsettings |
| 874 |
* \param self the settings object |
| 875 |
* \param subscription_id identifies the callback |
| 876 |
* \returns TRUE if success, FALSE otherwise |
| 877 |
*/ |
| 878 |
gboolean |
| 879 |
wp_settings_unsubscribe (WpSettings *self, guintptr subscription_id) |
| 880 |
{ |
| 881 |
gboolean ret = FALSE; |
| 882 |
g_return_val_if_fail (WP_IS_SETTINGS (self), FALSE); |
| 883 |
g_return_val_if_fail (subscription_id, FALSE); |
| 884 |
|
| 885 |
Callback *cb = (Callback *) subscription_id; |
| 886 |
|
| 887 |
ret = g_ptr_array_remove (self->callbacks, cb); |
| 888 |
|
| 889 |
wp_debug_object (self, "callback(%p) unsubscription %s", (void *) cb, |
| 890 |
(ret)? "succeeded": "failed"); |
| 891 |
|
| 892 |
return ret; |
| 893 |
} |
| 894 |
|
| 895 |
/*! |
| 896 |
* \brief Gets the WpSpaJson value of a setting |
| 897 |
* \ingroup wpsettings |
| 898 |
* \param self the settings object |
| 899 |
* \param name the name of the setting |
| 900 |
* \returns (transfer full) (nullable): The WpSpaJson value of the setting, or |
| 901 |
* NULL if the setting does not exist |
| 902 |
*/ |
| 903 |
WpSpaJson * |
| 904 |
wp_settings_get (WpSettings *self, const gchar *name) |
| 905 |
{ |
| 906 |
const gchar *value; |
| 907 |
g_autoptr (WpSettingsSpec) spec = NULL; |
| 908 |
|
| 909 |
g_return_val_if_fail (WP_IS_SETTINGS (self), NULL); |
| 910 |
g_return_val_if_fail (name, NULL); |
| 911 |
|
| 912 |
spec = wp_settings_get_spec (self, name); |
| 913 |
if (!spec) { |
| 914 |
wp_warning ("Setting '%s' does not exist in the settings schema", name); |
| 915 |
return NULL; |
| 916 |
} |
| 917 |
|
| 918 |
value = g_hash_table_lookup (self->settings, name); |
| 919 |
return value ? wp_spa_json_new_wrap_string (value) : |
| 920 |
wp_settings_spec_get_default_value (spec); |
| 921 |
} |
| 922 |
|
| 923 |
/*! |
| 924 |
* \brief Gets the WpSpaJson saved value of a setting |
| 925 |
* \ingroup wpsettings |
| 926 |
* \param self the settings object |
| 927 |
* \param name the name of the setting |
| 928 |
* \returns (transfer full) (nullable): The WpSpaJson saved value of the |
| 929 |
* setting, or NULL if the setting does not exist |
| 930 |
*/ |
| 931 |
WpSpaJson * |
| 932 |
wp_settings_get_saved (WpSettings *self, const gchar *name) |
| 933 |
{ |
| 934 |
const gchar *value; |
| 935 |
g_autoptr (WpSettingsSpec) spec = NULL; |
| 936 |
|
| 937 |
g_return_val_if_fail (WP_IS_SETTINGS (self), NULL); |
| 938 |
g_return_val_if_fail (name, NULL); |
| 939 |
|
| 940 |
spec = wp_settings_get_spec (self, name); |
| 941 |
if (!spec) { |
| 942 |
wp_warning ("Setting '%s' does not exist in the settings schema", name); |
| 943 |
return NULL; |
| 944 |
} |
| 945 |
|
| 946 |
value = g_hash_table_lookup (self->saved_settings, name); |
| 947 |
return value ? wp_spa_json_new_wrap_string (value) : NULL; |
| 948 |
} |
| 949 |
|
| 950 |
/*! |
| 951 |
* \brief Gets the specification of a setting |
| 952 |
* \ingroup wpsettings |
| 953 |
* \param self the settings object |
| 954 |
* \param name the name of the setting |
| 955 |
* \returns (transfer full) (nullable): the specification of the setting |
| 956 |
*/ |
| 957 |
WpSettingsSpec * |
| 958 |
wp_settings_get_spec (WpSettings *self, const gchar *name) |
| 959 |
{ |
| 960 |
WpSettingsSpec *spec; |
| 961 |
|
| 962 |
g_return_val_if_fail (WP_IS_SETTINGS (self), NULL); |
| 963 |
g_return_val_if_fail (name, NULL); |
| 964 |
|
| 965 |
spec = g_hash_table_lookup (self->schema, name); |
| 966 |
return spec ? wp_settings_spec_ref (spec) : NULL; |
| 967 |
} |
| 968 |
|
| 969 |
/*! |
| 970 |
* \brief Sets a new setting value |
| 971 |
* \ingroup wpsettings |
| 972 |
* \param self the settings object |
| 973 |
* \param name the name of the setting |
| 974 |
* \param value (transfer none): the JSON value of the setting |
| 975 |
* \returns TRUE if the setting could be set, FALSE otherwise |
| 976 |
*/ |
| 977 |
gboolean |
| 978 |
wp_settings_set (WpSettings *self, const gchar *name, WpSpaJson *value) |
| 979 |
{ |
| 980 |
g_autoptr (WpMetadata) m = NULL; |
| 981 |
g_autoptr (WpSettingsSpec) spec = NULL; |
| 982 |
g_autofree gchar *value_str = NULL; |
| 983 |
|
| 984 |
g_return_val_if_fail (WP_IS_SETTINGS (self), FALSE); |
| 985 |
g_return_val_if_fail (name, FALSE); |
| 986 |
g_return_val_if_fail (value, FALSE); |
| 987 |
|
| 988 |
m = g_weak_ref_get (&self->metadata); |
| 989 |
if (!m) |
| 990 |
return FALSE; |
| 991 |
|
| 992 |
spec = wp_settings_get_spec (self, name); |
| 993 |
if (!spec) { |
| 994 |
wp_warning ("Setting '%s' does not exist in the settings schema", name); |
| 995 |
return FALSE; |
| 996 |
} |
| 997 |
|
| 998 |
value_str = wp_spa_json_to_string (value); |
| 999 |
if (!wp_settings_spec_check_value (spec, value)) { |
| 1000 |
wp_warning ("Cannot set setting '%s' with value: %s", name, value_str); |
| 1001 |
return FALSE; |
| 1002 |
} |
| 1003 |
|
| 1004 |
g_hash_table_insert (self->settings, g_strdup (name), g_strdup (value_str)); |
| 1005 |
wp_metadata_set (m, 0, name, "Spa:String:JSON", value_str); |
| 1006 |
return TRUE; |
| 1007 |
} |
| 1008 |
|
| 1009 |
/*! |
| 1010 |
* \brief Resets the setting to its default value |
| 1011 |
* \ingroup wpsettings |
| 1012 |
* \param self the settings object |
| 1013 |
* \param name the name of the setting to reset |
| 1014 |
* \returns TRUE if the setting could be reset, FALSE otherwise |
| 1015 |
*/ |
| 1016 |
gboolean |
| 1017 |
wp_settings_reset (WpSettings *self, const char *name) |
| 1018 |
{ |
| 1019 |
g_autoptr (WpSettingsSpec) spec = NULL; |
| 1020 |
g_autoptr (WpSpaJson) def_value = NULL; |
| 1021 |
|
| 1022 |
g_return_val_if_fail (WP_IS_SETTINGS (self), FALSE); |
| 1023 |
g_return_val_if_fail (name, FALSE); |
| 1024 |
|
| 1025 |
spec = wp_settings_get_spec (self, name); |
| 1026 |
if (!spec) { |
| 1027 |
wp_warning ("Setting '%s' does not exist in the settings schema", name); |
| 1028 |
return FALSE; |
| 1029 |
} |
| 1030 |
|
| 1031 |
def_value = wp_settings_spec_get_default_value (spec); |
| 1032 |
return wp_settings_set (self, name, def_value); |
| 1033 |
} |
| 1034 |
|
| 1035 |
/*! |
| 1036 |
* \brief Saves a setting to make it persistent after reboot |
| 1037 |
* \ingroup wpsettings |
| 1038 |
* \param self the settings object |
| 1039 |
* \param name the name of the setting to be saved |
| 1040 |
* \returns TRUE if the setting could be saved, FALSE otherwise |
| 1041 |
*/ |
| 1042 |
gboolean |
| 1043 |
wp_settings_save (WpSettings *self, const char *name) |
| 1044 |
{ |
| 1045 |
g_autoptr (WpMetadata) mp = NULL; |
| 1046 |
g_autoptr (WpSpaJson) value = NULL; |
| 1047 |
g_autofree gchar *value_str = NULL; |
| 1048 |
|
| 1049 |
g_return_val_if_fail (WP_IS_SETTINGS (self), FALSE); |
| 1050 |
g_return_val_if_fail (name, FALSE); |
| 1051 |
|
| 1052 |
mp = g_weak_ref_get (&self->metadata_persistent); |
| 1053 |
if (!mp) |
| 1054 |
return FALSE; |
| 1055 |
|
| 1056 |
value = wp_settings_get (self, name); |
| 1057 |
if (!value) |
| 1058 |
return FALSE; |
| 1059 |
|
| 1060 |
value_str = wp_spa_json_to_string (value); |
| 1061 |
g_hash_table_insert (self->saved_settings, g_strdup (name), |
| 1062 |
g_strdup (value_str)); |
| 1063 |
wp_metadata_set (mp, 0, name, "Spa:String:JSON", value_str); |
| 1064 |
return TRUE; |
| 1065 |
} |
| 1066 |
|
| 1067 |
/*! |
| 1068 |
* \brief Deletes a saved setting to not make it persistent after reboot |
| 1069 |
* \ingroup wpsettings |
| 1070 |
* \param self the settings object |
| 1071 |
* \param name the name of the saved setting to be deleted |
| 1072 |
* \returns TRUE if the setting could be deleted, FALSE otherwise |
| 1073 |
*/ |
| 1074 |
gboolean |
| 1075 |
wp_settings_delete (WpSettings *self, const char *name) |
| 1076 |
{ |
| 1077 |
g_autoptr (WpMetadata) mp = NULL; |
| 1078 |
g_autoptr (WpSettingsSpec) spec = NULL; |
| 1079 |
|
| 1080 |
g_return_val_if_fail (WP_IS_SETTINGS (self), FALSE); |
| 1081 |
g_return_val_if_fail (name, FALSE); |
| 1082 |
|
| 1083 |
spec = wp_settings_get_spec (self, name); |
| 1084 |
if (!spec) { |
| 1085 |
wp_warning ("Setting '%s' does not exist in the settings schema", name); |
| 1086 |
return FALSE; |
| 1087 |
} |
| 1088 |
|
| 1089 |
mp = g_weak_ref_get (&self->metadata_persistent); |
| 1090 |
if (!mp) |
| 1091 |
return FALSE; |
| 1092 |
|
| 1093 |
g_hash_table_remove (self->saved_settings, name); |
| 1094 |
wp_metadata_set (mp, 0, name, NULL, NULL); |
| 1095 |
return TRUE; |
| 1096 |
} |
| 1097 |
|
| 1098 |
static void |
| 1099 |
settings_reset_cb (gpointer key, gpointer value, gpointer data) |
| 1100 |
{ |
| 1101 |
WpSettings *self = WP_SETTINGS (data); |
| 1102 |
const gchar *k = key; |
| 1103 |
|
| 1104 |
if (!wp_settings_reset (self, k)) |
| 1105 |
wp_warning_object (self, "Failed to reset setting %s", k); |
| 1106 |
} |
| 1107 |
|
| 1108 |
/*! |
| 1109 |
* \brief Resets all the settings to their default value |
| 1110 |
* \ingroup wpsettings |
| 1111 |
* \param self the settings object |
| 1112 |
*/ |
| 1113 |
void wp_settings_reset_all (WpSettings *self) |
| 1114 |
{ |
| 1115 |
g_return_if_fail (WP_IS_SETTINGS (self)); |
| 1116 |
|
| 1117 |
g_hash_table_foreach (self->settings, settings_reset_cb, self); |
| 1118 |
} |
| 1119 |
|
| 1120 |
static void |
| 1121 |
settings_save_cb (gpointer key, gpointer value, gpointer data) |
| 1122 |
{ |
| 1123 |
WpSettings *self = WP_SETTINGS (data); |
| 1124 |
const gchar *k = key; |
| 1125 |
|
| 1126 |
if (!wp_settings_save (self, k)) |
| 1127 |
wp_warning_object (self, "Failed to save setting %s", k); |
| 1128 |
} |
| 1129 |
|
| 1130 |
/*! |
| 1131 |
* \brief Saves all the settings to make them persistent after reboot |
| 1132 |
* \ingroup wpsettings |
| 1133 |
* \param self the settings object |
| 1134 |
*/ |
| 1135 |
void wp_settings_save_all (WpSettings *self) |
| 1136 |
{ |
| 1137 |
g_return_if_fail (WP_IS_SETTINGS (self)); |
| 1138 |
|
| 1139 |
g_hash_table_foreach (self->settings, settings_save_cb, self); |
| 1140 |
} |
| 1141 |
|
| 1142 |
/*! |
| 1143 |
* \brief Deletes all saved setting to not make them persistent after reboot |
| 1144 |
* \ingroup wpsettings |
| 1145 |
* \param self the settings object |
| 1146 |
*/ |
| 1147 |
void wp_settings_delete_all (WpSettings *self) |
| 1148 |
{ |
| 1149 |
g_autoptr (WpMetadata) mp = NULL; |
| 1150 |
|
| 1151 |
g_return_if_fail (WP_IS_SETTINGS (self)); |
| 1152 |
|
| 1153 |
g_hash_table_remove_all (self->saved_settings); |
| 1154 |
mp = g_weak_ref_get (&self->metadata_persistent); |
| 1155 |
if (mp) |
| 1156 |
wp_metadata_clear (mp); |
| 1157 |
} |
| 1158 |
|
| 1159 |
struct settings_iterator_data |
| 1160 |
{ |
| 1161 |
WpSettings *settings; |
| 1162 |
const gchar **keys; |
| 1163 |
guint len; |
| 1164 |
guint curr; |
| 1165 |
}; |
| 1166 |
|
| 1167 |
static void |
| 1168 |
settings_iterator_reset (WpIterator *it) |
| 1169 |
{ |
| 1170 |
struct settings_iterator_data *it_data = wp_iterator_get_user_data (it); |
| 1171 |
it_data->curr = 0; |
| 1172 |
} |
| 1173 |
|
| 1174 |
static gboolean |
| 1175 |
settings_iterator_next (WpIterator *it, GValue *item) |
| 1176 |
{ |
| 1177 |
struct settings_iterator_data *it_data = wp_iterator_get_user_data (it); |
| 1178 |
g_autoptr (WpSettingsItem) si = NULL; |
| 1179 |
const gchar *key, *value; |
| 1180 |
|
| 1181 |
if (it_data->curr < it_data->len) { |
| 1182 |
key = it_data->keys [it_data->curr++]; |
| 1183 |
value = g_hash_table_lookup (it_data->settings->settings, key); |
| 1184 |
g_return_val_if_fail (value, FALSE); |
| 1185 |
} else { |
| 1186 |
return FALSE; |
| 1187 |
} |
| 1188 |
|
| 1189 |
si = wp_settings_item_new (key, value); |
| 1190 |
g_value_init (item, WP_TYPE_SETTINGS_ITEM); |
| 1191 |
g_value_take_boxed (item, g_steal_pointer (&si)); |
| 1192 |
return TRUE; |
| 1193 |
} |
| 1194 |
|
| 1195 |
static void |
| 1196 |
settings_iterator_finalize (WpIterator *it) |
| 1197 |
{ |
| 1198 |
struct settings_iterator_data *it_data = wp_iterator_get_user_data (it); |
| 1199 |
g_clear_pointer (&it_data->keys, g_free); |
| 1200 |
g_clear_object (&it_data->settings); |
| 1201 |
} |
| 1202 |
|
| 1203 |
static const WpIteratorMethods settings_iterator_methods = { |
| 1204 |
.version = WP_ITERATOR_METHODS_VERSION, |
| 1205 |
.reset = settings_iterator_reset, |
| 1206 |
.next = settings_iterator_next, |
| 1207 |
.fold = NULL, |
| 1208 |
.foreach = NULL, |
| 1209 |
.finalize = settings_iterator_finalize, |
| 1210 |
}; |
| 1211 |
|
| 1212 |
/*! |
| 1213 |
* \brief Iterates over settings |
| 1214 |
* \ingroup wpsettings |
| 1215 |
* \param self the settings object |
| 1216 |
* \returns (transfer full): an iterator that iterates over the settings. |
| 1217 |
*/ |
| 1218 |
WpIterator * |
| 1219 |
wp_settings_new_iterator (WpSettings *self) |
| 1220 |
{ |
| 1221 |
g_autoptr (WpIterator) it = NULL; |
| 1222 |
struct settings_iterator_data *it_data; |
| 1223 |
g_autoptr (WpMetadata) m = NULL; |
| 1224 |
|
| 1225 |
g_return_val_if_fail (WP_IS_SETTINGS (self), NULL); |
| 1226 |
|
| 1227 |
m = g_weak_ref_get (&self->metadata); |
| 1228 |
if (!m) |
| 1229 |
return NULL; |
| 1230 |
|
| 1231 |
it = wp_iterator_new (&settings_iterator_methods, |
| 1232 |
sizeof (struct settings_iterator_data)); |
| 1233 |
it_data = wp_iterator_get_user_data (it); |
| 1234 |
it_data->settings = g_object_ref (self); |
| 1235 |
it_data->keys = (const gchar **)g_hash_table_get_keys_as_array ( |
| 1236 |
self->settings, &it_data->len); |
| 1237 |
it_data->curr = 0; |
| 1238 |
return g_steal_pointer (&it); |
| 1239 |
} |