2022-03-21 00:31:08 +05:30
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* PolyMC - Minecraft Launcher
|
|
|
|
* Copyright (c) 2022 Jamie Mansfield <jmansfield@cadixdev.org>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2021-03-24 05:29:43 +05:30
|
|
|
#include "FlamePage.h"
|
|
|
|
#include "ui_FlamePage.h"
|
2020-03-31 06:43:19 +05:30
|
|
|
|
2021-11-22 08:25:16 +05:30
|
|
|
#include <QKeyEvent>
|
|
|
|
|
2021-11-20 20:52:22 +05:30
|
|
|
#include "Application.h"
|
2022-05-08 12:52:50 +05:30
|
|
|
#include "FlameModel.h"
|
|
|
|
#include "InstanceImportTask.h"
|
2021-11-22 08:25:16 +05:30
|
|
|
#include "Json.h"
|
|
|
|
#include "ui/dialogs/NewInstanceDialog.h"
|
2022-10-22 03:49:51 +05:30
|
|
|
#include "ui/widgets/ProjectItem.h"
|
|
|
|
#include "modplatform/flame/FlameAPI.h"
|
|
|
|
|
|
|
|
static FlameAPI api;
|
2020-03-31 06:43:19 +05:30
|
|
|
|
2022-05-08 12:52:50 +05:30
|
|
|
FlamePage::FlamePage(NewInstanceDialog* dialog, QWidget* parent) : QWidget(parent), ui(new Ui::FlamePage), dialog(dialog)
|
2020-03-31 06:43:19 +05:30
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2021-03-24 05:29:43 +05:30
|
|
|
connect(ui->searchButton, &QPushButton::clicked, this, &FlamePage::triggerSearch);
|
2020-03-31 06:43:19 +05:30
|
|
|
ui->searchEdit->installEventFilter(this);
|
2021-03-27 03:45:20 +05:30
|
|
|
listModel = new Flame::ListModel(this);
|
|
|
|
ui->packView->setModel(listModel);
|
|
|
|
|
2021-06-21 02:25:09 +05:30
|
|
|
ui->versionSelectionBox->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
|
|
|
|
ui->versionSelectionBox->view()->parentWidget()->setMaximumHeight(300);
|
2021-06-18 22:01:14 +05:30
|
|
|
|
2021-03-27 03:45:20 +05:30
|
|
|
// index is used to set the sorting with the curseforge api
|
2022-04-18 03:28:51 +05:30
|
|
|
ui->sortByBox->addItem(tr("Sort by Featured"));
|
|
|
|
ui->sortByBox->addItem(tr("Sort by Popularity"));
|
|
|
|
ui->sortByBox->addItem(tr("Sort by Last Updated"));
|
|
|
|
ui->sortByBox->addItem(tr("Sort by Name"));
|
|
|
|
ui->sortByBox->addItem(tr("Sort by Author"));
|
|
|
|
ui->sortByBox->addItem(tr("Sort by Total Downloads"));
|
2021-03-27 03:45:20 +05:30
|
|
|
|
2021-04-09 01:58:55 +05:30
|
|
|
connect(ui->sortByBox, SIGNAL(currentIndexChanged(int)), this, SLOT(triggerSearch()));
|
2021-03-24 05:29:43 +05:30
|
|
|
connect(ui->packView->selectionModel(), &QItemSelectionModel::currentChanged, this, &FlamePage::onSelectionChanged);
|
2021-03-27 03:45:20 +05:30
|
|
|
connect(ui->versionSelectionBox, &QComboBox::currentTextChanged, this, &FlamePage::onVersionSelectionChanged);
|
2022-10-22 03:49:51 +05:30
|
|
|
|
|
|
|
ui->packView->setItemDelegate(new ProjectItemDelegate(this));
|
|
|
|
ui->packDescription->setMetaEntry("FlamePacks");
|
2020-03-31 06:43:19 +05:30
|
|
|
}
|
|
|
|
|
2021-03-24 05:29:43 +05:30
|
|
|
FlamePage::~FlamePage()
|
2020-03-31 06:43:19 +05:30
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2021-03-24 05:29:43 +05:30
|
|
|
bool FlamePage::eventFilter(QObject* watched, QEvent* event)
|
2020-03-31 06:43:19 +05:30
|
|
|
{
|
|
|
|
if (watched == ui->searchEdit && event->type() == QEvent::KeyPress) {
|
|
|
|
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
|
|
|
|
if (keyEvent->key() == Qt::Key_Return) {
|
|
|
|
triggerSearch();
|
|
|
|
keyEvent->accept();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QWidget::eventFilter(watched, event);
|
|
|
|
}
|
|
|
|
|
2021-03-24 05:29:43 +05:30
|
|
|
bool FlamePage::shouldDisplay() const
|
2020-03-31 06:43:19 +05:30
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-02-22 23:53:53 +05:30
|
|
|
void FlamePage::retranslate()
|
|
|
|
{
|
|
|
|
ui->retranslateUi(this);
|
|
|
|
}
|
|
|
|
|
2021-03-24 05:29:43 +05:30
|
|
|
void FlamePage::openedImpl()
|
2020-03-31 06:43:19 +05:30
|
|
|
{
|
|
|
|
suggestCurrent();
|
2021-03-27 03:45:20 +05:30
|
|
|
triggerSearch();
|
2020-03-31 06:43:19 +05:30
|
|
|
}
|
|
|
|
|
2021-03-24 05:29:43 +05:30
|
|
|
void FlamePage::triggerSearch()
|
2020-03-31 06:43:19 +05:30
|
|
|
{
|
2021-03-27 03:45:20 +05:30
|
|
|
listModel->searchWithTerm(ui->searchEdit->text(), ui->sortByBox->currentIndex());
|
2020-03-31 06:43:19 +05:30
|
|
|
}
|
|
|
|
|
2022-06-24 17:46:41 +05:30
|
|
|
void FlamePage::onSelectionChanged(QModelIndex curr, QModelIndex prev)
|
2020-03-31 06:43:19 +05:30
|
|
|
{
|
2021-03-27 03:45:20 +05:30
|
|
|
ui->versionSelectionBox->clear();
|
|
|
|
|
2022-06-24 17:46:41 +05:30
|
|
|
if (!curr.isValid()) {
|
2022-05-08 12:52:50 +05:30
|
|
|
if (isOpened) {
|
2020-03-31 06:43:19 +05:30
|
|
|
dialog->setSuggestedPack();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2020-04-30 00:47:51 +05:30
|
|
|
|
2022-06-24 17:46:41 +05:30
|
|
|
current = listModel->data(curr, Qt::UserRole).value<Flame::IndexedPack>();
|
2021-03-27 03:45:20 +05:30
|
|
|
|
2022-05-08 12:52:50 +05:30
|
|
|
if (current.versionsLoaded == false) {
|
2021-03-27 03:45:20 +05:30
|
|
|
qDebug() << "Loading flame modpack versions";
|
2022-02-27 17:44:12 +05:30
|
|
|
auto netJob = new NetJob(QString("Flame::PackVersions(%1)").arg(current.name), APPLICATION->network());
|
|
|
|
auto response = new QByteArray();
|
2021-03-27 03:45:20 +05:30
|
|
|
int addonId = current.addonId;
|
2022-05-08 12:52:50 +05:30
|
|
|
netJob->addNetAction(Net::Download::makeByteArray(QString("https://api.curseforge.com/v1/mods/%1/files").arg(addonId), response));
|
2021-11-22 08:25:16 +05:30
|
|
|
|
2022-06-24 17:46:41 +05:30
|
|
|
QObject::connect(netJob, &NetJob::succeeded, this, [this, response, addonId, curr] {
|
2022-05-08 12:52:50 +05:30
|
|
|
if (addonId != current.addonId) {
|
|
|
|
return; // wrong request
|
2022-02-27 16:25:24 +05:30
|
|
|
}
|
2021-03-27 03:45:20 +05:30
|
|
|
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();
|
2021-03-27 03:45:20 +05:30
|
|
|
qWarning() << *response;
|
|
|
|
return;
|
|
|
|
}
|
2022-05-08 12:52:50 +05:30
|
|
|
auto arr = Json::ensureArray(doc.object(), "data");
|
|
|
|
try {
|
2021-03-27 03:45:20 +05:30
|
|
|
Flame::loadIndexedPackVersions(current, arr);
|
2022-05-08 12:52:50 +05:30
|
|
|
} catch (const JSONValidationError& e) {
|
2021-03-27 03:45:20 +05:30
|
|
|
qDebug() << *response;
|
|
|
|
qWarning() << "Error while reading flame modpack version: " << e.cause();
|
|
|
|
}
|
|
|
|
|
2022-05-08 12:52:50 +05:30
|
|
|
for (auto version : current.versions) {
|
2021-03-27 03:45:20 +05:30
|
|
|
ui->versionSelectionBox->addItem(version.version, QVariant(version.downloadUrl));
|
|
|
|
}
|
|
|
|
|
2022-06-24 17:46:41 +05:30
|
|
|
QVariant current_updated;
|
|
|
|
current_updated.setValue(current);
|
|
|
|
|
|
|
|
if (!listModel->setData(curr, current_updated, Qt::UserRole))
|
|
|
|
qWarning() << "Failed to cache versions for the current pack!";
|
|
|
|
|
2022-06-24 17:56:35 +05:30
|
|
|
// TODO: Check whether it's a connection issue or the project disabled 3rd-party distribution.
|
|
|
|
if (current.versionsLoaded && ui->versionSelectionBox->count() < 1) {
|
|
|
|
ui->versionSelectionBox->addItem(tr("No version is available!"), -1);
|
|
|
|
}
|
2021-03-27 03:45:20 +05:30
|
|
|
suggestCurrent();
|
|
|
|
});
|
2022-05-08 12:52:50 +05:30
|
|
|
QObject::connect(netJob, &NetJob::finished, this, [response, netJob] {
|
2022-02-27 17:44:12 +05:30
|
|
|
netJob->deleteLater();
|
|
|
|
delete response;
|
|
|
|
});
|
2021-12-31 09:57:59 +05:30
|
|
|
netJob->start();
|
2022-05-08 12:52:50 +05:30
|
|
|
} else {
|
|
|
|
for (auto version : current.versions) {
|
2021-03-27 03:45:20 +05:30
|
|
|
ui->versionSelectionBox->addItem(version.version, QVariant(version.downloadUrl));
|
|
|
|
}
|
|
|
|
|
|
|
|
suggestCurrent();
|
|
|
|
}
|
2022-05-24 20:57:32 +05:30
|
|
|
|
2022-06-24 17:56:35 +05:30
|
|
|
// TODO: Check whether it's a connection issue or the project disabled 3rd-party distribution.
|
|
|
|
if (current.versionsLoaded && ui->versionSelectionBox->count() < 1) {
|
|
|
|
ui->versionSelectionBox->addItem(tr("No version is available!"), -1);
|
|
|
|
}
|
|
|
|
|
2022-05-24 20:57:32 +05:30
|
|
|
updateUi();
|
2020-03-31 06:43:19 +05:30
|
|
|
}
|
|
|
|
|
2021-03-24 05:29:43 +05:30
|
|
|
void FlamePage::suggestCurrent()
|
2020-03-31 06:43:19 +05:30
|
|
|
{
|
2022-05-08 12:52:50 +05:30
|
|
|
if (!isOpened) {
|
2020-03-31 06:43:19 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-12 20:12:07 +05:30
|
|
|
if (m_selected_version_index == -1) {
|
2021-06-19 19:49:39 +05:30
|
|
|
dialog->setSuggestedPack();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-12 20:12:07 +05:30
|
|
|
auto version = current.versions.at(m_selected_version_index);
|
|
|
|
|
|
|
|
QMap<QString, QString> extra_info;
|
|
|
|
extra_info.insert("pack_id", QString::number(current.addonId));
|
|
|
|
extra_info.insert("pack_version_id", QString::number(version.fileId));
|
|
|
|
|
2022-11-25 18:47:43 +05:30
|
|
|
dialog->setSuggestedPack(current.name, new InstanceImportTask(version.downloadUrl, this, std::move(extra_info)));
|
2020-03-31 06:43:19 +05:30
|
|
|
QString editedLogoName;
|
2021-03-24 05:29:43 +05:30
|
|
|
editedLogoName = "curseforge_" + current.logoName.section(".", 0, 0);
|
2022-05-08 12:52:50 +05:30
|
|
|
listModel->getLogo(current.logoName, current.logoUrl,
|
|
|
|
[this, editedLogoName](QString logo) { dialog->setSuggestedIconFromFile(logo, editedLogoName); });
|
2020-03-31 06:43:19 +05:30
|
|
|
}
|
2021-03-27 03:45:20 +05:30
|
|
|
|
|
|
|
void FlamePage::onVersionSelectionChanged(QString data)
|
|
|
|
{
|
2022-05-08 12:52:50 +05:30
|
|
|
if (data.isNull() || data.isEmpty()) {
|
2022-11-12 20:12:07 +05:30
|
|
|
m_selected_version_index = -1;
|
2021-03-27 03:45:20 +05:30
|
|
|
return;
|
|
|
|
}
|
2022-11-12 20:12:07 +05:30
|
|
|
|
|
|
|
m_selected_version_index = ui->versionSelectionBox->currentIndex();
|
|
|
|
Q_ASSERT(current.versions.at(m_selected_version_index).downloadUrl == ui->versionSelectionBox->currentData().toString());
|
|
|
|
|
2021-03-27 03:45:20 +05:30
|
|
|
suggestCurrent();
|
2021-04-09 01:58:55 +05:30
|
|
|
}
|
2022-05-24 20:57:32 +05:30
|
|
|
|
|
|
|
void FlamePage::updateUi()
|
|
|
|
{
|
|
|
|
QString text = "";
|
|
|
|
QString name = current.name;
|
|
|
|
|
|
|
|
if (current.extra.websiteUrl.isEmpty())
|
|
|
|
text = name;
|
|
|
|
else
|
|
|
|
text = "<a href=\"" + current.extra.websiteUrl + "\">" + name + "</a>";
|
|
|
|
if (!current.authors.empty()) {
|
|
|
|
auto authorToStr = [](Flame::ModpackAuthor& author) {
|
|
|
|
if (author.url.isEmpty()) {
|
|
|
|
return author.name;
|
|
|
|
}
|
|
|
|
return QString("<a href=\"%1\">%2</a>").arg(author.url, author.name);
|
|
|
|
};
|
|
|
|
QStringList authorStrs;
|
|
|
|
for (auto& author : current.authors) {
|
|
|
|
authorStrs.push_back(authorToStr(author));
|
|
|
|
}
|
|
|
|
text += "<br>" + tr(" by ") + authorStrs.join(", ");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(current.extraInfoLoaded) {
|
|
|
|
if (!current.extra.issuesUrl.isEmpty()
|
|
|
|
|| !current.extra.sourceUrl.isEmpty()
|
|
|
|
|| !current.extra.wikiUrl.isEmpty()) {
|
|
|
|
text += "<br><br>" + tr("External links:") + "<br>";
|
|
|
|
}
|
|
|
|
|
|
|
|
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>";
|
|
|
|
}
|
|
|
|
|
2022-10-22 03:49:51 +05:30
|
|
|
|
2022-05-24 20:57:32 +05:30
|
|
|
text += "<hr>";
|
2022-10-22 03:49:51 +05:30
|
|
|
text += api.getModDescription(current.addonId).toUtf8();
|
2022-05-24 20:57:32 +05:30
|
|
|
|
|
|
|
ui->packDescription->setHtml(text + current.description);
|
2022-10-22 03:49:51 +05:30
|
|
|
ui->packDescription->flush();
|
2022-05-24 20:57:32 +05:30
|
|
|
}
|