diff options
author | timoreo22 <timo.oreo34@gmail.com> | 2022-02-26 07:33:11 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-26 07:33:11 +0100 |
commit | ae354688c96927e95db8c06c53ada0b5d7e77f26 (patch) | |
tree | 5d864de33438ce4d82af03928b3d632f055a0490 /launcher/ui/pages/modplatform/flame/FlameModPage.cpp | |
parent | 27f37315f84536d75d95bc1879e8a4513fcfc831 (diff) | |
parent | c4f4e9e620ee4a7094542190bc18cc167d00a7ff (diff) | |
download | PrismLauncher-ae354688c96927e95db8c06c53ada0b5d7e77f26.tar.gz PrismLauncher-ae354688c96927e95db8c06c53ada0b5d7e77f26.tar.bz2 PrismLauncher-ae354688c96927e95db8c06c53ada0b5d7e77f26.zip |
Merge pull request #193 from flowln/develop
Allow for downloading multiple mods at once
Diffstat (limited to 'launcher/ui/pages/modplatform/flame/FlameModPage.cpp')
-rw-r--r-- | launcher/ui/pages/modplatform/flame/FlameModPage.cpp | 347 |
1 files changed, 181 insertions, 166 deletions
diff --git a/launcher/ui/pages/modplatform/flame/FlameModPage.cpp b/launcher/ui/pages/modplatform/flame/FlameModPage.cpp index 4afdd142..6d33a6ac 100644 --- a/launcher/ui/pages/modplatform/flame/FlameModPage.cpp +++ b/launcher/ui/pages/modplatform/flame/FlameModPage.cpp @@ -4,196 +4,211 @@ #include <QKeyEvent> #include "Application.h" -#include "Json.h" -#include "ui/dialogs/ModDownloadDialog.h" -#include "InstanceImportTask.h" #include "FlameModModel.h" +#include "InstanceImportTask.h" +#include "Json.h" #include "ModDownloadTask.h" #include "minecraft/MinecraftInstance.h" #include "minecraft/PackProfile.h" +#include "ui/dialogs/ModDownloadDialog.h" FlameModPage::FlameModPage(ModDownloadDialog *dialog, BaseInstance *instance) - : QWidget(dialog), m_instance(instance), ui(new Ui::FlameModPage), dialog(dialog) -{ - ui->setupUi(this); - connect(ui->searchButton, &QPushButton::clicked, this, &FlameModPage::triggerSearch); - ui->searchEdit->installEventFilter(this); - listModel = new FlameMod::ListModel(this); - ui->packView->setModel(listModel); - - ui->versionSelectionBox->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); - ui->versionSelectionBox->view()->parentWidget()->setMaximumHeight(300); - - // index is used to set the sorting with the flame api - 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 Downloads")); - - connect(ui->sortByBox, SIGNAL(currentIndexChanged(int)), this, SLOT(triggerSearch())); - connect(ui->packView->selectionModel(), &QItemSelectionModel::currentChanged, this, &FlameModPage::onSelectionChanged); - connect(ui->versionSelectionBox, &QComboBox::currentTextChanged, this, &FlameModPage::onVersionSelectionChanged); + : QWidget(dialog), m_instance(instance), ui(new Ui::FlameModPage), + dialog(dialog) { + ui->setupUi(this); + connect(ui->searchButton, &QPushButton::clicked, this, + &FlameModPage::triggerSearch); + ui->searchEdit->installEventFilter(this); + listModel = new FlameMod::ListModel(this); + ui->packView->setModel(listModel); + + ui->versionSelectionBox->view()->setVerticalScrollBarPolicy( + Qt::ScrollBarAsNeeded); + ui->versionSelectionBox->view()->parentWidget()->setMaximumHeight(300); + + // index is used to set the sorting with the flame api + 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 Downloads")); + + connect(ui->sortByBox, SIGNAL(currentIndexChanged(int)), this, + SLOT(triggerSearch())); + connect(ui->packView->selectionModel(), &QItemSelectionModel::currentChanged, + this, &FlameModPage::onSelectionChanged); + connect(ui->versionSelectionBox, &QComboBox::currentTextChanged, this, + &FlameModPage::onVersionSelectionChanged); + connect(ui->modSelectionButton, &QPushButton::clicked, this, + &FlameModPage::onModSelected); } -FlameModPage::~FlameModPage() -{ - delete ui; -} +FlameModPage::~FlameModPage() { delete ui; } -bool FlameModPage::eventFilter(QObject* watched, QEvent* event) -{ - 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; - } +bool FlameModPage::eventFilter(QObject *watched, QEvent *event) { + 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); + } + return QWidget::eventFilter(watched, event); } -bool FlameModPage::shouldDisplay() const -{ - return true; -} +bool FlameModPage::shouldDisplay() const { return true; } -void FlameModPage::openedImpl() -{ - suggestCurrent(); - triggerSearch(); +void FlameModPage::openedImpl() { + updateSelectionButton(); + triggerSearch(); } -void FlameModPage::triggerSearch() -{ - listModel->searchWithTerm(ui->searchEdit->text(), ui->sortByBox->currentIndex()); +void FlameModPage::triggerSearch() { + listModel->searchWithTerm(ui->searchEdit->text(), + ui->sortByBox->currentIndex()); } -void FlameModPage::onSelectionChanged(QModelIndex first, QModelIndex second) -{ - ui->versionSelectionBox->clear(); - - if(!first.isValid()) - { - if(isOpened) - { - dialog->setSuggestedMod(); - } - return; +void FlameModPage::onSelectionChanged(QModelIndex first, QModelIndex second) { + ui->versionSelectionBox->clear(); + + if (!first.isValid()) { + return; + } + + current = listModel->data(first, Qt::UserRole).value<FlameMod::IndexedPack>(); + QString text = ""; + QString name = current.name; + + if (current.websiteUrl.isEmpty()) + text = name; + else + text = "<a href=\"" + current.websiteUrl + "\">" + name + "</a>"; + if (!current.authors.empty()) { + auto authorToStr = [](FlameMod::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)); } - - current = listModel->data(first, Qt::UserRole).value<FlameMod::IndexedPack>(); - QString text = ""; - QString name = current.name; - - if (current.websiteUrl.isEmpty()) - text = name; - else - text = "<a href=\"" + current.websiteUrl + "\">" + name + "</a>"; - if (!current.authors.empty()) { - auto authorToStr = [](FlameMod::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(", "); + } + text += "<br><br>"; + + ui->packDescription->setHtml(text + current.description); + + if (!current.versionsLoaded) { + qDebug() << "Loading flame mod versions"; + + ui->modSelectionButton->setText(tr("Loading versions...")); + ui->modSelectionButton->setEnabled(false); + + auto netJob = + new NetJob(QString("Flame::ModVersions(%1)").arg(current.name), + APPLICATION->network()); + auto response = new QByteArray(); + int addonId = current.addonId; + netJob->addNetAction(Net::Download::makeByteArray( + QString("https://addons-ecs.forgesvc.net/api/v2/addon/%1/files") + .arg(addonId), + response)); + + QObject::connect(netJob, &NetJob::succeeded, this, [this, response] { + QJsonParseError parse_error; + QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error); + if (parse_error.error != QJsonParseError::NoError) { + qWarning() << "Error while parsing JSON response from Flame at " + << parse_error.offset + << " reason: " << parse_error.errorString(); + qWarning() << *response; + return; + } + QJsonArray arr = doc.array(); + try { + FlameMod::loadIndexedPackVersions(current, arr, APPLICATION->network(), + m_instance); + } catch (const JSONValidationError &e) { + qDebug() << *response; + qWarning() << "Error while reading Flame mod version: " << e.cause(); + } + auto packProfile = ((MinecraftInstance *)m_instance)->getPackProfile(); + QString mcVersion = packProfile->getComponentVersion("net.minecraft"); + QString loaderString = + (packProfile->getComponentVersion("net.minecraftforge").isEmpty()) + ? "fabric" + : "forge"; + for (int i = 0; i < current.versions.size(); i++) { + auto version = current.versions[i]; + if (!version.mcVersion.contains(mcVersion)) { + continue; } - text += "<br>" + tr(" by ") + authorStrs.join(", "); - } - text += "<br><br>"; - - ui->packDescription->setHtml(text + current.description); - - if (!current.versionsLoaded) - { - qDebug() << "Loading flame mod versions"; - auto netJob = new NetJob(QString("Flame::ModVersions(%1)").arg(current.name), APPLICATION->network()); - auto response = new QByteArray(); - int addonId = current.addonId; - netJob->addNetAction(Net::Download::makeByteArray(QString("https://addons-ecs.forgesvc.net/api/v2/addon/%1/files").arg(addonId), response)); - - QObject::connect(netJob, &NetJob::succeeded, this, [this, response] - { - QJsonParseError parse_error; - QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error); - if(parse_error.error != QJsonParseError::NoError) { - qWarning() << "Error while parsing JSON response from Flame at " << parse_error.offset << " reason: " << parse_error.errorString(); - qWarning() << *response; - return; - } - QJsonArray arr = doc.array(); - try - { - FlameMod::loadIndexedPackVersions(current, arr, APPLICATION->network(), m_instance); - } - catch(const JSONValidationError &e) - { - qDebug() << *response; - qWarning() << "Error while reading Flame mod version: " << e.cause(); - } - auto packProfile = ((MinecraftInstance *)m_instance)->getPackProfile(); - QString mcVersion = packProfile->getComponentVersion("net.minecraft"); - QString loaderString = (packProfile->getComponentVersion("net.minecraftforge").isEmpty()) ? "fabric" : "forge"; - for(int i = 0; i < current.versions.size(); i++) { - auto version = current.versions[i]; - if(!version.mcVersion.contains(mcVersion)){ - continue; - } - ui->versionSelectionBox->addItem(version.version, QVariant(i)); - } - if(ui->versionSelectionBox->count() == 0){ - ui->versionSelectionBox->addItem(tr("No Valid Version found!"), QVariant(-1)); - } - suggestCurrent(); - }); - QObject::connect(netJob, &NetJob::finished, this, [response, netJob] - { - netJob->deleteLater(); - delete response; - }); - netJob->start(); + ui->versionSelectionBox->addItem(version.version, QVariant(i)); + } + if (ui->versionSelectionBox->count() == 0) { + ui->versionSelectionBox->addItem(tr("No Valid Version found!"), + QVariant(-1)); + } + + ui->modSelectionButton->setText(tr("Cannot select invalid version :(")); + updateSelectionButton(); + }); + QObject::connect(netJob, &NetJob::finished, this, [response, netJob] { + netJob->deleteLater(); + delete response; + }); + netJob->start(); + } else { + for (int i = 0; i < current.versions.size(); i++) { + ui->versionSelectionBox->addItem(current.versions[i].version, + QVariant(i)); } - else - { - for(int i = 0; i < current.versions.size(); i++) { - ui->versionSelectionBox->addItem(current.versions[i].version, QVariant(i)); - } - if(ui->versionSelectionBox->count() == 0){ - ui->versionSelectionBox->addItem(tr("No Valid Version found!"), QVariant(-1)); - } - suggestCurrent(); + if (ui->versionSelectionBox->count() == 0) { + ui->versionSelectionBox->addItem(tr("No Valid Version found!"), + QVariant(-1)); } -} -void FlameModPage::suggestCurrent() -{ - if(!isOpened) - { - return; - } + updateSelectionButton(); + } +} - if (selectedVersion == -1) - { - dialog->setSuggestedMod(); - return; - } +void FlameModPage::updateSelectionButton() { + if (!isOpened || selectedVersion < 0) { + ui->modSelectionButton->setEnabled(false); + return; + } + + ui->modSelectionButton->setEnabled(true); + auto &version = current.versions[selectedVersion]; + if (!dialog->isModSelected(current.name, version.fileName)) { + ui->modSelectionButton->setText(tr("Select mod for download")); + } else { + ui->modSelectionButton->setText(tr("Deselect mod for download")); + } +} - auto version = current.versions[selectedVersion]; - dialog->setSuggestedMod(current.name, new ModDownloadTask(version.downloadUrl, version.fileName , dialog->mods)); +void FlameModPage::onVersionSelectionChanged(QString data) { + if (data.isNull() || data.isEmpty()) { + selectedVersion = -1; + return; + } + selectedVersion = ui->versionSelectionBox->currentData().toInt(); + updateSelectionButton(); } -void FlameModPage::onVersionSelectionChanged(QString data) -{ - if(data.isNull() || data.isEmpty()) - { - selectedVersion = -1; - return; - } - selectedVersion = ui->versionSelectionBox->currentData().toInt(); - suggestCurrent(); +void FlameModPage::onModSelected() { + auto &version = current.versions[selectedVersion]; + if (dialog->isModSelected(current.name, version.fileName)) { + dialog->removeSelectedMod(current.name); + } else { + dialog->addSelectedMod(current.name, + new ModDownloadTask(version.downloadUrl, + version.fileName, dialog->mods)); + } + + updateSelectionButton(); } |