forked from gort818/qtwebflix
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqtws.cpp
324 lines (279 loc) · 10.7 KB
/
qtws.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include "qtws.h"
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonValue>
#include <QFile>
#include <QDebug>
#include <QRegExp>
#include <QStringList>
using namespace std;
QtWS::QtWS(QString filename) {
this->multimedia = false;
this->menuDisabled = false;
this->download = false;
this->alwaysOnTop = false;
this->cacheMB = 50;
this->permissions.clear();
this->loadData(filename);
}
QList<QString> QtWS::getWScope() {
return scope;
}
bool QtWS::isInScope(QUrl url) {
for (int i = 0; i < scope.size(); i++) {
QString scopeUrl = this->getWScope().at(i);
QRegExp regex(scopeUrl);
QString urlBase = url.toString();
if (regex.indexIn(urlBase) != -1)
return true;
}
return false;
}
QString QtWS::getHome() {
return home;
}
QString QtWS::getIconPath() {
return iconPath;
}
QList<MenuAction> QtWS::getMenu() {
return this->menu;
}
QList<QString> QtWS::getPlugins() {
return plugins;
}
bool QtWS::isSaveSession() {
return saveSession;
}
QString QtWS::getName() {
return name;
}
bool QtWS::isAlwaysOnTop() {
return this->alwaysOnTop;
}
bool QtWS::isMenuDisabled() {
return this->menuDisabled;
}
bool QtWS::hasMultimedia() {
return this->multimedia;
}
bool QtWS::canDownload() {
return this->download;
}
int QtWS::getCacheMB() {
return this->cacheMB;
}
bool QtWS::hasPermission(QWebEnginePage::Feature permissionId) {
for (int i = 0; i < this->permissions.size(); i++) {
if (this->permissions.at(i) == (int)permissionId) {
return true;
}
}
return false;
}
int QtWS::getNumberOfPermissions() {
return this->permissions.size();
}
QString QtWS::getUserReadablePermissions() {
return this->userReadablePermissions.join(QString(", "));
}
void QtWS::loadData(QString filename) {
QFile file;
file.setFileName(filename);
file.open(QIODevice::ReadOnly | QIODevice::Text);
if (!file.exists()) {
throw QString("Configuration file ") + filename + QString(" not found");
}
QString content = file.readAll();
file.close();
QJsonDocument jsonDocument = QJsonDocument::fromJson(content.toUtf8());
QJsonObject jsonObject = jsonDocument.object();
if(jsonObject.isEmpty())
throw QString("Invalid configuration file");
QJsonValue titleInJson = jsonObject.value(QString("name"));
if (!titleInJson.isString()) {
throw QString("The title is not a string");
} else {
if (this->name.contains(QString("/")) || this->name.contains("\\"))
throw QString("Illegal name " + this->name + ": it cannot contain slashes or backslashes.");
this->name = titleInJson.toString();
}
QJsonValue scopeInJson = jsonObject.value(QString("scope"));
if (!scopeInJson.isArray()) {
throw QString("The scope is not an array");
return;
} else {
this->scope.clear();
QJsonArray array = scopeInJson.toArray();
for (int i = 0; i < array.size(); i++) {
QJsonValue scopePart = array.at(i);
if (!scopePart.isString()) {
throw QString("One of the scopes is not a string");
return;
} else {
this->scope.append(scopePart.toString());
}
}
}
QJsonValue pluginsInJson = jsonObject.value(QString("plugins"));
if (!pluginsInJson.isArray()) {
throw QString("The plugins are not an array");
return;
} else {
this->plugins.clear();
QJsonArray array = pluginsInJson.toArray();
for (int i = 0; i < array.size(); i++) {
QJsonValue plugin = array.at(i);
if (!plugin.isString()) {
throw QString("One of the plugins is not a string");
return;
} else {
this->plugins.append(plugin.toString());
}
}
}
QJsonValue homeInJson = jsonObject.value(QString("home"));
if (!homeInJson.isString()) {
throw QString("The home is not a string");
return;
} else {
this->home = homeInJson.toString();
}
QJsonValue iconInJson = jsonObject.value(QString("icon"));
if (!iconInJson.isString()) {
throw QString("The icon is not a string");
return;
} else {
this->iconPath = iconInJson.toString();
}
QJsonValue sessionInJson = jsonObject.value(QString("saveSession"));
if (!sessionInJson.isBool()) {
throw QString("Save session is not boolean");
} else {
this->saveSession = sessionInJson.toBool();
}
QJsonValue cacheInJson = jsonObject.value(QString("cacheMB"));
if (!cacheInJson.isUndefined()) {
if (!cacheInJson.isDouble()) {
throw QString("The cache is not a number");
return;
} else {
this->cacheMB = iconInJson.toInt(50);
}
}
QJsonValue alwaysOnTopVal = jsonObject.value(QString("alwaysOnTop"));
if (!alwaysOnTopVal.isBool()) {
this->alwaysOnTop = false;
} else {
this->alwaysOnTop = alwaysOnTopVal.toBool();
}
QJsonValue menuDisabledJson = jsonObject.value(QString("menuDisabled"));
if (!menuDisabledJson.isUndefined()) {
if (!menuDisabledJson.isBool()) {
throw QString("Menu disabling is not boolean");
} else {
this->menuDisabled = menuDisabledJson.toBool();
}
}
QJsonValue multimediaJson = jsonObject.value(QString("multimedia"));
if (!multimediaJson.isUndefined()) {
if (!multimediaJson.isBool()) {
throw QString("Multimedia is not boolean");
} else {
this->multimedia = multimediaJson.toBool();
}
}
QJsonValue downloadJson = jsonObject.value(QString("download"));
if (!downloadJson.isUndefined()) {
if (!downloadJson.isBool()) {
throw QString("Download is not boolean");
} else {
this->download = downloadJson.toBool();
}
}
QJsonValue permissionsInJson = jsonObject.value(QString("permissions"));
if (!permissionsInJson.isUndefined()) {
this->permissions.clear();
if (!permissionsInJson.isArray()) {
throw QString("Permissions is not an array");
} else {
QJsonArray permissionsArray = permissionsInJson.toArray();
for (int i = 0; i < permissionsArray.size(); i++) {
QJsonValue permissionItem = permissionsArray.at(i);
if (!permissionItem.isString()) {
throw QString("All the permissions have to be strings");
}
QString permissionRequested = permissionItem.toString();
if (permissionRequested == QString("Notifications")) {
this->permissions.append(0);
this->userReadablePermissions.append(QString("Notifications"));
} else if (permissionRequested == QString("Geolocation")) {
this->permissions.append(1);
this->userReadablePermissions.append(QString("Geolocation"));
} else if (permissionRequested == QString("MediaAudioCapture")) {
this->permissions.append(2);
this->userReadablePermissions.append(QString("Microphone"));
} else if (permissionRequested == QString("MediaVideoCapture")) {
this->permissions.append(3);
this->userReadablePermissions.append(QString("Camera"));
} else if (permissionRequested == QString("MediaAudioVideoCapture")) {
this->permissions.append(4);
this->userReadablePermissions.append(QString("Microphone+Camera"));
} else if (permissionRequested == QString("MouseLock")) {
this->permissions.append(5);
this->userReadablePermissions.append(QString("Mouse lock"));
} else if (permissionRequested == QString("DesktopVideoCapture")) {
this->permissions.append(6);
this->userReadablePermissions.append(QString("Desktop video capture"));
} else if (permissionRequested == QString("DesktopAudioVideoCapture")) {
this->permissions.append(7);
this->userReadablePermissions.append(QString("Desktop video+audio capture"));
} else
throw QString("Invalid permission \"") + permissionRequested + QString("\"");
}
}
}
QJsonValue menuInJson = jsonObject.value(QString("menu"));
if (!menuInJson.isUndefined()) {
this->menu.clear();
if (!menuInJson.isArray()) {
throw QString("Menu is not an array");
} else {
QJsonArray menuArray = menuInJson.toArray();
for (int i = 0; i < menuArray.size(); i++) {
QJsonValue element = menuArray.at(i);
if (!element.isObject()) {
throw QString("Menu item is not an object");
} else {
QJsonObject menuItem = element.toObject();
QJsonValue menuItemName = menuItem.value(QString("title"));
QJsonValue menuItemAction = menuItem.value(QString("action"));
QJsonValue menuItemIcon = menuItem.value(QString("icon"));
QJsonValue menuSeparator = menuItem.value(QString("separator"));
if(menuSeparator.isUndefined())
menuSeparator = QJsonValue(false);
if (!menuItemName.isString()) {
throw QString("Menu item does not have a title");
} else if (!menuItemAction.isString()) {
throw QString("Menu item does not have an action");
} else if (!menuItemIcon.isString() && !menuItemIcon.isUndefined()) {
throw QString("Menu item does not have a string icon name");
} else if (!menuSeparator.isBool()) {
throw QString("Menu separator is not bool");
} else {
if (menuItemIcon.isUndefined()) {
MenuAction action(menuItemName.toString(), menuItemAction.toString(), menuSeparator.toBool());
this->menu.append(action);
} else {
MenuAction action(menuItemName.toString(), menuItemAction.toString(), menuItemIcon.toString(), menuSeparator.toBool());
this->menu.append(action);
}
}
}
}
}
}
}
QString QtWS::getConfigName() {
return QString("qtws_") + this->name;
}