Repository navigation
Expand file tree
/
Copy pathjsonstorage.cpp
More file actions
147 lines (114 loc) · 3.74 KB
/
Copy pathjsonstorage.cpp
File metadata and controls
147 lines (114 loc) · 3.74 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "jsonstorage.h"
#include <QFileInfo>
JSONStorage::JSONStorage(QObject *parent) :
QObject(parent), _dbLevel(Prod), _settingsJson()
{
if (!loadSettings()) {
qWarning("Couldn't load settings.");
}
}
JSONStorage::~JSONStorage()
{
}
//These 3 files should be stored in the QStandardPaths DocumentsLocation: /home/nemo/Documents/landed28/
const QString JSONStorage::MASTERDBNAMETEST = "landeddb.masterdata.test.json";
const QString JSONStorage::MASTERDBNAMEPROD = "landeddb.masterdata.prod.json";
const QString JSONStorage::SETTINGSDBNAME = "landeddb.settings.json";
/*
landeddb.settings.json should look like this:
{
"dblevel" : "test"
}
*/
QString JSONStorage::buildFullPath(DBType dbType){
//For the moment, on Android, we stick the dbs in a sub-dir of the documents folder
//sub-dir is org.flyingsheep.landed
QString docPath = QStandardPaths::locate(QStandardPaths::DocumentsLocation, QString(), QStandardPaths::LocateDirectory);
qDebug() << "Document Path: " << docPath;
QString domainName = QCoreApplication::organizationDomain();
qDebug() << "App Name: " << domainName;
qDebug() << "dbType: " << dbType;
QString fullPath;
switch (dbType) {
case Settings:
fullPath = docPath + domainName + "/" + SETTINGSDBNAME;
break;
case Masterdata:
switch (_dbLevel) {
case Prod:
fullPath = docPath + domainName + "/" + MASTERDBNAMEPROD;
break;
case Test:
fullPath = docPath + domainName + "/" + MASTERDBNAMETEST;
break;
default:
fullPath = docPath + domainName + "/" + MASTERDBNAMEPROD;
}
break;
}
qDebug() << "Full path: " + fullPath;
return fullPath;
}
QString JSONStorage::openDatabase(DBType dbType) {
QString fullPath = buildFullPath(dbType);
QFileInfo check_file(fullPath);
if (check_file.exists() && check_file.isFile()) {
qDebug() << "File exists: " << fullPath;
QFile file;
file.setFileName(fullPath);
file.open(QIODevice::ReadOnly | QIODevice::Text);
qDebug() << "file.open error: "<< file.error();
QString cargo;
cargo = file.readAll();
file.close();
//qDebug() << cargo;
return cargo;
} else {
qDebug() << "File Not found: " << fullPath;
return "File Not found";
}
}
//functions below are associated with settings
//and may be refactored into a separate class / plugin
void JSONStorage::setDbLevel(DBLevel dbLevel) {
_dbLevel = dbLevel;
}
void JSONStorage::writeSetting(QJsonObject &json, QString settingName, QString settingValue) const
{
json[settingName] = settingValue;
}
QString JSONStorage::readSetting(const QJsonObject &json, QString settingName)
{
return json[settingName].toString();
}
bool JSONStorage::loadSettings()
{
//open the json settings file
QString fullPath = buildFullPath(Settings);
QFile settingsFile(fullPath);
if (!settingsFile.open(QIODevice::ReadOnly)) {
qWarning("Couldn't open save file.");
return false;
}
//read the contents into a bytearray
QByteArray settingsData = settingsFile.readAll();
settingsFile.close();
//store content as a local json structure
_settingsJson.fromJson(settingsData);
return true;
}
bool JSONStorage::saveSettings() const
{
QFile saveFile(QStringLiteral("save.json"));
if (!saveFile.open(QIODevice::WriteOnly)) {
qWarning("Couldn't open save file.");
return false;
}
/*
QJsonObject gameObject;
write(gameObject);
QJsonDocument saveDoc(gameObject);
saveFile.write(saveDoc.toJson());
*/
return true;
}