From c60ef656bf16db63fa049e71f7544bb095b774a9 Mon Sep 17 00:00:00 2001
From: Akira Ohgaki <akiraohgaki@gmail.com>
Date: Wed, 17 Aug 2016 12:45:04 +0900
Subject: [PATCH] Add core/config class

---
 src/core/config.cpp | 39 +++++++++++++++++++++++++++++++++++++++
 src/core/config.h   | 26 ++++++++++++++++++++++++++
 2 files changed, 65 insertions(+)
 create mode 100644 src/core/config.cpp
 create mode 100644 src/core/config.h

diff --git a/src/core/config.cpp b/src/core/config.cpp
new file mode 100644
index 0000000..49b3b99
--- /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 0000000..1a4ae9a
--- /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
-- 
GitLab