-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjsonfile.cpp
More file actions
260 lines (229 loc) · 9.28 KB
/
jsonfile.cpp
File metadata and controls
260 lines (229 loc) · 9.28 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
This file is part of WebTooth-Extractor and for personal use the
GPL3 license applies. For any kind of commercial usage, you have to
acquire a commercial license before use. The copyright of the WebTooth
application and source code remains with Patrick Scheller and any
contributor to the project agrees with his commits to renounce to the
copyright of his parts completely.
WebTooth-Extractor is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License.
WebTooth-Extractor is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with WebTooth-Extractor. If not, see http://www.gnu.org/licenses/ .
*/
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonParseError>
#include <QtDebug>
#include <QDir>
#include <QListWidgetItem>
#include <QDateTime>
#include <qtpropertymanager.h>
#include <qttreepropertybrowser.h>
#include "jsonfile.h"
JSONFile::JSONFile(QObject *parent) : QObject(parent)
{
}
JSONFile::~JSONFile()
{
}
bool JSONFile::readJsonFile(t_filterdata& filterlist, QHash<QString, QString>& projectdata, const QString &prjfilepath)
{
bool bRes = false;
QJsonParseError jerror;
QFile projFile(prjfilepath);
if (!projFile.exists())
{
qDebug() << "Error: File" << prjfilepath << "does not exist!";
return(bRes);
}
else
{
//qDebug() << "File" << prjfilepath << "exists";
}
QJsonDocument jdoc;
if (projFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
//qDebug() << "JSON-File could be opened";
jdoc = QJsonDocument::fromJson(projFile.readAll(),&jerror);
if(jerror.error != QJsonParseError::NoError)
{
qDebug() << "ERROR-JSON-Read:" << jerror.errorString() << "at position:" << jerror.offset;
projFile.close();
return (bRes);
}
else
{
//qDebug() << "QJsonDocument::fromJson() succeeded";
}
}
else
{
qDebug() << "ERROR-JSON: File could not be opened";
projFile.close();
return(bRes);
}
QJsonObject rootObj = jdoc.object();
projFile.close();
//QHashIterator<QString, QString> itPrjData(projectdata);
projectdata.insert("projectapp", "WebTooth-Extractor");
projectdata.insert("projectname", rootObj["projectname"].toString());
projectdata.insert("projectauthor", rootObj["projectauthor"].toString());
projectdata.insert("projectmodified", rootObj["projectmodified"].toString());
projectdata.insert("cropthumb", rootObj["cropthumb"].toString());
projectdata.insert("projecnotes", rootObj["projecnotes"].toString());
projectdata.insert("projecturl", rootObj["projecturl"].toString());
projectdata.insert("thumbheight", rootObj["thumbheight"].toString());
projectdata.insert("thumbwidth", rootObj["thumbwidth"].toString());
// typedef QHash<QString, QVector<QString>*> t_filterdata;
QJsonArray filters = rootObj["projectdata"].toArray();
qDebug() << "No. of filters found in the project file:" << filters.size();
int ideb = 0; // For Debug purpose
for(auto&& filterItem: filters)
{
//qDebug() << "XXX - filterItem";
const QJsonObject& filter = filterItem.toObject();
//filterlist.insert("filtername", new QVector<QString>(){filter["filtername"].toString()} );
//filterlist.insert("filtertype", new QVector<QString>(){filter["filtertype"].toString()} );
QJsonArray arrProperties = filter["filterdata"].toArray();
QVector<QString>* vattrlist= new QVector<QString>();
for (auto&& item: arrProperties)
{
const QJsonObject& attrProp = item.toObject();
if (filter["filtertype"].toString() == "tag")
{
//qDebug() << "Filtertype:" << filter["filtertype"].toString();
// qDebug() << " > Filter-Sizes:" << attrProp["H Tag Name"].toString().size() << "," << attrProp["H ID Name"].toString().size() << "," << attrProp["H Class Name"].toString().size()
// << "," << attrProp["P Tag Name"].toString().size() << "," << attrProp["P Attribute Name"].toString().size() << "," << attrProp["P ID Name"].toString().size()
// << "," << attrProp["P Class Name"].toString().size();
vattrlist->push_back(filter["filtertype"].toString());
vattrlist->push_back(attrProp["H Tag Name"].toString());
vattrlist->push_back(attrProp["H Attribute Name"].toString());
vattrlist->push_back(attrProp["H ID Name"].toString());
vattrlist->push_back(attrProp["H Class Name"].toString());
vattrlist->push_back(attrProp["P Tag Name"].toString());
vattrlist->push_back(attrProp["P Attribute Name"].toString());
vattrlist->push_back(attrProp["P ID Name"].toString());
vattrlist->push_back(attrProp["P Class Name"].toString());
vattrlist->push_back(attrProp["Occurrence Amount"].toString());
vattrlist->push_back(attrProp["Occurrence Function"].toString());
}
else if (filter["filtertype"].toString() == "reg")
{
//qDebug() << "XXX - RegExpr";
vattrlist->push_back(filter["filtertype"].toString());
vattrlist->push_back(attrProp["Regular Expression"].toString());
}
}
qDebug() << "vattrList contains no. of elements:" << vattrlist->size();
filterlist.insert(filter["filtername"].toString(), vattrlist);
//qDebug() << "YYY - End of JSON-Read";
bRes = true;
ideb++;
}
qDebug() << "iDeb count of loops:" << ideb;
qDebug() << "No. of elements in Filterlist:" << filterlist.size();
return(bRes);
}
bool JSONFile::writeJsonFile(const t_filterdata& filterlist, const QHash<QString, QString>& projectdata, const QString &prjfilepath)
{
qDebug() << "Entering writeJsonFile";
bool bRes = false;
QDir dir;
qDebug() << "Current Path: " << dir.currentPath() << flush;
//QFileInfo myfileInfo;
QFile projFile(prjfilepath);
if (!projFile.open(QIODevice::WriteOnly | QIODevice::Text))
{
qWarning("Couldn't open the project file.");
return(bRes);
}
// *******************************
QJsonObject rootObj;//root object
QHashIterator<QString, QString> itPrjData(projectdata);
while (itPrjData.hasNext())
{
itPrjData.next();
// if (itPrjData.key() == "projectauthor")
// qDebug() << "ProjectAuthor will be written to JSON:" << itPrjData.value();
rootObj[itPrjData.key()] = itPrjData.value();
}
//rootObj["projectapp"] = "WebTooth-Extractor";
//rootObj["projectname"] = "usertest";
//rootObj["projectauthor"] = "Lunix Sdlavrot";
//QDateTime now = QDateTime::currentDateTime();
//rootObj["projectmodified"] = now.toString(Qt::ISODate);
QJsonArray filters;
auto itFList = filterlist.constBegin();
while (itFList != filterlist.constEnd())
{
QJsonObject filter;
filter["filtername"] = itFList.key();
auto myvec = itFList.value();
filter["filtertype"] = myvec->at(0);
QJsonArray arrProperties;
QJsonObject objProperties;
if (myvec->at(0) == "tag")
{
objProperties["H Tag Name"] = myvec->at(1);
objProperties["H Attribute Name"] = myvec->at(2);
objProperties["H ID Name"] = myvec->at(3);
objProperties["H Class Name"] = myvec->at(4);
objProperties["P Tag Name"] = myvec->at(5);
objProperties["P Attribute Name"] = myvec->at(6);
objProperties["P ID Name"] = myvec->at(7);
objProperties["P Class Name"] = myvec->at(8);
objProperties["Occurrence Amount"] = myvec->at(9);
objProperties["Occurrence Function"] = myvec->at(10);
}
else if (myvec->at(0) == "reg")
{
objProperties["Regular Expression"] = myvec->at(1);
}
arrProperties.append(objProperties);
filter["filterdata"] = arrProperties;
filters.append(filter);
itFList++;
}
rootObj["projectdata"] = filters;
//qDebug() << "writeJsonFile: Before write to file.";
qint64 byteswritten = projFile.write( QJsonDocument(rootObj).toJson(/*QJsonDocument::Compact*/) );
//qDebug() << "writeJsonFile: After write to file.";
projFile.close();
if (byteswritten == -1)
{
qDebug() << "No Bytes written to the JsonFile :(";
qWarning("Error: Could not write to the project file.");
}
else
{
qDebug() << byteswritten << "Bytes have been written to the file";
bRes = true;
}
return(bRes);
}
bool JSONFile::openJsonFile()
{
bool bRes = false;
return(bRes);
}
QString JSONFile::getJsonFilePath(void)
{
QString sRes = "";
return(sRes);
}
qint64 JSONFile::getJsonFileSize(void)
{
qint64 iRes = 0;
return(iRes);
}
bool JSONFile::checkJsonFile()
{
bool bRes = false;
return(bRes);
}