// SPDX-License-Identifier: GPL-3.0-only /* * Prism Launcher - Minecraft Launcher * Copyright (C) 2022 Sefa Eyeoglu * Copyright (C) 2023 TheKodeToad * * 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 . */ #include "ResourceDownloadDialog.h" #include #include #include #include #include "Application.h" #include "ResourceDownloadTask.h" #include "minecraft/PackProfile.h" #include "minecraft/mod/ModFolderModel.h" #include "minecraft/mod/tasks/GetModDependenciesTask.h" #include "modplatform/ModIndex.h" #include "ui/dialogs/CustomMessageBox.h" #include "ui/dialogs/ProgressDialog.h" #include "ui/dialogs/ReviewMessageBox.h" #include "ui/pages/modplatform/ResourcePage.h" #include "ui/pages/modplatform/flame/FlameResourcePages.h" #include "ui/pages/modplatform/modrinth/ModrinthResourcePages.h" #include "modplatform/flame/FlameAPI.h" #include "modplatform/modrinth/ModrinthAPI.h" #include "ui/widgets/PageContainer.h" namespace ResourceDownload { ResourceDownloadDialog::ResourceDownloadDialog(QWidget* parent, ResourceFolderModel* baseModel, BaseInstance* instance, QString resourcesString, QString geometrySaveKey, bool suppressInitialSearch) : QDialog(parent) , m_base_model(baseModel) , m_buttons(QDialogButtonBox::Help | QDialogButtonBox::Ok | QDialogButtonBox::Cancel) , m_vertical_layout(this) , m_suppressInitialSearch(suppressInitialSearch) , m_instance(instance) , m_resourcesString(std::move(resourcesString)) , m_geometrySaveKey(std::move(geometrySaveKey)) { setObjectName(QStringLiteral("ResourceDownloadDialog")); resize(static_cast(std::max(0.5 * parent->width(), 400.0)), static_cast(std::max(0.75 * parent->height(), 400.0))); setWindowIcon(QIcon::fromTheme("new")); // small margins look ugly on macOS on modal windows #ifndef Q_OS_MACOS m_buttons.setContentsMargins(0, 0, 6, 6); #endif // 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); setWindowTitle(dialogTitle()); } void ResourceDownloadDialog::accept() { if (!geometrySaveKey().isEmpty()) { APPLICATION->settings()->set(geometrySaveKey(), QString::fromUtf8(saveGeometry().toBase64())); } QDialog::accept(); } void ResourceDownloadDialog::reject() { auto selected = getTasks(); if (selected.count() > 0) { auto reply = CustomMessageBox::selectable(this, tr("Confirmation Needed"), tr("You have %1 selected resources.\n" "Are you sure you want to close this dialog?") .arg(selected.count()), QMessageBox::Question, QMessageBox::Yes | QMessageBox::No, QMessageBox::No) ->exec(); if (reply != QMessageBox::Yes) { return; } } if (!geometrySaveKey().isEmpty()) { APPLICATION->settings()->set(geometrySaveKey(), QString::fromUtf8(saveGeometry().toBase64())); } QDialog::reject(); } // 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() { // small margins look ugly on macOS on modal windows #ifndef Q_OS_MACOS layout()->setContentsMargins(0, 0, 0, 0); #endif m_container = new PageContainer(this, {}, this); 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); okButton->setToolTip( tr("Opens a new popup to review your selected %1 and confirm your selection. Shortcut: Ctrl+Return").arg(resourcesString())); 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); } void ResourceDownloadDialog::confirm() { auto* confirmDialog = ReviewMessageBox::create(this, tr("Confirm %1 to download").arg(resourcesString())); confirmDialog->retranslateUi(resourcesString()); QHash dependencyExtraInfo; QStringList depNames; if (auto task = getModDependenciesTask(); task) { connect(task.get(), &Task::failed, this, [this](const QString& reason) { CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->exec(); }); auto weak = task.toWeakRef(); connect(task.get(), &Task::succeeded, this, [this, weak]() { QStringList warnings; if (auto task = weak.lock()) { warnings = task->warnings(); } if (warnings.count()) { CustomMessageBox::selectable(this, tr("Warnings"), warnings.join('\n'), QMessageBox::Warning)->exec(); } }); // Check for updates ProgressDialog progressDialog(this); progressDialog.setSkipButton(true, tr("Abort")); progressDialog.setWindowTitle(tr("Checking for dependencies...")); auto ret = progressDialog.execWithTask(task.get()); // If the dialog was skipped / some download error happened if (ret == QDialog::DialogCode::Rejected) { QMetaObject::invokeMethod(this, "reject", Qt::QueuedConnection); return; } for (const auto& dep : task->getDependecies()) { addResource(dep->pack, dep->version, "dependency"); depNames << dep->pack->name; } dependencyExtraInfo = task->getExtraInfo(); } auto selected = getTasks(); std::ranges::sort(selected, [](const DownloadTaskPtr& a, const DownloadTaskPtr& b) { return QString::compare(a->getName(), b->getName(), Qt::CaseInsensitive) < 0; }); for (auto& task : selected) { auto extraInfo = dependencyExtraInfo.value(task->getPack()->addonId.toString()); confirmDialog->appendResource({ .name = task->getName(), .filename = task->getFilename(), .provider = ModPlatform::ProviderCapabilities::name(task->getProvider()), .required_by = extraInfo.required_by, .version_type = task->getVersion().version_type.toString(), .enabled = !extraInfo.maybe_installed }); } if (confirmDialog->exec() != 0) { auto deselected = confirmDialog->deselectedResources(); for (auto* page : m_container->getPages()) { auto* res = static_cast(page); for (const auto& name : deselected) { res->removeResourceFromPage(name); } } this->accept(); } else { for (const auto& name : depNames) { removeResource(name); } } } bool ResourceDownloadDialog::selectPage(QString pageId) { return m_container->selectPage(std::move(pageId)); } ResourcePage* ResourceDownloadDialog::selectedPage() { auto* result = dynamic_cast(m_container->selectedPage()); Q_ASSERT(result != nullptr); return result; } void ResourceDownloadDialog::addResource(ModPlatform::IndexedPack::Ptr pack, ModPlatform::IndexedVersion& ver, QString downloadReason) { removeResource(pack->name); selectedPage()->addResourceToPage(pack, ver, getBaseModel(), std::move(downloadReason)); setButtonStatus(); } void ResourceDownloadDialog::removeResource(const QString& packName) { for (auto* page : m_container->getPages()) { static_cast(page)->removeResourceFromPage(packName); } setButtonStatus(); } void ResourceDownloadDialog::setButtonStatus() { auto selected = false; for (auto* page : m_container->getPages()) { auto* res = static_cast(page); selected = selected || res->hasSelectedPacks(); } m_buttons.button(QDialogButtonBox::Ok)->setEnabled(selected); } QList ResourceDownloadDialog::getTasks() { QList selected; for (auto* page : m_container->getPages()) { auto* res = static_cast(page); selected.append(res->selectedPacks()); } return selected; } void ResourceDownloadDialog::selectedPageChanged(BasePage* previous, BasePage* selected) { // If previous is null (first selection), nothing to sync if (!previous) { return; } auto* prevPage = dynamic_cast(previous); if (!prevPage) { qCritical() << "Page '" << previous->displayName() << "' in ResourceDownloadDialog is not a ResourcePage!"; return; } // Same effect as having a global search bar auto* result = dynamic_cast(selected); Q_ASSERT(result != nullptr); result->setSearchTerm(prevPage->getSearchTerm()); } void ResourceDownloadDialog::setResourceMetadata(const std::shared_ptr& meta) { switch (meta->provider) { case ModPlatform::ResourceProvider::MODRINTH: selectPage(Modrinth::id()); break; case ModPlatform::ResourceProvider::FLAME: selectPage(Flame::id()); break; } setWindowTitle(tr("Change %1 version").arg(meta->name)); m_container->hidePageList(); m_buttons.hide(); auto* page = selectedPage(); page->openProject(meta->project_id); } GetModDependenciesTask::Ptr ResourceDownloadDialog::getModDependenciesTask() { if (!APPLICATION->settings()->get("ModDependenciesDisabled").toBool()) { // dependencies if (auto* model = dynamic_cast(getBaseModel()); model) { QList> selectedVers; for (auto& selected : getTasks()) { selectedVers.append(std::make_shared(selected->getPack(), selected->getVersion())); } return makeShared(m_instance, model, selectedVers); } } return nullptr; } ResourceDownloadDialog* ResourceDownloadDialog::createMod(QWidget* parent, ResourceFolderModel* mods, BaseInstance* instance, bool suppressInitialSearch) { auto* dialog = new ResourceDownloadDialog(parent, mods, instance, tr("mods"), "ModDownloadGeometry", suppressInitialSearch); QList pages; auto loaders = static_cast(instance)->getPackProfile()->getSupportedModLoaders().value(); if (ModrinthAPI::validateModLoaders(loaders)) { auto* page = Modrinth::createModPage(dialog, *instance); page->setSuppressInitialSearch(suppressInitialSearch); pages.append(page); } if (APPLICATION->capabilities() & Application::SupportsFlame && FlameAPI::validateModLoaders(loaders)) { auto* flamePage = Flame::createModPage(dialog, *instance); flamePage->setSuppressInitialSearch(suppressInitialSearch); pages.append(flamePage); } dialog->initPages(pages); return dialog; } ResourceDownloadDialog* ResourceDownloadDialog::createResourcePack(QWidget* parent, ResourceFolderModel* mods, BaseInstance* instance, bool suppressInitialSearch) { auto* dialog = new ResourceDownloadDialog(parent, mods, instance, tr("resource packs"), "RPDownloadGeometry", suppressInitialSearch); QList pages; auto* page = Modrinth::createResourcePackResourcePage(dialog, *instance); page->setSuppressInitialSearch(suppressInitialSearch); pages.append(page); if (APPLICATION->capabilities() & Application::SupportsFlame) { auto* flamePage = Flame::createResourcePackResourcePage(dialog, *instance); flamePage->setSuppressInitialSearch(suppressInitialSearch); pages.append(flamePage); } dialog->initPages(pages); return dialog; } ResourceDownloadDialog* ResourceDownloadDialog::createTexturePack(QWidget* parent, ResourceFolderModel* mods, BaseInstance* instance, bool suppressInitialSearch) { auto* dialog = new ResourceDownloadDialog(parent, mods, instance, tr("texture packs"), "TPDownloadGeometry", suppressInitialSearch); QList pages; auto* page = Modrinth::createTexturePackResourcePage(dialog, *instance); page->setSuppressInitialSearch(suppressInitialSearch); pages.append(page); if (APPLICATION->capabilities() & Application::SupportsFlame) { auto* flamePage = Flame::createTexturePackResourcePage(dialog, *instance); flamePage->setSuppressInitialSearch(suppressInitialSearch); pages.append(flamePage); } dialog->initPages(pages); return dialog; } ResourceDownloadDialog* ResourceDownloadDialog::createShaderPack(QWidget* parent, ResourceFolderModel* mods, BaseInstance* instance, bool suppressInitialSearch) { auto* dialog = new ResourceDownloadDialog(parent, mods, instance, tr("shader packs"), "ShaderDownloadGeometry", suppressInitialSearch); QList pages; auto* page = Modrinth::createShaderPackResourcePage(dialog, *instance); page->setSuppressInitialSearch(suppressInitialSearch); pages.append(page); if (APPLICATION->capabilities() & Application::SupportsFlame) { auto* flamePage = Flame::createShaderPackResourcePage(dialog, *instance); flamePage->setSuppressInitialSearch(suppressInitialSearch); pages.append(flamePage); } dialog->initPages(pages); return dialog; } ResourceDownloadDialog* ResourceDownloadDialog::createDataPack(QWidget* parent, ResourceFolderModel* mods, BaseInstance* instance, bool suppressInitialSearch) { auto* dialog = new ResourceDownloadDialog(parent, mods, instance, tr("data packs"), "DataPackDownloadGeometry", suppressInitialSearch); QList pages; auto* page = Modrinth::createDataPackResourcePage(dialog, *instance); page->setSuppressInitialSearch(suppressInitialSearch); pages.append(page); if (APPLICATION->capabilities() & Application::SupportsFlame) { auto* flamePage = Flame::createDataPackResourcePage(dialog, *instance); flamePage->setSuppressInitialSearch(suppressInitialSearch); pages.append(flamePage); } dialog->initPages(pages); return dialog; } void ResourceDownloadDialog::initPages(QList pages) { m_pages = std::move(pages); initializeContainer(); connectButtons(); if (!geometrySaveKey().isEmpty()) { restoreGeometry(QByteArray::fromBase64(APPLICATION->settings()->get(geometrySaveKey()).toString().toUtf8())); } } } // namespace ResourceDownload