-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpreferences.cpp
More file actions
335 lines (279 loc) · 8.33 KB
/
preferences.cpp
File metadata and controls
335 lines (279 loc) · 8.33 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
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
325
326
327
328
329
330
331
332
333
334
335
#include "preferences.h"
#include <QDir>
#include <QCryptographicHash>
#include <QJsonDocument>
#include <QJsonObject>
#include <QDebug>
const QByteArray s1 = "__StreamPlay@";
const QByteArray s2 = "QStringQByteArrayQObject";
Preferences::Preferences(QObject *parent)
: QObject(parent), recentFiles()
{
#ifdef QT_DEBUG
s.clear();
#endif
// set up first-run settings
if (!s.contains("inbuilt/hasrun") || !s.value("inbuilt/hasrun").toBool())
{
qDebug() << "first run";
setSavePath( QDir::tempPath() + "/streamplay" );
setStreamPath( QDir::tempPath() + "/streamplay/stream" );
setSaveTorrentVideos(false);
setBittorrentPort(2323);
s.remove("user");
s.setValue("inbuilt/hasrun", true);
}
ch1();
}
// save path
QString Preferences::savePath()
{
return s.value("storage/savepath").toString();
}
void Preferences::setSavePath(QString path)
{
s.setValue("storage/savepath", QDir().cleanPath(path));
}
// stream path
QString Preferences::streamPath()
{
return s.value("storage/streampath").toString();
}
void Preferences::setStreamPath(QString path)
{
s.setValue("storage/streampath", QDir().cleanPath(path));
}
// recently opened files
QVector<Preferences::RecentFile>* Preferences::recentlyOpenedFiles()
{
if (!recentFiles)
{
int sz = s.beginReadArray("recentfiles");
recentFiles = new QVector<Preferences::RecentFile>;
for (int i=0;i<sz;i++)
{
s.setArrayIndex(i);
Preferences::RecentFile rf;
rf.name = s.value("name").toString();
rf.path = s.value("path").toString();
recentFiles->append(rf);
}
s.endArray();
emit recentFilesUpdated();
}
return recentFiles;
}
void Preferences::addRecentlyOpenedFile(QString name, QString path)
{
recentlyOpenedFiles();
RecentFile rf;
rf.name = name;
rf.path = path;
recentFiles->prepend(rf);
for (QVector<Preferences::RecentFile>::iterator it = recentFiles->begin()+1;
it != recentFiles->end(); )
{
if (rf.name == it->name)
it = recentFiles->erase(it);
else
++it;
}
// remove last if > 20 items
while (recentFiles->length() > 20)
recentFiles->erase(recentFiles->end()-1);
// repopulate settings
s.remove("recentfiles");
s.beginWriteArray("recentfiles");
for (int i=0;i<recentFiles->length();i++)
{
s.setArrayIndex(i);
s.setValue("name", recentFiles->at(i).name);
s.setValue("path", recentFiles->at(i).path);
}
s.endArray();
emit recentFilesUpdated();
}
bool Preferences::loadLicense(QString fileName)
{
// open file
QFile lf(fileName);
if (!lf.open(QIODevice::ReadOnly))
return false;
// read contents into json document
QJsonDocument doc = QJsonDocument::fromJson(lf.readAll());
lf.close();
if (doc.isNull() || !doc.isObject())
return false;
// check required fields
QJsonObject obj = doc.object();
if (!obj.contains("name") ||
!obj.contains("email") ||
!obj.contains("order") ||
!obj.contains("created") ||
!obj.contains("description") ||
!obj.contains("signature"))
return false;
// fields ok, add to settings
s.setValue("user/name", obj.value("name").toString().trimmed());
s.setValue("user/email", obj.value("email").toString().trimmed());
s.setValue("user/order", obj.value("order").toString().trimmed());
s.setValue("user/created", obj.value("created").toString().trimmed());
s.setValue("user/description", obj.value("description").toString().trimmed());
s.setValue("user/sig", obj.value("signature").toString().trimmed());
ch3();
if (cch == false)
s.remove("user");
return true;
}
QString Preferences::regName()
{
return s.value("user/name").toString();
}
QString Preferences::regEmail()
{
return s.value("user/email").toString();
}
QString Preferences::regOrder()
{
return s.value("user/order").toString();
}
void Preferences::clearRecentlyOpenedFiles()
{
recentlyOpenedFiles();
s.remove("recentfiles");
recentFiles->clear();
emit recentFilesUpdated();
}
// save torrents
bool Preferences::saveTorrentVideos()
{
return s.value("storage/savetorrentvideos").toBool();
}
void Preferences::setSaveTorrentVideos(bool b)
{
s.setValue("storage/savetorrentvideos", b);
}
// bt port
int Preferences::bittorrentPort()
{
return s.value("bittorrent/port").toInt();
}
void Preferences::setBittorrentPort(int port)
{
s.setValue("bittorrent/port", port);
}
void Preferences::ch1()
{
// check settings
if (!s.contains("user/name") ||
!s.contains("user/email") ||
!s.contains("user/order") ||
!s.contains("user/created") ||
!s.contains("user/description") ||
!s.contains("user/sig"))
{
cch = false;
return;
}
// read settings
QString name = s.value("user/name").toString().trimmed();
QString email = s.value("user/email").toString().trimmed();
QString order = s.value("user/order").toString().trimmed();
QString created = s.value("user/created").toString().trimmed();
QString description = s.value("user/description").toString().trimmed();
QString sig = s.value("user/sig").toString().trimmed();
// generate salt
QByteArray s3 = "";
for(int i=0; i < s1.length(); i++)
s3.append(s1[i] ^ s2[i]);
// hash
QByteArray h(s3);
h.append(name);
h.append(email);
h.append(order);
h.append(created);
h.append(description);
QByteArray cSig = QCryptographicHash::hash(h, QCryptographicHash::Sha512);
// compare return
cch = (cSig.toBase64().toBase64() == sig);
}
void Preferences::ch2()
{
// check settings
if (!s.contains("user/name") ||
!s.contains("user/email") ||
!s.contains("user/order") ||
!s.contains("user/created") ||
!s.contains("user/description") ||
!s.contains("user/sig"))
{
cch = false;
return;
}
// read settings
QString name = s.value("user/name").toString().trimmed();
QString email = s.value("user/email").toString().trimmed();
QString order = s.value("user/order").toString().trimmed();
QString created = s.value("user/created").toString().trimmed();
QString description = s.value("user/description").toString().trimmed();
QString sig = s.value("user/sig").toString().trimmed();
// generate salt
QByteArray s3 = "";
for(int i=0; i < s1.length(); i++)
s3.append(s1[i] ^ s2[i]);
// hash
QByteArray h(s3);
h.append(name);
h.append(email);
h.append(order);
h.append(created);
h.append(description);
QByteArray cSig = QCryptographicHash::hash(h, QCryptographicHash::Sha512);
// compare return
cch = (cSig.toBase64().toBase64() == sig);
}
void Preferences::ch3()
{
// check settings
if (!s.contains("user/name") ||
!s.contains("user/email") ||
!s.contains("user/order") ||
!s.contains("user/created") ||
!s.contains("user/description") ||
!s.contains("user/sig"))
{
cch = false;
return;
}
// read settings
QString name = s.value("user/name").toString().trimmed();
QString email = s.value("user/email").toString().trimmed();
QString order = s.value("user/order").toString().trimmed();
QString created = s.value("user/created").toString().trimmed();
QString description = s.value("user/description").toString().trimmed();
QString sig = s.value("user/sig").toString().trimmed();
// generate salt
QByteArray s3 = "";
for(int i=0; i < s1.length(); i++)
s3.append(s1[i] ^ s2[i]);
// hash
QByteArray h(s3);
h.append(name);
h.append(email);
h.append(order);
h.append(created);
h.append(description);
QByteArray cSig = QCryptographicHash::hash(h, QCryptographicHash::Sha512);
// compare return
cch = (cSig.toBase64().toBase64() == sig);
}
Preferences::~Preferences()
{
qDebug() << "running prefs destructor";
if (recentFiles) delete recentFiles;
// clear stream path
QDir(streamPath()).removeRecursively();
// clear save path
if (!saveTorrentVideos())
QDir(savePath()).removeRecursively();
}