2022-05-15 12:20:05 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
2022-05-15 12:25:58 +01:00
|
|
|
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
|
2022-06-12 12:50:58 +01:00
|
|
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
2022-05-15 12:20:05 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following copyright and
|
|
|
|
* permission notice:
|
|
|
|
*
|
|
|
|
* Copyright 2013-2021 MultiMC Contributors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-05-15 00:17:05 +01:00
|
|
|
#include "ModrinthModel.h"
|
|
|
|
|
|
|
|
#include "BuildConfig.h"
|
|
|
|
#include "Json.h"
|
|
|
|
#include "minecraft/MinecraftInstance.h"
|
|
|
|
#include "minecraft/PackProfile.h"
|
|
|
|
#include "ui/dialogs/ModDownloadDialog.h"
|
2022-07-29 03:00:00 +01:00
|
|
|
#include "ui/widgets/ProjectItem.h"
|
2022-05-15 00:17:05 +01:00
|
|
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
|
|
|
namespace Modrinth {
|
|
|
|
|
|
|
|
ModpackListModel::ModpackListModel(ModrinthPage* parent) : QAbstractListModel(parent), m_parent(parent) {}
|
|
|
|
|
|
|
|
auto ModpackListModel::debugName() const -> QString
|
|
|
|
{
|
|
|
|
return m_parent->debugName();
|
|
|
|
}
|
|
|
|
|
|
|
|
/******** Make data requests ********/
|
|
|
|
|
|
|
|
void ModpackListModel::fetchMore(const QModelIndex& parent)
|
|
|
|
{
|
|
|
|
if (parent.isValid())
|
|
|
|
return;
|
|
|
|
if (nextSearchOffset == 0) {
|
|
|
|
qWarning() << "fetchMore with 0 offset is wrong...";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
performPaginatedSearch();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ModpackListModel::data(const QModelIndex& index, int role) const -> QVariant
|
|
|
|
{
|
|
|
|
int pos = index.row();
|
|
|
|
if (pos >= modpacks.size() || pos < 0 || !index.isValid()) {
|
|
|
|
return QString("INVALID INDEX %1").arg(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
Modrinth::Modpack pack = modpacks.at(pos);
|
2022-07-29 03:00:00 +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;
|
2022-05-15 00:17:05 +01:00
|
|
|
}
|
2022-07-29 03:00:00 +01:00
|
|
|
case Qt::DecorationRole: {
|
|
|
|
if (m_logoMap.contains(pack.iconName))
|
|
|
|
return m_logoMap.value(pack.iconName);
|
|
|
|
|
|
|
|
QIcon icon = APPLICATION->getThemedIcon("screenshot-placeholder");
|
|
|
|
((ModpackListModel*)this)->requestLogo(pack.iconName, pack.iconUrl.toString());
|
|
|
|
return icon;
|
|
|
|
}
|
|
|
|
case Qt::UserRole: {
|
|
|
|
QVariant v;
|
|
|
|
v.setValue(pack);
|
|
|
|
return v;
|
2022-05-15 00:17:05 +01:00
|
|
|
}
|
2022-07-29 03:00:00 +01:00
|
|
|
case Qt::SizeHintRole:
|
|
|
|
return QSize(0, 58);
|
|
|
|
// Custom data
|
|
|
|
case UserDataTypes::TITLE:
|
|
|
|
return pack.name;
|
|
|
|
case UserDataTypes::DESCRIPTION:
|
|
|
|
return pack.description;
|
|
|
|
case UserDataTypes::SELECTED:
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
break;
|
2022-05-15 00:17:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-06-24 13:02:58 +01:00
|
|
|
bool ModpackListModel::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<Modrinth::Modpack>();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-05-15 00:17:05 +01:00
|
|
|
void ModpackListModel::performPaginatedSearch()
|
|
|
|
{
|
|
|
|
// TODO: Move to standalone API
|
|
|
|
NetJob* netJob = new NetJob("Modrinth::SearchModpack", APPLICATION->network());
|
2022-05-16 01:50:42 +01:00
|
|
|
auto searchAllUrl = QString(BuildConfig.MODRINTH_PROD_URL +
|
2022-05-16 00:45:27 +01:00
|
|
|
"/search?"
|
|
|
|
"offset=%1&"
|
2022-05-16 23:17:37 +01:00
|
|
|
"limit=%2&"
|
|
|
|
"query=%3&"
|
|
|
|
"index=%4&"
|
2022-05-15 00:17:05 +01:00
|
|
|
"facets=[[\"project_type:modpack\"]]")
|
2022-05-16 00:45:27 +01:00
|
|
|
.arg(nextSearchOffset)
|
2022-05-16 23:17:37 +01:00
|
|
|
.arg(m_modpacks_per_page)
|
2022-05-16 00:45:27 +01:00
|
|
|
.arg(currentSearchTerm)
|
|
|
|
.arg(currentSort);
|
2022-05-15 00:17:05 +01:00
|
|
|
|
|
|
|
netJob->addNetAction(Net::Download::makeByteArray(QUrl(searchAllUrl), &m_all_response));
|
|
|
|
|
|
|
|
QObject::connect(netJob, &NetJob::succeeded, this, [this] {
|
2022-05-15 01:29:48 +01:00
|
|
|
QJsonParseError parse_error_all{};
|
2022-05-15 00:17:05 +01:00
|
|
|
|
|
|
|
QJsonDocument doc_all = QJsonDocument::fromJson(m_all_response, &parse_error_all);
|
|
|
|
if (parse_error_all.error != QJsonParseError::NoError) {
|
|
|
|
qWarning() << "Error while parsing JSON response from " << debugName() << " at " << parse_error_all.offset
|
|
|
|
<< " reason: " << parse_error_all.errorString();
|
|
|
|
qWarning() << m_all_response;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
searchRequestFinished(doc_all);
|
|
|
|
});
|
|
|
|
QObject::connect(netJob, &NetJob::failed, this, &ModpackListModel::searchRequestFailed);
|
|
|
|
|
|
|
|
jobPtr = netJob;
|
|
|
|
jobPtr->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModpackListModel::refresh()
|
|
|
|
{
|
|
|
|
if (jobPtr) {
|
|
|
|
jobPtr->abort();
|
|
|
|
searchState = ResetRequested;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
beginResetModel();
|
|
|
|
modpacks.clear();
|
|
|
|
endResetModel();
|
|
|
|
searchState = None;
|
|
|
|
}
|
|
|
|
nextSearchOffset = 0;
|
|
|
|
performPaginatedSearch();
|
|
|
|
}
|
|
|
|
|
2022-05-16 00:59:07 +01:00
|
|
|
static auto sortFromIndex(int index) -> QString
|
|
|
|
{
|
|
|
|
switch(index){
|
|
|
|
default:
|
2022-06-24 09:47:02 +01:00
|
|
|
case 0:
|
2022-05-16 00:59:07 +01:00
|
|
|
return "relevance";
|
2022-06-24 09:47:02 +01:00
|
|
|
case 1:
|
2022-05-16 00:59:07 +01:00
|
|
|
return "downloads";
|
2022-06-24 09:47:02 +01:00
|
|
|
case 2:
|
2022-05-16 00:59:07 +01:00
|
|
|
return "follows";
|
2022-06-24 09:47:02 +01:00
|
|
|
case 3:
|
2022-05-16 00:59:07 +01:00
|
|
|
return "newest";
|
2022-06-24 09:47:02 +01:00
|
|
|
case 4:
|
2022-05-16 00:59:07 +01:00
|
|
|
return "updated";
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
2022-05-16 00:45:27 +01:00
|
|
|
|
2022-05-15 00:17:05 +01:00
|
|
|
void ModpackListModel::searchWithTerm(const QString& term, const int sort)
|
|
|
|
{
|
2022-05-16 00:45:27 +01:00
|
|
|
if(sort > 5 || sort < 0)
|
|
|
|
return;
|
|
|
|
|
2022-05-16 00:59:07 +01:00
|
|
|
auto sort_str = sortFromIndex(sort);
|
2022-05-16 00:45:27 +01:00
|
|
|
|
|
|
|
if (currentSearchTerm == term && currentSearchTerm.isNull() == term.isNull() && currentSort == sort_str) {
|
2022-05-15 00:17:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentSearchTerm = term;
|
2022-05-16 00:45:27 +01:00
|
|
|
currentSort = sort_str;
|
2022-05-15 00:17:05 +01:00
|
|
|
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModpackListModel::getLogo(const QString& logo, const QString& logoUrl, LogoCallback callback)
|
|
|
|
{
|
|
|
|
if (m_logoMap.contains(logo)) {
|
|
|
|
callback(APPLICATION->metacache()
|
2022-05-16 23:10:31 +01:00
|
|
|
->resolveEntry("ModrinthPacks", QString("logos/%1").arg(logo.section(".", 0, 0)))
|
2022-05-15 00:17:05 +01:00
|
|
|
->getFullPath());
|
|
|
|
} else {
|
|
|
|
requestLogo(logo, logoUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModpackListModel::requestLogo(QString logo, QString url)
|
|
|
|
{
|
2022-09-23 22:05:58 +01:00
|
|
|
if (m_loadingLogos.contains(logo) || m_failedLogos.contains(logo) || url.isEmpty()) {
|
2022-05-15 00:17:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MetaEntryPtr entry =
|
2022-05-16 19:17:19 +01:00
|
|
|
APPLICATION->metacache()->resolveEntry("ModrinthPacks", QString("logos/%1").arg(logo.section(".", 0, 0)));
|
2022-05-15 00:17:05 +01:00
|
|
|
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));
|
|
|
|
if (waitingCallbacks.contains(logo)) {
|
|
|
|
waitingCallbacks.value(logo)(fullPath);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
QObject::connect(job, &NetJob::failed, this, [this, logo, job] {
|
|
|
|
job->deleteLater();
|
|
|
|
emit logoFailed(logo);
|
|
|
|
});
|
|
|
|
|
|
|
|
job->start();
|
|
|
|
m_loadingLogos.append(logo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******** Request callbacks ********/
|
|
|
|
|
|
|
|
void ModpackListModel::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].iconName == logo) {
|
|
|
|
emit dataChanged(createIndex(i, 0), createIndex(i, 0), { Qt::DecorationRole });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModpackListModel::logoFailed(QString logo)
|
|
|
|
{
|
|
|
|
m_failedLogos.append(logo);
|
|
|
|
m_loadingLogos.removeAll(logo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModpackListModel::searchRequestFinished(QJsonDocument& doc_all)
|
|
|
|
{
|
|
|
|
jobPtr.reset();
|
|
|
|
|
|
|
|
QList<Modrinth::Modpack> newList;
|
|
|
|
|
|
|
|
auto packs_all = doc_all.object().value("hits").toArray();
|
|
|
|
for (auto packRaw : packs_all) {
|
|
|
|
auto packObj = packRaw.toObject();
|
|
|
|
|
|
|
|
Modrinth::Modpack pack;
|
|
|
|
try {
|
|
|
|
Modrinth::loadIndexedPack(pack, packObj);
|
|
|
|
newList.append(pack);
|
|
|
|
} catch (const JSONValidationError& e) {
|
|
|
|
qWarning() << "Error while loading mod from " << m_parent->debugName() << ": " << e.cause();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2022-05-15 01:29:48 +01:00
|
|
|
|
2022-05-16 23:17:37 +01:00
|
|
|
if (packs_all.size() < m_modpacks_per_page) {
|
2022-05-15 00:17:05 +01:00
|
|
|
searchState = Finished;
|
|
|
|
} else {
|
2022-05-16 23:17:37 +01:00
|
|
|
nextSearchOffset += m_modpacks_per_page;
|
2022-05-15 00:17:05 +01:00
|
|
|
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;
|
|
|
|
|
2022-05-15 00:17:05 +01:00
|
|
|
beginInsertRows(QModelIndex(), modpacks.size(), modpacks.size() + newList.size() - 1);
|
|
|
|
modpacks.append(newList);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModpackListModel::searchRequestFailed(QString reason)
|
|
|
|
{
|
2022-07-22 04:30:27 +01:00
|
|
|
auto failed_action = jobPtr->getFailedActions().at(0);
|
|
|
|
if (!failed_action->m_reply) {
|
2022-05-15 00:17:05 +01:00
|
|
|
// Network error
|
2022-05-16 00:45:27 +01:00
|
|
|
QMessageBox::critical(nullptr, tr("Error"), tr("A network error occurred. Could not load modpacks."));
|
2022-07-22 04:30:27 +01:00
|
|
|
} else if (failed_action->m_reply && failed_action->m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 409) {
|
2022-05-15 00:17:05 +01:00
|
|
|
// 409 Gone, notify user to update
|
|
|
|
QMessageBox::critical(nullptr, tr("Error"),
|
|
|
|
//: %1 refers to the launcher itself
|
|
|
|
QString("%1 %2")
|
|
|
|
.arg(m_parent->displayName())
|
2022-10-18 08:50:27 +01:00
|
|
|
.arg(tr("API version too old!\nPlease update %1!").arg(BuildConfig.LAUNCHER_DISPLAYNAME)));
|
2022-05-15 00:17:05 +01:00
|
|
|
}
|
|
|
|
jobPtr.reset();
|
|
|
|
|
|
|
|
if (searchState == ResetRequested) {
|
|
|
|
beginResetModel();
|
|
|
|
modpacks.clear();
|
|
|
|
endResetModel();
|
|
|
|
|
|
|
|
nextSearchOffset = 0;
|
|
|
|
performPaginatedSearch();
|
|
|
|
} else {
|
|
|
|
searchState = Finished;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Modrinth
|
|
|
|
|
|
|
|
/******** Helpers ********/
|