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