2023-06-19 23:36:18 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
|
|
|
* Prism Launcher - Minecraft Launcher
|
|
|
|
* Copyright (C) 2023 TheKodeToad <TheKodeToad@proton.me>
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2023-06-19 22:42:27 +01:00
|
|
|
#include "InstallLoaderDialog.h"
|
|
|
|
|
|
|
|
#include <QDialogButtonBox>
|
|
|
|
#include <QPushButton>
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
#include "Application.h"
|
|
|
|
#include "BuildConfig.h"
|
|
|
|
#include "DesktopServices.h"
|
|
|
|
#include "meta/Index.h"
|
|
|
|
#include "minecraft/MinecraftInstance.h"
|
|
|
|
#include "minecraft/PackProfile.h"
|
|
|
|
#include "ui/widgets/PageContainer.h"
|
|
|
|
#include "ui/widgets/VersionSelectWidget.h"
|
|
|
|
|
2023-07-01 16:39:21 +01:00
|
|
|
class InstallLoaderPage : public VersionSelectWidget, public BasePage {
|
2023-06-19 22:42:27 +01:00
|
|
|
public:
|
2023-07-01 16:39:21 +01:00
|
|
|
InstallLoaderPage(const QString& id,
|
2023-08-02 20:32:23 +01:00
|
|
|
const QString& iconName,
|
2023-07-01 19:32:04 +01:00
|
|
|
const QString& name,
|
2023-07-03 20:48:37 +01:00
|
|
|
const Version& oldestVersion,
|
2023-07-01 19:32:04 +01:00
|
|
|
const std::shared_ptr<PackProfile> profile)
|
2023-08-02 20:32:23 +01:00
|
|
|
: VersionSelectWidget(nullptr), uid(id), iconName(iconName), name(name)
|
2023-06-19 22:42:27 +01:00
|
|
|
{
|
|
|
|
const QString minecraftVersion = profile->getComponentVersion("net.minecraft");
|
2023-06-20 19:57:15 +01:00
|
|
|
setEmptyString(tr("No versions are currently available for Minecraft %1").arg(minecraftVersion));
|
2023-07-03 20:48:37 +01:00
|
|
|
setExactIfPresentFilter(BaseVersionList::ParentVersionRole, minecraftVersion);
|
|
|
|
|
|
|
|
if (oldestVersion != Version() && Version(minecraftVersion) < oldestVersion)
|
|
|
|
setExactFilter(BaseVersionList::ParentVersionRole, "AAA");
|
2023-06-19 22:42:27 +01:00
|
|
|
|
|
|
|
if (const QString currentVersion = profile->getComponentVersion(id); !currentVersion.isNull())
|
|
|
|
setCurrentVersion(currentVersion);
|
|
|
|
}
|
|
|
|
|
2023-07-03 17:32:59 +01:00
|
|
|
QString id() const override { return uid; }
|
|
|
|
QString displayName() const override { return name; }
|
|
|
|
QIcon icon() const override { return APPLICATION->getThemedIcon(iconName); }
|
2023-06-19 22:42:27 +01:00
|
|
|
|
|
|
|
void openedImpl() override
|
|
|
|
{
|
2023-07-03 17:32:59 +01:00
|
|
|
if (loaded)
|
2023-06-19 22:42:27 +01:00
|
|
|
return;
|
|
|
|
|
2023-07-03 17:32:59 +01:00
|
|
|
const auto versions = APPLICATION->metadataIndex()->get(uid);
|
2023-06-19 22:42:27 +01:00
|
|
|
if (!versions)
|
|
|
|
return;
|
|
|
|
|
|
|
|
initialize(versions.get());
|
2023-07-03 17:32:59 +01:00
|
|
|
loaded = true;
|
2023-06-19 22:42:27 +01:00
|
|
|
}
|
|
|
|
|
2023-07-01 19:32:04 +01:00
|
|
|
void setParentContainer(BasePageContainer* container) override
|
|
|
|
{
|
|
|
|
auto dialog = dynamic_cast<QDialog*>(dynamic_cast<PageContainer*>(container)->parent());
|
|
|
|
connect(view(), &QAbstractItemView::doubleClicked, dialog, &QDialog::accept);
|
|
|
|
}
|
|
|
|
|
2023-06-19 22:42:27 +01:00
|
|
|
private:
|
2023-07-03 17:32:59 +01:00
|
|
|
const QString uid;
|
|
|
|
const QString iconName;
|
|
|
|
const QString name;
|
|
|
|
bool loaded = false;
|
2023-06-19 22:42:27 +01:00
|
|
|
};
|
|
|
|
|
2023-07-03 17:32:59 +01:00
|
|
|
static InstallLoaderPage* pageCast(BasePage* page)
|
2023-07-01 17:02:39 +01:00
|
|
|
{
|
|
|
|
auto result = dynamic_cast<InstallLoaderPage*>(page);
|
|
|
|
Q_ASSERT(result != nullptr);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
InstallLoaderDialog::InstallLoaderDialog(std::shared_ptr<PackProfile> profile, const QString& uid, QWidget* parent)
|
2023-07-03 17:32:59 +01:00
|
|
|
: QDialog(parent), profile(std::move(profile)), container(new PageContainer(this, QString(), this)), buttons(new QDialogButtonBox(this))
|
2023-06-19 22:42:27 +01:00
|
|
|
{
|
|
|
|
auto layout = new QVBoxLayout(this);
|
|
|
|
|
2023-07-03 17:32:59 +01:00
|
|
|
container->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
|
|
|
|
layout->addWidget(container);
|
2023-06-19 22:42:27 +01:00
|
|
|
|
|
|
|
auto buttonLayout = new QHBoxLayout(this);
|
|
|
|
|
|
|
|
auto refreshButton = new QPushButton(tr("&Refresh"), this);
|
2023-08-02 20:23:09 +01:00
|
|
|
connect(refreshButton, &QPushButton::clicked, this, [this] { pageCast(container->selectedPage())->loadList(); });
|
2023-06-19 22:42:27 +01:00
|
|
|
buttonLayout->addWidget(refreshButton);
|
|
|
|
|
2023-07-03 17:32:59 +01:00
|
|
|
buttons->setOrientation(Qt::Horizontal);
|
|
|
|
buttons->setStandardButtons(QDialogButtonBox::Cancel | QDialogButtonBox::Ok);
|
|
|
|
connect(buttons, &QDialogButtonBox::accepted, this, &QDialog::accept);
|
|
|
|
connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject);
|
|
|
|
buttonLayout->addWidget(buttons);
|
2023-06-19 22:42:27 +01:00
|
|
|
|
|
|
|
layout->addLayout(buttonLayout);
|
|
|
|
|
|
|
|
setWindowTitle(dialogTitle());
|
2023-07-03 17:32:59 +01:00
|
|
|
setWindowModality(Qt::WindowModal);
|
|
|
|
resize(520, 347);
|
2023-06-22 18:18:33 +01:00
|
|
|
|
2023-07-03 17:32:59 +01:00
|
|
|
for (BasePage* page : container->getPages()) {
|
2023-07-01 17:02:39 +01:00
|
|
|
if (page->id() == uid)
|
2023-07-03 17:32:59 +01:00
|
|
|
container->selectPage(page->id());
|
|
|
|
|
|
|
|
connect(pageCast(page), &VersionSelectWidget::selectedVersionChanged, this, [this, page] {
|
|
|
|
if (page->id() == container->selectedPage()->id())
|
|
|
|
validate(container->selectedPage());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
connect(container, &PageContainer::selectedPageChanged, this, [this](BasePage* previous, BasePage* current) { validate(current); });
|
|
|
|
pageCast(container->selectedPage())->selectSearch();
|
|
|
|
validate(container->selectedPage());
|
2023-06-19 22:42:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QList<BasePage*> InstallLoaderDialog::getPages()
|
|
|
|
{
|
2023-08-06 20:06:52 +01:00
|
|
|
return { // NeoForge
|
|
|
|
new InstallLoaderPage("net.neoforged", "neoforged", tr("NeoForge"), {}, profile),
|
|
|
|
// Forge
|
2023-07-03 20:48:37 +01:00
|
|
|
new InstallLoaderPage("net.minecraftforge", "forge", tr("Forge"), {}, profile),
|
2023-06-20 00:28:42 +01:00
|
|
|
// Fabric
|
2023-08-06 16:43:30 +01:00
|
|
|
new InstallLoaderPage("net.fabricmc.fabric-loader", "fabricmc", tr("Fabric"), Version("1.14"), profile),
|
2023-06-19 22:42:27 +01:00
|
|
|
// Quilt
|
2023-07-03 20:48:37 +01:00
|
|
|
new InstallLoaderPage("org.quiltmc.quilt-loader", "quiltmc", tr("Quilt"), Version("1.14"), profile),
|
2023-06-19 22:42:27 +01:00
|
|
|
// LiteLoader
|
2023-07-03 20:48:37 +01:00
|
|
|
new InstallLoaderPage("com.mumfrey.liteloader", "liteloader", tr("LiteLoader"), {}, profile)
|
2023-06-19 22:42:27 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
QString InstallLoaderDialog::dialogTitle()
|
|
|
|
{
|
|
|
|
return tr("Install Loader");
|
|
|
|
}
|
|
|
|
|
2023-07-03 17:32:59 +01:00
|
|
|
void InstallLoaderDialog::validate(BasePage* page)
|
|
|
|
{
|
|
|
|
buttons->button(QDialogButtonBox::Ok)->setEnabled(pageCast(page)->selectedVersion() != nullptr);
|
|
|
|
}
|
|
|
|
|
2023-06-19 22:42:27 +01:00
|
|
|
void InstallLoaderDialog::done(int result)
|
|
|
|
{
|
|
|
|
if (result == Accepted) {
|
2023-07-03 17:32:59 +01:00
|
|
|
auto* page = pageCast(container->selectedPage());
|
2023-06-19 22:42:27 +01:00
|
|
|
if (page->selectedVersion()) {
|
2023-07-03 17:32:59 +01:00
|
|
|
profile->setComponentVersion(page->id(), page->selectedVersion()->descriptor());
|
|
|
|
profile->resolve(Net::Mode::Online);
|
2023-06-19 22:42:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QDialog::done(result);
|
|
|
|
}
|