aboutsummaryrefslogtreecommitdiff
path: root/launcher/ui
diff options
context:
space:
mode:
authorflow <thiagodonato300@gmail.com>2022-03-07 16:22:57 -0300
committerflow <thiagodonato300@gmail.com>2022-03-07 16:22:57 -0300
commitf714adf6d2cc94f20ba37f2776d0d61e22267f0e (patch)
treea72a3f07b0edc45e8c07f97daa8b2786d7cf3ad3 /launcher/ui
parent39bd04f06ff42623f7349096d707c4a877fc7cd7 (diff)
downloadPrismLauncher-f714adf6d2cc94f20ba37f2776d0d61e22267f0e.tar.gz
PrismLauncher-f714adf6d2cc94f20ba37f2776d0d61e22267f0e.tar.bz2
PrismLauncher-f714adf6d2cc94f20ba37f2776d0d61e22267f0e.zip
refactor: move NetJob away from ModModel to ModAPI
This is done so that 1. ModAPI behaves more like an actual API instead of just a helper, and 2. Allows for more easily creating other mod providers that may or may not use network tasks (foreshadowing lol)
Diffstat (limited to 'launcher/ui')
-rw-r--r--launcher/ui/pages/modplatform/ModModel.cpp47
-rw-r--r--launcher/ui/pages/modplatform/ModModel.h13
-rw-r--r--launcher/ui/pages/modplatform/ModPage.h2
-rw-r--r--launcher/ui/pages/modplatform/flame/FlameModModel.cpp10
-rw-r--r--launcher/ui/pages/modplatform/flame/FlameModModel.h2
-rw-r--r--launcher/ui/pages/modplatform/flame/FlameModPage.cpp12
-rw-r--r--launcher/ui/pages/modplatform/flame/FlameModPage.h2
-rw-r--r--launcher/ui/pages/modplatform/modrinth/ModrinthModel.cpp13
-rw-r--r--launcher/ui/pages/modplatform/modrinth/ModrinthModel.h4
-rw-r--r--launcher/ui/pages/modplatform/modrinth/ModrinthPage.cpp15
-rw-r--r--launcher/ui/pages/modplatform/modrinth/ModrinthPage.h2
11 files changed, 41 insertions, 81 deletions
diff --git a/launcher/ui/pages/modplatform/ModModel.cpp b/launcher/ui/pages/modplatform/ModModel.cpp
index 5be578c1..705f384e 100644
--- a/launcher/ui/pages/modplatform/ModModel.cpp
+++ b/launcher/ui/pages/modplatform/ModModel.cpp
@@ -91,30 +91,16 @@ void ListModel::fetchMore(const QModelIndex& parent)
void ListModel::getLogo(const QString& logo, const QString& logoUrl, LogoCallback callback)
{
if (m_logoMap.contains(logo)) {
- callback(APPLICATION->metacache()->resolveEntry(m_parent->metaEntryBase(), QString("logos/%1").arg(logo.section(".", 0, 0)))->getFullPath());
+ callback(APPLICATION->metacache()
+ ->resolveEntry(m_parent->metaEntryBase(), QString("logos/%1").arg(logo.section(".", 0, 0)))
+ ->getFullPath());
} else {
requestLogo(logo, logoUrl);
}
}
-void ListModel::requestModVersions(ModPlatform::IndexedPack const& current)
-{
- auto netJob = new NetJob(QString("%1::ModVersions(%2)").arg(m_parent->debugName()).arg(current.name), APPLICATION->network());
- auto response = new QByteArray();
- QString addonId = current.addonId.toString();
-
- netJob->addNetAction(Net::Download::makeByteArray(m_parent->apiProvider()->getVersionsURL(addonId), response));
-
- QObject::connect(netJob, &NetJob::succeeded, this, [this, response, addonId]{
- m_parent->onRequestVersionsSucceeded(m_parent, response, addonId);
- });
-
- QObject::connect(netJob, &NetJob::finished, this, [response, netJob] {
- netJob->deleteLater();
- delete response;
- });
-
- netJob->start();
+void ListModel::requestModVersions(ModPlatform::IndexedPack const& current) {
+ m_parent->apiProvider()->getVersions(this, current.addonId.toString(), m_parent->debugName());
}
void ListModel::performPaginatedSearch()
@@ -124,16 +110,9 @@ void ListModel::performPaginatedSearch()
->getPackProfile()
->getComponentVersion("net.fabricmc.fabric-loader")
.isEmpty();
- auto netJob = new NetJob(QString("%1::Search").arg(m_parent->debugName()), APPLICATION->network());
- auto searchUrl = m_parent->apiProvider()->getModSearchURL(
- nextSearchOffset, currentSearchTerm, getSorts()[currentSort], hasFabric ? ModAPI::Fabric : ModAPI::Forge, mcVersion);
-
- netJob->addNetAction(Net::Download::makeByteArray(QUrl(searchUrl), &response));
- jobPtr = netJob;
- jobPtr->start();
- QObject::connect(netJob, &NetJob::succeeded, this, &ListModel::searchRequestFinished);
- QObject::connect(netJob, &NetJob::failed, this, &ListModel::searchRequestFailed);
+ m_parent->apiProvider()->searchMods(
+ this, { nextSearchOffset, currentSearchTerm, getSorts()[currentSort], hasFabric ? ModAPI::Fabric : ModAPI::Forge, mcVersion });
}
void ListModel::searchWithTerm(const QString& term, const int sort)
@@ -160,9 +139,7 @@ void ListModel::searchRequestFailed(QString reason)
if (jobPtr->first()->m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 409) {
// 409 Gone, notify user to update
QMessageBox::critical(nullptr, tr("Error"),
- QString("%1 %2")
- .arg(m_parent->displayName())
- .arg(tr("API version too old!\nPlease update PolyMC!")));
+ QString("%1 %2").arg(m_parent->displayName()).arg(tr("API version too old!\nPlease update PolyMC!")));
// self-destruct
((ModDownloadDialog*)((ModPage*)parent())->parentWidget())->reject();
}
@@ -180,11 +157,17 @@ void ListModel::searchRequestFailed(QString reason)
}
}
+void ListModel::versionRequestSucceeded(QJsonDocument doc, QString addonId)
+{
+ m_parent->onRequestVersionsSucceeded(doc, addonId);
+}
+
void ListModel::requestLogo(QString logo, QString url)
{
if (m_loadingLogos.contains(logo) || m_failedLogos.contains(logo)) { return; }
- MetaEntryPtr entry = APPLICATION->metacache()->resolveEntry(m_parent->metaEntryBase(), QString("logos/%1").arg(logo.section(".", 0, 0)));
+ MetaEntryPtr entry =
+ APPLICATION->metacache()->resolveEntry(m_parent->metaEntryBase(), QString("logos/%1").arg(logo.section(".", 0, 0)));
auto job = new NetJob(QString("%1 Icon Download %2").arg(m_parent->debugName()).arg(logo), APPLICATION->network());
job->addNetAction(Net::Download::makeCached(QUrl(url), entry));
diff --git a/launcher/ui/pages/modplatform/ModModel.h b/launcher/ui/pages/modplatform/ModModel.h
index 64c17b4a..28bf05bb 100644
--- a/launcher/ui/pages/modplatform/ModModel.h
+++ b/launcher/ui/pages/modplatform/ModModel.h
@@ -1,9 +1,10 @@
#pragma once
+#include <qjsondocument.h>
#include <QAbstractListModel>
-#include "modplatform/ModIndex.h"
#include "modplatform/ModAPI.h"
+#include "modplatform/ModIndex.h"
#include "net/NetJob.h"
class ModPage;
@@ -26,6 +27,8 @@ class ListModel : public QAbstractListModel {
QVariant data(const QModelIndex& index, int role) const override;
Qt::ItemFlags flags(const QModelIndex& index) const override;
+ void setActiveJob(NetJob::Ptr ptr) { jobPtr = ptr; }
+
bool canFetchMore(const QModelIndex& parent) const override;
void fetchMore(const QModelIndex& parent) override;
@@ -34,10 +37,14 @@ class ListModel : public QAbstractListModel {
virtual void requestModVersions(const ModPlatform::IndexedPack& current);
- protected slots:
- virtual void searchRequestFinished() = 0;
+ public slots:
+ virtual void searchRequestFinished(QJsonDocument& doc) = 0;
void searchRequestFailed(QString reason);
+ void versionRequestSucceeded(QJsonDocument doc, QString addonId);
+
+ protected slots:
+
void logoFailed(QString logo);
void logoLoaded(QString logo, QIcon out);
diff --git a/launcher/ui/pages/modplatform/ModPage.h b/launcher/ui/pages/modplatform/ModPage.h
index e0d7c95c..aa0e8894 100644
--- a/launcher/ui/pages/modplatform/ModPage.h
+++ b/launcher/ui/pages/modplatform/ModPage.h
@@ -37,7 +37,7 @@ class ModPage : public QWidget, public BasePage {
virtual bool shouldDisplay() const override = 0;
const ModAPI* apiProvider() const { return api.get(); };
- virtual void onRequestVersionsSucceeded(ModPage*, QByteArray*, QString) = 0;
+ virtual void onRequestVersionsSucceeded(QJsonDocument&, QString) = 0;
void openedImpl() override;
bool eventFilter(QObject* watched, QEvent* event) override;
diff --git a/launcher/ui/pages/modplatform/flame/FlameModModel.cpp b/launcher/ui/pages/modplatform/flame/FlameModModel.cpp
index 283f9ce7..cff29a79 100644
--- a/launcher/ui/pages/modplatform/flame/FlameModModel.cpp
+++ b/launcher/ui/pages/modplatform/flame/FlameModModel.cpp
@@ -11,18 +11,10 @@ ListModel::ListModel(FlameModPage* parent) : ModPlatform::ListModel(parent) {}
ListModel::~ListModel() {}
-void FlameMod::ListModel::searchRequestFinished()
+void FlameMod::ListModel::searchRequestFinished(QJsonDocument& doc)
{
jobPtr.reset();
- QJsonParseError parse_error;
- QJsonDocument doc = QJsonDocument::fromJson(response, &parse_error);
- if(parse_error.error != QJsonParseError::NoError) {
- qWarning() << "Error while parsing JSON response from Flame at " << parse_error.offset << " reason: " << parse_error.errorString();
- qWarning() << response;
- return;
- }
-
QList<ModPlatform::IndexedPack> newList;
auto packs = doc.array();
for(auto packRaw : packs) {
diff --git a/launcher/ui/pages/modplatform/flame/FlameModModel.h b/launcher/ui/pages/modplatform/flame/FlameModModel.h
index ae919e63..022ec32e 100644
--- a/launcher/ui/pages/modplatform/flame/FlameModModel.h
+++ b/launcher/ui/pages/modplatform/flame/FlameModModel.h
@@ -30,7 +30,7 @@ class ListModel : public ModPlatform::ListModel {
virtual ~ListModel();
private slots:
- void searchRequestFinished() override;
+ void searchRequestFinished(QJsonDocument& doc) override;
private:
const char** getSorts() const override;
diff --git a/launcher/ui/pages/modplatform/flame/FlameModPage.cpp b/launcher/ui/pages/modplatform/flame/FlameModPage.cpp
index 6564265c..19f58280 100644
--- a/launcher/ui/pages/modplatform/flame/FlameModPage.cpp
+++ b/launcher/ui/pages/modplatform/flame/FlameModPage.cpp
@@ -36,23 +36,17 @@ FlameModPage::FlameModPage(ModDownloadDialog* dialog, BaseInstance* instance)
bool FlameModPage::shouldDisplay() const { return true; }
-void FlameModPage::onRequestVersionsSucceeded(ModPage* instance, QByteArray* response, QString addonId)
+void FlameModPage::onRequestVersionsSucceeded(QJsonDocument& doc, QString addonId)
{
if (addonId != current.addonId) {
return; // wrong request
}
- QJsonParseError parse_error;
- QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
- if (parse_error.error != QJsonParseError::NoError) {
- qWarning() << "Error while parsing JSON response from Flame at " << parse_error.offset << " reason: " << parse_error.errorString();
- qWarning() << *response;
- return;
- }
+
QJsonArray arr = doc.array();
try {
FlameMod::loadIndexedPackVersions(current, arr, APPLICATION->network(), m_instance);
} catch (const JSONValidationError& e) {
- qDebug() << *response;
+ qDebug() << doc;
qWarning() << "Error while reading Flame mod version: " << e.cause();
}
auto packProfile = ((MinecraftInstance*)m_instance)->getPackProfile();
diff --git a/launcher/ui/pages/modplatform/flame/FlameModPage.h b/launcher/ui/pages/modplatform/flame/FlameModPage.h
index 9c6f0e6c..f15b51ec 100644
--- a/launcher/ui/pages/modplatform/flame/FlameModPage.h
+++ b/launcher/ui/pages/modplatform/flame/FlameModPage.h
@@ -22,5 +22,5 @@ class FlameModPage : public ModPage {
bool shouldDisplay() const override;
private:
- void onRequestVersionsSucceeded(ModPage*, QByteArray*, QString) override;
+ void onRequestVersionsSucceeded(QJsonDocument&, QString) override;
};
diff --git a/launcher/ui/pages/modplatform/modrinth/ModrinthModel.cpp b/launcher/ui/pages/modplatform/modrinth/ModrinthModel.cpp
index dc3d1469..784b1128 100644
--- a/launcher/ui/pages/modplatform/modrinth/ModrinthModel.cpp
+++ b/launcher/ui/pages/modplatform/modrinth/ModrinthModel.cpp
@@ -10,19 +10,10 @@ ListModel::ListModel(ModrinthPage* parent) : ModPlatform::ListModel(parent) {}
ListModel::~ListModel() {}
-void Modrinth::ListModel::searchRequestFinished()
+void Modrinth::ListModel::searchRequestFinished(QJsonDocument& doc)
{
jobPtr.reset();
-
- QJsonParseError parse_error;
- QJsonDocument doc = QJsonDocument::fromJson(response, &parse_error);
- if (parse_error.error != QJsonParseError::NoError) {
- qWarning() << "Error while parsing JSON response from Modrinth at " << parse_error.offset
- << " reason: " << parse_error.errorString();
- qWarning() << response;
- return;
- }
-
+
QList<ModPlatform::IndexedPack> newList;
auto packs = doc.object().value("hits").toArray();
for (auto packRaw : packs) {
diff --git a/launcher/ui/pages/modplatform/modrinth/ModrinthModel.h b/launcher/ui/pages/modplatform/modrinth/ModrinthModel.h
index b8937b50..d095b18c 100644
--- a/launcher/ui/pages/modplatform/modrinth/ModrinthModel.h
+++ b/launcher/ui/pages/modplatform/modrinth/ModrinthModel.h
@@ -29,8 +29,8 @@ class ListModel : public ModPlatform::ListModel {
ListModel(ModrinthPage* parent);
virtual ~ListModel();
- private slots:
- void searchRequestFinished() override;
+ public slots:
+ void searchRequestFinished(QJsonDocument& doc) override;
private:
const char** getSorts() const override;
diff --git a/launcher/ui/pages/modplatform/modrinth/ModrinthPage.cpp b/launcher/ui/pages/modplatform/modrinth/ModrinthPage.cpp
index 944f8afb..fa703e38 100644
--- a/launcher/ui/pages/modplatform/modrinth/ModrinthPage.cpp
+++ b/launcher/ui/pages/modplatform/modrinth/ModrinthPage.cpp
@@ -35,22 +35,15 @@ ModrinthPage::ModrinthPage(ModDownloadDialog* dialog, BaseInstance* instance)
bool ModrinthPage::shouldDisplay() const { return true; }
-void ModrinthPage::onRequestVersionsSucceeded(ModPage* instance, QByteArray* response, QString addonId)
+void ModrinthPage::onRequestVersionsSucceeded(QJsonDocument& response, QString addonId)
{
if (addonId != current.addonId) { return; }
- QJsonParseError parse_error;
- QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
- if (parse_error.error != QJsonParseError::NoError) {
- qWarning() << "Error while parsing JSON response from Modrinth at " << parse_error.offset
- << " reason: " << parse_error.errorString();
- qWarning() << *response;
- return;
- }
- QJsonArray arr = doc.array();
+
+ QJsonArray arr = response.array();
try {
Modrinth::loadIndexedPackVersions(current, arr, APPLICATION->network(), m_instance);
} catch (const JSONValidationError& e) {
- qDebug() << *response;
+ qDebug() << response;
qWarning() << "Error while reading Modrinth mod version: " << e.cause();
}
auto packProfile = ((MinecraftInstance*)m_instance)->getPackProfile();
diff --git a/launcher/ui/pages/modplatform/modrinth/ModrinthPage.h b/launcher/ui/pages/modplatform/modrinth/ModrinthPage.h
index 7b1d0a00..f6d1eef0 100644
--- a/launcher/ui/pages/modplatform/modrinth/ModrinthPage.h
+++ b/launcher/ui/pages/modplatform/modrinth/ModrinthPage.h
@@ -22,5 +22,5 @@ class ModrinthPage : public ModPage {
bool shouldDisplay() const override;
private:
- void onRequestVersionsSucceeded(ModPage*, QByteArray*, QString) override;
+ void onRequestVersionsSucceeded(QJsonDocument&, QString) override;
};