-
Notifications
You must be signed in to change notification settings - Fork 13
/
store.cpp
428 lines (366 loc) · 12.8 KB
/
store.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#include "store.h"
bool initialInfo = true;
Worker *infoThread = nullptr;
Store::Store() : rootView(rootObject()), context(rootContext())
{
worker = new Worker({}, true);
// Start server
worker->checkServer();
context->setContextProperty("storeProg", QVariant(0.2));
connect(worker, &Worker::updateStatus, this, [this](QString stat) {
qDebug() << "LOG: " << stat;
if (stat.startsWith("TOTAL:"))
{
// Hacky trick to force redrawing Repeater
QStringList pagesList;
setProperty("pages", QVariant::fromValue(pagesList));
int totalItems = stat.mid(6).trimmed().toInt();
if (totalItems < 50)
return;
for (int i = 50; i < totalItems; i += 50)
pagesList.push_back(QString::number(i));
pagesList.push_back(QString::number(totalItems));
setProperty("pages", QVariant::fromValue(pagesList));
}
});
connect(worker, &Worker::updateProgress, this, [this](int prog) {
context->setContextProperty("storeProg", QVariant(0.2 + prog / 100.0 * 0.7));
});
connect(worker, &Worker::socketClosed, this, [this]() {
context->setContextProperty("titleVisible", QVariant(false));
setProperty("isBusy", false);
if (initialInfo && infoThread != nullptr) {
infoThread->work();
initialInfo = false;
}
});
connect(worker, &Worker::readAll, this, [this](QByteArray bytes) {
if (booksParent != nullptr) {
delete booksParent;
booksParent = nullptr;
}
_books.clear();
emit booksChanged();
context->setContextProperty("storeProg", QVariant(0.95));
QJsonParseError jsonError;
QJsonDocument document = QJsonDocument::fromJson(bytes, &jsonError);
if (jsonError.error != QJsonParseError::NoError)
{
qDebug() << "fromJson failed: " << jsonError.errorString();
context->setContextProperty("storeError", bytes);
return;
}
if (!document.isArray())
{
context->setContextProperty("storeError", QVariant("ERR: Server response malformed"));
return;
}
// QList<QObject *> booksList;
QJsonArray list = document.array();
if (list.size() == 0)
{
context->setContextProperty("storeError", QVariant("No result found"));
return;
}
booksParent = new QObject();
for (auto book : list)
{
if (!book.isObject())
continue;
QJsonObject bookObj = book.toObject();
Book *item = new Book(booksParent);
item->_name = bookObj.value("name").toString();
item->_author = bookObj.value("author").toString();
item->_imgFile = bookObj.value("img").toString();
if (item->_imgFile.size() > 0) {
item->_imgFile.prepend("image://gray/");
}
item->_url = bookObj.value("url").toString();
_books.push_back(item);
}
emit booksChanged();
context->setContextProperty("storeProg", QVariant(1));
context->setContextProperty("storeError", QVariant(""));
});
}
Store::~Store()
{
if (worker != nullptr)
delete worker;
if (infoThread != nullptr)
delete infoThread;
if (serverProc != nullptr)
delete serverProc;
}
void Store::open()
{
if (!loadConfig())
qDebug() << "config.json malformed";
newQuery(0);
if (_cookieAvailable)
{
infoThread = new Worker({"INFO"}, true);
connect(infoThread, &Worker::readAll, this, [this](QByteArray bytes) {
QJsonParseError jsonError;
QJsonDocument document = QJsonDocument::fromJson(bytes, &jsonError);
if (jsonError.error != QJsonParseError::NoError)
{
qDebug() << "fromJson failed: " << jsonError.errorString();
qDebug() << "ERR: " << bytes;
return;
}
if (!document.isObject())
return;
QJsonObject jsonObj = document.object();
QString downloads = jsonObj.value("today_download").toString("");
if (downloads.length() > 0)
{
auto counts = downloads.split("/");
downloads.prepend("↓ ");
if (counts[0] == counts[1])
{
downloads.prepend("⚠️ ");
}
this->setProperty("accountStatus", downloads);
}
QJsonValue historyList = jsonObj.value("today_list");
if (!historyList.isArray())
return;
QJsonArray downloadedBooks = historyList.toArray();
for (auto book : downloadedBooks)
{
auto bookObj = book.toObject();
QString url = bookObj.value("url").toString();
bool found = false;
for (auto seen : _downloadList)
{
if (url == seen->property("url"))
{
found = true;
break;
}
}
if (found)
continue;
Book *item = new Book(nullptr);
item->_url = url;
item->_name = bookObj.value("name").toString();
_downloadList.push_back(item);
}
emit downloadListChanged();
});
}
else
{
setProperty("accountStatus", "⚠️ Cookie is not configured");
}
}
void Store::newQuery(int page = 0)
{
_currentPage = page;
QStringList args = {
"LIST",
_exactMatch,
_fromYear,
_toYear,
_language,
_extension,
_order,
_query,
QString::number(page + 1)
};
stopQuery();
setProperty("isBusy", true);
worker->args = args;
context->setContextProperty("storeProg", QVariant(0.2));
worker->work();
}
void Store::openSavedList(int page = 0)
{
_currentPage = page;
QStringList args = {
"SAVE",
QString::number(page + 1)
};
stopQuery();
setProperty("isBusy", true);
worker->args = args;
context->setContextProperty("storeProg", QVariant(0.2));
worker->work();
}
void Store::stopQuery()
{
if (worker->isRunning())
worker->terminate();
setProperty("isBusy", false);
}
bool Store::loadConfig()
{
QFile file(QCoreApplication::applicationDirPath() + "/config.json");
if (!file.open(QIODevice::ReadOnly))
return false;
QByteArray bytes = file.readAll();
file.close();
QJsonParseError jsonError;
QJsonDocument document = QJsonDocument::fromJson(bytes, &jsonError);
if (jsonError.error != QJsonParseError::NoError)
{
qDebug() << "fromJson failed: " << jsonError.errorString();
return false;
}
if (!document.isObject())
return false;
QJsonObject jsonObj = document.object();
_cookieAvailable = jsonObj.value("cookie").toString("").length() > 0;
QJsonValue defaultQueryValue = jsonObj.value("defaultQuery");
if (!defaultQueryValue.isObject())
return false;
QJsonObject defaultQueryObj = defaultQueryValue.toObject();
_exactMatch = defaultQueryObj.value("exactMatch").toString("");
_fromYear = defaultQueryObj.value("fromYear").toString("");
_toYear = defaultQueryObj.value("toYear").toString("");
_language = defaultQueryObj.value("language").toString("");
_extension = defaultQueryObj.value("extension").toString("");
_order = defaultQueryObj.value("order").toString("");
_query = defaultQueryObj.value("query").toString("");
return true;
}
bool Store::setConfig()
{
QFile file(QCoreApplication::applicationDirPath() + "/config.json");
if (!file.open(QIODevice::ReadOnly))
{
qDebug() << "Can't open config.json in read-only";
return false;
}
QByteArray bytes = file.readAll();
file.close();
QJsonParseError jsonError;
QJsonDocument document = QJsonDocument::fromJson(bytes, &jsonError);
if (jsonError.error != QJsonParseError::NoError)
{
qDebug() << "fromJson failed: " << jsonError.errorString();
return false;
}
if (!document.isObject())
{
qDebug() << "config.json malformed";
return false;
}
QJsonObject jsonObj = document.object();
QJsonObject defaultQuery;
defaultQuery.insert("exactMatch", _exactMatch);
defaultQuery.insert("fromYear", _fromYear);
defaultQuery.insert("toYear", _toYear);
defaultQuery.insert("language", _language);
defaultQuery.insert("extension", _extension);
defaultQuery.insert("order", _order);
defaultQuery.insert("query", _query);
jsonObj.remove("defaultQuery");
jsonObj.insert("defaultQuery", defaultQuery);
file.remove();
if (!file.open(QIODevice::WriteOnly))
{
qDebug() << "Can't open config.json in write-only";
return false;
}
QByteArray writeBytes = QJsonDocument(jsonObj).toJson(QJsonDocument::Indented);
QTextStream iStream(&file);
iStream.setCodec("utf-8");
iStream << writeBytes;
file.close();
return true;
}
void Store::download(Book* book)
{
if (book->worker == nullptr) {
book->worker = new Worker({"DOWN", book->_dlUrl});
connect(book->worker, &Worker::updateProgress, book, &Book::updateProgress);
connect(book->worker, &Worker::updateStatus, book, [book](QString stat) {
qDebug() << "LOG: " << stat;
if (stat.startsWith("ERR:"))
{
book->setProperty("status", QVariant(stat.trimmed()));
book->worker->terminate();
delete book->worker;
}
});
connect(book->worker, &Worker::socketClosed, book, [book]() {
if (infoThread != nullptr && !infoThread->isRunning())
{
infoThread->work();
}
});
}
book->setParent(nullptr);
_downloadList.prepend(book);
emit downloadListChanged();
book->setProperty("status", QVariant("Downloading"));
book->worker->work();
}
Book::~Book() {
if (worker != nullptr) {
delete worker;
}
}
void Book::getDetail(QObject* popup)
{
if (_metadownloaded)
{
return;
}
Worker *metaWorker = new Worker({"META", _url}, true);
connect(metaWorker, &Worker::readAll, this, [this, metaWorker, popup](QByteArray bytes) {
QJsonParseError jsonError;
QJsonDocument document = QJsonDocument::fromJson(bytes, &jsonError);
if (jsonError.error != QJsonParseError::NoError)
{
qDebug() << "fromJson failed: " << jsonError.errorString();
setProperty("desc", bytes);
return;
}
if (!document.isObject())
{
return;
}
QJsonObject detail = document.object();
setProperty("name", detail.value("name").toString());
setProperty("author", detail.value("author").toString());
setProperty("dlUrl", detail.value("dlUrl").toString());
setProperty("desc", detail.value("description").toString());
auto imgFile = detail.value("img").toString();
if (imgFile.size() > 0)
{
imgFile.prepend("image://gray/");
}
setProperty("imgFile", imgFile);
QJsonArray similarsArray = detail.value("similars").toArray();
QList<QObject *> recList;
for (auto recom : similarsArray)
{
QJsonObject bookObj = recom.toObject();
Book *item = new Book(this);
item->setProperty("imgFile", bookObj.value("img").toString());
item->setProperty("url", bookObj.value("url").toString());
recList.push_back(item);
}
setProperty("similars", QVariant::fromValue(recList));
setProperty("status", "Download");
_metadownloaded = true;
popup->setProperty("isBusy", false);
qDebug() << "Meta downloaded";
delete metaWorker;
});
qDebug() << "Meta downloading";
popup->setProperty("isBusy", true);
metaWorker->work();
}
void Book::updateProgress(int prog)
{
if (prog == 100)
{
setProperty("status", QVariant("Downloaded"));
return;
}
setProperty("status", QVariant(QString::number(prog) + "%"));
}