File indexing completed on 2024-04-28 16:53:05
0001 /* 0002 * SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 #include "Configuration.h" 0008 0009 #include <QDebug> 0010 #include <QMetaProperty> 0011 #include <chrono> 0012 0013 using namespace std::chrono_literals; 0014 0015 Q_GLOBAL_STATIC(SystemMonitorConfiguration, s_config) 0016 0017 SystemMonitorConfiguration *Configuration::globalConfig() 0018 { 0019 return s_config; 0020 } 0021 0022 Configuration::Configuration(QObject *parent) 0023 : QObject(parent) 0024 , m_saveTimer(std::make_unique<QTimer>()) 0025 { 0026 m_saveTimer->setInterval(500ms); 0027 m_saveTimer->setSingleShot(true); 0028 connect(m_saveTimer.get(), &QTimer::timeout, globalConfig(), &SystemMonitorConfiguration::save); 0029 } 0030 0031 void Configuration::propertyChanged() 0032 { 0033 auto signal = metaObject()->method(senderSignalIndex()); 0034 // Strip "Changed" from the signal name to get property name 0035 auto propertyName = signal.name().chopped(7); 0036 auto property = metaObject()->property(metaObject()->indexOfProperty(propertyName)); 0037 0038 if (property.isValid()) { 0039 globalConfig()->setProperty(property.name(), property.read(this)); 0040 m_saveTimer->start(); 0041 } else { 0042 qWarning() << "Property" << propertyName << "was not found!"; 0043 } 0044 } 0045 0046 void Configuration::classBegin() 0047 { 0048 } 0049 0050 void Configuration::componentComplete() 0051 { 0052 auto propertyChangedMethod = metaObject()->method(metaObject()->indexOfMethod("propertyChanged()")); 0053 0054 for (auto i = 0; i < metaObject()->propertyCount(); ++i) { 0055 auto property = metaObject()->property(i); 0056 0057 if (property.name() == QStringLiteral("config")) { 0058 continue; 0059 } 0060 0061 if (globalConfig()->metaObject()->indexOfProperty(property.name()) == -1) { 0062 qWarning() << "Property" << property.name() << "not found in configuration, ignoring"; 0063 continue; 0064 } 0065 0066 property.write(this, globalConfig()->property(property.name())); 0067 0068 if (property.hasNotifySignal()) { 0069 connect(this, property.notifySignal(), this, propertyChangedMethod); 0070 } else { 0071 qWarning() << "Property" << property.name() << "is not notifiable!"; 0072 } 0073 } 0074 0075 Q_EMIT configurationLoaded(); 0076 }