File indexing completed on 2024-04-14 15:40:18

0001 /*
0002  * SPDX-FileCopyrightText: 2018 Nicolas Fella <nicolas.fella@gmx.de>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-only
0005  *
0006  */
0007 
0008 #include <QString>
0009 
0010 #include "debug.h"
0011 #include "gsettingsitem.h"
0012 
0013 QVariant GSettingsItem::value(const QString &key) const
0014 {
0015     if (!m_settings) {
0016         return {};
0017     }
0018 
0019     GVariant *gvalue = g_settings_get_value(m_settings, key.toLatin1().data());
0020 
0021     QVariant toReturn;
0022 
0023     switch (g_variant_classify(gvalue)) {
0024     case G_VARIANT_CLASS_BOOLEAN:
0025         toReturn = QVariant((bool)g_variant_get_boolean(gvalue));
0026         break;
0027     case G_VARIANT_CLASS_STRING:
0028         toReturn = QVariant(QString::fromUtf8(g_variant_get_string(gvalue, nullptr)));
0029         break;
0030     default:
0031         qCWarning(PLASMAPA()) << "Unhandled variant type in value()";
0032     }
0033 
0034     g_variant_unref(gvalue);
0035 
0036     return toReturn;
0037 }
0038 
0039 void GSettingsItem::set(const QString &key, const QVariant &val)
0040 {
0041     if (!m_settings) {
0042         return;
0043     }
0044 
0045     // It might be hard to detect the right GVariant type from
0046     // complex QVariant types such as string lists or more detailed
0047     // types such as integers (GVariant has different sizes),
0048     // therefore we get the current value for the key and convert
0049     // to QVariant using the GVariant type
0050     GVariant *oldValue = g_settings_get_value(m_settings, key.toLatin1().data());
0051     GVariant *newValue = nullptr;
0052 
0053     switch (g_variant_type_peek_string(g_variant_get_type(oldValue))[0]) {
0054     case G_VARIANT_CLASS_BOOLEAN:
0055         newValue = g_variant_new_boolean(val.toBool());
0056         break;
0057     case G_VARIANT_CLASS_STRING:
0058         newValue = g_variant_new_string(val.toString().toUtf8().constData());
0059         break;
0060     default:
0061         qCWarning(PLASMAPA()) << "Unhandled variant type in set()";
0062     }
0063 
0064     if (newValue) {
0065         g_settings_set_value(m_settings, key.toLatin1().data(), newValue);
0066     }
0067 
0068     g_variant_unref(oldValue);
0069 }
0070 
0071 bool GSettingsItem::isValid() const
0072 {
0073     return m_settings;
0074 }
0075 
0076 GSettingsItem::GSettingsItem(const QString &key, QObject *parent)
0077     : QObject(parent)
0078 {
0079     const char schemaId[] = "org.freedesktop.pulseaudio.module-group";
0080 
0081     // g_settings_new_with_path asserts if the schema doesn't exist, check this manually to avoid an abort.
0082     auto *defaultSource = g_settings_schema_source_get_default();
0083     if (!defaultSource) {
0084         qCWarning(PLASMAPA) << "No GSettings schemas are installed on the system";
0085         return;
0086     }
0087 
0088     auto *schema = g_settings_schema_source_lookup(defaultSource, schemaId, true /*recursive*/);
0089     if (!schema) {
0090         qCWarning(PLASMAPA) << "Settings schema" << schemaId << "is not installed";
0091         return;
0092     }
0093 
0094     m_settings = g_settings_new_with_path(schemaId, key.toLatin1().data());
0095     g_settings_schema_unref(schema);
0096 
0097     g_signal_connect(m_settings, "changed", G_CALLBACK(GSettingsItem::settingChanged), this);
0098 }
0099 
0100 GSettingsItem::~GSettingsItem()
0101 {
0102     g_settings_sync();
0103     if (m_settings) {
0104         g_object_unref(m_settings);
0105     }
0106 }