You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
prodejna/core/settingsservice.h

79 lines
2.1 KiB
C

#ifndef SETTINGSSERVICE_H
#define SETTINGSSERVICE_H
#include <QString>
#include <QSharedPointer>
#include <QJsonObject>
#include <QJsonDocument>
#include <QMetaProperty>
#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 <class T>
QSharedPointer<T> loadSettings() {
QSharedPointer<System> sys = loadSystem();
QObject *objSettings = new T();
QSharedPointer<T> 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 <class T>
void saveSettings(QSharedPointer<T> 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<QObject> m_settings;
QSharedPointer<System> loadSystem();
void saveSystem(const QJsonDocument &doc);
};
#endif // SETTINGSSERVICE_H