Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* A library for Qt app
*
* LICENSE: The GNU Lesser General Public License, version 3.0
*
* @author Akira Ohgaki <akiraohgaki@gmail.com>
* @copyright Akira Ohgaki
* @license https://opensource.org/licenses/LGPL-3.0 The GNU Lesser General Public License, version 3.0
* @link https://github.com/akiraohgaki/qtlibs
*/
#include "config.h"
#include "file.h"
#include "dir.h"
#include "json.h"
namespace qtlibs {
Config::Config(const QString &configDirPath, QObject *parent) :
QObject(parent), configDirPath_(configDirPath)
{}
QString Config::configDirPath() const
{
return configDirPath_;
}
void Config::setConfigDirPath(const QString &configDirPath)
{
configDirPath_ = configDirPath;
}
QJsonObject Config::get(const QString &name)
{
QString configFilePath = configDirPath() + "/" + name + ".json";
QByteArray json = qtlibs::File(configFilePath).readData();
if (json.isEmpty()) {
json = QString("{}").toUtf8(); // Blank JSON data as default
}
return qtlibs::Json(json).toObject();
}
bool Config::set(const QString &name, const QJsonObject &object)
{
QString configFilePath = configDirPath() + "/" + name + ".json";
QByteArray json = qtlibs::Json(object).toJson();
qtlibs::Dir(configDirPath()).make();
if (qtlibs::File(configFilePath).writeData(json)) {
return true;
}
return false;
}
} // namespace qtlibs