diff --git a/src/core/config.cpp b/src/core/config.cpp new file mode 100644 index 0000000000000000000000000000000000000000..49b3b99a7e918765e7957aebcd2307577bbbcb68 --- /dev/null +++ b/src/core/config.cpp @@ -0,0 +1,39 @@ +#include "../utility/file.h" +#include "../utility/json.h" + +#include "config.h" + +namespace Core { + +Config::Config(const QString &configsDir, QObject *parent) : + QObject(parent), _configsDir(configsDir) +{} + +QJsonObject Config::get(const QString &name) +{ + QString configFile = _configsDir + "/" + name + ".json"; + + if (!_cacheData.contains(name)) { + QString json = Utility::File::readText(configFile); + if (json.isEmpty()) { + json = "{}"; // Blank JSON data as default + } + _cacheData[name] = Utility::Json::convertStrToObj(json); + } + return _cacheData[name].toObject(); +} + +bool Config::set(const QString &name, const QJsonObject &jsonObj) +{ + QString configFile = _configsDir + "/" + name + ".json"; + QString json = Utility::Json::convertObjToStr(jsonObj); + + Utility::File::makeDir(_configsDir); + if (Utility::File::writeText(configFile, json)) { + _cacheData[name] = jsonObj; + return true; + } + return false; +} + +} // namespace Core diff --git a/src/core/config.h b/src/core/config.h new file mode 100644 index 0000000000000000000000000000000000000000..1a4ae9a70ef43f566421155f2eceb80d4a34fe89 --- /dev/null +++ b/src/core/config.h @@ -0,0 +1,26 @@ +#ifndef CORE_CONFIG_H +#define CORE_CONFIG_H + +#include <QObject> +#include <QJsonObject> + +namespace Core { + +class Config : public QObject +{ + Q_OBJECT + +private: + QString _configsDir; + QJsonObject _cacheData; + +public: + explicit Config(const QString &configsDir, QObject *parent = 0); + + QJsonObject get(const QString &name); + bool set(const QString &name, const QJsonObject &jsonObj); +}; + +} // namespace Core + +#endif // CORE_CONFIG_H