-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappcontroller.cpp
163 lines (148 loc) · 6.37 KB
/
appcontroller.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
#include "appcontroller.h"
#include "dataitems/libraryitem.h"
#include "dataitems/comicitem.h"
#include "dataitems/folderitem.h"
#include <chrono>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkReply>
#include <QRestAccessManager>
#include <QRestReply>
#include <QSettings>
using namespace std::chrono_literals;
AppController::AppController(QObject *parent)
: QObject(parent)
, m_networkAccessManager(new QNetworkAccessManager(this))
, m_restAccessManager(new QRestAccessManager(m_networkAccessManager, this))
, m_librariesModel(new QStandardItemModel(this))
, m_comicsModel(new QStandardItemModel(this))
, m_foldersModel(new QStandardItemModel(this))
{
m_librariesModel->setItemRoleNames(LibraryItem::roleNames());
m_comicsModel->setItemRoleNames(ComicItem::roleNames());
m_foldersModel->setItemRoleNames(FolderItem::roleNames());
connect(this, &AppController::currentComicIdChanged, this, [this](){
// "selected" is "current"
setProperty("selectedComicOpened", false);
});
}
void AppController::connectServer(QUrl serverBaseUrl)
{
setProperty("connectionState", Connecting);
serverBaseUrl.setPath("/v2/");
QNetworkRequestFactory api(serverBaseUrl);
QNetworkRequest req(api.createRequest("version"));
req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::ManualRedirectPolicy);
req.setTransferTimeout(5s);
m_restAccessManager->get(req, this, [=](QRestReply &reply){
qDebug() << reply.httpStatus() << reply << reply.isHttpStatusSuccess();
if (reply.isSuccess()) {
m_requestFactory.setBaseUrl(serverBaseUrl);
m_requestFactory.clearCommonHeaders();
QHttpHeaders commonHeaders;
commonHeaders.append("X-Request-Id", "114514");
m_requestFactory.setCommonHeaders(commonHeaders);
setProperty("connectionState", Connected);
setProperty("lastErrorMessage", "");
} else {
setProperty("connectionState", NotConnected);
setProperty("lastErrorMessage", QStringLiteral("%1: %2").arg(reply.httpStatus()).arg(reply.errorString()));
}
});
}
void AppController::updateLibraries()
{
m_restAccessManager->get(apiFactory().createRequest("libraries"), this, [=](QRestReply &reply){
if (reply.isSuccess()) {
std::optional<QJsonDocument> libraries = reply.readJson();
if (libraries && !(*libraries).isEmpty() && (*libraries).isArray()) {
const QJsonArray array = (*libraries).array();
m_librariesModel->clear();
for (const QJsonValue & value : array) {
QJsonObject libraryObj = value.toObject();
m_librariesModel->appendRow(new LibraryItem(libraryObj["id"].toInt(),
libraryObj["name"].toString(),
libraryObj["uuid"].toString()));
}
}
}
});
}
void AppController::updateComicsInFolder(QString folderId)
{
qDebug() << folderId;
m_restAccessManager->get(apiFactory().createRequest(QString("library/%1/folder/%2/content")
.arg(m_currentLibraryId).arg(folderId)),
this, [=](QRestReply &reply){
qDebug() << m_currentLibraryId << folderId << reply.httpStatus() << reply.errorString();
if (reply.isSuccess()) {
std::optional<QJsonDocument> libraries = reply.readJson();
if (libraries && !(*libraries).isEmpty() && (*libraries).isArray()) {
const QJsonArray array = (*libraries).array();
m_comicsModel->clear();
m_foldersModel->clear();
// always add a top-leven folder entry...for now
QJsonObject root;
root["id"] = "1";
m_foldersModel->appendRow(new FolderItem(root, "Root"));
for (const QJsonValue & value : array) {
QJsonObject obj = value.toObject();
// qDebug() << obj;
if (obj["type"].toString() == QLatin1String("folder")) {
m_foldersModel->appendRow(new FolderItem(obj, obj["folder_name"].toString()));
} else {
m_comicsModel->appendRow(new ComicItem(obj, obj["file_name"].toString()));
}
}
}
}
});
}
void AppController::openComic()
{
m_restAccessManager->get(apiFactory().createRequest(QString("library/%1/comic/%2/remote")
.arg(m_currentLibraryId).arg(m_currentComicId)),
this, [=](QRestReply &reply){
qDebug() << m_currentLibraryId << m_currentComicId << reply.httpStatus() << reply.errorString();
if (reply.isSuccess()) {
setProperty("selectedComicOpened", true);
} else {
setProperty("selectedComicOpened", false);
}
});
}
void AppController::closeComic()
{
// the api seems not proceed by the server, so we simply do nothing but set the property
setProperty("selectedComicOpened", false);
}
QString AppController::coverImageSource(QString comicHash)
{
QUrl url(apiFactory().baseUrl());
url.setPath(url.path() + "library/" + QString::number(m_currentLibraryId) + "/cover/" + comicHash + ".jpg");
return url.toString();
}
QModelIndex AppController::currentComicModelIndex()
{
if (m_currentComicId.isEmpty()) return QModelIndex();
for (int i = 0; i < m_comicsModel->rowCount(); i++) {
QModelIndex index = m_comicsModel->index(i, 0);
if (m_comicsModel->data(index, ComicItem::IdRole) == m_currentComicId) {
return index;
}
}
return QModelIndex();
}
QString AppController::comicImageSource(int page)
{
QNetworkRequest req = apiFactory().createRequest(QString("library/%1/comic/%2/page/%3/remote")
.arg(m_currentLibraryId)
.arg(m_currentComicId)
.arg(page));
return req.url().toString();
}
QNetworkRequestFactory AppController::apiFactory() const
{
return m_requestFactory;
}