diff --git a/launcher/ui/pages/modplatform/ModModel.cpp b/launcher/ui/pages/modplatform/ModModel.cpp index 015fcf7d..b8b90f84 100644 --- a/launcher/ui/pages/modplatform/ModModel.cpp +++ b/launcher/ui/pages/modplatform/ModModel.cpp @@ -1,6 +1,6 @@ #include "ModModel.h" -#include "ModPage.h" +#include "Json.h" #include "minecraft/MinecraftInstance.h" #include "minecraft/PackProfile.h" #include "ui/dialogs/ModDownloadDialog.h" @@ -11,18 +11,6 @@ namespace ModPlatform { ListModel::ListModel(ModPage* parent) : QAbstractListModel(parent), m_parent(parent) {} -ListModel::~ListModel() {} - -int ListModel::rowCount(const QModelIndex& parent) const -{ - return modpacks.size(); -} - -int ListModel::columnCount(const QModelIndex& parent) const -{ - return 1; -} - QVariant ListModel::data(const QModelIndex& index, int role) const { int pos = index.row(); @@ -73,16 +61,6 @@ void ListModel::logoFailed(QString logo) m_loadingLogos.removeAll(logo); } -Qt::ItemFlags ListModel::flags(const QModelIndex& index) const -{ - return QAbstractListModel::flags(index); -} - -bool ListModel::canFetchMore(const QModelIndex& parent) const -{ - return searchState == CanPossiblyFetchMore; -} - void ListModel::fetchMore(const QModelIndex& parent) { if (parent.isValid()) return; @@ -140,6 +118,38 @@ void ListModel::searchWithTerm(const QString& term, const int sort) performPaginatedSearch(); } +void ListModel::searchRequestFinished(QJsonDocument& doc) +{ + jobPtr.reset(); + + QList newList; + auto packs = documentToArray(doc); + + for (auto packRaw : packs) { + auto packObj = packRaw.toObject(); + + ModPlatform::IndexedPack pack; + try { + loadIndexedPack(pack, packObj); + newList.append(pack); + } catch (const JSONValidationError& e) { + qWarning() << "Error while loading mod from " << m_parent->debugName() << ": " << e.cause(); + continue; + } + } + + if (packs.size() < 25) { + searchState = Finished; + } else { + nextSearchOffset += 25; + searchState = CanPossiblyFetchMore; + } + + beginInsertRows(QModelIndex(), modpacks.size(), modpacks.size() + newList.size() - 1); + modpacks.append(newList); + endInsertRows(); +} + void ListModel::searchRequestFailed(QString reason) { if (jobPtr->first()->m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 409) { diff --git a/launcher/ui/pages/modplatform/ModModel.h b/launcher/ui/pages/modplatform/ModModel.h index e971149c..e0cc098d 100644 --- a/launcher/ui/pages/modplatform/ModModel.h +++ b/launcher/ui/pages/modplatform/ModModel.h @@ -18,29 +18,30 @@ class ListModel : public QAbstractListModel { public: ListModel(ModPage* parent); - virtual ~ListModel(); + virtual ~ListModel() = default; - int rowCount(const QModelIndex& parent) const override; - int columnCount(const QModelIndex& parent) const override; + inline int rowCount(const QModelIndex& parent) const override { return modpacks.size(); }; + inline int columnCount(const QModelIndex& parent) const override { return 1; }; + inline Qt::ItemFlags flags(const QModelIndex& index) const override { return QAbstractListModel::flags(index); }; QString debugName() const; /* Retrieve information from the model at a given index with the given role */ QVariant data(const QModelIndex& index, int role) const override; - Qt::ItemFlags flags(const QModelIndex& index) const override; - void setActiveJob(NetJob::Ptr ptr) { jobPtr = ptr; } + inline void setActiveJob(NetJob::Ptr ptr) { jobPtr = ptr; } - bool canFetchMore(const QModelIndex& parent) const override; + /* Ask the API for more information */ void fetchMore(const QModelIndex& parent) override; + void searchWithTerm(const QString& term, const int sort); + void requestModVersions(const ModPlatform::IndexedPack& current); void getLogo(const QString& logo, const QString& logoUrl, LogoCallback callback); - void searchWithTerm(const QString& term, const int sort); - virtual void requestModVersions(const ModPlatform::IndexedPack& current); + inline bool canFetchMore(const QModelIndex& parent) const override { return searchState == CanPossiblyFetchMore; }; public slots: - virtual void searchRequestFinished(QJsonDocument& doc) = 0; + void searchRequestFinished(QJsonDocument& doc); void searchRequestFailed(QString reason); void versionRequestSucceeded(QJsonDocument doc, QString addonId); @@ -53,6 +54,8 @@ class ListModel : public QAbstractListModel { void performPaginatedSearch(); protected: + virtual void loadIndexedPack(ModPlatform::IndexedPack& m, QJsonObject& obj) = 0; + virtual QJsonArray documentToArray(QJsonDocument& obj) const = 0; virtual const char** getSorts() const = 0; void requestLogo(QString file, QString url); @@ -73,6 +76,5 @@ class ListModel : public QAbstractListModel { enum SearchState { None, CanPossiblyFetchMore, ResetRequested, Finished } searchState = None; NetJob::Ptr jobPtr; - QByteArray response; }; } // namespace ModPlatform diff --git a/launcher/ui/pages/modplatform/flame/FlameModModel.cpp b/launcher/ui/pages/modplatform/flame/FlameModModel.cpp index cff29a79..7588a714 100644 --- a/launcher/ui/pages/modplatform/flame/FlameModModel.cpp +++ b/launcher/ui/pages/modplatform/flame/FlameModModel.cpp @@ -1,53 +1,7 @@ #include "FlameModModel.h" -#include "FlameModPage.h" -#include "minecraft/PackProfile.h" - -#include namespace FlameMod { -ListModel::ListModel(FlameModPage* parent) : ModPlatform::ListModel(parent) {} - -ListModel::~ListModel() {} - - -void FlameMod::ListModel::searchRequestFinished(QJsonDocument& doc) -{ - jobPtr.reset(); - - QList newList; - auto packs = doc.array(); - for(auto packRaw : packs) { - auto packObj = packRaw.toObject(); - - ModPlatform::IndexedPack pack; - try - { - FlameMod::loadIndexedPack(pack, packObj); - newList.append(pack); - } - catch(const JSONValidationError &e) - { - qWarning() << "Error while loading mod from Flame: " << e.cause(); - continue; - } - } - if(packs.size() < 25) { - searchState = Finished; - } else { - nextSearchOffset += 25; - searchState = CanPossiblyFetchMore; - } - beginInsertRows(QModelIndex(), modpacks.size(), modpacks.size() + newList.size() - 1); - modpacks.append(newList); - endInsertRows(); -} - -const char* sorts[6]{ "Featured", "Popularity", "LastUpdated", "Name", "Author", "TotalDownloads" }; - -const char** FlameMod::ListModel::getSorts() const -{ - return sorts; -} +const char* ListModel::sorts[6]{ "Featured", "Popularity", "LastUpdated", "Name", "Author", "TotalDownloads" }; } // namespace FlameMod diff --git a/launcher/ui/pages/modplatform/flame/FlameModModel.h b/launcher/ui/pages/modplatform/flame/FlameModModel.h index 022ec32e..204834c9 100644 --- a/launcher/ui/pages/modplatform/flame/FlameModModel.h +++ b/launcher/ui/pages/modplatform/flame/FlameModModel.h @@ -1,39 +1,25 @@ #pragma once -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "BaseInstance.h" #include "FlameModPage.h" #include "modplatform/flame/FlameModIndex.h" namespace FlameMod { -typedef std::function LogoCallback; - class ListModel : public ModPlatform::ListModel { Q_OBJECT public: - ListModel(FlameModPage* parent); - virtual ~ListModel(); - - private slots: - void searchRequestFinished(QJsonDocument& doc) override; + ListModel(FlameModPage* parent) : ModPlatform::ListModel(parent) {} +; + virtual ~ListModel() = default; private: - const char** getSorts() const override; + void loadIndexedPack(ModPlatform::IndexedPack& m, QJsonObject& obj) override { FlameMod::loadIndexedPack(m, obj); }; + + QJsonArray documentToArray(QJsonDocument& obj) const override { return obj.array(); }; + + static const char* sorts[6]; + const char** getSorts() const override { return sorts; }; }; -} // namespace Modrinth +} // namespace FlameMod diff --git a/launcher/ui/pages/modplatform/modrinth/ModrinthModel.cpp b/launcher/ui/pages/modplatform/modrinth/ModrinthModel.cpp index 784b1128..9361546e 100644 --- a/launcher/ui/pages/modplatform/modrinth/ModrinthModel.cpp +++ b/launcher/ui/pages/modplatform/modrinth/ModrinthModel.cpp @@ -1,49 +1,7 @@ #include "ModrinthModel.h" -#include "ModrinthPage.h" -#include "minecraft/MinecraftInstance.h" - -#include namespace Modrinth { -ListModel::ListModel(ModrinthPage* parent) : ModPlatform::ListModel(parent) {} - -ListModel::~ListModel() {} - -void Modrinth::ListModel::searchRequestFinished(QJsonDocument& doc) -{ - jobPtr.reset(); - - QList newList; - auto packs = doc.object().value("hits").toArray(); - for (auto packRaw : packs) { - auto packObj = packRaw.toObject(); - - ModPlatform::IndexedPack pack; - try { - Modrinth::loadIndexedPack(pack, packObj); - newList.append(pack); - } catch (const JSONValidationError& e) { - qWarning() << "Error while loading mod from Modrinth: " << e.cause(); - continue; - } - } - if (packs.size() < 25) { - searchState = Finished; - } else { - nextSearchOffset += 25; - searchState = CanPossiblyFetchMore; - } - beginInsertRows(QModelIndex(), modpacks.size(), modpacks.size() + newList.size() - 1); - modpacks.append(newList); - endInsertRows(); -} - -const char* sorts[5]{ "relevance", "downloads", "follows", "updated", "newest" }; - -const char** Modrinth::ListModel::getSorts() const -{ - return sorts; -} +const char* ListModel::sorts[5] { "relevance", "downloads", "follows", "updated", "newest" }; } // namespace Modrinth diff --git a/launcher/ui/pages/modplatform/modrinth/ModrinthModel.h b/launcher/ui/pages/modplatform/modrinth/ModrinthModel.h index d095b18c..9137190d 100644 --- a/launcher/ui/pages/modplatform/modrinth/ModrinthModel.h +++ b/launcher/ui/pages/modplatform/modrinth/ModrinthModel.h @@ -1,39 +1,24 @@ #pragma once -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "BaseInstance.h" #include "ModrinthPage.h" #include "modplatform/modrinth/ModrinthPackIndex.h" namespace Modrinth { -typedef std::function LogoCallback; - class ListModel : public ModPlatform::ListModel { Q_OBJECT public: - ListModel(ModrinthPage* parent); - virtual ~ListModel(); - - public slots: - void searchRequestFinished(QJsonDocument& doc) override; + ListModel(ModrinthPage* parent) : ModPlatform::ListModel(parent){}; + virtual ~ListModel() = default; private: - const char** getSorts() const override; + void loadIndexedPack(ModPlatform::IndexedPack& m, QJsonObject& obj) override { Modrinth::loadIndexedPack(m, obj); }; + + QJsonArray documentToArray(QJsonDocument& obj) const override { return obj.object().value("hits").toArray(); }; + + static const char* sorts[5]; + const char** getSorts() const override { return sorts; }; }; } // namespace Modrinth