diff --git a/deepin-devicemanager/configs/org_deepin_devicemanager.hpp b/deepin-devicemanager/configs/org_deepin_devicemanager.hpp new file mode 100644 index 00000000..990ac6ba --- /dev/null +++ b/deepin-devicemanager/configs/org_deepin_devicemanager.hpp @@ -0,0 +1,208 @@ +// SPDX-FileCopyrightText: 2024 - 2025 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef ORG_DEEPIN_DEVICEMANAGER_H +#define ORG_DEEPIN_DEVICEMANAGER_H + +#include +#include +#include +#include +#include +#include + +class org_deepin_devicemanager : public QObject { + Q_OBJECT + + Q_PROPERTY(QString rules READ rules WRITE setRules NOTIFY rulesChanged) + Q_PROPERTY(double specialComType READ specialComType WRITE setSpecialComType NOTIFY specialComTypeChanged) +public: + explicit org_deepin_devicemanager(QThread *thread, const QString &appId, const QString &name, const QString &subpath, QObject *parent = nullptr) + : QObject(parent) { + + if (!thread->isRunning()) { + qWarning() << QStringLiteral("Warning: The provided thread is not running."); + } + Q_ASSERT(QThread::currentThread() != thread); + auto worker = new QObject(); + worker->moveToThread(thread); + QMetaObject::invokeMethod(worker, [=]() { + auto config = DTK_CORE_NAMESPACE::DConfig::create(appId, name, subpath, nullptr); + if (!config) { + qWarning() << QStringLiteral("Failed to create DConfig instance."); + worker->deleteLater(); + return; + } + config->moveToThread(QThread::currentThread()); + initialize(config); + worker->deleteLater(); + }); + } + explicit org_deepin_devicemanager(QThread *thread, DTK_CORE_NAMESPACE::DConfigBackend *backend, const QString &appId, const QString &name, const QString &subpath, QObject *parent = nullptr) + : QObject(parent) { + + if (!thread->isRunning()) { + qWarning() << QStringLiteral("Warning: The provided thread is not running."); + } + Q_ASSERT(QThread::currentThread() != thread); + auto worker = new QObject(); + worker->moveToThread(thread); + QMetaObject::invokeMethod(worker, [=]() { + auto config = DTK_CORE_NAMESPACE::DConfig::create(backend, appId, name, subpath, nullptr); + if (!config) { + qWarning() << QStringLiteral("Failed to create DConfig instance."); + worker->deleteLater(); + return; + } + config->moveToThread(QThread::currentThread()); + initialize(config); + worker->deleteLater(); + }); + } + explicit org_deepin_devicemanager(QThread *thread, const QString &name, const QString &subpath, QObject *parent = nullptr) + : QObject(parent) { + + if (!thread->isRunning()) { + qWarning() << QStringLiteral("Warning: The provided thread is not running."); + } + Q_ASSERT(QThread::currentThread() != thread); + auto worker = new QObject(); + worker->moveToThread(thread); + QMetaObject::invokeMethod(worker, [=]() { + auto config = DTK_CORE_NAMESPACE::DConfig::create(name, subpath, nullptr); + if (!config) { + qWarning() << QStringLiteral("Failed to create DConfig instance."); + worker->deleteLater(); + return; + } + config->moveToThread(QThread::currentThread()); + initialize(config); + worker->deleteLater(); + }); + } + explicit org_deepin_devicemanager(QThread *thread, DTK_CORE_NAMESPACE::DConfigBackend *backend, const QString &name, const QString &subpath, QObject *parent = nullptr) + : QObject(parent) { + + if (!thread->isRunning()) { + qWarning() << QStringLiteral("Warning: The provided thread is not running."); + } + Q_ASSERT(QThread::currentThread() != thread); + auto worker = new QObject(); + worker->moveToThread(thread); + QMetaObject::invokeMethod(worker, [=]() { + auto config = DTK_CORE_NAMESPACE::DConfig::create(backend, name, subpath, nullptr); + if (!config) { + qWarning() << QStringLiteral("Failed to create DConfig instance."); + worker->deleteLater(); + return; + } + config->moveToThread(QThread::currentThread()); + initialize(config); + worker->deleteLater(); + }); + } + ~org_deepin_devicemanager() { + if (m_config.loadRelaxed()) { + m_config.loadRelaxed()->deleteLater(); + } + } + + QString rules() const { + return p_rules; + } + void setRules(const QString &value) { + auto oldValue = p_rules; + p_rules = value; + markPropertySet(0); + if (auto config = m_config.loadRelaxed()) { + QMetaObject::invokeMethod(config, [this, value]() { + m_config.loadRelaxed()->setValue(QStringLiteral("rules"), value); + }); + } + if (p_rules != oldValue) { + Q_EMIT rulesChanged(); + } + } + double specialComType() const { + return p_specialComType; + } + void setSpecialComType(const double &value) { + auto oldValue = p_specialComType; + p_specialComType = value; + markPropertySet(1); + if (auto config = m_config.loadRelaxed()) { + QMetaObject::invokeMethod(config, [this, value]() { + m_config.loadRelaxed()->setValue(QStringLiteral("specialComType"), value); + }); + } + if (p_specialComType != oldValue) { + Q_EMIT specialComTypeChanged(); + } + } +Q_SIGNALS: + void rulesChanged(); + void specialComTypeChanged(); +private: + void initialize(DTK_CORE_NAMESPACE::DConfig *config) { + Q_ASSERT(!m_config.loadRelaxed()); + m_config.storeRelaxed(config); + if (testPropertySet(0)) { + config->setValue(QStringLiteral("rules"), QVariant::fromValue(p_rules)); + } else { + updateValue(QStringLiteral("rules"), QVariant::fromValue(p_rules)); + } + if (testPropertySet(1)) { + config->setValue(QStringLiteral("specialComType"), QVariant::fromValue(p_specialComType)); + } else { + updateValue(QStringLiteral("specialComType"), QVariant::fromValue(p_specialComType)); + } + + connect(config, &DTK_CORE_NAMESPACE::DConfig::valueChanged, this, [this](const QString &key) { + updateValue(key); + }, Qt::DirectConnection); + } + void updateValue(const QString &key, const QVariant &fallback = QVariant()) { + Q_ASSERT(QThread::currentThread() == m_config.loadRelaxed()->thread()); + const QVariant &value = m_config.loadRelaxed()->value(key, fallback); + if (key == QStringLiteral("rules")) { + auto newValue = qvariant_cast(value); + QMetaObject::invokeMethod(this, [this, newValue]() { + if (p_rules != newValue) { + p_rules = newValue; + Q_EMIT rulesChanged(); + } + }); + return; + } + if (key == QStringLiteral("specialComType")) { + auto newValue = qvariant_cast(value); + QMetaObject::invokeMethod(this, [this, newValue]() { + if (p_specialComType != newValue) { + p_specialComType = newValue; + Q_EMIT specialComTypeChanged(); + } + }); + return; + } + } + inline void markPropertySet(const int index) { + if (index < 32) { + m_propertySetStatus0.fetchAndOrOrdered(1 << (index - 0)); + return; + } + Q_UNREACHABLE(); + } + inline bool testPropertySet(const int index) const { + if (index < 32) { + return (m_propertySetStatus0.loadRelaxed() & (1 << (index - 0))); + } + Q_UNREACHABLE(); + } + QAtomicPointer m_config = nullptr; + QString p_rules { QStringLiteral("*.debug=false;*.info=false;*.warning=true") }; + double p_specialComType { -1 }; + QAtomicInteger m_propertySetStatus0 = 0; +}; + +#endif // ORG_DEEPIN_DEVICEMANAGER_H