-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreset.cpp
305 lines (283 loc) · 10.7 KB
/
preset.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Copyright 2022-present Contributors to the jobman project.
// SPDX-License-Identifier: BSD-3-Clause
// https://github.com/mikaelsundell/jobman
#include "preset.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QFile>
#include <QPointer>
#include <QDebug>
Option::Option(QObject* parent)
: QObject(parent)
{
}
Task::Task(QObject* parent)
: QObject(parent)
{
}
class PresetPrivate
{
public:
PresetPrivate();
void init();
bool read();
public:
QString id;
QString error;
QString filename;
QString name;
QList<QString> description;
QList<Option*> options;
QList<Task*> tasks;
QUuid uuid;
bool valid;
QPointer<Preset> preset;
};
PresetPrivate::PresetPrivate()
: valid(false)
, uuid(QUuid::createUuid())
{
}
void
PresetPrivate::init()
{
}
bool
PresetPrivate::read()
{
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
error = QString("Failed to open file: %1").arg(filename);
valid = false;
return valid;
}
QByteArray jsonData = file.readAll();
file.close();
QJsonParseError jsonError;
QJsonDocument document = QJsonDocument::fromJson(jsonData, &jsonError);
if (document.isNull()) {
error = QString("Failed to create json document for file:\n"
"%1\n\n"
"Parse error:\n"
"%2 at offset %3")
.arg(filename)
.arg(jsonError.errorString())
.arg(jsonError.offset);
valid = false;
return valid;
}
if (!document.isObject()) {
error = QString("Json document is not an object for file: %1").arg(filename);
valid = false;
return valid;
}
QJsonObject json = document.object();
if (json.contains("id") && json["id"].isString()) {
id = json["id"].toString();
}
else {
error = QString("Json for preset: \"%1\" contains no a unique id").arg(filename);
valid = false;
return valid;
}
if (json.contains("name") && json["name"].isString()) {
name = json["name"].toString();
}
if (json.contains("options") && json["options"].isArray()) {
QJsonArray optionsArray = json["options"].toArray();
for (int i = 0; i < optionsArray.size(); ++i) {
QJsonObject jsonoption = optionsArray[i].toObject();
Option* option = new Option(preset.data());
if (jsonoption.contains("id") && jsonoption["id"].isString()) option->id = jsonoption["id"].toString();
if (jsonoption.contains("name") && jsonoption["name"].isString()) option->name = jsonoption["name"].toString();
if (jsonoption.contains("flag") && jsonoption["flag"].isString()) option->flag = jsonoption["flag"].toString();
if (jsonoption.contains("type") && jsonoption["type"].isString()) option->type = jsonoption["type"].toString();
if (jsonoption.contains("value")) option->value = jsonoption["value"].toVariant();
if (jsonoption.contains("default")) option->defaultvalue = jsonoption["default"].toVariant();
if (jsonoption.contains("minimum")) option->minimum = jsonoption["minimum"].toVariant();
if (jsonoption.contains("maximum")) option->maximum = jsonoption["maximum"].toVariant();
if (jsonoption.contains("options") && jsonoption["options"].isArray()) {
QJsonArray optionsArray = jsonoption["options"].toArray();
for (const QJsonValue &opt : optionsArray) {
QJsonObject optObj = opt.toObject();
QString label;
QVariant optValue;
if (optObj.contains("label") && optObj["label"].isString())
label = optObj["label"].toString();
if (optObj.contains("value"))
optValue = optObj["value"].toVariant();
option->options.append(qMakePair(label, optValue));
}
}
if (option->options.count()) {
bool hasdefault, hasvalue = false;
for (const QPair<QString, QVariant>& pair : option->options) {
if (pair.second == option->defaultvalue) {
hasdefault = true;
}
if (pair.second == option->value) {
hasvalue = true;
}
}
if (!hasdefault) {
error = QString("Json for option: \"%1\" does not contain default value in options").arg(option->name);
valid = false;
return valid;
}
if (!hasvalue) {
error = QString("Json for option: \"%1\" does not contain value in options").arg(option->name);
valid = false;
return valid;
}
}
if (!option->name.isEmpty() && !option->type.isEmpty() && !option->defaultvalue.isValid() && !option->value.isValid()) {
if (option->name.length() > 0) {
error = QString("Json for option: \"%1\" does not contain all required attributes").arg(option->name);
} else {
error = QString("Json for option: %1 does not contain all required attributes").arg(i);
}
if (option->id.isEmpty()) {
error += QString("\nMissing attribute: %1").arg("id");
}
if (option->type.isEmpty()) {
error += QString("\nMissing attribute: %1").arg("type");
}
if (option->defaultvalue.isValid()) {
error += QString("\nMissing attribute: %1").arg("default");
}
if (option->value.isValid()) {
error += QString("\nMissing attribute: %1").arg("value");
}
valid = false;
return valid;
}
else {
if (!(option->type == "Checkbox" || option->type == "Double" || option->type == "File" || option->type == "Slider" || option->type == "Dropdown" || option->type == "Text") ) {
error = QString("Json for option: %1 contains an invalid type: %1, valid types are Slider, Dropdown, Text and File").arg(i);
valid = false;
return valid;
}
}
options.append(option);
}
}
if (json.contains("tasks") && json["tasks"].isArray()) {
QJsonArray tasksArray = json["tasks"].toArray();
for (int i = 0; i < tasksArray.size(); ++i) {
QJsonObject jsontask = tasksArray[i].toObject();
Task* task = new Task(preset.data());
if (jsontask.contains("id") && jsontask["id"].isString()) task->id = jsontask["id"].toString();
if (jsontask.contains("name") && jsontask["name"].isString()) task->name = jsontask["name"].toString();
if (jsontask.contains("command") && jsontask["command"].isString()) task->command = jsontask["command"].toString();
if (jsontask.contains("extension") && jsontask["extension"].isString()) task->extension = jsontask["extension"].toString();
if (jsontask.contains("output") && jsontask["output"].isString()) task->output = jsontask["output"].toString();
if (jsontask.contains("arguments") && jsontask["arguments"].isString()) task->arguments = jsontask["arguments"].toString();
if (jsontask.contains("startin") && jsontask["startin"].isString()) task->startin = jsontask["startin"].toString();
if (jsontask.contains("dependson") && jsontask["dependson"].isString()) task->dependson = jsontask["dependson"].toString();
if (jsontask.contains("documentation") && jsontask["documentation"].isArray()) {
QJsonArray docarray = jsontask["documentation"].toArray();
for (int i = 0; i < docarray.size(); ++i) {
task->documentation.append(docarray[i].toString());
}
}
if (!task->id.isEmpty() && !task->name.isEmpty() && !task->command.isEmpty() && !task->extension.isEmpty() && !task->arguments.isEmpty()) {
if (task->dependson.length() > 0) {
bool found = false;
for (int j = 0; j < tasks.size(); ++j) {
if (tasks[j]->id == task->dependson) {
found = true;
break;
}
}
if (!found) {
error = QString("Json for task: \"%1\" contains a dependson id that can not be found").arg(task->name);
valid = false;
return valid;
}
}
tasks.append(task);
} else {
if (task->name.length() > 0) {
error = QString("Json for task: \"%1\" does not contain all required attributes").arg(task->name);
} else {
error = QString("Json for task: %1 does not contain all required attributes").arg(i);
}
if (task->id.isEmpty()) {
error += QString("\nMissing attribute: %1").arg("id");
}
if (task->name.isEmpty()) {
error += QString("\nMissing attribute: %1").arg("name");
}
if (task->command.isEmpty()) {
error += QString("\nMissing attribute: %1").arg("command");
}
if (task->extension.isEmpty()) {
error += QString("\nMissing attribute: %1").arg("extension");
}
if (task->arguments.isEmpty()) {
error += QString("\nMissing attribute: %1").arg("arguments");
}
valid = false;
return valid;
}
}
}
valid = true;
return valid;
}
Preset::Preset()
: p(new PresetPrivate())
{
p->preset = this;
p->init();
}
Preset::~Preset()
{
}
bool
Preset::read(const QString& filename)
{
p->filename = filename;
return p->read();
}
bool
Preset::valid() const
{
return p->valid;
}
QString
Preset::error() const
{
return p->error;
}
QString
Preset::id() const
{
return p->id;
}
QString
Preset::filename() const
{
return p->filename;
}
QString
Preset::name() const
{
return p->name;
}
QList<Option*>
Preset::options() const
{
return p->options;
}
QList<Task*>
Preset::tasks() const
{
return p->tasks;
}
QUuid
Preset::uuid() const
{
return p->uuid;
}