2022-05-15 11:20:05 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
2022-05-15 11:25:58 +00:00
|
|
|
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
|
2022-06-12 11:50:58 +00:00
|
|
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
2022-05-15 11:20:05 +00: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-01-14 08:43:42 +00:00
|
|
|
#include "ModrinthModel.h"
|
2022-05-14 23:17:05 +00:00
|
|
|
|
|
|
|
#include "BuildConfig.h"
|
|
|
|
#include "Json.h"
|
2022-01-15 07:51:47 +00:00
|
|
|
#include "minecraft/MinecraftInstance.h"
|
|
|
|
#include "minecraft/PackProfile.h"
|
2022-02-04 15:24:19 +00:00
|
|
|
#include "ui/dialogs/ModDownloadDialog.h"
|
2022-01-14 08:43:42 +00:00
|
|
|
|
2022-02-04 15:24:19 +00:00
|
|
|
#include <QMessageBox>
|
2022-01-14 08:43:42 +00:00
|
|
|
|
|
|
|
namespace Modrinth {
|
|
|
|
|
2022-05-14 23:17:05 +00:00
|
|
|
ModpackListModel::ModpackListModel(ModrinthPage* parent) : QAbstractListModel(parent), m_parent(parent) {}
|
2022-01-14 08:43:42 +00:00
|
|
|
|
2022-05-14 23:17:05 +00:00
|
|
|
auto ModpackListModel::debugName() const -> QString
|
2022-01-14 08:43:42 +00:00
|
|
|
{
|
2022-05-14 23:17:05 +00:00
|
|
|
return m_parent->debugName();
|
2022-01-14 08:43:42 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 23:17:05 +00:00
|
|
|
/******** Make data requests ********/
|
2022-01-14 08:43:42 +00:00
|
|
|
|
2022-05-14 23:17:05 +00:00
|
|
|
void ModpackListModel::fetchMore(const QModelIndex& parent)
|
2022-01-14 08:43:42 +00:00
|
|
|
{
|
2022-05-14 23:17:05 +00:00
|
|
|
if (parent.isValid())
|
|
|
|
return;
|
|
|
|
if (nextSearchOffset == 0) {
|
|
|
|
qWarning() << "fetchMore with 0 offset is wrong...";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
performPaginatedSearch();
|
2022-01-14 08:43:42 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 23:17:05 +00:00
|
|
|
auto ModpackListModel::data(const QModelIndex& index, int role) const -> QVariant
|
2022-01-14 08:43:42 +00:00
|
|
|
{
|
|
|
|
int pos = index.row();
|
2022-05-14 23:17:05 +00:00
|
|
|
if (pos >= modpacks.size() || pos < 0 || !index.isValid()) {
|
2022-01-14 08:43:42 +00:00
|
|
|
return QString("INVALID INDEX %1").arg(pos);
|
|
|
|
}
|
|
|
|
|
2022-05-14 23:17:05 +00:00
|
|
|
Modrinth::Modpack pack = modpacks.at(pos);
|
|
|
|
if (role == Qt::DisplayRole) {
|
2022-01-14 08:43:42 +00:00
|
|
|
return pack.name;
|
2022-05-14 23:17:05 +00:00
|
|
|
} else if (role == Qt::ToolTipRole) {
|
|
|
|
if (pack.description.length() > 100) {
|
|
|
|
// some magic to prevent to long tooltips and replace html linebreaks
|
2022-01-14 08:43:42 +00:00
|
|
|
QString edit = pack.description.left(97);
|
|
|
|
edit = edit.left(edit.lastIndexOf("<br>")).left(edit.lastIndexOf(" ")).append("...");
|
|
|
|
return edit;
|
|
|
|
}
|
|
|
|
return pack.description;
|
2022-05-14 23:17:05 +00:00
|
|
|
} else if (role == Qt::DecorationRole) {
|
|
|
|
if (m_logoMap.contains(pack.iconName)) {
|
2022-05-16 15:59:32 +00:00
|
|
|
auto icon = m_logoMap.value(pack.iconName);
|
|
|
|
auto icon_scaled = QIcon(icon.pixmap(48, 48).scaledToWidth(48));
|
|
|
|
|
|
|
|
return icon_scaled;
|
2022-01-14 08:43:42 +00:00
|
|
|
}
|
|
|
|
QIcon icon = APPLICATION->getThemedIcon("screenshot-placeholder");
|
2022-05-14 23:17:05 +00:00
|
|
|
((ModpackListModel*)this)->requestLogo(pack.iconName, pack.iconUrl.toString());
|
2022-01-14 08:43:42 +00:00
|
|
|
return icon;
|
2022-05-14 23:17:05 +00:00
|
|
|
} else if (role == Qt::UserRole) {
|
2022-01-14 08:43:42 +00:00
|
|
|
QVariant v;
|
|
|
|
v.setValue(pack);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2022-05-14 23:17:05 +00:00
|
|
|
return {};
|
2022-01-14 08:43:42 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 23:17:05 +00:00
|
|
|
void ModpackListModel::performPaginatedSearch()
|
2022-01-14 08:43:42 +00:00
|
|
|
{
|
2022-05-14 23:17:05 +00:00
|
|
|
// TODO: Move to standalone API
|
|
|
|
NetJob* netJob = new NetJob("Modrinth::SearchModpack", APPLICATION->network());
|
2022-05-16 00:50:42 +00:00
|
|
|
auto searchAllUrl = QString(BuildConfig.MODRINTH_PROD_URL +
|
2022-05-15 23:45:27 +00:00
|
|
|
"/search?"
|
|
|
|
"offset=%1&"
|
2022-05-16 22:17:37 +00:00
|
|
|
"limit=%2&"
|
|
|
|
"query=%3&"
|
|
|
|
"index=%4&"
|
2022-05-14 23:17:05 +00:00
|
|
|
"facets=[[\"project_type:modpack\"]]")
|
2022-05-15 23:45:27 +00:00
|
|
|
.arg(nextSearchOffset)
|
2022-05-16 22:17:37 +00:00
|
|
|
.arg(m_modpacks_per_page)
|
2022-05-15 23:45:27 +00:00
|
|
|
.arg(currentSearchTerm)
|
|
|
|
.arg(currentSort);
|
2022-05-14 23:17:05 +00:00
|
|
|
|
|
|
|
netJob->addNetAction(Net::Download::makeByteArray(QUrl(searchAllUrl), &m_all_response));
|
|
|
|
|
|
|
|
QObject::connect(netJob, &NetJob::succeeded, this, [this] {
|
2022-05-15 00:29:48 +00:00
|
|
|
QJsonParseError parse_error_all{};
|
2022-05-14 23:17:05 +00: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;
|
2022-01-14 08:43:42 +00:00
|
|
|
}
|
2022-05-14 23:17:05 +00:00
|
|
|
|
|
|
|
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;
|
2022-01-14 08:43:42 +00:00
|
|
|
}
|
2022-05-14 23:17:05 +00:00
|
|
|
nextSearchOffset = 0;
|
|
|
|
performPaginatedSearch();
|
2022-01-14 08:43:42 +00:00
|
|
|
}
|
|
|
|
|
2022-05-15 23:59:07 +00:00
|
|
|
static auto sortFromIndex(int index) -> QString
|
2022-01-14 08:43:42 +00:00
|
|
|
{
|
2022-05-15 23:59:07 +00:00
|
|
|
switch(index){
|
|
|
|
default:
|
|
|
|
case 1:
|
|
|
|
return "relevance";
|
|
|
|
case 2:
|
|
|
|
return "downloads";
|
|
|
|
case 3:
|
|
|
|
return "follows";
|
|
|
|
case 4:
|
|
|
|
return "newest";
|
|
|
|
case 5:
|
|
|
|
return "updated";
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
2022-01-14 08:43:42 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 23:17:05 +00:00
|
|
|
void ModpackListModel::searchWithTerm(const QString& term, const int sort)
|
2022-01-14 08:43:42 +00:00
|
|
|
{
|
2022-05-15 23:45:27 +00:00
|
|
|
if(sort > 5 || sort < 0)
|
|
|
|
return;
|
|
|
|
|
2022-05-15 23:59:07 +00:00
|
|
|
auto sort_str = sortFromIndex(sort);
|
2022-05-15 23:45:27 +00:00
|
|
|
|
|
|
|
if (currentSearchTerm == term && currentSearchTerm.isNull() == term.isNull() && currentSort == sort_str) {
|
2022-01-14 08:43:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-14 23:17:05 +00:00
|
|
|
currentSearchTerm = term;
|
2022-05-15 23:45:27 +00:00
|
|
|
currentSort = sort_str;
|
2022-05-14 23:17:05 +00:00
|
|
|
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModpackListModel::getLogo(const QString& logo, const QString& logoUrl, LogoCallback callback)
|
|
|
|
{
|
|
|
|
if (m_logoMap.contains(logo)) {
|
|
|
|
callback(APPLICATION->metacache()
|
2022-05-16 22:10:31 +00:00
|
|
|
->resolveEntry("ModrinthPacks", QString("logos/%1").arg(logo.section(".", 0, 0)))
|
2022-05-14 23:17:05 +00:00
|
|
|
->getFullPath());
|
|
|
|
} else {
|
|
|
|
requestLogo(logo, logoUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModpackListModel::requestLogo(QString logo, QString url)
|
|
|
|
{
|
|
|
|
if (m_loadingLogos.contains(logo) || m_failedLogos.contains(logo)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MetaEntryPtr entry =
|
2022-05-16 18:17:19 +00:00
|
|
|
APPLICATION->metacache()->resolveEntry("ModrinthPacks", QString("logos/%1").arg(logo.section(".", 0, 0)));
|
2022-05-14 23:17:05 +00:00
|
|
|
auto job = new NetJob(QString("%1 Icon Download %2").arg(m_parent->debugName()).arg(logo), APPLICATION->network());
|
2022-01-14 08:43:42 +00:00
|
|
|
job->addNetAction(Net::Download::makeCached(QUrl(url), entry));
|
|
|
|
|
|
|
|
auto fullPath = entry->getFullPath();
|
2022-05-14 23:17:05 +00:00
|
|
|
QObject::connect(job, &NetJob::succeeded, this, [this, logo, fullPath, job] {
|
2022-01-18 11:28:55 +00:00
|
|
|
job->deleteLater();
|
2022-01-14 08:43:42 +00:00
|
|
|
emit logoLoaded(logo, QIcon(fullPath));
|
2022-05-14 23:17:05 +00:00
|
|
|
if (waitingCallbacks.contains(logo)) {
|
2022-01-14 08:43:42 +00:00
|
|
|
waitingCallbacks.value(logo)(fullPath);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-05-14 23:17:05 +00:00
|
|
|
QObject::connect(job, &NetJob::failed, this, [this, logo, job] {
|
2022-01-18 11:28:55 +00:00
|
|
|
job->deleteLater();
|
2022-01-14 08:43:42 +00:00
|
|
|
emit logoFailed(logo);
|
|
|
|
});
|
|
|
|
|
|
|
|
job->start();
|
|
|
|
m_loadingLogos.append(logo);
|
|
|
|
}
|
|
|
|
|
2022-05-14 23:17:05 +00:00
|
|
|
/******** Request callbacks ********/
|
2022-01-14 08:43:42 +00:00
|
|
|
|
2022-05-14 23:17:05 +00:00
|
|
|
void ModpackListModel::logoLoaded(QString logo, QIcon out)
|
2022-01-14 08:43:42 +00:00
|
|
|
{
|
2022-05-14 23:17:05 +00:00
|
|
|
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 });
|
|
|
|
}
|
2022-01-14 08:43:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 23:17:05 +00:00
|
|
|
void ModpackListModel::logoFailed(QString logo)
|
2022-01-14 08:43:42 +00:00
|
|
|
{
|
2022-05-14 23:17:05 +00:00
|
|
|
m_failedLogos.append(logo);
|
|
|
|
m_loadingLogos.removeAll(logo);
|
2022-01-14 08:43:42 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 23:17:05 +00:00
|
|
|
void ModpackListModel::searchRequestFinished(QJsonDocument& doc_all)
|
2022-01-14 08:43:42 +00:00
|
|
|
{
|
|
|
|
jobPtr.reset();
|
|
|
|
|
2022-05-14 23:17:05 +00:00
|
|
|
QList<Modrinth::Modpack> newList;
|
2022-01-14 08:43:42 +00:00
|
|
|
|
2022-05-14 23:17:05 +00:00
|
|
|
auto packs_all = doc_all.object().value("hits").toArray();
|
|
|
|
for (auto packRaw : packs_all) {
|
2022-01-14 08:43:42 +00:00
|
|
|
auto packObj = packRaw.toObject();
|
|
|
|
|
2022-05-14 23:17:05 +00:00
|
|
|
Modrinth::Modpack pack;
|
|
|
|
try {
|
2022-01-14 08:43:42 +00:00
|
|
|
Modrinth::loadIndexedPack(pack, packObj);
|
|
|
|
newList.append(pack);
|
2022-05-14 23:17:05 +00:00
|
|
|
} catch (const JSONValidationError& e) {
|
|
|
|
qWarning() << "Error while loading mod from " << m_parent->debugName() << ": " << e.cause();
|
2022-01-14 08:43:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2022-05-15 00:29:48 +00:00
|
|
|
|
2022-05-16 22:17:37 +00:00
|
|
|
if (packs_all.size() < m_modpacks_per_page) {
|
2022-01-14 08:43:42 +00:00
|
|
|
searchState = Finished;
|
|
|
|
} else {
|
2022-05-16 22:17:37 +00:00
|
|
|
nextSearchOffset += m_modpacks_per_page;
|
2022-01-14 08:43:42 +00:00
|
|
|
searchState = CanPossiblyFetchMore;
|
|
|
|
}
|
2022-05-14 23:17:05 +00:00
|
|
|
|
2022-01-14 08:43:42 +00:00
|
|
|
beginInsertRows(QModelIndex(), modpacks.size(), modpacks.size() + newList.size() - 1);
|
|
|
|
modpacks.append(newList);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2022-05-14 23:17:05 +00:00
|
|
|
void ModpackListModel::searchRequestFailed(QString reason)
|
2022-01-14 08:43:42 +00:00
|
|
|
{
|
2022-05-14 23:17:05 +00:00
|
|
|
if (!jobPtr->first()->m_reply) {
|
|
|
|
// Network error
|
2022-05-15 23:45:27 +00:00
|
|
|
QMessageBox::critical(nullptr, tr("Error"), tr("A network error occurred. Could not load modpacks."));
|
2022-05-14 23:17:05 +00:00
|
|
|
} else if (jobPtr->first()->m_reply && jobPtr->first()->m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 409) {
|
|
|
|
// 409 Gone, notify user to update
|
|
|
|
QMessageBox::critical(nullptr, tr("Error"),
|
|
|
|
//: %1 refers to the launcher itself
|
|
|
|
QString("%1 %2")
|
|
|
|
.arg(m_parent->displayName())
|
|
|
|
.arg(tr("API version too old!\nPlease update %1!").arg(BuildConfig.LAUNCHER_NAME)));
|
2022-02-04 15:24:19 +00:00
|
|
|
}
|
2022-01-14 08:43:42 +00:00
|
|
|
jobPtr.reset();
|
|
|
|
|
2022-05-14 23:17:05 +00:00
|
|
|
if (searchState == ResetRequested) {
|
2022-01-14 08:43:42 +00:00
|
|
|
beginResetModel();
|
|
|
|
modpacks.clear();
|
|
|
|
endResetModel();
|
|
|
|
|
|
|
|
nextSearchOffset = 0;
|
|
|
|
performPaginatedSearch();
|
|
|
|
} else {
|
|
|
|
searchState = Finished;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-14 23:17:05 +00:00
|
|
|
} // namespace Modrinth
|
|
|
|
|
|
|
|
/******** Helpers ********/
|