-
Notifications
You must be signed in to change notification settings - Fork 1
/
fileconfigure.cpp
68 lines (60 loc) · 2.05 KB
/
fileconfigure.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "fileconfigure.h"
FileConfigure::FileConfigure()
{
QFileInfo dirfile(this->m_config_path);
if(dirfile.exists() == false) {
qDebug() << "make dir:" << this->m_config_path;
QDir().mkdir(dirfile.dir().absolutePath());
QFile::copy(":/config.json", this->m_config_path);
QFile().setPermissions(this->m_config_path, QFileDevice::ReadOwner|QFileDevice::WriteOwner|QFileDevice::ExeOwner);
}
QFile pfile(this->m_config_path);
pfile.open(QIODevice::ReadWrite);
this->m_config = QJsonDocument::fromJson(pfile.readAll()).object();
pfile.close();
}
QJsonValue FileConfigure::getValue(QString path)
{
FileConfigure self_conf;
QStringList path_list = path.split(".");
int loop_times = 0;
QJsonValue temp_result;
QJsonObject temp_object = self_conf.m_config;
for(auto item : path_list) {
loop_times++;
temp_result = temp_object[item];
if(loop_times != path_list.size()) temp_object = temp_result.toObject();
}
return temp_result;
}
int FileConfigure::add_collection(QString path)
{
FileConfigure self_conf;
QJsonArray temp_object = self_conf.m_config["search_path"].toArray();
temp_object.append(path);
self_conf.m_config["search_path"] = temp_object;
QFile pfile(self_conf.m_config_path);
pfile.open(QIODevice::ReadWrite);
pfile.write(QJsonDocument(self_conf.m_config).toJson());
pfile.close();
return 0;
}
int FileConfigure::remove_collection(QString path)
{
FileConfigure self_conf;
QJsonArray temp_object = self_conf.m_config["search_path"].toArray();
//temp_object.append(path);
int loop_i;
for(loop_i = 0; loop_i < temp_object.size(); ++loop_i) {
if(temp_object.at(loop_i) == path) {
break;
}
}
temp_object.removeAt(loop_i);
self_conf.m_config["search_path"] = temp_object;
QFile pfile(self_conf.m_config_path);
pfile.open(QIODevice::ReadWrite|QFile::Truncate);
pfile.write(QJsonDocument(self_conf.m_config).toJson());
pfile.close();
return 0;
}