This repository has been archived by the owner on Feb 16, 2021. It is now read-only.
forked from pinkierton/harkach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadsView.cpp
86 lines (70 loc) · 2.27 KB
/
ThreadsView.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
#include "Attachment.h"
#include "ThreadsView.h"
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QStringBuilder>
ThreadsView::ThreadsView(QQuickItem *parent) : QQuickItem(parent), mDownloading(false)
{
}
ThreadModel *ThreadsView::getThreadModel() const noexcept {
return const_cast<ThreadModel* const>(&mThreadModel);
}
QString ThreadsView::board() const
{
return mBoard;
}
void ThreadsView::setBoard(const QString &board)
{
if (mBoard != board) {
mBoard = board;
requestThreads(board);
emit boardChanged();
}
}
bool ThreadsView::downloading() const
{
return mDownloading;
}
void ThreadsView::setDownloading(bool downloading)
{
if (mDownloading != downloading) {
mDownloading = downloading;
emit downloadingChanged();
}
}
void ThreadsView::requestThreads(const QString &board) {
setDownloading(true);
mThreadModel.resetThreads();
const QUrl getThreadsUrl(QStringLiteral("https://2ch.hk/") % board % QStringLiteral("/catalog.json"));
//const QUrl getThreadsUrl(QStringLiteral("https://2ch.hk/") % board % QStringLiteral("/threads.json"));
QNetworkRequest request(getThreadsUrl);
QNetworkReply *reply = manager.get(request);
connect(reply, &QNetworkReply::finished, this, &ThreadsView::processThreads);
}
void ThreadsView::processThreads() {
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
auto error = reply->error();
if (error) {
qWarning() << Q_FUNC_INFO << error;
} else {
QByteArray data = reply->readAll();
QJsonParseError parseError;
QJsonDocument doc = QJsonDocument::fromJson(data, &parseError);
if (parseError.error) {
qWarning() << parseError.error << parseError.errorString();
} else {
QList<ThreadObject*> threads;
threads.reserve(256);
QJsonArray threadArr = doc.object()[QLatin1String("threads")].toArray();
for (const QJsonValue &threadRef : qAsConst(threadArr)) {
threads << new ThreadObject(threadRef.toObject());
}
mThreadModel.setThreads(std::move(threads));
}
}
reply->deleteLater();
setDownloading(false);
}