diff --git a/src/App.cpp b/src/App.cpp index 56b1e0b..789dc3f 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -4,18 +4,18 @@ #include #include #include +#include #include "App.h" App::App(const CommandLine& cmd, QObject * parent) : QObject(parent) , _settings(std::make_shared()) - , _nm(std::make_unique( - _settings, + , _nm(_settings, cmd.isSet(CommandLine::hostname) ? cmd.value(CommandLine::hostname) : _settings->hostname(), - this)) + this) , _commandLine(cmd) , _engine(new QQmlApplicationEngine(this)) { @@ -54,11 +54,9 @@ void App::reload() ***************************************************************************/ void App::initalize() { - _nm->create(this); - // Global context variables to inject into QML const std::pair contextVars[] = { - { "networkManager", _nm.get()}, + { "networkManager", &_nm}, { "settings", _settings.get()}, { "app", this }, }; @@ -67,7 +65,7 @@ void App::initalize() context->setContextProperty(var.first, QVariant::fromValue(var.second)); } - _engine->setNetworkAccessManagerFactory(_nm.get()); + _engine->setNetworkAccessManagerFactory(&_nm); _engine->addImportPath(QStringLiteral("qrc:/")); _engine->load(makeUrl(QStringLiteral("main.qml"))); } diff --git a/src/App.h b/src/App.h index 5a7c423..4ddc150 100644 --- a/src/App.h +++ b/src/App.h @@ -18,7 +18,7 @@ class App : public QObject std::shared_ptr _settings; - std::unique_ptr _nm; + NetworkManager _nm; const CommandLine& _commandLine; diff --git a/src/models/DownloadsModel.cpp b/src/models/DownloadsModel.cpp index 2e65150..11b6c5f 100644 --- a/src/models/DownloadsModel.cpp +++ b/src/models/DownloadsModel.cpp @@ -44,20 +44,22 @@ void DownloadsModel::setupWebsocket() { [=](QAbstractSocket::SocketError error) { qDebug() << "error: " << error << _webSocket.errorString(); }); - auto resolved = NetworkManager::instance().resolvedPath().resolved(QString("api/v1/downloads")); + auto resolved = NetworkManager::instance().resolvedPath().resolved( + QString("api/v1/downloads")); bool ssl = !resolved.scheme().compare("https", Qt::CaseInsensitive); resolved.setScheme(ssl ? "wss" : "ws"); QNetworkRequest request; request.setUrl(resolved); - request.setRawHeader("Authorization", - QString("Basic %1") - .arg(QByteArray(QString("%1:%2") - .arg(NetworkManager::instance().username()) - .arg(NetworkManager::instance().password()) - .toUtf8()) - .toBase64()) - .toUtf8()); + request.setRawHeader( + "Authorization", + QString("Basic %1") + .arg(QByteArray(QString("%1:%2") + .arg(NetworkManager::instance().username()) + .arg(NetworkManager::instance().password()) + .toUtf8()) + .toBase64()) + .toUtf8()); _webSocket.open(request); } @@ -101,8 +103,8 @@ void DownloadsModel::onTextMessageReceived(const QString &message) { info.progress = entry["progress"].toDouble() * 100; info.tries = entry["tries"].toInt(); const auto &manga = entry["manga"]; - info.title = manga["title"].toString(); - info.thumbnailUrl = manga["thumbnailUrl"].toString(); + info.title = manga["title"].toString(); + info.thumbnailUrl = manga["thumbnailUrl"].toString(); info.chapterInfo.processChapter(entry["chapter"].toObject()); } @@ -189,9 +191,7 @@ QVariant DownloadsModel::data(const QModelIndex &index, int role) const { } case RoleThumbnail: { - qDebug() << "entry url: " << entry.thumbnailUrl; - return NetworkManager::instance().resolvedPath().resolved( - entry.thumbnailUrl); + return NetworkManager::instance().resolvedPath().resolved(entry.thumbnailUrl.mid(1)); } // case Role @@ -237,21 +237,27 @@ QHash DownloadsModel::roleNames() const { * Method: clear() * *****************************************************************************/ -void DownloadsModel::clear() { NetworkManager::instance().get("downloads/clear"); } +void DownloadsModel::clear() { + NetworkManager::instance().get("downloads/clear"); +} /****************************************************************************** * * Method: clear() * *****************************************************************************/ -void DownloadsModel::pause() { NetworkManager::instance().get("downloads/stop"); } +void DownloadsModel::pause() { + NetworkManager::instance().get("downloads/stop"); +} /****************************************************************************** * * Method: clear() * *****************************************************************************/ -void DownloadsModel::start() { NetworkManager::instance().get("downloads/start"); } +void DownloadsModel::start() { + NetworkManager::instance().get("downloads/start"); +} /****************************************************************************** * @@ -259,7 +265,8 @@ void DownloadsModel::start() { NetworkManager::instance().get("downloads/start") * *****************************************************************************/ void DownloadsModel::cancel(qint32 index) { - NetworkManager::instance().deleteResource(QStringLiteral("download/%1/chapter/%2") - .arg(_queue[index].mangaId) - .arg(_queue[index].chapterIndex)); + NetworkManager::instance().deleteResource( + QStringLiteral("download/%1/chapter/%2") + .arg(_queue[index].mangaId) + .arg(_queue[index].chapterIndex)); } diff --git a/src/models/DownloadsModel.h b/src/models/DownloadsModel.h index 2559e15..1ee6f5c 100644 --- a/src/models/DownloadsModel.h +++ b/src/models/DownloadsModel.h @@ -1,11 +1,10 @@ #pragma once +#include #include #include #include -#include "ChaptersModel.h" -#include "MangaDetailsModel.h" #include "common_structs.h" class DownloadsModel : public QAbstractListModel, public QQmlParserStatus { @@ -21,7 +20,6 @@ class DownloadsModel : public QAbstractListModel, public QQmlParserStatus { std::vector _queue; typedef quint32 MangaId; - //QMap _mangaInfo; protected: void classBegin() override; diff --git a/src/models/LibraryModel.cpp b/src/models/LibraryModel.cpp index 3b98940..79773ab 100644 --- a/src/models/LibraryModel.cpp +++ b/src/models/LibraryModel.cpp @@ -3,9 +3,9 @@ #include #include #include -#include #include #include +#include #include "../networkmanager.h" /****************************************************************************** @@ -13,30 +13,21 @@ * LibraryModel * *****************************************************************************/ -LibraryModel::LibraryModel(QObject* parent) - : QAbstractListModel(parent) -{ -} +LibraryModel::LibraryModel(QObject *parent) : QAbstractListModel(parent) {} /****************************************************************************** * * classBegin * *****************************************************************************/ -void LibraryModel::classBegin() -{ - refreshLibrary(); -} +void LibraryModel::classBegin() { refreshLibrary(); } /****************************************************************************** * * componentComplete * *****************************************************************************/ -void LibraryModel::componentComplete() -{ -} - +void LibraryModel::componentComplete() {} /****************************************************************************** * @@ -57,37 +48,31 @@ int LibraryModel::rowCount(const QModelIndex &parent) const { * *****************************************************************************/ QVariant LibraryModel::data(const QModelIndex &index, int role) const { - if (!((index.isValid()) && - (index.row() >= 0) && - (index.row() < rowCount()))) - { + if (!((index.isValid()) && (index.row() >= 0) && + (index.row() < rowCount()))) { return {}; } - const auto& entry = _entries[index.row()]; - - switch (role) - { - case RoleTitle: - { - return entry.title; - } - case RoleThumbnail: - { - return NetworkManager::instance().resolvedPath().resolved(entry.thumbnailUrl.mid(1)); - } - case RoleId: - { - return entry.id; - } - case RoleUnread: - { - return entry.unread; - } - - //case Role - default: - return {}; + const auto &entry = _entries[index.row()]; + + switch (role) { + case RoleTitle: { + return entry.title; + } + case RoleThumbnail: { + return NetworkManager::instance().resolvedPath().resolved( + entry.thumbnailUrl.mid(1)); + } + case RoleId: { + return entry.id; + } + case RoleUnread: { + return entry.unread; + } + + // case Role + default: + return {}; } return {}; @@ -99,10 +84,10 @@ QVariant LibraryModel::data(const QModelIndex &index, int role) const { * *****************************************************************************/ QHash LibraryModel::roleNames() const { - static QHash roles = { {RoleTitle, "title"}, - {RoleThumbnail, "thumbnailUrl"}, - {RoleUnread, "unread"}, - {RoleId, "mangaId"} }; + static QHash roles = {{RoleTitle, "title"}, + {RoleThumbnail, "thumbnailUrl"}, + {RoleUnread, "unread"}, + {RoleId, "mangaId"}}; return roles; } @@ -112,47 +97,44 @@ QHash LibraryModel::roleNames() const { * Method: refreshLibrary() * *****************************************************************************/ -void LibraryModel::refreshLibrary() -{ +void LibraryModel::refreshLibrary() { auto entriesSize = _entries.size(); _entries.clear(); - NetworkManager::instance().get(QUrl("category"), this, - [&](const auto& doc) - { - for (const auto& entry_arr : doc.array()) { - const auto& entry = entry_arr.toObject(); - NetworkManager::instance().get(QUrl(u"category/"_qs % QString::number(entry["id"].toInt())), this, - [&](const auto& doc1) - { - bool reset = static_cast(doc1.array().size()) != entriesSize; - if (reset) { - beginResetModel(); - } - - for (const auto& entry_arr : doc1.array()) { - const auto& entry = entry_arr.toObject(); - auto& info = _entries.emplace_back(); - info.id = entry["id"].toInt(); - info.sourceId = entry["sourceId"].toString(); - info.url = entry["url"].toString(); - info.title = entry["title"].toString(); - info.thumbnailUrl = entry["thumbnailUrl"].toString(); - info.initalized = entry["intialized"].toBool(); - info.author = entry["author"].toString(); - info.artist = entry["artist"].toString(); - info.genre = entry["genre"].toString(); - info.status = entry["status"].toString(); - info.unread = entry["unreadCount"].toInt(); - } - - if (reset) { - endResetModel(); - } - else { - emit dataChanged(createIndex(0, 0), createIndex(_entries.size(), 0)); - } - }); + NetworkManager::instance().get(QUrl("category"), this, [&](const auto &doc) { + for (const auto &entry_arr : doc.array()) { + const auto &entry = entry_arr.toObject(); + NetworkManager::instance().get( + QUrl(u"category/"_qs % QString::number(entry["id"].toInt())), this, + [&](const auto &doc1) { + bool reset = + static_cast(doc1.array().size()) != entriesSize; + if (reset) { + beginResetModel(); + } + + for (const auto &entry_arr : doc1.array()) { + const auto &entry = entry_arr.toObject(); + auto &info = _entries.emplace_back(); + info.id = entry["id"].toInt(); + info.sourceId = entry["sourceId"].toString(); + info.url = entry["url"].toString(); + info.title = entry["title"].toString(); + info.thumbnailUrl = entry["thumbnailUrl"].toString(); + info.initalized = entry["intialized"].toBool(); + info.author = entry["author"].toString(); + info.artist = entry["artist"].toString(); + info.genre = entry["genre"].toString(); + info.status = entry["status"].toString(); + info.unread = entry["unreadCount"].toInt(); + } + + if (reset) { + endResetModel(); + } else { + emit dataChanged(createIndex(0, 0), + createIndex(_entries.size(), 0)); + } + }); } }); } - diff --git a/src/models/UpdatesModel.cpp b/src/models/UpdatesModel.cpp index daf2a80..fcc7260 100644 --- a/src/models/UpdatesModel.cpp +++ b/src/models/UpdatesModel.cpp @@ -5,8 +5,8 @@ #include #include #include -#include #include +#include #include #include "../networkmanager.h" @@ -16,29 +16,23 @@ * UpdatesModel * *****************************************************************************/ -UpdatesModel::UpdatesModel(QObject* parent) - : QAbstractListModel(parent) -{ -} +UpdatesModel::UpdatesModel(QObject *parent) : QAbstractListModel(parent) {} /****************************************************************************** * * closed * *****************************************************************************/ -void UpdatesModel::closed() -{ -} +void UpdatesModel::closed() {} /****************************************************************************** * * onConnected * *****************************************************************************/ -void UpdatesModel::onConnected() -{ - connect(&_webSocket, &QWebSocket::textMessageReceived, - this, &UpdatesModel::onTextMessageReceived); +void UpdatesModel::onConnected() { + connect(&_webSocket, &QWebSocket::textMessageReceived, this, + &UpdatesModel::onTextMessageReceived); } /****************************************************************************** @@ -46,11 +40,11 @@ void UpdatesModel::onConnected() * onTextMessageReceived * *****************************************************************************/ -void UpdatesModel::onTextMessageReceived(const QString& message) -{ +void UpdatesModel::onTextMessageReceived(const QString &message) { QJsonDocument doc = QJsonDocument::fromJson(message.toUtf8()); - //{"statusMap":{"COMPLETE" : 26,"FAILED" : 69,"RUNNING" : 1,"PENDING" : 123}, "running":true} + //{"statusMap":{"COMPLETE" : 26,"FAILED" : 69,"RUNNING" : 1,"PENDING" : 123}, + //"running":true} _running = doc["running"].toBool(); auto statusMap = doc["statusMap"].toObject(); @@ -65,32 +59,41 @@ void UpdatesModel::onTextMessageReceived(const QString& message) emit totalChanged(); emit completeChanged(); emit runningChanged(); - } -void UpdatesModel::classBegin() { } +void UpdatesModel::classBegin() {} /****************************************************************************** * * componentComplete * *****************************************************************************/ -void UpdatesModel::componentComplete() -{ - connect(&_webSocket, &QWebSocket::connected, this, &UpdatesModel::onConnected); +void UpdatesModel::componentComplete() { + connect(&_webSocket, &QWebSocket::connected, this, + &UpdatesModel::onConnected); connect(&_webSocket, &QWebSocket::disconnected, this, &UpdatesModel::closed); - connect(&_webSocket, QOverload::of(&QWebSocket::error), - [=](QAbstractSocket::SocketError error){ - qDebug() << "error: " << error << _webSocket.errorString(); - }); - - auto resolved = NetworkManager::instance().resolvedPath().resolved(QString("api/v1/update")); + connect(&_webSocket, + QOverload::of(&QWebSocket::error), + [=](QAbstractSocket::SocketError error) { + qDebug() << "error: " << error << _webSocket.errorString(); + }); + + auto resolved = NetworkManager::instance().resolvedPath().resolved( + QString("api/v1/update")); bool ssl = !resolved.scheme().compare("https", Qt::CaseInsensitive); resolved.setScheme(ssl ? "wss" : "ws"); QNetworkRequest request; request.setUrl(resolved); - request.setRawHeader("Authorization", QString("Basic %1").arg(QByteArray(QString("%1:%2").arg(NetworkManager::instance().username()).arg(NetworkManager::instance().password()).toUtf8()).toBase64()).toUtf8()); + request.setRawHeader( + "Authorization", + QString("Basic %1") + .arg(QByteArray(QString("%1:%2") + .arg(NetworkManager::instance().username()) + .arg(NetworkManager::instance().password()) + .toUtf8()) + .toBase64()) + .toUtf8()); _webSocket.open(request); @@ -102,18 +105,18 @@ void UpdatesModel::componentComplete() * Method: onDownloadsUpdated() * *****************************************************************************/ -void UpdatesModel::onDownloadsUpdated(const std::vector& queueInfo) -{ - for (auto& info : queueInfo) { +void UpdatesModel::onDownloadsUpdated(const std::vector &queueInfo) { + for (auto &info : queueInfo) { int row = 0; - for (auto& source : _sources) { + for (auto &source : _sources) { if (info.mangaId == source.id && - info.chapterInfo.chapterNumber == source.chapterInfo.chapterNumber) - { + info.chapterInfo.chapterNumber == source.chapterInfo.chapterNumber) { source.queueInfo = std::make_shared(info); source.chapterInfo.downloaded = info.progress >= 100; source.chapterInfo.downloadPrepairing = false; - emit dataChanged(createIndex(row, 0), createIndex(row, 0), { RoleDownloadProgress, RoleDownloaded, RoleDownloadPrepairing}); + emit dataChanged( + createIndex(row, 0), createIndex(row, 0), + {RoleDownloadProgress, RoleDownloaded, RoleDownloadPrepairing}); break; } row++; @@ -140,101 +143,81 @@ int UpdatesModel::rowCount(const QModelIndex &parent) const { * *****************************************************************************/ QVariant UpdatesModel::data(const QModelIndex &index, int role) const { - if (!((index.isValid()) && - (index.row() >= 0) && - (index.row() < rowCount()))) - { + if (!((index.isValid()) && (index.row() >= 0) && + (index.row() < rowCount()))) { return {}; } - const auto& entry = _sources[index.row()]; + const auto &entry = _sources[index.row()]; - switch (role) - { - case RoleThumbnailUrl: - { - return NetworkManager::instance().resolvedPath().resolved(entry.thumbnailUrl.mid(1)); - } - case RoleTitle: - { - return entry.title; - } - case RoleUrl: - { - return entry.url; - } - case RoleId: - { - return entry.id; - } - case RoleInitialized: - { - return entry.isInitialized; - } - case RoleInLibrary: - { - return entry.inLibrary; - } - case RoleFreshData: - { - return entry.freshData; - } - case RoleChapterUrl: - { - return entry.url; - } - case RoleName: - { - return entry.chapterInfo.name; - } - case RoleChapterNumber: - { - return entry.chapterInfo.chapterNumber; - } - case RoleRead: - { - return entry.chapterInfo.read; - } - case RoleChapterIndex: - { - return entry.chapterInfo.index; - } - case RolePageCount: - { - return entry.chapterInfo.pageCount; - } - case RoleChapterCount: - { - return entry.chapterInfo.chapterCount; - } + switch (role) { + case RoleThumbnailUrl: { + return NetworkManager::instance().resolvedPath().resolved( + entry.thumbnailUrl.mid(1)); + } + case RoleTitle: { + return entry.title; + } + case RoleUrl: { + return entry.url; + } + case RoleId: { + return entry.id; + } + case RoleInitialized: { + return entry.isInitialized; + } + case RoleInLibrary: { + return entry.inLibrary; + } + case RoleFreshData: { + return entry.freshData; + } + case RoleChapterUrl: { + return entry.url; + } + case RoleName: { + return entry.chapterInfo.name; + } + case RoleChapterNumber: { + return entry.chapterInfo.chapterNumber; + } + case RoleRead: { + return entry.chapterInfo.read; + } + case RoleChapterIndex: { + return entry.chapterInfo.index; + } + case RolePageCount: { + return entry.chapterInfo.pageCount; + } + case RoleChapterCount: { + return entry.chapterInfo.chapterCount; + } - case RoleLastPageRead: - { - return entry.chapterInfo.lastPageRead; - } + case RoleLastPageRead: { + return entry.chapterInfo.lastPageRead; + } - case RoleDownloaded: - { - return entry.chapterInfo.downloaded; - } + case RoleDownloaded: { + return entry.chapterInfo.downloaded; + } - case RoleFetchedAt: - { - return entry.chapterInfo.fetchedAt; - } + case RoleFetchedAt: { + return entry.chapterInfo.fetchedAt; + } - case RoleDownloadProgress: - { - if (!entry.queueInfo) { - return -1; - } - return entry.queueInfo->progress; - } - case RoleDownloadPrepairing: - return entry.chapterInfo.downloadPrepairing.value_or(false); + case RoleDownloadProgress: { + if (!entry.queueInfo) { + return -1; + } + return entry.queueInfo->progress; + } + case RoleDownloadPrepairing: + return entry.chapterInfo.downloadPrepairing.value_or(false); - default: - return {}; + default: + return {}; } return {}; @@ -246,25 +229,26 @@ QVariant UpdatesModel::data(const QModelIndex &index, int role) const { * *****************************************************************************/ QHash UpdatesModel::roleNames() const { - static QHash roles = { {RoleThumbnailUrl, "thumbnailUrl"}, - {RoleTitle, "title"}, - {RoleId, "mangaId"}, - {RoleUrl, "url"}, - {RoleInitialized, "isInitialized"}, - {RoleFreshData, "freshData"}, - {RoleInLibrary, "inLibrary"}, - {RoleChapterUrl, "chapterUrl"}, - {RoleName, "name"}, - {RoleChapterNumber, "chapterNumber"}, - {RoleRead, "read"}, - {RoleChapterIndex, "chapterIndex"}, - {RolePageCount, "pageCount"}, - {RoleDownloaded, "downloaded"}, - {RoleLastPageRead, "lastPageRead"}, - {RoleChapterCount, "chapterCount"}, - {RoleFetchedAt, "fetchedAt"}, - {RoleDownloadProgress,"downloadProgress"}, - {RoleDownloadPrepairing,"downloadPrepairing"}, + static QHash roles = { + {RoleThumbnailUrl, "thumbnailUrl"}, + {RoleTitle, "title"}, + {RoleId, "mangaId"}, + {RoleUrl, "url"}, + {RoleInitialized, "isInitialized"}, + {RoleFreshData, "freshData"}, + {RoleInLibrary, "inLibrary"}, + {RoleChapterUrl, "chapterUrl"}, + {RoleName, "name"}, + {RoleChapterNumber, "chapterNumber"}, + {RoleRead, "read"}, + {RoleChapterIndex, "chapterIndex"}, + {RolePageCount, "pageCount"}, + {RoleDownloaded, "downloaded"}, + {RoleLastPageRead, "lastPageRead"}, + {RoleChapterCount, "chapterCount"}, + {RoleFetchedAt, "fetchedAt"}, + {RoleDownloadProgress, "downloadProgress"}, + {RoleDownloadPrepairing, "downloadPrepairing"}, }; return roles; @@ -275,8 +259,7 @@ QHash UpdatesModel::roleNames() const { * Method: pageRefresh() * *****************************************************************************/ -void UpdatesModel::pageRefresh() -{ +void UpdatesModel::pageRefresh() { _pageNumber = 0; _hasNext = false; _sources.clear(); @@ -289,49 +272,50 @@ void UpdatesModel::pageRefresh() * Method: next() * *****************************************************************************/ -void UpdatesModel::next() -{ +void UpdatesModel::next() { if (_pageNumber && !_hasNext) { return; } - NetworkManager::instance().get(QUrl(u"update/recentChapters/"_qs % QString::number(_pageNumber++)), this, - [&](const auto& reply) - { - if (!downloads) { - downloads = std::make_shared(); - downloads->setupWebsocket(); - connect(downloads.get(), &DownloadsModel::downloadsUpdated, this, &UpdatesModel::onDownloadsUpdated); - } + NetworkManager::instance().get( + QUrl(u"update/recentChapters/"_qs % QString::number(_pageNumber++)), this, + [&](const auto &reply) { + if (!downloads) { + downloads = std::make_shared(); + downloads->setupWebsocket(); + connect(downloads.get(), &DownloadsModel::downloadsUpdated, this, + &UpdatesModel::onDownloadsUpdated); + } - if (reply.isEmpty()) { - return; - } + if (reply.isEmpty()) { + return; + } - _hasNext = reply["hasNextPage"].toBool(); + _hasNext = reply["hasNextPage"].toBool(); - auto pageArray = reply["page"].toArray(); - beginInsertRows({}, _sources.size(), _sources.size() + pageArray.count() - 1); + auto pageArray = reply["page"].toArray(); + beginInsertRows({}, _sources.size(), + _sources.size() + pageArray.count() - 1); - _sources.reserve(reply.array().count()); + _sources.reserve(reply.array().count()); - for (const auto& entry_arr : pageArray) { - const auto& entry = entry_arr.toObject(); - const auto& manga = entry["manga"].toObject(); - auto& info = _sources.emplace_back(); - info.id = manga["id"].toInt(); - info.sourceId = manga["sourceId"].toString(); - info.title = manga["title"].toString(); - info.thumbnailUrl = manga["thumbnailUrl"].toString(); - info.url = manga["url"].toString(); - info.isInitialized = manga["isInitialized"].toBool(); - info.inLibrary = manga["inLibrary"].toBool(); + for (const auto &entry_arr : pageArray) { + const auto &entry = entry_arr.toObject(); + const auto &manga = entry["manga"].toObject(); + auto &info = _sources.emplace_back(); + info.id = manga["id"].toInt(); + info.sourceId = manga["sourceId"].toString(); + info.title = manga["title"].toString(); + info.thumbnailUrl = manga["thumbnailUrl"].toString(); + info.url = manga["url"].toString(); + info.isInitialized = manga["isInitialized"].toBool(); + info.inLibrary = manga["inLibrary"].toBool(); - info.chapterInfo.processChapter(entry["chapter"].toObject()); - } + info.chapterInfo.processChapter(entry["chapter"].toObject()); + } - endInsertRows(); - }); + endInsertRows(); + }); } /****************************************************************************** @@ -339,8 +323,7 @@ void UpdatesModel::next() * Method: refresh() * *****************************************************************************/ -void UpdatesModel::refresh() -{ +void UpdatesModel::refresh() { QUrlQuery query; query.addQueryItem("category", 0); NetworkManager::instance().post("update/fetch", query); @@ -351,9 +334,8 @@ void UpdatesModel::refresh() * Method: downloadChapter() * *****************************************************************************/ -void UpdatesModel::downloadChapter(int index) -{ - auto& entry = _sources[index]; +void UpdatesModel::downloadChapter(int index) { + auto &entry = _sources[index]; if (entry.chapterInfo.downloaded) { return; } @@ -362,8 +344,11 @@ void UpdatesModel::downloadChapter(int index) entry.chapterInfo.downloadPrepairing = true; qDebug() << "source index? " << index; - emit dataChanged(createIndex(index, 0), createIndex(index, 0), { RoleDownloadPrepairing }); - NetworkManager::instance().get(QStringLiteral("download/%1/chapter/%2").arg(entry.id).arg(entry.chapterInfo.index)); + emit dataChanged(createIndex(index, 0), createIndex(index, 0), + {RoleDownloadPrepairing}); + NetworkManager::instance().get(QStringLiteral("download/%1/chapter/%2") + .arg(entry.id) + .arg(entry.chapterInfo.index)); } /****************************************************************************** @@ -371,17 +356,15 @@ void UpdatesModel::downloadChapter(int index) * Method: chapterRead() * *****************************************************************************/ -void UpdatesModel::chapterRead(qint32 mangaId, quint32 chapter) -{ +void UpdatesModel::chapterRead(qint32 mangaId, quint32 chapter) { int i = 0; - for (auto& info : _sources) { - if (info.id == mangaId && - info.chapterInfo.index == chapter) - { + for (auto &info : _sources) { + if (info.id == mangaId && info.chapterInfo.index == chapter) { info.chapterInfo.read = true; - NetworkManager::instance().patch("read", "true", - QStringLiteral("manga/%1/chapter/%2").arg(mangaId).arg(chapter)); - emit dataChanged(createIndex(i, 0), createIndex(i, 0), { RoleRead }); + NetworkManager::instance().patch( + "read", "true", + QStringLiteral("manga/%1/chapter/%2").arg(mangaId).arg(chapter)); + emit dataChanged(createIndex(i, 0), createIndex(i, 0), {RoleRead}); break; } ++i; diff --git a/src/models/common_structs.h b/src/models/common_structs.h index 43e4583..1c42952 100644 --- a/src/models/common_structs.h +++ b/src/models/common_structs.h @@ -42,6 +42,6 @@ struct QueueInfo { quint32 progress; quint32 tries; QString title; - QUrl thumbnailUrl; + QString thumbnailUrl; ChapterInfo chapterInfo; }; diff --git a/src/networkmanager.cpp b/src/networkmanager.cpp index 1f25268..53b443a 100644 --- a/src/networkmanager.cpp +++ b/src/networkmanager.cpp @@ -26,6 +26,22 @@ NetworkManager::NetworkManager( , _host(host.endsWith('/') ? host : host + '/') , _settings(settings) { + _username = _settings->username(); + _password = _settings->password(); + + connect(_settings.get(), &Settings::hostnameChanged, [&]() { + _host = _settings->hostname(); + }); + + connect(_settings.get(), &Settings::usernameChanged, [&]() { + _username = _settings->username(); + }); + + connect(_settings.get(), &Settings::passwordChanged, [&]() { + _password = _settings->password(); + }); + + _man = create(parent); } /******************************************************************** @@ -44,28 +60,14 @@ NetworkManager& NetworkManager::instance() { ********************************************************************/ QNetworkAccessManager *NetworkManager::create(QObject *parent) { - man = new QNetworkAccessManager(parent); - _cache = new QNetworkDiskCache(parent); - _cache->setCacheDirectory( + auto man = new QNetworkAccessManager(parent); + auto cache = new QNetworkDiskCache(parent); + cache->setCacheDirectory( QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/network-cache"); - _cache->setMaximumCacheSize(419430400); - man->setCache(_cache); + cache->setMaximumCacheSize(419430400); - _username = _settings->username(); - _password = _settings->password(); - - connect(_settings.get(), &Settings::hostnameChanged, [&]() { - _host = _settings->hostname(); - }); - - connect(_settings.get(), &Settings::usernameChanged, [&]() { - _username = _settings->username(); - }); - - connect(_settings.get(), &Settings::passwordChanged, [&]() { - _password = _settings->password(); - }); + man->setCache(cache); connect( man, @@ -127,7 +129,7 @@ void NetworkManager::get(const QUrl& uri, QObject* context, const Callback& call QNetworkRequest req(_host.resolved(QString("api/v1/")).resolved(uri)); - auto* reply = man->get(req); + auto* reply = _man->get(req); connect(reply, &QNetworkReply::finished, context, [=,this]() @@ -180,7 +182,7 @@ void NetworkManager::post(const QString &endpoint, const QUrlQuery &query) // request.setRawHeader("Authorization", "Basic " + // QByteArray(QString("%1:%2").arg(_username).arg(_password)).toBase64()); - man->post(request, dataParam.append(query.toString().toStdString().c_str())); + _man->post(request, dataParam.append(query.toString().toStdString().c_str())); } /******************************************************************** @@ -217,7 +219,7 @@ void NetworkManager::get( request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache); - QNetworkReply *reply = man->get(request); + QNetworkReply *reply = _man->get(request); connect(reply, &QNetworkReply::finished, this, [&]() { QNetworkReply *r = qobject_cast(sender()); @@ -248,7 +250,7 @@ void NetworkManager::deleteResource(const QString &endpoint) request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache); - QNetworkReply *reply = man->deleteResource(request); + QNetworkReply *reply = _man->deleteResource(request); connect( reply, &QNetworkReply::finished, this, &NetworkManager::endpointReply); diff --git a/src/networkmanager.h b/src/networkmanager.h index 002952b..09dfa76 100644 --- a/src/networkmanager.h +++ b/src/networkmanager.h @@ -30,8 +30,7 @@ class NetworkManager : public QObject, public QQmlNetworkAccessManagerFactory { QString _username; QString _password; - QNetworkDiskCache *_cache; - QNetworkAccessManager *man; + QNetworkAccessManager *_man; std::shared_ptr _settings; @@ -74,7 +73,7 @@ private slots: request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache); - auto reply = man->sendCustomRequest(request, "PATCH", patchData); + auto reply = _man->sendCustomRequest(request, "PATCH", patchData); patchData->setParent(reply); connect(reply, &QNetworkReply::finished, this, &NetworkManager::patchReply); @@ -107,7 +106,7 @@ private slots: request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache); - QNetworkReply *reply = man->get(request); + QNetworkReply *reply = _man->get(request); connect(reply, &QNetworkReply::finished, this, f); diff --git a/src/qml/NavigationBase.qml b/src/qml/NavigationBase.qml index a145420..7ea104d 100644 --- a/src/qml/NavigationBase.qml +++ b/src/qml/NavigationBase.qml @@ -16,7 +16,7 @@ Item { } Repeater { - model: [ "Library.qml", "Updates.qml", "SourceExtensionsBase.qml", "Downloads.qml", "Settings.qml" ] + model: [ "Library.qml" , "Updates.qml", "SourceExtensionsBase.qml", "Downloads.qml", "Settings.qml" ] Loader { id: delegate //active: SwipeView.isCurrentItem || SwipeView.isNextItem || SwipeView.isPreviousItem