2022-12-17 00:44:21 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* Prism Launcher - Minecraft Launcher
|
|
|
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
2023-06-19 23:36:18 +01:00
|
|
|
* Copyright (C) 2023 TheKodeToad <TheKodeToad@proton.me>
|
2022-12-17 00:44:21 +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/>.
|
|
|
|
*/
|
|
|
|
|
2022-11-25 12:23:46 +00:00
|
|
|
#include "ResourceDownloadDialog.h"
|
2023-04-14 21:18:37 +01:00
|
|
|
#include <QEventLoop>
|
|
|
|
#include <QList>
|
2022-11-25 12:23:46 +00:00
|
|
|
|
|
|
|
#include <QPushButton>
|
2023-06-02 22:04:06 +01:00
|
|
|
#include <algorithm>
|
2022-11-25 12:23:46 +00:00
|
|
|
|
|
|
|
#include "Application.h"
|
|
|
|
#include "ResourceDownloadTask.h"
|
|
|
|
|
2022-12-17 00:44:21 +00:00
|
|
|
#include "minecraft/mod/ModFolderModel.h"
|
2022-12-16 23:26:10 +00:00
|
|
|
#include "minecraft/mod/ResourcePackFolderModel.h"
|
2022-12-30 17:06:07 +00:00
|
|
|
#include "minecraft/mod/ShaderPackFolderModel.h"
|
2023-06-02 22:04:06 +01:00
|
|
|
#include "minecraft/mod/TexturePackFolderModel.h"
|
2022-12-17 00:44:21 +00:00
|
|
|
|
2023-04-19 22:57:09 +01:00
|
|
|
#include "minecraft/mod/tasks/GetModDependenciesTask.h"
|
2023-04-14 21:02:33 +01:00
|
|
|
#include "modplatform/ModIndex.h"
|
2023-04-19 22:57:09 +01:00
|
|
|
#include "ui/dialogs/CustomMessageBox.h"
|
|
|
|
#include "ui/dialogs/ProgressDialog.h"
|
2022-11-25 12:23:46 +00:00
|
|
|
#include "ui/dialogs/ReviewMessageBox.h"
|
2022-12-17 00:44:21 +00:00
|
|
|
|
2022-11-25 12:23:46 +00:00
|
|
|
#include "ui/pages/modplatform/ResourcePage.h"
|
2022-12-17 00:44:21 +00:00
|
|
|
|
|
|
|
#include "ui/pages/modplatform/flame/FlameResourcePages.h"
|
|
|
|
#include "ui/pages/modplatform/modrinth/ModrinthResourcePages.h"
|
|
|
|
|
2022-11-25 12:23:46 +00:00
|
|
|
#include "ui/widgets/PageContainer.h"
|
|
|
|
|
2022-12-16 22:03:52 +00:00
|
|
|
namespace ResourceDownload {
|
|
|
|
|
2022-11-25 12:23:46 +00:00
|
|
|
ResourceDownloadDialog::ResourceDownloadDialog(QWidget* parent, const std::shared_ptr<ResourceFolderModel> base_model)
|
2023-06-02 22:04:06 +01:00
|
|
|
: QDialog(parent)
|
|
|
|
, m_base_model(base_model)
|
|
|
|
, m_buttons(QDialogButtonBox::Help | QDialogButtonBox::Ok | QDialogButtonBox::Cancel)
|
|
|
|
, m_vertical_layout(this)
|
2022-11-25 12:23:46 +00:00
|
|
|
{
|
|
|
|
setObjectName(QStringLiteral("ResourceDownloadDialog"));
|
|
|
|
|
|
|
|
resize(std::max(0.5 * parent->width(), 400.0), std::max(0.75 * parent->height(), 400.0));
|
|
|
|
|
|
|
|
setWindowIcon(APPLICATION->getThemedIcon("new"));
|
|
|
|
|
|
|
|
// Bonk Qt over its stupid head and make sure it understands which button is the default one...
|
|
|
|
// See: https://stackoverflow.com/questions/24556831/qbuttonbox-set-default-button
|
|
|
|
auto OkButton = m_buttons.button(QDialogButtonBox::Ok);
|
|
|
|
OkButton->setEnabled(false);
|
|
|
|
OkButton->setDefault(true);
|
|
|
|
OkButton->setAutoDefault(true);
|
|
|
|
OkButton->setText(tr("Review and confirm"));
|
|
|
|
OkButton->setShortcut(tr("Ctrl+Return"));
|
|
|
|
|
|
|
|
auto CancelButton = m_buttons.button(QDialogButtonBox::Cancel);
|
|
|
|
CancelButton->setDefault(false);
|
|
|
|
CancelButton->setAutoDefault(false);
|
|
|
|
|
|
|
|
auto HelpButton = m_buttons.button(QDialogButtonBox::Help);
|
|
|
|
HelpButton->setDefault(false);
|
|
|
|
HelpButton->setAutoDefault(false);
|
|
|
|
|
|
|
|
setWindowModality(Qt::WindowModal);
|
|
|
|
}
|
|
|
|
|
2022-12-17 00:44:21 +00:00
|
|
|
void ResourceDownloadDialog::accept()
|
|
|
|
{
|
|
|
|
if (!geometrySaveKey().isEmpty())
|
|
|
|
APPLICATION->settings()->set(geometrySaveKey(), saveGeometry().toBase64());
|
|
|
|
|
|
|
|
QDialog::accept();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceDownloadDialog::reject()
|
|
|
|
{
|
|
|
|
if (!geometrySaveKey().isEmpty())
|
|
|
|
APPLICATION->settings()->set(geometrySaveKey(), saveGeometry().toBase64());
|
|
|
|
|
|
|
|
QDialog::reject();
|
|
|
|
}
|
|
|
|
|
2022-11-25 12:23:46 +00:00
|
|
|
// NOTE: We can't have this in the ctor because PageContainer calls a virtual function, and so
|
|
|
|
// won't work with subclasses if we put it in this ctor.
|
|
|
|
void ResourceDownloadDialog::initializeContainer()
|
|
|
|
{
|
2023-05-28 10:15:39 +01:00
|
|
|
m_container = new PageContainer(this, {}, this);
|
2022-11-25 12:23:46 +00:00
|
|
|
m_container->setSizePolicy(QSizePolicy::Policy::Preferred, QSizePolicy::Policy::Expanding);
|
|
|
|
m_container->layout()->setContentsMargins(0, 0, 0, 0);
|
|
|
|
m_vertical_layout.addWidget(m_container);
|
|
|
|
|
|
|
|
m_container->addButtons(&m_buttons);
|
|
|
|
|
|
|
|
connect(m_container, &PageContainer::selectedPageChanged, this, &ResourceDownloadDialog::selectedPageChanged);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceDownloadDialog::connectButtons()
|
|
|
|
{
|
|
|
|
auto OkButton = m_buttons.button(QDialogButtonBox::Ok);
|
2023-06-02 22:04:06 +01:00
|
|
|
OkButton->setToolTip(
|
|
|
|
tr("Opens a new popup to review your selected %1 and confirm your selection. Shortcut: Ctrl+Return").arg(resourcesString()));
|
2022-11-25 12:23:46 +00:00
|
|
|
connect(OkButton, &QPushButton::clicked, this, &ResourceDownloadDialog::confirm);
|
|
|
|
|
|
|
|
auto CancelButton = m_buttons.button(QDialogButtonBox::Cancel);
|
|
|
|
connect(CancelButton, &QPushButton::clicked, this, &ResourceDownloadDialog::reject);
|
|
|
|
|
|
|
|
auto HelpButton = m_buttons.button(QDialogButtonBox::Help);
|
|
|
|
connect(HelpButton, &QPushButton::clicked, m_container, &PageContainer::help);
|
|
|
|
}
|
|
|
|
|
2023-04-21 18:37:17 +01:00
|
|
|
static ModPlatform::ProviderCapabilities ProviderCaps;
|
|
|
|
|
2023-06-19 15:42:16 +01:00
|
|
|
QStringList getRequiredBy(QList<ResourceDownloadDialog::DownloadTaskPtr> tasks, ResourceDownloadDialog::DownloadTaskPtr pack)
|
2023-05-04 19:52:48 +01:00
|
|
|
{
|
2023-06-19 15:42:16 +01:00
|
|
|
auto addonId = pack->getPack()->addonId;
|
|
|
|
auto provider = pack->getPack()->provider;
|
|
|
|
auto version = pack->getVersionID();
|
2023-05-04 19:52:48 +01:00
|
|
|
auto req = QStringList();
|
2023-05-04 21:54:46 +01:00
|
|
|
for (auto& task : tasks) {
|
2023-06-19 15:42:16 +01:00
|
|
|
if (provider != task->getPack()->provider)
|
|
|
|
continue;
|
2023-05-04 21:54:46 +01:00
|
|
|
auto deps = task->getVersion().dependencies;
|
|
|
|
if (auto dep = std::find_if(deps.begin(), deps.end(),
|
2023-06-19 15:42:16 +01:00
|
|
|
[addonId, provider, version](const ModPlatform::Dependency& d) {
|
|
|
|
return d.type == ModPlatform::DependencyType::REQUIRED &&
|
|
|
|
(provider == ModPlatform::ResourceProvider::MODRINTH && d.addonId.toString().isEmpty()
|
|
|
|
? version == d.version
|
|
|
|
: d.addonId == addonId);
|
2023-05-04 21:54:46 +01:00
|
|
|
});
|
2023-05-04 22:04:24 +01:00
|
|
|
dep != deps.end()) {
|
2023-05-04 21:54:46 +01:00
|
|
|
req.append(task->getName());
|
2023-05-04 19:52:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2022-11-25 12:23:46 +00:00
|
|
|
void ResourceDownloadDialog::confirm()
|
|
|
|
{
|
2023-01-08 15:28:55 +00:00
|
|
|
auto confirm_dialog = ReviewMessageBox::create(this, tr("Confirm %1 to download").arg(resourcesString()));
|
|
|
|
confirm_dialog->retranslateUi(resourcesString());
|
2022-11-25 12:23:46 +00:00
|
|
|
|
2023-04-19 22:57:09 +01:00
|
|
|
if (auto task = getModDependenciesTask(); task) {
|
|
|
|
connect(task.get(), &Task::failed, this,
|
|
|
|
[&](QString reason) { CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->exec(); });
|
|
|
|
|
|
|
|
connect(task.get(), &Task::succeeded, this, [&]() {
|
|
|
|
QStringList warnings = task->warnings();
|
|
|
|
if (warnings.count()) {
|
|
|
|
CustomMessageBox::selectable(this, tr("Warnings"), warnings.join('\n'), QMessageBox::Warning)->exec();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Check for updates
|
|
|
|
ProgressDialog progress_dialog(this);
|
|
|
|
progress_dialog.setSkipButton(true, tr("Abort"));
|
|
|
|
progress_dialog.setWindowTitle(tr("Checking for dependencies..."));
|
|
|
|
auto ret = progress_dialog.execWithTask(task.get());
|
|
|
|
|
|
|
|
// If the dialog was skipped / some download error happened
|
|
|
|
if (ret == QDialog::DialogCode::Rejected) {
|
|
|
|
QMetaObject::invokeMethod(this, "reject", Qt::QueuedConnection);
|
|
|
|
return;
|
2023-06-18 22:36:37 +01:00
|
|
|
} else {
|
|
|
|
for (auto dep : task->getDependecies())
|
2023-05-04 20:35:16 +01:00
|
|
|
addResource(dep->pack, dep->version);
|
2023-06-18 22:36:37 +01:00
|
|
|
}
|
2023-04-14 21:02:33 +01:00
|
|
|
}
|
2023-04-19 22:57:09 +01:00
|
|
|
|
2023-06-02 22:04:06 +01:00
|
|
|
auto selected = getTasks();
|
|
|
|
std::sort(selected.begin(), selected.end(), [](const DownloadTaskPtr& a, const DownloadTaskPtr& b) {
|
|
|
|
return QString::compare(a->getName(), b->getName(), Qt::CaseInsensitive) < 0;
|
|
|
|
});
|
|
|
|
for (auto& task : selected) {
|
2023-05-04 20:35:16 +01:00
|
|
|
confirm_dialog->appendResource({ task->getName(), task->getFilename(), task->getCustomPath(),
|
2023-06-19 15:42:16 +01:00
|
|
|
ProviderCaps.name(task->getProvider()), getRequiredBy(selected, task) });
|
2022-11-25 12:23:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (confirm_dialog->exec()) {
|
|
|
|
auto deselected = confirm_dialog->deselectedResources();
|
2023-06-02 22:04:06 +01:00
|
|
|
for (auto page : m_container->getPages()) {
|
|
|
|
auto res = static_cast<ResourcePage*>(page);
|
|
|
|
for (auto name : deselected)
|
|
|
|
res->removeResourceFromPage(name);
|
2022-11-25 12:23:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this->accept();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ResourceDownloadDialog::selectPage(QString pageId)
|
|
|
|
{
|
|
|
|
return m_container->selectPage(pageId);
|
|
|
|
}
|
|
|
|
|
2023-06-19 22:42:27 +01:00
|
|
|
ResourcePage* ResourceDownloadDialog::selectedPage()
|
2022-11-25 12:23:46 +00:00
|
|
|
{
|
2023-06-19 22:42:27 +01:00
|
|
|
ResourcePage* result = dynamic_cast<ResourcePage*>(m_container->selectedPage());
|
|
|
|
Q_ASSERT(result != nullptr);
|
|
|
|
return result;
|
2022-11-25 12:23:46 +00:00
|
|
|
}
|
|
|
|
|
2023-06-02 22:04:06 +01:00
|
|
|
void ResourceDownloadDialog::addResource(ModPlatform::IndexedPack::Ptr pack, ModPlatform::IndexedVersion& ver)
|
2022-11-25 12:23:46 +00:00
|
|
|
{
|
2023-06-02 22:04:06 +01:00
|
|
|
removeResource(pack->name);
|
2023-06-19 22:42:27 +01:00
|
|
|
selectedPage()->addResourceToPage(pack, ver, getBaseModel());
|
2023-06-02 22:04:06 +01:00
|
|
|
setButtonStatus();
|
2022-11-25 12:23:46 +00:00
|
|
|
}
|
|
|
|
|
2023-06-02 22:04:06 +01:00
|
|
|
void ResourceDownloadDialog::removeResource(const QString& pack_name)
|
2022-11-25 12:23:46 +00:00
|
|
|
{
|
2023-06-02 22:04:06 +01:00
|
|
|
for (auto page : m_container->getPages()) {
|
|
|
|
static_cast<ResourcePage*>(page)->removeResourceFromPage(pack_name);
|
|
|
|
}
|
|
|
|
setButtonStatus();
|
2022-11-25 12:23:46 +00:00
|
|
|
}
|
|
|
|
|
2023-06-02 22:04:06 +01:00
|
|
|
void ResourceDownloadDialog::setButtonStatus()
|
2022-11-25 12:23:46 +00:00
|
|
|
{
|
2023-06-02 22:04:06 +01:00
|
|
|
auto selected = false;
|
|
|
|
for (auto page : m_container->getPages()) {
|
|
|
|
auto res = static_cast<ResourcePage*>(page);
|
|
|
|
selected = selected || res->hasSelectedPacks();
|
2022-12-18 18:41:46 +00:00
|
|
|
}
|
2023-06-02 22:04:06 +01:00
|
|
|
m_buttons.button(QDialogButtonBox::Ok)->setEnabled(selected);
|
2022-11-25 12:23:46 +00:00
|
|
|
}
|
|
|
|
|
2022-12-18 18:41:46 +00:00
|
|
|
const QList<ResourceDownloadDialog::DownloadTaskPtr> ResourceDownloadDialog::getTasks()
|
2022-11-25 12:23:46 +00:00
|
|
|
{
|
2023-06-02 22:04:06 +01:00
|
|
|
QList<DownloadTaskPtr> selected;
|
|
|
|
for (auto page : m_container->getPages()) {
|
|
|
|
auto res = static_cast<ResourcePage*>(page);
|
|
|
|
selected.append(res->selectedPacks());
|
|
|
|
}
|
|
|
|
return selected;
|
2022-11-25 12:23:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceDownloadDialog::selectedPageChanged(BasePage* previous, BasePage* selected)
|
|
|
|
{
|
|
|
|
auto* prev_page = dynamic_cast<ResourcePage*>(previous);
|
|
|
|
if (!prev_page) {
|
|
|
|
qCritical() << "Page '" << previous->displayName() << "' in ResourceDownloadDialog is not a ResourcePage!";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Same effect as having a global search bar
|
2023-06-19 22:42:27 +01:00
|
|
|
selectedPage()->setSearchTerm(prev_page->getSearchTerm());
|
2022-11-25 12:23:46 +00:00
|
|
|
}
|
2022-12-16 22:03:52 +00:00
|
|
|
|
2022-12-17 00:44:21 +00:00
|
|
|
ModDownloadDialog::ModDownloadDialog(QWidget* parent, const std::shared_ptr<ModFolderModel>& mods, BaseInstance* instance)
|
|
|
|
: ResourceDownloadDialog(parent, mods), m_instance(instance)
|
|
|
|
{
|
2022-12-30 19:59:35 +00:00
|
|
|
setWindowTitle(dialogTitle());
|
|
|
|
|
2022-12-17 00:44:21 +00:00
|
|
|
initializeContainer();
|
|
|
|
connectButtons();
|
|
|
|
|
|
|
|
if (!geometrySaveKey().isEmpty())
|
|
|
|
restoreGeometry(QByteArray::fromBase64(APPLICATION->settings()->get(geometrySaveKey()).toByteArray()));
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<BasePage*> ModDownloadDialog::getPages()
|
|
|
|
{
|
|
|
|
QList<BasePage*> pages;
|
|
|
|
|
|
|
|
pages.append(ModrinthModPage::create(this, *m_instance));
|
|
|
|
if (APPLICATION->capabilities() & Application::SupportsFlame)
|
|
|
|
pages.append(FlameModPage::create(this, *m_instance));
|
|
|
|
|
|
|
|
return pages;
|
|
|
|
}
|
|
|
|
|
2023-04-19 22:57:09 +01:00
|
|
|
GetModDependenciesTask::Ptr ModDownloadDialog::getModDependenciesTask()
|
|
|
|
{
|
|
|
|
if (auto model = dynamic_cast<ModFolderModel*>(getBaseModel().get()); model) {
|
2023-05-04 19:52:48 +01:00
|
|
|
QList<std::shared_ptr<GetModDependenciesTask::PackDependency>> selectedVers;
|
2023-05-04 20:35:16 +01:00
|
|
|
for (auto& selected : getTasks()) {
|
2023-05-04 19:52:48 +01:00
|
|
|
selectedVers.append(std::make_shared<GetModDependenciesTask::PackDependency>(selected->getPack(), selected->getVersion()));
|
2023-04-19 22:57:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return makeShared<GetModDependenciesTask>(this, m_instance, model, selectedVers);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
|
2022-12-16 23:26:10 +00:00
|
|
|
ResourcePackDownloadDialog::ResourcePackDownloadDialog(QWidget* parent,
|
|
|
|
const std::shared_ptr<ResourcePackFolderModel>& resource_packs,
|
|
|
|
BaseInstance* instance)
|
|
|
|
: ResourceDownloadDialog(parent, resource_packs), m_instance(instance)
|
|
|
|
{
|
|
|
|
setWindowTitle(dialogTitle());
|
|
|
|
|
|
|
|
initializeContainer();
|
|
|
|
connectButtons();
|
|
|
|
|
|
|
|
if (!geometrySaveKey().isEmpty())
|
|
|
|
restoreGeometry(QByteArray::fromBase64(APPLICATION->settings()->get(geometrySaveKey()).toByteArray()));
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<BasePage*> ResourcePackDownloadDialog::getPages()
|
|
|
|
{
|
|
|
|
QList<BasePage*> pages;
|
|
|
|
|
|
|
|
pages.append(ModrinthResourcePackPage::create(this, *m_instance));
|
|
|
|
if (APPLICATION->capabilities() & Application::SupportsFlame)
|
|
|
|
pages.append(FlameResourcePackPage::create(this, *m_instance));
|
|
|
|
|
|
|
|
return pages;
|
|
|
|
}
|
|
|
|
|
2023-01-29 21:07:49 +00:00
|
|
|
TexturePackDownloadDialog::TexturePackDownloadDialog(QWidget* parent,
|
|
|
|
const std::shared_ptr<TexturePackFolderModel>& resource_packs,
|
|
|
|
BaseInstance* instance)
|
|
|
|
: ResourceDownloadDialog(parent, resource_packs), m_instance(instance)
|
|
|
|
{
|
|
|
|
setWindowTitle(dialogTitle());
|
|
|
|
|
|
|
|
initializeContainer();
|
|
|
|
connectButtons();
|
|
|
|
|
|
|
|
if (!geometrySaveKey().isEmpty())
|
|
|
|
restoreGeometry(QByteArray::fromBase64(APPLICATION->settings()->get(geometrySaveKey()).toByteArray()));
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<BasePage*> TexturePackDownloadDialog::getPages()
|
|
|
|
{
|
|
|
|
QList<BasePage*> pages;
|
|
|
|
|
|
|
|
pages.append(ModrinthTexturePackPage::create(this, *m_instance));
|
|
|
|
if (APPLICATION->capabilities() & Application::SupportsFlame)
|
|
|
|
pages.append(FlameTexturePackPage::create(this, *m_instance));
|
|
|
|
|
|
|
|
return pages;
|
|
|
|
}
|
|
|
|
|
2022-12-30 17:06:07 +00:00
|
|
|
ShaderPackDownloadDialog::ShaderPackDownloadDialog(QWidget* parent,
|
|
|
|
const std::shared_ptr<ShaderPackFolderModel>& shaders,
|
|
|
|
BaseInstance* instance)
|
|
|
|
: ResourceDownloadDialog(parent, shaders), m_instance(instance)
|
|
|
|
{
|
|
|
|
setWindowTitle(dialogTitle());
|
|
|
|
|
|
|
|
initializeContainer();
|
|
|
|
connectButtons();
|
|
|
|
|
|
|
|
if (!geometrySaveKey().isEmpty())
|
|
|
|
restoreGeometry(QByteArray::fromBase64(APPLICATION->settings()->get(geometrySaveKey()).toByteArray()));
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<BasePage*> ShaderPackDownloadDialog::getPages()
|
|
|
|
{
|
|
|
|
QList<BasePage*> pages;
|
|
|
|
pages.append(ModrinthShaderPackPage::create(this, *m_instance));
|
|
|
|
return pages;
|
|
|
|
}
|
|
|
|
|
2022-12-16 22:03:52 +00:00
|
|
|
} // namespace ResourceDownload
|