#ifndef SETTINGSSERVICE_H #define SETTINGSSERVICE_H #include #include #include #include #include #include "data/system.h" #include "service.h" #include "core_global.h" class CORESHARED_EXPORT SettingsService : public IService { public: explicit SettingsService(QObject *parent = NULL); SettingsService(const QString &pluginId, QObject *parent = NULL); template QSharedPointer loadSettings() { QSharedPointer sys = loadSystem(); QObject *objSettings = new T(); QSharedPointer settingsObj((T*)objSettings); if (sys.isNull()) { return settingsObj; } QString settingStr = sys->settings(); QJsonDocument jsonDoc = QJsonDocument::fromJson(settingStr.toUtf8()); QJsonValue val = jsonDoc.object()["Settings"]; if (!val.isObject()) { return settingsObj; } for (int i = 0; i < objSettings->metaObject()->propertyCount(); i++) { const char *propName = objSettings->metaObject()->property(i).name(); QVariant varVal = val.toObject()[propName].toVariant(); objSettings->setProperty(propName, varVal); } return settingsObj; } template void saveSettings(QSharedPointer objSettings) { QObject *objSettingsQo = objSettings.data(); QJsonDocument jsonDoc; QJsonObject jsonObj; for (int i = 0; i < objSettingsQo->metaObject()->propertyCount(); i++) { const char *propName = objSettings->metaObject()->property(i).name(); jsonObj[propName] = QJsonValue::fromVariant(objSettingsQo->property(propName)); } QJsonObject jsonSettings; jsonSettings["Settings"] = jsonObj; jsonDoc.setObject(jsonSettings); saveSystem(jsonDoc); } private: QString m_pluginId; QSharedPointer m_settings; QSharedPointer loadSystem(); void saveSystem(const QJsonDocument &doc); }; #endif // SETTINGSSERVICE_H