2021-03-24 05:29:43 +05:30
|
|
|
#include "FlameModel.h"
|
2021-03-27 03:45:20 +05:30
|
|
|
#include <Json.h>
|
2022-05-08 12:52:50 +05:30
|
|
|
#include "Application.h"
|
2022-10-22 03:49:51 +05:30
|
|
|
#include "ui/widgets/ProjectItem.h"
|
2020-03-31 06:43:19 +05:30
|
|
|
|
|
|
|
#include <Version.h>
|
|
|
|
|
|
|
|
#include <QtMath>
|
|
|
|
|
2021-03-24 05:29:43 +05:30
|
|
|
namespace Flame {
|
2020-03-31 06:43:19 +05:30
|
|
|
|
2022-05-08 12:52:50 +05:30
|
|
|
ListModel::ListModel(QObject* parent) : QAbstractListModel(parent) {}
|
2020-03-31 06:43:19 +05:30
|
|
|
|
2022-05-08 12:52:50 +05:30
|
|
|
ListModel::~ListModel() {}
|
2020-03-31 06:43:19 +05:30
|
|
|
|
2022-05-08 12:52:50 +05:30
|
|
|
int ListModel::rowCount(const QModelIndex& parent) const
|
2020-03-31 06:43:19 +05:30
|
|
|
{
|
2022-11-13 20:05:55 +05:30
|
|
|
return parent.isValid() ? 0 : modpacks.size();
|
2020-03-31 06:43:19 +05:30
|
|
|
}
|
|
|
|
|
2022-05-08 12:52:50 +05:30
|
|
|
int ListModel::columnCount(const QModelIndex& parent) const
|
2020-03-31 06:43:19 +05:30
|
|
|
{
|
2022-11-13 20:05:55 +05:30
|
|
|
return parent.isValid() ? 0 : 1;
|
2020-03-31 06:43:19 +05:30
|
|
|
}
|
|
|
|
|
2022-05-08 12:52:50 +05:30
|
|
|
QVariant ListModel::data(const QModelIndex& index, int role) const
|
2020-03-31 06:43:19 +05:30
|
|
|
{
|
|
|
|
int pos = index.row();
|
2022-05-08 12:52:50 +05:30
|
|
|
if (pos >= modpacks.size() || pos < 0 || !index.isValid()) {
|
2020-03-31 06:43:19 +05:30
|
|
|
return QString("INVALID INDEX %1").arg(pos);
|
|
|
|
}
|
|
|
|
|
2021-03-27 03:45:20 +05:30
|
|
|
IndexedPack pack = modpacks.at(pos);
|
2022-10-22 03:49:51 +05:30
|
|
|
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;
|
|
|
|
} case Qt::DecorationRole: {
|
|
|
|
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;
|
|
|
|
} case Qt::UserRole: {
|
|
|
|
QVariant v;
|
|
|
|
v.setValue(pack);
|
|
|
|
return v;
|
2020-03-31 06:43:19 +05:30
|
|
|
}
|
2022-10-22 03:49:51 +05:30
|
|
|
case Qt::SizeHintRole:
|
|
|
|
return QSize(0, 58);
|
|
|
|
case UserDataTypes::TITLE:
|
|
|
|
return pack.name;
|
|
|
|
case UserDataTypes::DESCRIPTION:
|
|
|
|
return pack.description;
|
|
|
|
case UserDataTypes::SELECTED:
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
break;
|
2020-03-31 06:43:19 +05:30
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2022-06-24 17:46:41 +05:30
|
|
|
bool ListModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
|
|
{
|
|
|
|
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 06:43:19 +05:30
|
|
|
void ListModel::logoLoaded(QString logo, QIcon out)
|
|
|
|
{
|
|
|
|
m_loadingLogos.removeAll(logo);
|
|
|
|
m_logoMap.insert(logo, out);
|
2022-05-08 12:52:50 +05:30
|
|
|
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 06:43:19 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListModel::logoFailed(QString logo)
|
|
|
|
{
|
|
|
|
m_failedLogos.append(logo);
|
|
|
|
m_loadingLogos.removeAll(logo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListModel::requestLogo(QString logo, QString url)
|
|
|
|
{
|
2022-05-08 12:52:50 +05:30
|
|
|
if (m_loadingLogos.contains(logo) || m_failedLogos.contains(logo)) {
|
2020-03-31 06:43:19 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-22 03:51:12 +05:30
|
|
|
MetaEntryPtr entry = APPLICATION->metacache()->resolveEntry("FlamePacks", QString("logos/%1").arg(logo.section(".", 0, 0)));
|
2022-01-18 16:58:55 +05:30
|
|
|
auto job = new NetJob(QString("Flame Icon Download %1").arg(logo), APPLICATION->network());
|
2020-03-31 06:43:19 +05:30
|
|
|
job->addNetAction(Net::Download::makeCached(QUrl(url), entry));
|
|
|
|
|
|
|
|
auto fullPath = entry->getFullPath();
|
2022-05-08 12:52:50 +05:30
|
|
|
QObject::connect(job, &NetJob::succeeded, this, [this, logo, fullPath, job] {
|
2022-01-18 16:58:55 +05:30
|
|
|
job->deleteLater();
|
2020-03-31 06:43:19 +05:30
|
|
|
emit logoLoaded(logo, QIcon(fullPath));
|
2022-05-08 12:52:50 +05:30
|
|
|
if (waitingCallbacks.contains(logo)) {
|
2020-03-31 06:43:19 +05:30
|
|
|
waitingCallbacks.value(logo)(fullPath);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-05-08 12:52:50 +05:30
|
|
|
QObject::connect(job, &NetJob::failed, this, [this, logo, job] {
|
2022-01-18 16:58:55 +05:30
|
|
|
job->deleteLater();
|
2020-03-31 06:43:19 +05:30
|
|
|
emit logoFailed(logo);
|
|
|
|
});
|
|
|
|
|
2021-12-31 09:57:59 +05:30
|
|
|
job->start();
|
2020-03-31 06:43:19 +05:30
|
|
|
|
|
|
|
m_loadingLogos.append(logo);
|
|
|
|
}
|
|
|
|
|
2022-05-08 12:52:50 +05:30
|
|
|
void ListModel::getLogo(const QString& logo, const QString& logoUrl, LogoCallback callback)
|
2020-03-31 06:43:19 +05:30
|
|
|
{
|
2022-05-08 12:52:50 +05:30
|
|
|
if (m_logoMap.contains(logo)) {
|
2021-11-22 03:51:12 +05:30
|
|
|
callback(APPLICATION->metacache()->resolveEntry("FlamePacks", QString("logos/%1").arg(logo.section(".", 0, 0)))->getFullPath());
|
2022-05-08 12:52:50 +05:30
|
|
|
} else {
|
2020-03-31 06:43:19 +05:30
|
|
|
requestLogo(logo, logoUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-08 12:52:50 +05:30
|
|
|
Qt::ItemFlags ListModel::flags(const QModelIndex& index) const
|
2020-03-31 06:43:19 +05:30
|
|
|
{
|
|
|
|
return QAbstractListModel::flags(index);
|
|
|
|
}
|
|
|
|
|
2020-04-01 05:38:11 +05:30
|
|
|
bool ListModel::canFetchMore(const QModelIndex& parent) const
|
2020-03-31 06:43:19 +05:30
|
|
|
{
|
2020-04-01 05:38:11 +05:30
|
|
|
return searchState == CanPossiblyFetchMore;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListModel::fetchMore(const QModelIndex& parent)
|
|
|
|
{
|
|
|
|
if (parent.isValid())
|
|
|
|
return;
|
2022-05-08 12:52:50 +05:30
|
|
|
if (nextSearchOffset == 0) {
|
2020-04-01 05:38:11 +05:30
|
|
|
qWarning() << "fetchMore with 0 offset is wrong...";
|
2020-03-31 06:43:19 +05:30
|
|
|
return;
|
|
|
|
}
|
2020-04-01 05:38:11 +05:30
|
|
|
performPaginatedSearch();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListModel::performPaginatedSearch()
|
|
|
|
{
|
2022-05-08 12:52:50 +05:30
|
|
|
NetJob* netJob = new NetJob("Flame::Search", APPLICATION->network());
|
2020-03-31 06:43:19 +05:30
|
|
|
auto searchUrl = QString(
|
2022-05-08 12:52:50 +05:30
|
|
|
"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);
|
|
|
|
|
2020-03-31 06:43:19 +05:30
|
|
|
netJob->addNetAction(Net::Download::makeByteArray(QUrl(searchUrl), &response));
|
|
|
|
jobPtr = netJob;
|
2021-12-31 09:57:59 +05:30
|
|
|
jobPtr->start();
|
2020-03-31 06:43:19 +05:30
|
|
|
QObject::connect(netJob, &NetJob::succeeded, this, &ListModel::searchRequestFinished);
|
|
|
|
QObject::connect(netJob, &NetJob::failed, this, &ListModel::searchRequestFailed);
|
|
|
|
}
|
|
|
|
|
2021-03-27 03:45:20 +05:30
|
|
|
void ListModel::searchWithTerm(const QString& term, int sort)
|
2020-04-01 05:38:11 +05:30
|
|
|
{
|
2022-05-08 12:52:50 +05:30
|
|
|
if (currentSearchTerm == term && currentSearchTerm.isNull() == term.isNull() && currentSort == sort) {
|
2020-04-01 05:38:11 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
currentSearchTerm = term;
|
2021-03-27 03:45:20 +05:30
|
|
|
currentSort = sort;
|
2022-05-08 12:52:50 +05:30
|
|
|
if (jobPtr) {
|
2020-04-01 05:38:11 +05:30
|
|
|
jobPtr->abort();
|
|
|
|
searchState = ResetRequested;
|
|
|
|
return;
|
2022-05-08 12:52:50 +05:30
|
|
|
} else {
|
2020-04-01 05:38:11 +05:30
|
|
|
beginResetModel();
|
|
|
|
modpacks.clear();
|
|
|
|
endResetModel();
|
|
|
|
searchState = None;
|
|
|
|
}
|
|
|
|
nextSearchOffset = 0;
|
|
|
|
performPaginatedSearch();
|
|
|
|
}
|
|
|
|
|
2021-03-24 05:29:43 +05:30
|
|
|
void Flame::ListModel::searchRequestFinished()
|
2020-03-31 06:43:19 +05:30
|
|
|
{
|
|
|
|
jobPtr.reset();
|
|
|
|
|
|
|
|
QJsonParseError parse_error;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(response, &parse_error);
|
2022-05-08 12:52:50 +05:30
|
|
|
if (parse_error.error != QJsonParseError::NoError) {
|
|
|
|
qWarning() << "Error while parsing JSON response from CurseForge at " << parse_error.offset
|
|
|
|
<< " reason: " << parse_error.errorString();
|
2020-03-31 06:43:19 +05:30
|
|
|
qWarning() << response;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-27 03:45:20 +05:30
|
|
|
QList<Flame::IndexedPack> newList;
|
2022-05-08 12:52:50 +05:30
|
|
|
auto packs = Json::ensureArray(doc.object(), "data");
|
|
|
|
for (auto packRaw : packs) {
|
2021-03-27 03:45:20 +05:30
|
|
|
auto packObj = packRaw.toObject();
|
|
|
|
|
|
|
|
Flame::IndexedPack pack;
|
2022-05-08 12:52:50 +05:30
|
|
|
try {
|
2021-03-27 03:45:20 +05:30
|
|
|
Flame::loadIndexedPack(pack, packObj);
|
|
|
|
newList.append(pack);
|
2022-05-08 12:52:50 +05:30
|
|
|
} catch (const JSONValidationError& e) {
|
2021-03-27 03:45:20 +05:30
|
|
|
qWarning() << "Error while loading pack from CurseForge: " << e.cause();
|
2020-04-01 05:38:11 +05:30
|
|
|
continue;
|
2020-03-31 06:43:19 +05:30
|
|
|
}
|
|
|
|
}
|
2022-05-08 12:52:50 +05:30
|
|
|
if (packs.size() < 25) {
|
2020-04-01 05:38:11 +05:30
|
|
|
searchState = Finished;
|
|
|
|
} else {
|
|
|
|
nextSearchOffset += 25;
|
|
|
|
searchState = CanPossiblyFetchMore;
|
|
|
|
}
|
2022-06-25 04:39:44 +05:30
|
|
|
|
|
|
|
// When you have a Qt build with assertions turned on, proceeding here will abort the application
|
|
|
|
if (newList.size() == 0)
|
|
|
|
return;
|
|
|
|
|
2020-04-01 05:38:11 +05:30
|
|
|
beginInsertRows(QModelIndex(), modpacks.size(), modpacks.size() + newList.size() - 1);
|
|
|
|
modpacks.append(newList);
|
|
|
|
endInsertRows();
|
2020-03-31 06:43:19 +05:30
|
|
|
}
|
|
|
|
|
2021-03-24 05:29:43 +05:30
|
|
|
void Flame::ListModel::searchRequestFailed(QString reason)
|
2020-03-31 06:43:19 +05:30
|
|
|
{
|
|
|
|
jobPtr.reset();
|
2020-04-01 05:38:11 +05:30
|
|
|
|
2022-05-08 12:52:50 +05:30
|
|
|
if (searchState == ResetRequested) {
|
2020-04-01 05:38:11 +05:30
|
|
|
beginResetModel();
|
|
|
|
modpacks.clear();
|
|
|
|
endResetModel();
|
|
|
|
|
|
|
|
nextSearchOffset = 0;
|
|
|
|
performPaginatedSearch();
|
|
|
|
} else {
|
|
|
|
searchState = Finished;
|
|
|
|
}
|
2020-03-31 06:43:19 +05:30
|
|
|
}
|
|
|
|
|
2022-05-08 12:52:50 +05:30
|
|
|
} // namespace Flame
|