2021-03-23 23:59:43 +00:00
|
|
|
#include "FlameModel.h"
|
2021-11-20 15:22:22 +00:00
|
|
|
#include "Application.h"
|
2021-03-26 22:15:20 +00:00
|
|
|
#include <Json.h>
|
2020-03-31 02:13:19 +01:00
|
|
|
|
|
|
|
#include <MMCStrings.h>
|
|
|
|
#include <Version.h>
|
|
|
|
|
|
|
|
#include <QtMath>
|
|
|
|
#include <QLabel>
|
|
|
|
|
|
|
|
#include <RWStorage.h>
|
|
|
|
|
2021-03-23 23:59:43 +00:00
|
|
|
namespace Flame {
|
2020-03-31 02:13:19 +01:00
|
|
|
|
|
|
|
ListModel::ListModel(QObject *parent) : QAbstractListModel(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();
|
|
|
|
if(pos >= modpacks.size() || pos < 0 || !index.isValid())
|
|
|
|
{
|
|
|
|
return QString("INVALID INDEX %1").arg(pos);
|
|
|
|
}
|
|
|
|
|
2021-03-26 22:15:20 +00:00
|
|
|
IndexedPack pack = modpacks.at(pos);
|
2020-03-31 02:13:19 +01:00
|
|
|
if(role == Qt::DisplayRole)
|
|
|
|
{
|
|
|
|
return pack.name;
|
|
|
|
}
|
|
|
|
else if (role == 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;
|
|
|
|
}
|
|
|
|
else if(role == Qt::DecorationRole)
|
|
|
|
{
|
|
|
|
if(m_logoMap.contains(pack.logoName))
|
|
|
|
{
|
|
|
|
return (m_logoMap.value(pack.logoName));
|
|
|
|
}
|
2021-11-20 15:22:22 +00:00
|
|
|
QIcon icon = APPLICATION->getThemedIcon("screenshot-placeholder");
|
2020-03-31 02:13:19 +01:00
|
|
|
((ListModel *)this)->requestLogo(pack.logoName, pack.logoUrl);
|
|
|
|
return icon;
|
|
|
|
}
|
|
|
|
else if(role == Qt::UserRole)
|
|
|
|
{
|
|
|
|
QVariant v;
|
|
|
|
v.setValue(pack);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListModel::logoLoaded(QString logo, QIcon out)
|
|
|
|
{
|
|
|
|
m_loadingLogos.removeAll(logo);
|
|
|
|
m_logoMap.insert(logo, out);
|
|
|
|
for(int i = 0; i < modpacks.size(); i++) {
|
|
|
|
if(modpacks[i].logoName == logo) {
|
|
|
|
emit dataChanged(createIndex(i, 0), createIndex(i, 0), {Qt::DecorationRole});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListModel::logoFailed(QString logo)
|
|
|
|
{
|
|
|
|
m_failedLogos.append(logo);
|
|
|
|
m_loadingLogos.removeAll(logo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListModel::requestLogo(QString logo, QString url)
|
|
|
|
{
|
|
|
|
if(m_loadingLogos.contains(logo) || m_failedLogos.contains(logo))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-21 22:21:12 +00:00
|
|
|
MetaEntryPtr entry = APPLICATION->metacache()->resolveEntry("FlamePacks", QString("logos/%1").arg(logo.section(".", 0, 0)));
|
2021-03-23 23:59:43 +00:00
|
|
|
NetJob *job = new NetJob(QString("Flame Icon Download %1").arg(logo));
|
2020-03-31 02:13:19 +01:00
|
|
|
job->addNetAction(Net::Download::makeCached(QUrl(url), entry));
|
|
|
|
|
|
|
|
auto fullPath = entry->getFullPath();
|
2020-06-07 16:46:12 +01:00
|
|
|
QObject::connect(job, &NetJob::succeeded, this, [this, logo, fullPath]
|
2020-03-31 02:13:19 +01:00
|
|
|
{
|
|
|
|
emit logoLoaded(logo, QIcon(fullPath));
|
|
|
|
if(waitingCallbacks.contains(logo))
|
|
|
|
{
|
|
|
|
waitingCallbacks.value(logo)(fullPath);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
QObject::connect(job, &NetJob::failed, this, [this, logo]
|
|
|
|
{
|
|
|
|
emit logoFailed(logo);
|
|
|
|
});
|
|
|
|
|
2021-11-21 22:21:12 +00:00
|
|
|
job->start(APPLICATION->network());
|
2020-03-31 02:13:19 +01:00
|
|
|
|
|
|
|
m_loadingLogos.append(logo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListModel::getLogo(const QString &logo, const QString &logoUrl, LogoCallback callback)
|
|
|
|
{
|
|
|
|
if(m_logoMap.contains(logo))
|
|
|
|
{
|
2021-11-21 22:21:12 +00:00
|
|
|
callback(APPLICATION->metacache()->resolveEntry("FlamePacks", QString("logos/%1").arg(logo.section(".", 0, 0)))->getFullPath());
|
2020-03-31 02:13:19 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
requestLogo(logo, logoUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::ItemFlags ListModel::flags(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
return QAbstractListModel::flags(index);
|
|
|
|
}
|
|
|
|
|
2020-04-01 01:08:11 +01:00
|
|
|
bool ListModel::canFetchMore(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;
|
|
|
|
if(nextSearchOffset == 0) {
|
|
|
|
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()
|
|
|
|
{
|
2021-03-23 23:59:43 +00:00
|
|
|
NetJob *netJob = new NetJob("Flame::Search");
|
2020-03-31 02:13:19 +01:00
|
|
|
auto searchUrl = QString(
|
|
|
|
"https://addons-ecs.forgesvc.net/api/v2/addon/search?"
|
|
|
|
"categoryId=0&"
|
|
|
|
"gameId=432&"
|
2020-04-01 01:08:11 +01:00
|
|
|
"index=%1&"
|
2020-03-31 02:13:19 +01:00
|
|
|
"pageSize=25&"
|
2020-04-01 01:08:11 +01:00
|
|
|
"searchFilter=%2&"
|
2020-03-31 02:13:19 +01:00
|
|
|
"sectionId=4471&"
|
2021-03-26 22:15:20 +00:00
|
|
|
"sort=%3"
|
|
|
|
).arg(nextSearchOffset).arg(currentSearchTerm).arg(currentSort);
|
2020-03-31 02:13:19 +01:00
|
|
|
netJob->addNetAction(Net::Download::makeByteArray(QUrl(searchUrl), &response));
|
|
|
|
jobPtr = netJob;
|
2021-11-21 22:21:12 +00:00
|
|
|
jobPtr->start(APPLICATION->network());
|
2020-03-31 02:13:19 +01:00
|
|
|
QObject::connect(netJob, &NetJob::succeeded, this, &ListModel::searchRequestFinished);
|
|
|
|
QObject::connect(netJob, &NetJob::failed, this, &ListModel::searchRequestFailed);
|
|
|
|
}
|
|
|
|
|
2021-03-26 22:15:20 +00:00
|
|
|
void ListModel::searchWithTerm(const QString& term, int sort)
|
2020-04-01 01:08:11 +01:00
|
|
|
{
|
2021-03-26 22:15:20 +00: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;
|
2020-04-01 01:08:11 +01:00
|
|
|
if(jobPtr) {
|
|
|
|
jobPtr->abort();
|
|
|
|
searchState = ResetRequested;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
beginResetModel();
|
|
|
|
modpacks.clear();
|
|
|
|
endResetModel();
|
|
|
|
searchState = None;
|
|
|
|
}
|
|
|
|
nextSearchOffset = 0;
|
|
|
|
performPaginatedSearch();
|
|
|
|
}
|
|
|
|
|
2021-03-23 23:59:43 +00:00
|
|
|
void Flame::ListModel::searchRequestFinished()
|
2020-03-31 02:13:19 +01:00
|
|
|
{
|
|
|
|
jobPtr.reset();
|
|
|
|
|
|
|
|
QJsonParseError parse_error;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(response, &parse_error);
|
|
|
|
if(parse_error.error != QJsonParseError::NoError) {
|
2021-03-23 23:59:43 +00:00
|
|
|
qWarning() << "Error while parsing JSON response from CurseForge at " << parse_error.offset << " reason: " << parse_error.errorString();
|
2020-03-31 02:13:19 +01:00
|
|
|
qWarning() << response;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-26 22:15:20 +00:00
|
|
|
QList<Flame::IndexedPack> newList;
|
|
|
|
auto packs = doc.array();
|
|
|
|
for(auto packRaw : packs) {
|
|
|
|
auto packObj = packRaw.toObject();
|
|
|
|
|
|
|
|
Flame::IndexedPack pack;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Flame::loadIndexedPack(pack, packObj);
|
|
|
|
newList.append(pack);
|
2020-03-31 02:13:19 +01:00
|
|
|
}
|
2021-03-26 22:15:20 +00:00
|
|
|
catch(const JSONValidationError &e)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2021-03-26 22:15:20 +00:00
|
|
|
if(packs.size() < 25) {
|
2020-04-01 01:08:11 +01:00
|
|
|
searchState = Finished;
|
|
|
|
} else {
|
|
|
|
nextSearchOffset += 25;
|
|
|
|
searchState = CanPossiblyFetchMore;
|
|
|
|
}
|
|
|
|
beginInsertRows(QModelIndex(), modpacks.size(), modpacks.size() + newList.size() - 1);
|
|
|
|
modpacks.append(newList);
|
|
|
|
endInsertRows();
|
2020-03-31 02:13:19 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
if(searchState == ResetRequested) {
|
|
|
|
beginResetModel();
|
|
|
|
modpacks.clear();
|
|
|
|
endResetModel();
|
|
|
|
|
|
|
|
nextSearchOffset = 0;
|
|
|
|
performPaginatedSearch();
|
|
|
|
} else {
|
|
|
|
searchState = Finished;
|
|
|
|
}
|
2020-03-31 02:13:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-04-01 01:08:11 +01:00
|
|
|
|