2021-03-23 23:59:43 +00:00
|
|
|
#include "FlameModel.h"
|
2021-03-26 22:15:20 +00:00
|
|
|
#include <Json.h>
|
2022-05-08 08:22:50 +01:00
|
|
|
#include "Application.h"
|
2023-08-18 18:03:02 +01:00
|
|
|
#include "modplatform/ResourceAPI.h"
|
|
|
|
#include "modplatform/flame/FlameAPI.h"
|
2022-10-21 23:19:51 +01:00
|
|
|
#include "ui/widgets/ProjectItem.h"
|
2020-03-31 02:13:19 +01:00
|
|
|
|
2023-06-02 00:39:04 +01:00
|
|
|
#include "net/ApiDownload.h"
|
|
|
|
|
2020-03-31 02:13:19 +01:00
|
|
|
#include <Version.h>
|
|
|
|
|
|
|
|
#include <QtMath>
|
|
|
|
|
2021-03-23 23:59:43 +00:00
|
|
|
namespace Flame {
|
2020-03-31 02:13:19 +01:00
|
|
|
|
2022-05-08 08:22:50 +01:00
|
|
|
ListModel::ListModel(QObject* parent) : QAbstractListModel(parent) {}
|
2020-03-31 02:13:19 +01:00
|
|
|
|
2022-05-08 08:22:50 +01:00
|
|
|
ListModel::~ListModel() {}
|
2020-03-31 02:13:19 +01:00
|
|
|
|
2022-05-08 08:22:50 +01:00
|
|
|
int ListModel::rowCount(const QModelIndex& parent) const
|
2020-03-31 02:13:19 +01:00
|
|
|
{
|
2022-11-13 14:35:55 +00:00
|
|
|
return parent.isValid() ? 0 : modpacks.size();
|
2020-03-31 02:13:19 +01:00
|
|
|
}
|
|
|
|
|
2022-05-08 08:22:50 +01:00
|
|
|
int ListModel::columnCount(const QModelIndex& parent) const
|
2020-03-31 02:13:19 +01:00
|
|
|
{
|
2022-11-13 14:35:55 +00:00
|
|
|
return parent.isValid() ? 0 : 1;
|
2020-03-31 02:13:19 +01:00
|
|
|
}
|
|
|
|
|
2022-05-08 08:22:50 +01:00
|
|
|
QVariant ListModel::data(const QModelIndex& index, int role) const
|
2020-03-31 02:13:19 +01:00
|
|
|
{
|
|
|
|
int pos = index.row();
|
2022-05-08 08:22:50 +01:00
|
|
|
if (pos >= modpacks.size() || pos < 0 || !index.isValid()) {
|
2020-03-31 02:13:19 +01:00
|
|
|
return QString("INVALID INDEX %1").arg(pos);
|
|
|
|
}
|
|
|
|
|
2021-03-26 22:15:20 +00:00
|
|
|
IndexedPack pack = modpacks.at(pos);
|
2022-10-21 23:19:51 +01:00
|
|
|
switch (role) {
|
|
|
|
case Qt::ToolTipRole: {
|
|
|
|
if (pack.description.length() > 100) {
|
|
|
|
// some magic to prevent to long tooltips and replace html linebreaks
|
|
|
|
QString edit = pack.description.left(97);
|
|
|
|
edit = edit.left(edit.lastIndexOf("<br>")).left(edit.lastIndexOf(" ")).append("...");
|
|
|
|
return edit;
|
|
|
|
}
|
|
|
|
return pack.description;
|
2023-07-27 23:16:00 +01:00
|
|
|
}
|
|
|
|
case Qt::DecorationRole: {
|
2022-10-21 23:19:51 +01:00
|
|
|
if (m_logoMap.contains(pack.logoName)) {
|
|
|
|
return (m_logoMap.value(pack.logoName));
|
|
|
|
}
|
|
|
|
QIcon icon = APPLICATION->getThemedIcon("screenshot-placeholder");
|
|
|
|
((ListModel*)this)->requestLogo(pack.logoName, pack.logoUrl);
|
|
|
|
return icon;
|
2023-07-27 23:16:00 +01:00
|
|
|
}
|
|
|
|
case Qt::UserRole: {
|
2022-10-21 23:19:51 +01:00
|
|
|
QVariant v;
|
|
|
|
v.setValue(pack);
|
|
|
|
return v;
|
2020-03-31 02:13:19 +01:00
|
|
|
}
|
2022-10-21 23:19:51 +01:00
|
|
|
case Qt::SizeHintRole:
|
|
|
|
return QSize(0, 58);
|
|
|
|
case UserDataTypes::TITLE:
|
|
|
|
return pack.name;
|
|
|
|
case UserDataTypes::DESCRIPTION:
|
|
|
|
return pack.description;
|
|
|
|
case UserDataTypes::SELECTED:
|
|
|
|
return false;
|
2023-06-08 18:26:09 +01:00
|
|
|
case UserDataTypes::INSTALLED:
|
|
|
|
return false;
|
2022-10-21 23:19:51 +01:00
|
|
|
default:
|
|
|
|
break;
|
2020-03-31 02:13:19 +01:00
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2023-08-10 19:38:32 +01:00
|
|
|
bool ListModel::setData(const QModelIndex& index, const QVariant& value, [[maybe_unused]] int role)
|
2022-06-24 13:16:41 +01:00
|
|
|
{
|
|
|
|
int pos = index.row();
|
|
|
|
if (pos >= modpacks.size() || pos < 0 || !index.isValid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
modpacks[pos] = value.value<Flame::IndexedPack>();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-31 02:13:19 +01:00
|
|
|
void ListModel::logoLoaded(QString logo, QIcon out)
|
|
|
|
{
|
|
|
|
m_loadingLogos.removeAll(logo);
|
|
|
|
m_logoMap.insert(logo, out);
|
2022-05-08 08:22:50 +01:00
|
|
|
for (int i = 0; i < modpacks.size(); i++) {
|
|
|
|
if (modpacks[i].logoName == logo) {
|
|
|
|
emit dataChanged(createIndex(i, 0), createIndex(i, 0), { Qt::DecorationRole });
|
2020-03-31 02:13:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListModel::logoFailed(QString logo)
|
|
|
|
{
|
|
|
|
m_failedLogos.append(logo);
|
|
|
|
m_loadingLogos.removeAll(logo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListModel::requestLogo(QString logo, QString url)
|
|
|
|
{
|
2022-05-08 08:22:50 +01:00
|
|
|
if (m_loadingLogos.contains(logo) || m_failedLogos.contains(logo)) {
|
2020-03-31 02:13:19 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-27 23:16:00 +01:00
|
|
|
MetaEntryPtr entry = APPLICATION->metacache()->resolveEntry("FlamePacks", QString("logos/%1").arg(logo));
|
2022-01-18 11:28:55 +00:00
|
|
|
auto job = new NetJob(QString("Flame Icon Download %1").arg(logo), APPLICATION->network());
|
2023-06-02 00:39:04 +01:00
|
|
|
job->addNetAction(Net::ApiDownload::makeCached(QUrl(url), entry));
|
2020-03-31 02:13:19 +01:00
|
|
|
|
|
|
|
auto fullPath = entry->getFullPath();
|
2022-05-08 08:22:50 +01:00
|
|
|
QObject::connect(job, &NetJob::succeeded, this, [this, logo, fullPath, job] {
|
2022-01-18 11:28:55 +00:00
|
|
|
job->deleteLater();
|
2020-03-31 02:13:19 +01:00
|
|
|
emit logoLoaded(logo, QIcon(fullPath));
|
2022-05-08 08:22:50 +01:00
|
|
|
if (waitingCallbacks.contains(logo)) {
|
2020-03-31 02:13:19 +01:00
|
|
|
waitingCallbacks.value(logo)(fullPath);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-05-08 08:22:50 +01:00
|
|
|
QObject::connect(job, &NetJob::failed, this, [this, logo, job] {
|
2022-01-18 11:28:55 +00:00
|
|
|
job->deleteLater();
|
2020-03-31 02:13:19 +01:00
|
|
|
emit logoFailed(logo);
|
|
|
|
});
|
|
|
|
|
2021-12-31 04:27:59 +00:00
|
|
|
job->start();
|
2020-03-31 02:13:19 +01:00
|
|
|
|
|
|
|
m_loadingLogos.append(logo);
|
|
|
|
}
|
|
|
|
|
2022-05-08 08:22:50 +01:00
|
|
|
void ListModel::getLogo(const QString& logo, const QString& logoUrl, LogoCallback callback)
|
2020-03-31 02:13:19 +01:00
|
|
|
{
|
2022-05-08 08:22:50 +01:00
|
|
|
if (m_logoMap.contains(logo)) {
|
2023-07-27 23:16:00 +01:00
|
|
|
callback(APPLICATION->metacache()->resolveEntry("FlamePacks", QString("logos/%1").arg(logo))->getFullPath());
|
2022-05-08 08:22:50 +01:00
|
|
|
} else {
|
2020-03-31 02:13:19 +01:00
|
|
|
requestLogo(logo, logoUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-08 08:22:50 +01:00
|
|
|
Qt::ItemFlags ListModel::flags(const QModelIndex& index) const
|
2020-03-31 02:13:19 +01:00
|
|
|
{
|
|
|
|
return QAbstractListModel::flags(index);
|
|
|
|
}
|
|
|
|
|
2023-07-01 07:51:15 +01:00
|
|
|
bool ListModel::canFetchMore([[maybe_unused]] const QModelIndex& parent) const
|
2020-03-31 02:13:19 +01:00
|
|
|
{
|
2020-04-01 01:08:11 +01:00
|
|
|
return searchState == CanPossiblyFetchMore;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListModel::fetchMore(const QModelIndex& parent)
|
|
|
|
{
|
|
|
|
if (parent.isValid())
|
|
|
|
return;
|
2022-05-08 08:22:50 +01:00
|
|
|
if (nextSearchOffset == 0) {
|
2020-04-01 01:08:11 +01:00
|
|
|
qWarning() << "fetchMore with 0 offset is wrong...";
|
2020-03-31 02:13:19 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-04-01 01:08:11 +01:00
|
|
|
performPaginatedSearch();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListModel::performPaginatedSearch()
|
|
|
|
{
|
2023-08-18 18:03:02 +01:00
|
|
|
if (currentSearchTerm.startsWith("#")) {
|
2023-08-18 18:19:51 +01:00
|
|
|
auto projectId = currentSearchTerm.mid(1);
|
2023-08-18 18:03:02 +01:00
|
|
|
if (!projectId.isEmpty()) {
|
|
|
|
ResourceAPI::ProjectInfoCallbacks callbacks;
|
|
|
|
|
|
|
|
// Use defaults if no callbacks are set
|
|
|
|
if (!callbacks.on_fail)
|
|
|
|
callbacks.on_fail = [this](QString reason) { searchRequestFailed(reason); };
|
|
|
|
|
|
|
|
if (!callbacks.on_succeed)
|
2023-08-18 20:24:30 +01:00
|
|
|
callbacks.on_succeed = [this](auto& doc, auto& pack) { searchRequestForOneSucceeded(doc); };
|
2023-08-18 18:03:02 +01:00
|
|
|
static const FlameAPI api;
|
|
|
|
if (auto job = api.getProjectInfo({ projectId }, std::move(callbacks)); job) {
|
|
|
|
jobPtr = job;
|
|
|
|
jobPtr->start();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2023-01-24 19:52:09 +00:00
|
|
|
auto netJob = makeShared<NetJob>("Flame::Search", APPLICATION->network());
|
2020-03-31 02:13:19 +01:00
|
|
|
auto searchUrl = QString(
|
2022-05-08 08:22:50 +01:00
|
|
|
"https://api.curseforge.com/v1/mods/search?"
|
|
|
|
"gameId=432&"
|
|
|
|
"classId=4471&"
|
|
|
|
"index=%1&"
|
|
|
|
"pageSize=25&"
|
|
|
|
"searchFilter=%2&"
|
|
|
|
"sortField=%3&"
|
|
|
|
"sortOrder=desc")
|
|
|
|
.arg(nextSearchOffset)
|
|
|
|
.arg(currentSearchTerm)
|
|
|
|
.arg(currentSort + 1);
|
|
|
|
|
2023-06-25 20:36:27 +01:00
|
|
|
netJob->addNetAction(Net::ApiDownload::makeByteArray(QUrl(searchUrl), response));
|
2020-03-31 02:13:19 +01:00
|
|
|
jobPtr = netJob;
|
2021-12-31 04:27:59 +00:00
|
|
|
jobPtr->start();
|
2023-01-24 19:52:09 +00:00
|
|
|
QObject::connect(netJob.get(), &NetJob::succeeded, this, &ListModel::searchRequestFinished);
|
|
|
|
QObject::connect(netJob.get(), &NetJob::failed, this, &ListModel::searchRequestFailed);
|
2020-03-31 02:13:19 +01:00
|
|
|
}
|
|
|
|
|
2021-03-26 22:15:20 +00:00
|
|
|
void ListModel::searchWithTerm(const QString& term, int sort)
|
2020-04-01 01:08:11 +01:00
|
|
|
{
|
2022-05-08 08:22:50 +01:00
|
|
|
if (currentSearchTerm == term && currentSearchTerm.isNull() == term.isNull() && currentSort == sort) {
|
2020-04-01 01:08:11 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
currentSearchTerm = term;
|
2021-03-26 22:15:20 +00:00
|
|
|
currentSort = sort;
|
2023-08-18 18:03:02 +01:00
|
|
|
if (hasActiveSearchJob()) {
|
2020-04-01 01:08:11 +01:00
|
|
|
jobPtr->abort();
|
|
|
|
searchState = ResetRequested;
|
|
|
|
return;
|
|
|
|
}
|
2023-08-18 18:03:02 +01:00
|
|
|
beginResetModel();
|
|
|
|
modpacks.clear();
|
|
|
|
endResetModel();
|
|
|
|
searchState = None;
|
|
|
|
|
2020-04-01 01:08:11 +01:00
|
|
|
nextSearchOffset = 0;
|
|
|
|
performPaginatedSearch();
|
|
|
|
}
|
|
|
|
|
2021-03-23 23:59:43 +00:00
|
|
|
void Flame::ListModel::searchRequestFinished()
|
2020-03-31 02:13:19 +01:00
|
|
|
{
|
2023-08-18 18:03:02 +01:00
|
|
|
if (hasActiveSearchJob())
|
|
|
|
return;
|
2020-03-31 02:13:19 +01:00
|
|
|
|
|
|
|
QJsonParseError parse_error;
|
2023-06-15 20:59:41 +01:00
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
|
2022-05-08 08:22:50 +01:00
|
|
|
if (parse_error.error != QJsonParseError::NoError) {
|
|
|
|
qWarning() << "Error while parsing JSON response from CurseForge at " << parse_error.offset
|
|
|
|
<< " reason: " << parse_error.errorString();
|
2023-06-15 20:59:41 +01:00
|
|
|
qWarning() << *response;
|
2020-03-31 02:13:19 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-26 22:15:20 +00:00
|
|
|
QList<Flame::IndexedPack> newList;
|
2022-05-08 08:22:50 +01:00
|
|
|
auto packs = Json::ensureArray(doc.object(), "data");
|
|
|
|
for (auto packRaw : packs) {
|
2021-03-26 22:15:20 +00:00
|
|
|
auto packObj = packRaw.toObject();
|
|
|
|
|
|
|
|
Flame::IndexedPack pack;
|
2022-05-08 08:22:50 +01:00
|
|
|
try {
|
2021-03-26 22:15:20 +00:00
|
|
|
Flame::loadIndexedPack(pack, packObj);
|
|
|
|
newList.append(pack);
|
2022-05-08 08:22:50 +01:00
|
|
|
} catch (const JSONValidationError& e) {
|
2021-03-26 22:15:20 +00:00
|
|
|
qWarning() << "Error while loading pack from CurseForge: " << e.cause();
|
2020-04-01 01:08:11 +01:00
|
|
|
continue;
|
2020-03-31 02:13:19 +01:00
|
|
|
}
|
|
|
|
}
|
2022-05-08 08:22:50 +01:00
|
|
|
if (packs.size() < 25) {
|
2020-04-01 01:08:11 +01:00
|
|
|
searchState = Finished;
|
|
|
|
} else {
|
|
|
|
nextSearchOffset += 25;
|
|
|
|
searchState = CanPossiblyFetchMore;
|
|
|
|
}
|
2022-06-25 00:09:44 +01:00
|
|
|
|
|
|
|
// When you have a Qt build with assertions turned on, proceeding here will abort the application
|
|
|
|
if (newList.size() == 0)
|
|
|
|
return;
|
|
|
|
|
2020-04-01 01:08:11 +01:00
|
|
|
beginInsertRows(QModelIndex(), modpacks.size(), modpacks.size() + newList.size() - 1);
|
|
|
|
modpacks.append(newList);
|
|
|
|
endInsertRows();
|
2020-03-31 02:13:19 +01:00
|
|
|
}
|
|
|
|
|
2023-08-18 18:03:02 +01:00
|
|
|
void Flame::ListModel::searchRequestForOneSucceeded(QJsonDocument& doc)
|
|
|
|
{
|
|
|
|
jobPtr.reset();
|
|
|
|
|
|
|
|
auto packObj = Json::ensureObject(doc.object(), "data");
|
|
|
|
|
|
|
|
Flame::IndexedPack pack;
|
|
|
|
try {
|
|
|
|
Flame::loadIndexedPack(pack, packObj);
|
|
|
|
} catch (const JSONValidationError& e) {
|
|
|
|
qWarning() << "Error while loading pack from CurseForge: " << e.cause();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
beginInsertRows(QModelIndex(), modpacks.size(), modpacks.size() + 1);
|
|
|
|
modpacks.append({ pack });
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2021-03-23 23:59:43 +00:00
|
|
|
void Flame::ListModel::searchRequestFailed(QString reason)
|
2020-03-31 02:13:19 +01:00
|
|
|
{
|
|
|
|
jobPtr.reset();
|
2020-04-01 01:08:11 +01:00
|
|
|
|
2022-05-08 08:22:50 +01:00
|
|
|
if (searchState == ResetRequested) {
|
2020-04-01 01:08:11 +01:00
|
|
|
beginResetModel();
|
|
|
|
modpacks.clear();
|
|
|
|
endResetModel();
|
|
|
|
|
|
|
|
nextSearchOffset = 0;
|
|
|
|
performPaginatedSearch();
|
|
|
|
} else {
|
|
|
|
searchState = Finished;
|
|
|
|
}
|
2020-03-31 02:13:19 +01:00
|
|
|
}
|
|
|
|
|
2022-05-08 08:22:50 +01:00
|
|
|
} // namespace Flame
|