File indexing completed on 2024-05-12 05:29:27

0001 // SPDX-FileCopyrightText: 2019, 2022 Mikhail Zolotukhin <zomial@protonmail.com>
0002 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0003 
0004 #include "gsettings.h"
0005 
0006 #include <gio/gio.h>
0007 
0008 namespace GSettingsEditor
0009 {
0010 constinit unsigned s_applyId = 0;
0011 
0012 #if GLIB_CHECK_VERSION(2, 74, 0)
0013 void applySettings(void *)
0014 #else
0015 int applySettings(void *)
0016 #endif
0017 {
0018     g_settings_sync();
0019     s_applyId = 0;
0020 #if !GLIB_CHECK_VERSION(2, 74, 0)
0021     return G_SOURCE_REMOVE;
0022 #endif
0023 }
0024 
0025 bool checkParamExists(const char *paramName, const char *category)
0026 {
0027     GSettingsSchemaSource *gSettingsSchemaSource = g_settings_schema_source_get_default();
0028     g_autoptr(GSettingsSchema) gSettingsSchema = g_settings_schema_source_lookup(gSettingsSchemaSource, category, true);
0029 
0030     return gSettingsSchema && g_settings_schema_has_key(gSettingsSchema, paramName);
0031 }
0032 
0033 void setValue(const char *paramName, const QVariant &paramValue, const char *category)
0034 {
0035     if (!checkParamExists(paramName, category)) {
0036         Q_ASSERT_X(false, "gsettings", QLatin1String("%1 doesn't exist in %2").arg(paramName, category).toLatin1().constData());
0037         return;
0038     }
0039 
0040     g_autoptr(GSettings) gsettings = g_settings_new(category);
0041 
0042     if (paramValue.type() == QVariant::Type::String) {
0043         g_settings_set_string(gsettings, paramName, paramValue.toString().toUtf8().constData());
0044     } else if (paramValue.type() == QVariant::Type::UInt) {
0045         g_settings_set_uint(gsettings, paramName, paramValue.toInt());
0046     } else if (paramValue.type() == QVariant::Type::Int) {
0047         g_settings_set_int(gsettings, paramName, paramValue.toInt());
0048     } else if (paramValue.type() == QVariant::Type::Bool) {
0049         g_settings_set_boolean(gsettings, paramName, paramValue.toBool());
0050     } else if (paramValue.type() == QVariant::Type::Double) {
0051         g_settings_set_double(gsettings, paramName, paramValue.toDouble());
0052     }
0053 
0054     if (s_applyId == 0) {
0055 #if GLIB_CHECK_VERSION(2, 74, 0)
0056         s_applyId = g_timeout_add_once(100, applySettings, nullptr);
0057 #else
0058         s_applyId = g_timeout_add(100, applySettings, nullptr);
0059 #endif
0060     }
0061 }
0062 
0063 void setValueAsEnum(const char *paramName, int paramValue, const char *category)
0064 {
0065     if (!checkParamExists(paramName, category)) {
0066         Q_ASSERT_X(false, "gsettings", QLatin1String("%1 doesn't exist in %2").arg(paramName, category).toLatin1().constData());
0067         return;
0068     }
0069 
0070     g_autoptr(GSettings) gsettings = g_settings_new(category);
0071     g_settings_set_enum(gsettings, paramName, paramValue);
0072 
0073     if (s_applyId == 0) {
0074 #if GLIB_CHECK_VERSION(2, 74, 0)
0075         s_applyId = g_timeout_add_once(100, applySettings, nullptr);
0076 #else
0077         s_applyId = g_timeout_add(100, applySettings, nullptr);
0078 #endif
0079     }
0080 }
0081 }