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
|
/*
* SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "Configuration.h"
#include <QDebug>
#include <QMetaProperty>
#include <chrono>
using namespace std::chrono_literals;
Q_GLOBAL_STATIC(SystemMonitorConfiguration, s_config)
SystemMonitorConfiguration *Configuration::globalConfig()
{
return s_config;
}
Configuration::Configuration(QObject *parent)
: QObject(parent)
, m_saveTimer(std::make_unique<QTimer>())
{
m_saveTimer->setInterval(500ms);
m_saveTimer->setSingleShot(true);
connect(m_saveTimer.get(), &QTimer::timeout, globalConfig(), &SystemMonitorConfiguration::save);
}
void Configuration::propertyChanged()
{
auto signal = metaObject()->method(senderSignalIndex());
// Strip "Changed" from the signal name to get property name
auto propertyName = signal.name().chopped(7);
auto property = metaObject()->property(metaObject()->indexOfProperty(propertyName));
if (property.isValid()) {
globalConfig()->setProperty(property.name(), property.read(this));
m_saveTimer->start();
} else {
qWarning() << "Property" << propertyName << "was not found!";
}
}
void Configuration::classBegin()
{
}
void Configuration::componentComplete()
{
auto propertyChangedMethod = metaObject()->method(metaObject()->indexOfMethod("propertyChanged()"));
for (auto i = 0; i < metaObject()->propertyCount(); ++i) {
auto property = metaObject()->property(i);
if (property.name() == QStringLiteral("config")) {
continue;
}
if (globalConfig()->metaObject()->indexOfProperty(property.name()) == -1) {
qWarning() << "Property" << property.name() << "not found in configuration, ignoring";
continue;
}
property.write(this, globalConfig()->property(property.name()));
if (property.hasNotifySignal()) {
connect(this, property.notifySignal(), this, propertyChangedMethod);
} else {
qWarning() << "Property" << property.name() << "is not notifiable!";
}
}
Q_EMIT configurationLoaded();
}
#include "moc_Configuration.cpp"
|