1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
|
/* SPDX-FileCopyrightText: 2022-2024 - Sébastien Wilmet <swilmet@gnome.org>
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
#include "tepl-settings.h"
#include <handy.h>
/**
* SECTION:settings
* @title: TeplSettings
* @short_description: Singleton class containing #GSettings objects
*
* #TeplSettings is a singleton class to have a central access to some
* #GSettings objects and having convenience features that are useful for a text
* editor.
*
* The central access permits to:
* - Share the same #GSettings objects between different parts of the
* application.
* - Avoid the need to store #GSettings objects a bit everywhere in different
* classes.
*/
#define KEY_SYSTEM_FONT "monospace-font-name"
#define KEY_SYSTEM_COLOR_SCHEME "color-scheme"
struct _TeplSettingsPrivate
{
GSettings *settings_desktop_interface;
/* Invariant: the following 3 instance variables are either all NULL or
* all set.
*/
GSettings *settings_font;
gchar *key_use_default_font;
gchar *key_editor_font;
/* Invariant: the following 2 instance variables are either all NULL or
* all set.
*/
GSettings *settings_theme_variant;
gchar *key_theme_variant;
/* Invariant: the following 3 instance variables are either all NULL or
* all set.
*/
GSettings *settings_style_scheme;
gchar *key_style_scheme_id_for_light_theme;
gchar *key_style_scheme_id_for_dark_theme;
guint handle_theme_variant : 1;
};
enum
{
SIGNAL_FONT_CHANGED,
N_SIGNALS
};
enum
{
PROP_0,
PROP_STYLE_SCHEME_ID,
N_PROPERTIES
};
static guint signals[N_SIGNALS];
static GParamSpec *properties[N_PROPERTIES];
/* TeplSettings is a singleton. */
static TeplSettings *singleton = NULL;
G_DEFINE_TYPE_WITH_PRIVATE (TeplSettings, tepl_settings, G_TYPE_OBJECT)
static void
tepl_settings_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
TeplSettings *settings = TEPL_SETTINGS (object);
switch (prop_id)
{
case PROP_STYLE_SCHEME_ID:
g_value_take_string (value, tepl_settings_get_style_scheme_id (settings));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
tepl_settings_dispose (GObject *object)
{
TeplSettings *self = TEPL_SETTINGS (object);
g_clear_object (&self->priv->settings_desktop_interface);
g_clear_object (&self->priv->settings_font);
g_clear_pointer (&self->priv->key_use_default_font, g_free);
g_clear_pointer (&self->priv->key_editor_font, g_free);
g_clear_object (&self->priv->settings_theme_variant);
g_clear_pointer (&self->priv->key_theme_variant, g_free);
g_clear_object (&self->priv->settings_style_scheme);
g_clear_pointer (&self->priv->key_style_scheme_id_for_light_theme, g_free);
g_clear_pointer (&self->priv->key_style_scheme_id_for_dark_theme, g_free);
G_OBJECT_CLASS (tepl_settings_parent_class)->dispose (object);
}
static void
tepl_settings_finalize (GObject *object)
{
if (singleton == TEPL_SETTINGS (object))
{
singleton = NULL;
}
G_OBJECT_CLASS (tepl_settings_parent_class)->finalize (object);
}
static void
tepl_settings_class_init (TeplSettingsClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->get_property = tepl_settings_get_property;
object_class->dispose = tepl_settings_dispose;
object_class->finalize = tepl_settings_finalize;
/**
* TeplSettings::font-changed:
* @settings: the #TeplSettings emitting the signal.
*
* The ::font-changed signal is emitted when the return value of
* tepl_settings_get_selected_font() has potentially changed.
*
* It takes into account the @use_default_font_key provided with
* tepl_settings_provide_font_settings() to avoid unnecessary signal
* emission. In other words, for example if @use_default_font_key is
* %FALSE, the signal is not emitted even if the default (system's) font
* has been modified.
*
* The relation between this signal and a potential zoom in and zoom out
* feature, to temporarily make the font larger or smaller (so the zoom
* level not being stored in #GSettings): when this signal is emitted,
* it means that the user has explicitly changed the font setting, and
* as such he or she probably wants to use that font instead. So the
* expected behavior in that case is to reset the zoom level. But it's
* left as an exercise for another class, since #TeplSettings is only
* for #GSettings.
*
* Since: 6.2
*/
signals[SIGNAL_FONT_CHANGED] =
g_signal_new ("font-changed",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_FIRST,
0, NULL, NULL, NULL,
G_TYPE_NONE, 0);
/**
* TeplSettings:style-scheme-id:
*
* See tepl_settings_provide_style_scheme_settings().
*
* The value of this property depends on whether the GTK theme is light
* or dark, and uses the corresponding #GSettings key.
*
* Since: 6.11
*/
properties[PROP_STYLE_SCHEME_ID] =
g_param_spec_string ("style-scheme-id",
"style-scheme-id",
"",
NULL,
G_PARAM_READABLE |
G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class, N_PROPERTIES, properties);
}
static gboolean
use_default_font (TeplSettings *self)
{
if (self->priv->settings_font == NULL)
{
return TRUE;
}
return g_settings_get_boolean (self->priv->settings_font,
self->priv->key_use_default_font);
}
static void
system_font_changed_cb (GSettings *settings_desktop_interface,
gchar *key,
gpointer user_data)
{
TeplSettings *self = TEPL_SETTINGS (user_data);
if (use_default_font (self))
{
g_signal_emit (self, signals[SIGNAL_FONT_CHANGED], 0);
}
}
static void
use_default_font_changed_cb (GSettings *settings_font,
gchar *key,
gpointer user_data)
{
TeplSettings *self = TEPL_SETTINGS (user_data);
g_signal_emit (self, signals[SIGNAL_FONT_CHANGED], 0);
}
static void
editor_font_changed_cb (GSettings *settings_font,
gchar *key,
gpointer user_data)
{
TeplSettings *self = TEPL_SETTINGS (user_data);
if (!use_default_font (self))
{
g_signal_emit (self, signals[SIGNAL_FONT_CHANGED], 0);
}
}
static void
tepl_settings_init (TeplSettings *self)
{
self->priv = tepl_settings_get_instance_private (self);
self->priv->settings_desktop_interface = g_settings_new ("org.gnome.desktop.interface");
g_signal_connect_object (self->priv->settings_desktop_interface,
"changed::" KEY_SYSTEM_FONT,
G_CALLBACK (system_font_changed_cb),
self,
0);
}
/**
* tepl_settings_get_singleton:
*
* Returns: (transfer none): the #TeplSettings singleton instance.
* Since: 6.2
*/
TeplSettings *
tepl_settings_get_singleton (void)
{
if (singleton == NULL)
{
singleton = g_object_new (TEPL_TYPE_SETTINGS, NULL);
}
return singleton;
}
void
_tepl_settings_unref_singleton (void)
{
if (singleton != NULL)
{
g_object_unref (singleton);
}
/* singleton is not set to NULL here, it is set to NULL in
* tepl_settings_finalize() (i.e. when we are sure that the ref count
* reaches 0).
*/
}
/**
* tepl_settings_peek_desktop_interface_settings:
* @self: the #TeplSettings instance.
*
* Returns: (transfer none): the #GSettings for `"org.gnome.desktop.interface"`.
* Since: 6.2
*/
GSettings *
tepl_settings_peek_desktop_interface_settings (TeplSettings *self)
{
g_return_val_if_fail (TEPL_IS_SETTINGS (self), NULL);
return self->priv->settings_desktop_interface;
}
/**
* tepl_settings_provide_font_settings:
* @self: the #TeplSettings instance.
* @font_settings: the #GSettings object containing the keys.
* @use_default_font_key: a key of type boolean.
* @editor_font_key: a key of type string.
*
* This function can only be called once, to provide two keys:
* - @use_default_font_key: must be of type boolean, to know whether to use the
* system's font (the default font), or the editor font.
* - @editor_font_key: must be of type string, containing the font name to have
* a different font for the text editor, rather than using the system's font.
*
* See tepl_settings_get_selected_font().
*
* Since: 6.2
*/
void
tepl_settings_provide_font_settings (TeplSettings *self,
GSettings *font_settings,
const gchar *use_default_font_key,
const gchar *editor_font_key)
{
gchar *detailed_signal = NULL;
g_return_if_fail (TEPL_IS_SETTINGS (self));
g_return_if_fail (G_IS_SETTINGS (font_settings));
g_return_if_fail (use_default_font_key != NULL);
g_return_if_fail (editor_font_key != NULL);
g_return_if_fail (self->priv->settings_font == NULL);
g_return_if_fail (self->priv->key_use_default_font == NULL);
g_return_if_fail (self->priv->key_editor_font == NULL);
self->priv->settings_font = g_object_ref (font_settings);
self->priv->key_use_default_font = g_strdup (use_default_font_key);
self->priv->key_editor_font = g_strdup (editor_font_key);
detailed_signal = g_strconcat ("changed::", use_default_font_key, NULL);
g_signal_connect_object (font_settings,
detailed_signal,
G_CALLBACK (use_default_font_changed_cb),
self,
0);
g_free (detailed_signal);
detailed_signal = g_strconcat ("changed::", editor_font_key, NULL);
g_signal_connect_object (font_settings,
detailed_signal,
G_CALLBACK (editor_font_changed_cb),
self,
0);
g_free (detailed_signal);
if (!use_default_font (self))
{
g_signal_emit (self, signals[SIGNAL_FONT_CHANGED], 0);
}
}
/**
* tepl_settings_get_selected_font:
* @self: the #TeplSettings instance.
*
* If tepl_settings_provide_font_settings() has *not* been called, this function
* always returns the current value of the system's fixed width (monospace)
* font.
*
* If tepl_settings_provide_font_settings() *has* been called, this function
* returns the font name depending on the boolean value of
* @use_default_font_key.
*
* See also the #TeplSettings::font-changed signal,
* tepl_utils_override_font_string() and tepl_prefs_create_font_component().
*
* Returns: the selected font name.
* Since: 6.2
*/
gchar *
tepl_settings_get_selected_font (TeplSettings *self)
{
g_return_val_if_fail (TEPL_IS_SETTINGS (self), NULL);
if (use_default_font (self))
{
return g_settings_get_string (self->priv->settings_desktop_interface,
KEY_SYSTEM_FONT);
}
return g_settings_get_string (self->priv->settings_font,
self->priv->key_editor_font);
}
static void
update_theme_variant (TeplSettings *self)
{
HdyStyleManager *style_manager = hdy_style_manager_get_default ();
/* For a text editor, prefer a light theme. See the documentation of the
* GtkSettings:gtk-application-prefer-dark-theme property:
*
* "Dark themes should not be used for documents, where large spaces are
* white/light and the dark chrome creates too much contrast (web
* browser, text editor...)."
*/
HdyColorScheme color_scheme = HDY_COLOR_SCHEME_PREFER_LIGHT;
if (self->priv->settings_theme_variant != NULL)
{
TeplSettingsThemeVariant variant;
variant = g_settings_get_enum (self->priv->settings_theme_variant,
self->priv->key_theme_variant);
switch (variant)
{
case TEPL_SETTINGS_THEME_VARIANT_SYSTEM:
if (!hdy_style_manager_get_system_supports_color_schemes (style_manager))
{
/* Do as if the 'system' enum value didn't exist,
* with 'light' as the default.
*/
color_scheme = HDY_COLOR_SCHEME_FORCE_LIGHT;
}
break;
case TEPL_SETTINGS_THEME_VARIANT_LIGHT:
color_scheme = HDY_COLOR_SCHEME_FORCE_LIGHT;
break;
case TEPL_SETTINGS_THEME_VARIANT_DARK:
color_scheme = HDY_COLOR_SCHEME_FORCE_DARK;
break;
default:
g_return_if_reached ();
}
}
hdy_style_manager_set_color_scheme (style_manager, color_scheme);
}
static void
theme_variant_changed_cb (GSettings *settings,
const gchar *key,
TeplSettings *self)
{
update_theme_variant (self);
}
/**
* tepl_settings_handle_theme_variant:
* @self: the #TeplSettings instance.
* @theme_variant_settings: (nullable): a #GSettings object, or %NULL.
* @theme_variant_setting_key: (nullable): a #GSettings key of type enum
* #TeplSettingsThemeVariant, or %NULL.
*
* This function permits to setup correctly the GTK theme variant to use.
*
* If @theme_variant_settings and @theme_variant_setting_key are %NULL, only the
* system's settings will be taken into account.
*
* If @theme_variant_settings and @theme_variant_setting_key are provided, they
* are taken into account according to #TeplSettingsThemeVariant.
*
* You should call this function only once, as subsequent calls are ignored (the
* setting used cannot be reconfigured).
*
* Since: 6.10
*/
void
tepl_settings_handle_theme_variant (TeplSettings *self,
GSettings *theme_variant_settings,
const gchar *theme_variant_setting_key)
{
g_return_if_fail (TEPL_IS_SETTINGS (self));
g_return_if_fail (theme_variant_settings == NULL || G_IS_SETTINGS (theme_variant_settings));
if (self->priv->handle_theme_variant)
{
/* Already done. Cannot be re-configured. */
return;
}
g_return_if_fail (self->priv->settings_theme_variant == NULL);
g_return_if_fail (self->priv->key_theme_variant == NULL);
hdy_init ();
if (theme_variant_settings != NULL &&
theme_variant_setting_key != NULL)
{
gchar *detailed_signal;
self->priv->settings_theme_variant = g_object_ref (theme_variant_settings);
self->priv->key_theme_variant = g_strdup (theme_variant_setting_key);
detailed_signal = g_strconcat ("changed::", theme_variant_setting_key, NULL);
g_signal_connect_object (theme_variant_settings,
detailed_signal,
G_CALLBACK (theme_variant_changed_cb),
self,
G_CONNECT_DEFAULT);
g_free (detailed_signal);
}
update_theme_variant (self);
self->priv->handle_theme_variant = TRUE;
}
static void
style_scheme_setting_changed_cb (GSettings *style_scheme_settings,
const gchar *key,
TeplSettings *self)
{
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_STYLE_SCHEME_ID]);
}
static void
style_manager_dark_notify_cb (HdyStyleManager *style_manager,
GParamSpec *pspec,
TeplSettings *self)
{
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_STYLE_SCHEME_ID]);
}
/**
* tepl_settings_provide_style_scheme_settings:
* @self: the #TeplSettings instance.
* @style_scheme_settings: the #GSettings object containing the keys.
* @key_for_light_theme_variant: a key of type string.
* @key_for_dark_theme_variant: a key of type string.
*
* This function can only be called once, to provide two keys:
* - @key_for_light_theme_variant: must be of type string, containing the ID of
* a #GtkSourceStyleScheme that should be used when the GTK theme variant is
* light.
* - @key_for_dark_theme_variant: the same, but when the GTK theme variant is
* dark.
*
* See tepl_settings_get_style_scheme_id().
*
* Since: 6.11
*/
void
tepl_settings_provide_style_scheme_settings (TeplSettings *self,
GSettings *style_scheme_settings,
const gchar *key_for_light_theme_variant,
const gchar *key_for_dark_theme_variant)
{
gchar *detailed_signal;
HdyStyleManager *style_manager;
g_return_if_fail (TEPL_IS_SETTINGS (self));
g_return_if_fail (G_IS_SETTINGS (style_scheme_settings));
g_return_if_fail (key_for_light_theme_variant != NULL);
g_return_if_fail (key_for_dark_theme_variant != NULL);
g_return_if_fail (self->priv->settings_style_scheme == NULL);
g_return_if_fail (self->priv->key_style_scheme_id_for_light_theme == NULL);
g_return_if_fail (self->priv->key_style_scheme_id_for_dark_theme == NULL);
self->priv->settings_style_scheme = g_object_ref (style_scheme_settings);
self->priv->key_style_scheme_id_for_light_theme = g_strdup (key_for_light_theme_variant);
self->priv->key_style_scheme_id_for_dark_theme = g_strdup (key_for_dark_theme_variant);
detailed_signal = g_strconcat ("changed::", key_for_light_theme_variant, NULL);
g_signal_connect_object (style_scheme_settings,
detailed_signal,
G_CALLBACK (style_scheme_setting_changed_cb),
self,
G_CONNECT_DEFAULT);
g_free (detailed_signal);
detailed_signal = g_strconcat ("changed::", key_for_dark_theme_variant, NULL);
g_signal_connect_object (style_scheme_settings,
detailed_signal,
G_CALLBACK (style_scheme_setting_changed_cb),
self,
G_CONNECT_DEFAULT);
g_free (detailed_signal);
style_manager = hdy_style_manager_get_default ();
g_signal_connect_object (style_manager,
"notify::dark",
G_CALLBACK (style_manager_dark_notify_cb),
self,
G_CONNECT_DEFAULT);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_STYLE_SCHEME_ID]);
}
/**
* tepl_settings_get_style_scheme_id:
* @self: the #TeplSettings instance.
*
* Returns: (transfer full) (nullable): the value of the
* #TeplSettings:style-scheme-id property.
* Since: 6.11
*/
gchar *
tepl_settings_get_style_scheme_id (TeplSettings *self)
{
HdyStyleManager *style_manager;
g_return_val_if_fail (TEPL_IS_SETTINGS (self), NULL);
if (self->priv->settings_style_scheme == NULL)
{
return NULL;
}
style_manager = hdy_style_manager_get_default ();
if (hdy_style_manager_get_dark (style_manager))
{
return g_settings_get_string (self->priv->settings_style_scheme,
self->priv->key_style_scheme_id_for_dark_theme);
}
return g_settings_get_string (self->priv->settings_style_scheme,
self->priv->key_style_scheme_id_for_light_theme);
}
gchar *
_tepl_settings_get_default_style_scheme_id (TeplSettings *self)
{
g_return_val_if_fail (TEPL_IS_SETTINGS (self), NULL);
if (self->priv->settings_style_scheme != NULL)
{
HdyStyleManager *style_manager = hdy_style_manager_get_default ();
const gchar *key_style_scheme_id;
GVariant *default_value;
gchar *default_style_scheme_id = NULL;
if (hdy_style_manager_get_dark (style_manager))
{
key_style_scheme_id = self->priv->key_style_scheme_id_for_dark_theme;
}
else
{
key_style_scheme_id = self->priv->key_style_scheme_id_for_light_theme;
}
default_value = g_settings_get_default_value (self->priv->settings_style_scheme,
key_style_scheme_id);
if (default_value != NULL)
{
default_style_scheme_id = g_variant_dup_string (default_value, NULL);
g_variant_unref (default_value);
}
if (default_style_scheme_id != NULL)
{
return default_style_scheme_id;
}
}
return g_strdup ("tango");
}
GSettings *
_tepl_settings_get_style_scheme_settings (TeplSettings *self,
const gchar **key_for_light_theme_variant,
const gchar **key_for_dark_theme_variant)
{
g_return_val_if_fail (TEPL_IS_SETTINGS (self), NULL);
if (key_for_light_theme_variant != NULL)
{
*key_for_light_theme_variant = self->priv->key_style_scheme_id_for_light_theme;
}
if (key_for_dark_theme_variant != NULL)
{
*key_for_dark_theme_variant = self->priv->key_style_scheme_id_for_dark_theme;
}
return self->priv->settings_style_scheme;
}
/**
* tepl_settings_get_range_uint:
* @settings: a #GSettings.
* @key: a key part of @settings.
* @min: (out): the minimum value allowed by the range.
* @max: (out): the maximum value allowed by the range.
*
* This function introspects a #GSettings key to get its range, if the key as a
* "u" type (unsigned integer). %FALSE is returned if the introspection failed.
*
* See g_settings_schema_key_get_range() for more flexibility.
*
* Returns: whether the operation was successful.
* Since: 6.2
*/
gboolean
tepl_settings_get_range_uint (GSettings *settings,
const gchar *key,
guint32 *min,
guint32 *max)
{
GSettingsSchema *schema = NULL;
GSettingsSchemaKey *schema_key = NULL;
GVariant *range = NULL;
gchar *range_type = NULL;
GVariant *range_values = NULL;
const gchar *range_values_format;
gboolean success = FALSE;
g_return_val_if_fail (G_IS_SETTINGS (settings), FALSE);
g_return_val_if_fail (key != NULL, FALSE);
g_return_val_if_fail (min != NULL, FALSE);
g_return_val_if_fail (max != NULL, FALSE);
*min = 0;
*max = 0;
g_object_get (settings,
"settings-schema", &schema,
NULL);
g_return_val_if_fail (schema != NULL, FALSE);
if (!g_settings_schema_has_key (schema, key))
{
goto out;
}
schema_key = g_settings_schema_get_key (schema, key);
range = g_settings_schema_key_get_range (schema_key);
g_variant_get (range, "(sv)", &range_type, &range_values);
if (g_strcmp0 (range_type, "range") != 0)
{
goto out;
}
range_values_format = g_variant_get_type_string (range_values);
if (g_strcmp0 (range_values_format, "(uu)") != 0)
{
goto out;
}
g_variant_get (range_values, "(uu)", min, max);
success = TRUE;
out:
g_clear_pointer (&schema, g_settings_schema_unref);
g_clear_pointer (&schema_key, g_settings_schema_key_unref);
g_clear_pointer (&range, g_variant_unref);
g_free (range_type);
g_clear_pointer (&range_values, g_variant_unref);
return success;
}
/**
* tepl_settings_reset_all:
* @schema_id: a #GSettings schema id.
*
* Resets all the #GSettings keys belonging to @schema_id.
*
* Since: 6.13
*/
void
tepl_settings_reset_all (const gchar *schema_id)
{
GSettings *settings;
GSettingsSchema *schema = NULL;
gchar **keys = NULL;
g_return_if_fail (schema_id != NULL);
settings = g_settings_new (schema_id);
g_object_get (settings,
"settings-schema", &schema,
NULL);
if (schema != NULL)
{
keys = g_settings_schema_list_keys (schema);
}
if (keys != NULL)
{
gint i;
for (i = 0; keys[i] != NULL; i++)
{
g_settings_reset (settings, keys[i]);
}
}
g_object_unref (settings);
g_clear_pointer (&schema, g_settings_schema_unref);
g_strfreev (keys);
}
|