2022-05-14 18:56:38 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
2022-01-31 14:25:36 +00:00
|
|
|
/*
|
2022-05-14 18:56:38 +01:00
|
|
|
* PolyMC - Minecraft Launcher
|
2022-05-15 12:25:58 +01:00
|
|
|
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
|
2022-01-31 14:25:36 +00:00
|
|
|
*
|
2022-05-14 18:56:38 +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.
|
2022-01-31 14:25:36 +00:00
|
|
|
*
|
2022-05-14 18:56:38 +01:00
|
|
|
* 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.
|
2022-01-31 14:25:36 +00:00
|
|
|
*
|
2022-05-14 18:56:38 +01:00
|
|
|
* 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
|
|
|
|
* Copyright 2021-2022 kb1000
|
|
|
|
*
|
|
|
|
* 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-31 14:25:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ModrinthPage.h"
|
|
|
|
#include "ui_ModrinthPage.h"
|
|
|
|
|
2022-05-15 00:17:05 +01:00
|
|
|
#include "ModrinthModel.h"
|
|
|
|
|
2022-05-15 11:43:02 +01:00
|
|
|
#include "BuildConfig.h"
|
2022-05-15 00:17:05 +01:00
|
|
|
#include "InstanceImportTask.h"
|
|
|
|
#include "Json.h"
|
2023-01-06 20:26:26 +00:00
|
|
|
#include "Markdown.h"
|
2022-05-15 00:17:05 +01:00
|
|
|
|
2022-07-29 03:00:00 +01:00
|
|
|
#include "ui/widgets/ProjectItem.h"
|
|
|
|
|
2023-06-02 00:39:04 +01:00
|
|
|
#include "net/ApiDownload.h"
|
|
|
|
|
2022-05-15 00:17:05 +01:00
|
|
|
#include <QComboBox>
|
2022-01-31 14:25:36 +00:00
|
|
|
#include <QKeyEvent>
|
2022-05-15 00:17:05 +01:00
|
|
|
#include <QPushButton>
|
2022-01-31 14:25:36 +00:00
|
|
|
|
2022-05-15 00:17:05 +01:00
|
|
|
ModrinthPage::ModrinthPage(NewInstanceDialog* dialog, QWidget* parent) : QWidget(parent), ui(new Ui::ModrinthPage), dialog(dialog)
|
2022-01-31 14:25:36 +00:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2022-05-15 00:17:05 +01:00
|
|
|
|
|
|
|
connect(ui->searchButton, &QPushButton::clicked, this, &ModrinthPage::triggerSearch);
|
|
|
|
ui->searchEdit->installEventFilter(this);
|
|
|
|
m_model = new Modrinth::ModpackListModel(this);
|
|
|
|
ui->packView->setModel(m_model);
|
|
|
|
|
|
|
|
ui->versionSelectionBox->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
|
|
|
|
ui->versionSelectionBox->view()->parentWidget()->setMaximumHeight(300);
|
|
|
|
|
2022-05-16 00:45:27 +01:00
|
|
|
ui->sortByBox->addItem(tr("Sort by Relevance"));
|
2022-05-15 00:17:05 +01:00
|
|
|
ui->sortByBox->addItem(tr("Sort by Total Downloads"));
|
2022-05-16 00:45:27 +01:00
|
|
|
ui->sortByBox->addItem(tr("Sort by Follows"));
|
|
|
|
ui->sortByBox->addItem(tr("Sort by Newest"));
|
|
|
|
ui->sortByBox->addItem(tr("Sort by Last Updated"));
|
2022-05-15 00:17:05 +01:00
|
|
|
|
|
|
|
connect(ui->sortByBox, SIGNAL(currentIndexChanged(int)), this, SLOT(triggerSearch()));
|
|
|
|
connect(ui->packView->selectionModel(), &QItemSelectionModel::currentChanged, this, &ModrinthPage::onSelectionChanged);
|
|
|
|
connect(ui->versionSelectionBox, &QComboBox::currentTextChanged, this, &ModrinthPage::onVersionSelectionChanged);
|
2022-07-29 03:00:00 +01:00
|
|
|
|
|
|
|
ui->packView->setItemDelegate(new ProjectItemDelegate(this));
|
2022-09-10 22:49:31 +01:00
|
|
|
ui->packDescription->setMetaEntry(metaEntryBase());
|
2022-01-31 14:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ModrinthPage::~ModrinthPage()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2022-05-14 18:56:38 +01:00
|
|
|
void ModrinthPage::retranslate()
|
|
|
|
{
|
|
|
|
ui->retranslateUi(this);
|
|
|
|
}
|
|
|
|
|
2022-01-31 14:25:36 +00:00
|
|
|
void ModrinthPage::openedImpl()
|
|
|
|
{
|
|
|
|
BasePage::openedImpl();
|
|
|
|
triggerSearch();
|
|
|
|
}
|
|
|
|
|
2022-05-15 00:17:05 +01:00
|
|
|
bool ModrinthPage::eventFilter(QObject* watched, QEvent* event)
|
2022-01-31 14:25:36 +00:00
|
|
|
{
|
|
|
|
if (watched == ui->searchEdit && event->type() == QEvent::KeyPress) {
|
2022-05-15 00:17:05 +01:00
|
|
|
auto* keyEvent = reinterpret_cast<QKeyEvent*>(event);
|
2022-01-31 14:25:36 +00:00
|
|
|
if (keyEvent->key() == Qt::Key_Return) {
|
|
|
|
this->triggerSearch();
|
|
|
|
keyEvent->accept();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QObject::eventFilter(watched, event);
|
|
|
|
}
|
|
|
|
|
2022-06-24 13:02:58 +01:00
|
|
|
void ModrinthPage::onSelectionChanged(QModelIndex curr, QModelIndex prev)
|
2022-05-15 00:17:05 +01:00
|
|
|
{
|
|
|
|
ui->versionSelectionBox->clear();
|
|
|
|
|
2022-06-24 13:02:58 +01:00
|
|
|
if (!curr.isValid()) {
|
2022-05-15 00:17:05 +01:00
|
|
|
if (isOpened) {
|
|
|
|
dialog->setSuggestedPack();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-24 13:02:58 +01:00
|
|
|
current = m_model->data(curr, Qt::UserRole).value<Modrinth::Modpack>();
|
2022-05-15 00:17:05 +01:00
|
|
|
auto name = current.name;
|
|
|
|
|
|
|
|
if (!current.extraInfoLoaded) {
|
|
|
|
qDebug() << "Loading modrinth modpack information";
|
|
|
|
|
|
|
|
auto netJob = new NetJob(QString("Modrinth::PackInformation(%1)").arg(current.name), APPLICATION->network());
|
2023-06-15 20:59:41 +01:00
|
|
|
auto response = std::make_shared<QByteArray>();
|
2022-05-15 00:17:05 +01:00
|
|
|
|
|
|
|
QString id = current.id;
|
2022-01-31 14:25:36 +00:00
|
|
|
|
2023-06-02 00:39:04 +01:00
|
|
|
netJob->addNetAction(Net::ApiDownload::makeByteArray(QString("%1/project/%2").arg(BuildConfig.MODRINTH_PROD_URL, id), response));
|
2022-05-15 00:17:05 +01:00
|
|
|
|
2022-06-24 13:02:58 +01:00
|
|
|
QObject::connect(netJob, &NetJob::succeeded, this, [this, response, id, curr] {
|
2022-05-15 00:17:05 +01:00
|
|
|
if (id != current.id) {
|
|
|
|
return; // wrong request?
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonParseError parse_error;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
|
|
|
|
if (parse_error.error != QJsonParseError::NoError) {
|
|
|
|
qWarning() << "Error while parsing JSON response from Modrinth at " << parse_error.offset
|
|
|
|
<< " reason: " << parse_error.errorString();
|
|
|
|
qWarning() << *response;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto obj = Json::requireObject(doc);
|
|
|
|
|
|
|
|
try {
|
|
|
|
Modrinth::loadIndexedInfo(current, obj);
|
|
|
|
} catch (const JSONValidationError& e) {
|
|
|
|
qDebug() << *response;
|
|
|
|
qWarning() << "Error while reading modrinth modpack version: " << e.cause();
|
|
|
|
}
|
|
|
|
|
|
|
|
updateUI();
|
2022-06-24 13:02:58 +01:00
|
|
|
|
|
|
|
QVariant current_updated;
|
|
|
|
current_updated.setValue(current);
|
|
|
|
|
|
|
|
if (!m_model->setData(curr, current_updated, Qt::UserRole))
|
|
|
|
qWarning() << "Failed to cache extra info for the current pack!";
|
|
|
|
|
2022-05-15 00:17:05 +01:00
|
|
|
suggestCurrent();
|
|
|
|
});
|
2023-06-15 20:59:41 +01:00
|
|
|
QObject::connect(netJob, &NetJob::finished, this, [response, netJob] { netJob->deleteLater(); });
|
2022-05-15 00:17:05 +01:00
|
|
|
netJob->start();
|
|
|
|
} else
|
|
|
|
updateUI();
|
|
|
|
|
|
|
|
if (!current.versionsLoaded) {
|
|
|
|
qDebug() << "Loading modrinth modpack versions";
|
|
|
|
|
|
|
|
auto netJob = new NetJob(QString("Modrinth::PackVersions(%1)").arg(current.name), APPLICATION->network());
|
2023-06-15 20:59:41 +01:00
|
|
|
auto response = std::make_shared<QByteArray>();
|
2022-05-15 00:17:05 +01:00
|
|
|
|
|
|
|
QString id = current.id;
|
|
|
|
|
|
|
|
netJob->addNetAction(
|
2023-06-02 00:39:04 +01:00
|
|
|
Net::ApiDownload::makeByteArray(QString("%1/project/%2/version").arg(BuildConfig.MODRINTH_PROD_URL, id), response));
|
2022-05-15 00:17:05 +01:00
|
|
|
|
2022-06-24 13:02:58 +01:00
|
|
|
QObject::connect(netJob, &NetJob::succeeded, this, [this, response, id, curr] {
|
2022-05-15 00:17:05 +01:00
|
|
|
if (id != current.id) {
|
|
|
|
return; // wrong request?
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonParseError parse_error;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
|
|
|
|
if (parse_error.error != QJsonParseError::NoError) {
|
|
|
|
qWarning() << "Error while parsing JSON response from Modrinth at " << parse_error.offset
|
|
|
|
<< " reason: " << parse_error.errorString();
|
|
|
|
qWarning() << *response;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Modrinth::loadIndexedVersions(current, doc);
|
|
|
|
} catch (const JSONValidationError& e) {
|
|
|
|
qDebug() << *response;
|
|
|
|
qWarning() << "Error while reading modrinth modpack version: " << e.cause();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto version : current.versions) {
|
2022-06-24 13:11:11 +01:00
|
|
|
if (!version.name.contains(version.version))
|
|
|
|
ui->versionSelectionBox->addItem(QString("%1 — %2").arg(version.name, version.version), QVariant(version.id));
|
|
|
|
else
|
|
|
|
ui->versionSelectionBox->addItem(version.name, QVariant(version.id));
|
2022-05-15 00:17:05 +01:00
|
|
|
}
|
|
|
|
|
2022-06-24 13:02:58 +01:00
|
|
|
QVariant current_updated;
|
|
|
|
current_updated.setValue(current);
|
|
|
|
|
|
|
|
if (!m_model->setData(curr, current_updated, Qt::UserRole))
|
|
|
|
qWarning() << "Failed to cache versions for the current pack!";
|
|
|
|
|
2022-05-15 00:17:05 +01:00
|
|
|
suggestCurrent();
|
|
|
|
});
|
2023-06-15 20:59:41 +01:00
|
|
|
QObject::connect(netJob, &NetJob::finished, this, [response, netJob] { netJob->deleteLater(); });
|
2022-05-15 00:17:05 +01:00
|
|
|
netJob->start();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
for (auto version : current.versions) {
|
2022-06-24 13:11:11 +01:00
|
|
|
if (!version.name.contains(version.version))
|
|
|
|
ui->versionSelectionBox->addItem(QString("%1 - %2").arg(version.name, version.version), QVariant(version.id));
|
|
|
|
else
|
|
|
|
ui->versionSelectionBox->addItem(version.name, QVariant(version.id));
|
2022-05-15 00:17:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
suggestCurrent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModrinthPage::updateUI()
|
|
|
|
{
|
|
|
|
QString text = "";
|
|
|
|
|
2022-05-15 19:38:27 +01:00
|
|
|
if (current.extra.projectUrl.isEmpty())
|
2022-05-15 00:17:05 +01:00
|
|
|
text = current.name;
|
|
|
|
else
|
2022-05-15 01:47:35 +01:00
|
|
|
text = "<a href=\"" + current.extra.projectUrl + "\">" + current.name + "</a>";
|
2022-05-15 00:17:05 +01:00
|
|
|
|
2022-05-15 11:12:31 +01:00
|
|
|
// TODO: Implement multiple authors with links
|
|
|
|
text += "<br>" + tr(" by ") + QString("<a href=%1>%2</a>").arg(std::get<1>(current.author).toString(), std::get<0>(current.author));
|
2022-05-15 00:17:05 +01:00
|
|
|
|
2022-06-24 13:02:58 +01:00
|
|
|
if (current.extraInfoLoaded) {
|
2022-05-24 15:52:27 +01:00
|
|
|
if (!current.extra.donate.isEmpty()) {
|
|
|
|
text += "<br><br>" + tr("Donate information: ");
|
|
|
|
auto donateToStr = [](Modrinth::DonationData& donate) -> QString {
|
|
|
|
return QString("<a href=\"%1\">%2</a>").arg(donate.url, donate.platform);
|
|
|
|
};
|
|
|
|
QStringList donates;
|
|
|
|
for (auto& donate : current.extra.donate) {
|
|
|
|
donates.append(donateToStr(donate));
|
|
|
|
}
|
|
|
|
text += donates.join(", ");
|
|
|
|
}
|
|
|
|
|
2023-06-15 20:59:41 +01:00
|
|
|
if (!current.extra.issuesUrl.isEmpty() || !current.extra.sourceUrl.isEmpty() || !current.extra.wikiUrl.isEmpty() ||
|
|
|
|
!current.extra.discordUrl.isEmpty()) {
|
2022-05-24 15:52:27 +01:00
|
|
|
text += "<br><br>" + tr("External links:") + "<br>";
|
2022-05-24 13:46:58 +01:00
|
|
|
}
|
2022-05-24 15:52:27 +01:00
|
|
|
|
|
|
|
if (!current.extra.issuesUrl.isEmpty())
|
|
|
|
text += "- " + tr("Issues: <a href=%1>%1</a>").arg(current.extra.issuesUrl) + "<br>";
|
|
|
|
if (!current.extra.wikiUrl.isEmpty())
|
|
|
|
text += "- " + tr("Wiki: <a href=%1>%1</a>").arg(current.extra.wikiUrl) + "<br>";
|
|
|
|
if (!current.extra.sourceUrl.isEmpty())
|
|
|
|
text += "- " + tr("Source code: <a href=%1>%1</a>").arg(current.extra.sourceUrl) + "<br>";
|
|
|
|
if (!current.extra.discordUrl.isEmpty())
|
|
|
|
text += "- " + tr("Discord: <a href=%1>%1</a>").arg(current.extra.discordUrl) + "<br>";
|
2022-05-24 13:46:58 +01:00
|
|
|
}
|
|
|
|
|
2022-05-24 15:52:27 +01:00
|
|
|
text += "<hr>";
|
2022-05-15 00:17:05 +01:00
|
|
|
|
2023-01-06 20:26:26 +00:00
|
|
|
text += markdownToHTML(current.extra.body.toUtf8());
|
2022-05-15 00:17:05 +01:00
|
|
|
|
|
|
|
ui->packDescription->setHtml(text + current.description);
|
2022-10-11 18:19:29 +01:00
|
|
|
ui->packDescription->flush();
|
2022-05-15 00:17:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ModrinthPage::suggestCurrent()
|
|
|
|
{
|
|
|
|
if (!isOpened) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (selectedVersion.isEmpty()) {
|
|
|
|
dialog->setSuggestedPack();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& ver : current.versions) {
|
|
|
|
if (ver.id == selectedVersion) {
|
2022-11-12 14:42:07 +00:00
|
|
|
QMap<QString, QString> extra_info;
|
|
|
|
extra_info.insert("pack_id", current.id);
|
|
|
|
extra_info.insert("pack_version_id", ver.id);
|
|
|
|
|
2022-11-25 13:17:43 +00:00
|
|
|
dialog->setSuggestedPack(current.name, ver.version, new InstanceImportTask(ver.download_url, this, std::move(extra_info)));
|
2022-05-15 00:26:20 +01:00
|
|
|
auto iconName = current.iconName;
|
|
|
|
m_model->getLogo(iconName, current.iconUrl.toString(),
|
2022-05-15 11:12:31 +01:00
|
|
|
[this, iconName](QString logo) { dialog->setSuggestedIconFromFile(logo, iconName); });
|
2022-05-15 00:17:05 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModrinthPage::triggerSearch()
|
|
|
|
{
|
|
|
|
m_model->searchWithTerm(ui->searchEdit->text(), ui->sortByBox->currentIndex());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModrinthPage::onVersionSelectionChanged(QString data)
|
|
|
|
{
|
|
|
|
if (data.isNull() || data.isEmpty()) {
|
|
|
|
selectedVersion = "";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
selectedVersion = ui->versionSelectionBox->currentData().toString();
|
|
|
|
suggestCurrent();
|
2022-01-31 14:25:36 +00:00
|
|
|
}
|