2022-03-03 00:17:10 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <QAbstractListModel>
|
|
|
|
|
2022-03-07 19:22:57 +00:00
|
|
|
#include "modplatform/ModIndex.h"
|
2022-03-03 00:17:10 +00:00
|
|
|
#include "net/NetJob.h"
|
|
|
|
|
|
|
|
class ModPage;
|
2022-04-02 21:34:26 +00:00
|
|
|
class Version;
|
2022-03-03 00:17:10 +00:00
|
|
|
|
|
|
|
namespace ModPlatform {
|
|
|
|
|
2022-03-08 14:12:35 +00:00
|
|
|
using LogoMap = QMap<QString, QIcon>;
|
|
|
|
using LogoCallback = std::function<void (QString)>;
|
2022-03-03 00:17:10 +00:00
|
|
|
|
|
|
|
class ListModel : public QAbstractListModel {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
ListModel(ModPage* parent);
|
2022-03-08 14:12:35 +00:00
|
|
|
~ListModel() override = default;
|
2022-03-03 00:17:10 +00:00
|
|
|
|
2022-03-08 14:12:35 +00:00
|
|
|
inline auto rowCount(const QModelIndex& parent) const -> int override { return modpacks.size(); };
|
|
|
|
inline auto columnCount(const QModelIndex& parent) const -> int override { return 1; };
|
|
|
|
inline auto flags(const QModelIndex& index) const -> Qt::ItemFlags override { return QAbstractListModel::flags(index); };
|
2022-03-06 19:04:24 +00:00
|
|
|
|
2022-03-08 14:12:35 +00:00
|
|
|
auto debugName() const -> QString;
|
2022-03-07 19:46:08 +00:00
|
|
|
|
|
|
|
/* Retrieve information from the model at a given index with the given role */
|
2022-03-08 14:12:35 +00:00
|
|
|
auto data(const QModelIndex& index, int role) const -> QVariant override;
|
2022-07-18 22:17:44 +00:00
|
|
|
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
|
2022-03-06 19:04:24 +00:00
|
|
|
|
2022-03-07 20:46:18 +00:00
|
|
|
inline void setActiveJob(NetJob::Ptr ptr) { jobPtr = ptr; }
|
2022-07-15 16:18:12 +00:00
|
|
|
inline NetJob* activeJob() { return jobPtr.get(); }
|
2022-03-07 19:22:57 +00:00
|
|
|
|
2022-03-07 20:46:18 +00:00
|
|
|
/* Ask the API for more information */
|
2022-03-03 00:17:10 +00:00
|
|
|
void fetchMore(const QModelIndex& parent) override;
|
2022-04-02 22:21:02 +00:00
|
|
|
void refresh();
|
2022-04-14 13:27:03 +00:00
|
|
|
void searchWithTerm(const QString& term, const int sort, const bool filter_changed);
|
2022-07-18 22:17:44 +00:00
|
|
|
void requestModInfo(ModPlatform::IndexedPack& current, QModelIndex index);
|
2022-07-18 22:28:46 +00:00
|
|
|
void requestModVersions(const ModPlatform::IndexedPack& current, QModelIndex index);
|
2022-03-03 00:17:10 +00:00
|
|
|
|
2022-03-07 22:29:59 +00:00
|
|
|
virtual void loadIndexedPack(ModPlatform::IndexedPack& m, QJsonObject& obj) = 0;
|
2022-05-24 12:38:48 +00:00
|
|
|
virtual void loadExtraPackInfo(ModPlatform::IndexedPack& m, QJsonObject& obj) {};
|
2022-03-07 22:29:59 +00:00
|
|
|
virtual void loadIndexedPackVersions(ModPlatform::IndexedPack& m, QJsonArray& arr) = 0;
|
|
|
|
|
2022-03-03 00:17:10 +00:00
|
|
|
void getLogo(const QString& logo, const QString& logoUrl, LogoCallback callback);
|
|
|
|
|
2022-03-08 14:12:35 +00:00
|
|
|
inline auto canFetchMore(const QModelIndex& parent) const -> bool override { return searchState == CanPossiblyFetchMore; };
|
2022-03-06 18:23:00 +00:00
|
|
|
|
2022-03-07 19:22:57 +00:00
|
|
|
public slots:
|
2022-03-07 20:46:18 +00:00
|
|
|
void searchRequestFinished(QJsonDocument& doc);
|
2022-03-06 19:04:24 +00:00
|
|
|
void searchRequestFailed(QString reason);
|
2022-03-03 02:01:23 +00:00
|
|
|
|
2022-07-18 22:17:44 +00:00
|
|
|
void infoRequestFinished(QJsonDocument& doc, ModPlatform::IndexedPack& pack, const QModelIndex& index);
|
2022-05-24 12:38:48 +00:00
|
|
|
|
2022-07-18 22:28:46 +00:00
|
|
|
void versionRequestSucceeded(QJsonDocument doc, QString addonId, const QModelIndex& index);
|
2022-03-07 19:22:57 +00:00
|
|
|
|
|
|
|
protected slots:
|
|
|
|
|
2022-03-03 00:17:10 +00:00
|
|
|
void logoFailed(QString logo);
|
|
|
|
void logoLoaded(QString logo, QIcon out);
|
|
|
|
|
2022-03-06 19:04:24 +00:00
|
|
|
void performPaginatedSearch();
|
2022-03-03 00:17:10 +00:00
|
|
|
|
|
|
|
protected:
|
2022-03-08 14:12:35 +00:00
|
|
|
virtual auto documentToArray(QJsonDocument& obj) const -> QJsonArray = 0;
|
|
|
|
virtual auto getSorts() const -> const char** = 0;
|
2022-03-03 02:01:23 +00:00
|
|
|
|
2022-03-03 00:17:10 +00:00
|
|
|
void requestLogo(QString file, QString url);
|
|
|
|
|
2022-04-02 21:34:26 +00:00
|
|
|
inline auto getMineVersions() const -> std::list<Version>;
|
2022-03-24 21:39:53 +00:00
|
|
|
|
2022-03-03 00:17:10 +00:00
|
|
|
protected:
|
|
|
|
ModPage* m_parent;
|
|
|
|
|
|
|
|
QList<ModPlatform::IndexedPack> modpacks;
|
2022-03-06 19:04:24 +00:00
|
|
|
|
2022-03-03 00:17:10 +00:00
|
|
|
LogoMap m_logoMap;
|
|
|
|
QMap<QString, LogoCallback> waitingCallbacks;
|
2022-03-06 19:04:24 +00:00
|
|
|
QStringList m_failedLogos;
|
|
|
|
QStringList m_loadingLogos;
|
2022-03-03 00:17:10 +00:00
|
|
|
|
|
|
|
QString currentSearchTerm;
|
|
|
|
int currentSort = 0;
|
|
|
|
int nextSearchOffset = 0;
|
|
|
|
enum SearchState { None, CanPossiblyFetchMore, ResetRequested, Finished } searchState = None;
|
2022-03-06 19:04:24 +00:00
|
|
|
|
2022-03-03 00:17:10 +00:00
|
|
|
NetJob::Ptr jobPtr;
|
|
|
|
};
|
|
|
|
} // namespace ModPlatform
|