2022-03-03 00:17:10 +00:00
|
|
|
#include "ModModel.h"
|
|
|
|
|
2022-03-26 21:21:08 +00:00
|
|
|
#include "BuildConfig.h"
|
2022-03-07 20:46:18 +00:00
|
|
|
#include "Json.h"
|
2022-03-03 02:01:23 +00:00
|
|
|
#include "minecraft/MinecraftInstance.h"
|
|
|
|
#include "minecraft/PackProfile.h"
|
2022-03-03 00:17:10 +00:00
|
|
|
#include "ui/dialogs/ModDownloadDialog.h"
|
|
|
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
|
|
|
namespace ModPlatform {
|
|
|
|
|
|
|
|
ListModel::ListModel(ModPage* parent) : QAbstractListModel(parent), m_parent(parent) {}
|
|
|
|
|
2022-03-08 14:12:35 +00:00
|
|
|
auto ListModel::debugName() const -> QString
|
2022-03-07 22:29:59 +00:00
|
|
|
{
|
|
|
|
return m_parent->debugName();
|
|
|
|
}
|
|
|
|
|
|
|
|
/******** Make data requests ********/
|
|
|
|
|
|
|
|
void ListModel::fetchMore(const QModelIndex& parent)
|
|
|
|
{
|
2022-05-08 08:22:50 +01:00
|
|
|
if (parent.isValid())
|
|
|
|
return;
|
2022-03-07 22:29:59 +00:00
|
|
|
if (nextSearchOffset == 0) {
|
|
|
|
qWarning() << "fetchMore with 0 offset is wrong...";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
performPaginatedSearch();
|
|
|
|
}
|
|
|
|
|
2022-03-08 14:12:35 +00:00
|
|
|
auto ListModel::data(const QModelIndex& index, int role) const -> QVariant
|
2022-03-03 00:17:10 +00:00
|
|
|
{
|
|
|
|
int pos = index.row();
|
2022-05-08 08:22:50 +01:00
|
|
|
if (pos >= modpacks.size() || pos < 0 || !index.isValid()) {
|
|
|
|
return QString("INVALID INDEX %1").arg(pos);
|
|
|
|
}
|
2022-03-03 00:17:10 +00:00
|
|
|
|
|
|
|
ModPlatform::IndexedPack pack = modpacks.at(pos);
|
2022-04-29 00:14:03 +01:00
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole: {
|
|
|
|
return pack.name;
|
2022-03-03 00:17:10 +00:00
|
|
|
}
|
2022-04-29 00:14:03 +01:00
|
|
|
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;
|
2022-05-08 08:22:50 +01:00
|
|
|
}
|
2022-04-29 00:14:03 +01:00
|
|
|
case Qt::DecorationRole: {
|
|
|
|
if (m_logoMap.contains(pack.logoName)) {
|
|
|
|
return (m_logoMap.value(pack.logoName));
|
|
|
|
}
|
|
|
|
QIcon icon = APPLICATION->getThemedIcon("screenshot-placeholder");
|
|
|
|
// un-const-ify this
|
|
|
|
((ListModel*)this)->requestLogo(pack.logoName, pack.logoUrl);
|
|
|
|
return icon;
|
|
|
|
}
|
|
|
|
case Qt::UserRole: {
|
|
|
|
QVariant v;
|
|
|
|
v.setValue(pack);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
case Qt::FontRole: {
|
|
|
|
QFont font;
|
|
|
|
if (m_parent->getDialog()->isModSelected(pack.name)) {
|
|
|
|
font.setBold(true);
|
|
|
|
font.setUnderline(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
2022-03-03 00:17:10 +00:00
|
|
|
}
|
|
|
|
|
2022-03-08 14:12:35 +00:00
|
|
|
return {};
|
2022-03-03 00:17:10 +00:00
|
|
|
}
|
|
|
|
|
2022-03-07 19:46:08 +00:00
|
|
|
void ListModel::requestModVersions(ModPlatform::IndexedPack const& current)
|
|
|
|
{
|
2022-02-20 19:55:26 +00:00
|
|
|
auto profile = (dynamic_cast<MinecraftInstance*>((dynamic_cast<ModPage*>(parent()))->m_instance))->getPackProfile();
|
|
|
|
|
2022-05-19 07:40:28 +01:00
|
|
|
m_parent->apiProvider()->getVersions(this, { current.addonId.toString(), getMineVersions(), profile->getModLoaders() });
|
2022-03-06 18:23:00 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 02:01:23 +00:00
|
|
|
void ListModel::performPaginatedSearch()
|
|
|
|
{
|
2022-02-20 19:55:26 +00:00
|
|
|
auto profile = (dynamic_cast<MinecraftInstance*>((dynamic_cast<ModPage*>(parent()))->m_instance))->getPackProfile();
|
|
|
|
|
2022-05-08 08:22:50 +01:00
|
|
|
m_parent->apiProvider()->searchMods(
|
2022-05-19 07:40:28 +01:00
|
|
|
this, { nextSearchOffset, currentSearchTerm, getSorts()[currentSort], profile->getModLoaders(), getMineVersions() });
|
2022-03-03 02:01:23 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 23:21:02 +01:00
|
|
|
void ListModel::refresh()
|
2022-03-03 00:17:10 +00:00
|
|
|
{
|
|
|
|
if (jobPtr) {
|
|
|
|
jobPtr->abort();
|
|
|
|
searchState = ResetRequested;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
beginResetModel();
|
|
|
|
modpacks.clear();
|
|
|
|
endResetModel();
|
|
|
|
searchState = None;
|
|
|
|
}
|
|
|
|
nextSearchOffset = 0;
|
|
|
|
performPaginatedSearch();
|
|
|
|
}
|
|
|
|
|
2022-04-14 14:27:03 +01:00
|
|
|
void ListModel::searchWithTerm(const QString& term, const int sort, const bool filter_changed)
|
2022-04-02 23:21:02 +01:00
|
|
|
{
|
2022-05-08 08:22:50 +01:00
|
|
|
if (currentSearchTerm == term && currentSearchTerm.isNull() == term.isNull() && currentSort == sort && !filter_changed) {
|
|
|
|
return;
|
|
|
|
}
|
2022-04-14 14:27:03 +01:00
|
|
|
|
2022-04-02 23:21:02 +01:00
|
|
|
currentSearchTerm = term;
|
|
|
|
currentSort = sort;
|
|
|
|
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
2022-03-07 22:29:59 +00:00
|
|
|
void ListModel::getLogo(const QString& logo, const QString& logoUrl, LogoCallback callback)
|
|
|
|
{
|
|
|
|
if (m_logoMap.contains(logo)) {
|
|
|
|
callback(APPLICATION->metacache()
|
|
|
|
->resolveEntry(m_parent->metaEntryBase(), QString("logos/%1").arg(logo.section(".", 0, 0)))
|
|
|
|
->getFullPath());
|
|
|
|
} else {
|
|
|
|
requestLogo(logo, logoUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListModel::requestLogo(QString logo, QString url)
|
|
|
|
{
|
2022-05-08 08:22:50 +01:00
|
|
|
if (m_loadingLogos.contains(logo) || m_failedLogos.contains(logo)) {
|
|
|
|
return;
|
|
|
|
}
|
2022-03-07 22:29:59 +00:00
|
|
|
|
|
|
|
MetaEntryPtr entry =
|
|
|
|
APPLICATION->metacache()->resolveEntry(m_parent->metaEntryBase(), QString("logos/%1").arg(logo.section(".", 0, 0)));
|
|
|
|
auto job = new NetJob(QString("%1 Icon Download %2").arg(m_parent->debugName()).arg(logo), APPLICATION->network());
|
|
|
|
job->addNetAction(Net::Download::makeCached(QUrl(url), entry));
|
|
|
|
|
|
|
|
auto fullPath = entry->getFullPath();
|
|
|
|
QObject::connect(job, &NetJob::succeeded, this, [this, logo, fullPath, job] {
|
|
|
|
job->deleteLater();
|
|
|
|
emit logoLoaded(logo, QIcon(fullPath));
|
2022-05-08 08:22:50 +01:00
|
|
|
if (waitingCallbacks.contains(logo)) {
|
|
|
|
waitingCallbacks.value(logo)(fullPath);
|
|
|
|
}
|
2022-03-07 22:29:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
QObject::connect(job, &NetJob::failed, this, [this, logo, job] {
|
|
|
|
job->deleteLater();
|
|
|
|
emit logoFailed(logo);
|
|
|
|
});
|
|
|
|
|
|
|
|
job->start();
|
|
|
|
m_loadingLogos.append(logo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******** Request callbacks ********/
|
|
|
|
|
|
|
|
void ListModel::logoLoaded(QString logo, QIcon out)
|
|
|
|
{
|
|
|
|
m_loadingLogos.removeAll(logo);
|
|
|
|
m_logoMap.insert(logo, out);
|
|
|
|
for (int i = 0; i < modpacks.size(); i++) {
|
2022-05-08 08:22:50 +01:00
|
|
|
if (modpacks[i].logoName == logo) {
|
|
|
|
emit dataChanged(createIndex(i, 0), createIndex(i, 0), { Qt::DecorationRole });
|
|
|
|
}
|
2022-03-07 22:29:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListModel::logoFailed(QString logo)
|
|
|
|
{
|
|
|
|
m_failedLogos.append(logo);
|
|
|
|
m_loadingLogos.removeAll(logo);
|
|
|
|
}
|
|
|
|
|
2022-03-07 20:46:18 +00:00
|
|
|
void ListModel::searchRequestFinished(QJsonDocument& doc)
|
|
|
|
{
|
|
|
|
jobPtr.reset();
|
|
|
|
|
|
|
|
QList<ModPlatform::IndexedPack> newList;
|
|
|
|
auto packs = documentToArray(doc);
|
|
|
|
|
|
|
|
for (auto packRaw : packs) {
|
|
|
|
auto packObj = packRaw.toObject();
|
|
|
|
|
|
|
|
ModPlatform::IndexedPack pack;
|
|
|
|
try {
|
|
|
|
loadIndexedPack(pack, packObj);
|
|
|
|
newList.append(pack);
|
|
|
|
} catch (const JSONValidationError& e) {
|
|
|
|
qWarning() << "Error while loading mod from " << m_parent->debugName() << ": " << e.cause();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (packs.size() < 25) {
|
|
|
|
searchState = Finished;
|
|
|
|
} else {
|
|
|
|
nextSearchOffset += 25;
|
|
|
|
searchState = CanPossiblyFetchMore;
|
|
|
|
}
|
|
|
|
|
|
|
|
beginInsertRows(QModelIndex(), modpacks.size(), modpacks.size() + newList.size() - 1);
|
|
|
|
modpacks.append(newList);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2022-03-03 00:17:10 +00:00
|
|
|
void ListModel::searchRequestFailed(QString reason)
|
|
|
|
{
|
2022-04-18 13:36:36 +01:00
|
|
|
if (!jobPtr->first()->m_reply) {
|
|
|
|
// Network error
|
|
|
|
QMessageBox::critical(nullptr, tr("Error"), tr("A network error occurred. Could not load mods."));
|
|
|
|
} else if (jobPtr->first()->m_reply && jobPtr->first()->m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 409) {
|
2022-03-03 00:17:10 +00:00
|
|
|
// 409 Gone, notify user to update
|
|
|
|
QMessageBox::critical(nullptr, tr("Error"),
|
2022-03-26 21:21:08 +00:00
|
|
|
//: %1 refers to the launcher itself
|
2022-05-08 08:22:50 +01:00
|
|
|
QString("%1 %2")
|
|
|
|
.arg(m_parent->displayName())
|
|
|
|
.arg(tr("API version too old!\nPlease update %1!").arg(BuildConfig.LAUNCHER_NAME)));
|
2022-03-03 00:17:10 +00:00
|
|
|
}
|
|
|
|
jobPtr.reset();
|
|
|
|
|
|
|
|
if (searchState == ResetRequested) {
|
|
|
|
beginResetModel();
|
|
|
|
modpacks.clear();
|
|
|
|
endResetModel();
|
|
|
|
|
|
|
|
nextSearchOffset = 0;
|
|
|
|
performPaginatedSearch();
|
|
|
|
} else {
|
|
|
|
searchState = Finished;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-07 19:22:57 +00:00
|
|
|
void ListModel::versionRequestSucceeded(QJsonDocument doc, QString addonId)
|
|
|
|
{
|
2022-03-07 22:29:59 +00:00
|
|
|
auto& current = m_parent->getCurrent();
|
2022-05-08 08:22:50 +01:00
|
|
|
if (addonId != current.addonId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto arr = doc.isObject() ? Json::ensureArray(doc.object(), "data") : doc.array();
|
2022-03-24 21:39:53 +00:00
|
|
|
|
2022-03-07 22:29:59 +00:00
|
|
|
try {
|
|
|
|
loadIndexedPackVersions(current, arr);
|
|
|
|
} catch (const JSONValidationError& e) {
|
|
|
|
qDebug() << doc;
|
|
|
|
qWarning() << "Error while reading " << debugName() << " mod version: " << e.cause();
|
|
|
|
}
|
2022-03-03 00:17:10 +00:00
|
|
|
|
2022-03-07 22:29:59 +00:00
|
|
|
m_parent->updateModVersions();
|
2022-03-03 00:17:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ModPlatform
|
2022-03-24 21:39:53 +00:00
|
|
|
|
|
|
|
/******** Helpers ********/
|
|
|
|
|
2022-04-02 22:34:26 +01:00
|
|
|
auto ModPlatform::ListModel::getMineVersions() const -> std::list<Version>
|
2022-03-24 21:39:53 +00:00
|
|
|
{
|
2022-04-02 22:34:26 +01:00
|
|
|
return m_parent->getFilter()->versions;
|
2022-03-24 21:39:53 +00:00
|
|
|
}
|