From 9c6727e27f55dd888c8cfc5147fdf0f6f8378f46 Mon Sep 17 00:00:00 2001 From: flow Date: Mon, 21 Feb 2022 21:34:06 -0300 Subject: [PATCH] feat: change task container in ModDownloadDialog to a QHash Previously, we used a unique_ptr to a ModDownloadTask to keep track of the selected mod to download when we accepted the dialog. In order to allow multiple mods to be selected at once for download, this has been changed to a QHash where the key is the mods name (since it doesn't seem right to allow for multiple versions of the same mod to be downloaded at once), and the value is a pointer to the corresponding ModDownloadTask. --- launcher/ModDownloadTask.h | 1 + launcher/ui/dialogs/ModDownloadDialog.cpp | 33 +++++++++++++++++--- launcher/ui/dialogs/ModDownloadDialog.h | 8 +++-- launcher/ui/pages/instance/ModFolderPage.cpp | 3 +- 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/launcher/ModDownloadTask.h b/launcher/ModDownloadTask.h index 7e4f1b7d..ddada5a2 100644 --- a/launcher/ModDownloadTask.h +++ b/launcher/ModDownloadTask.h @@ -10,6 +10,7 @@ class ModDownloadTask : public Task { Q_OBJECT public: explicit ModDownloadTask(const QUrl sourceUrl, const QString filename, const std::shared_ptr mods); + const QString& getFilename() const { return filename; } public slots: bool abort() override; diff --git a/launcher/ui/dialogs/ModDownloadDialog.cpp b/launcher/ui/dialogs/ModDownloadDialog.cpp index 6b807b8c..c2bf2d6f 100644 --- a/launcher/ui/dialogs/ModDownloadDialog.cpp +++ b/launcher/ui/dialogs/ModDownloadDialog.cpp @@ -52,6 +52,7 @@ ModDownloadDialog::ModDownloadDialog(const std::shared_ptr &mods HelpButton->setDefault(false); HelpButton->setAutoDefault(false); connect(HelpButton, &QPushButton::clicked, m_container, &PageContainer::help); + QMetaObject::connectSlotsByName(this); setWindowModality(Qt::WindowModal); setWindowTitle("Download mods"); @@ -83,16 +84,38 @@ QList ModDownloadDialog::getPages() }; } -void ModDownloadDialog::setSuggestedMod(const QString& name, ModDownloadTask* task) +void ModDownloadDialog::addSelectedMod(const QString& name, ModDownloadTask* task) { - modTask.reset(task); - m_buttons->button(QDialogButtonBox::Ok)->setEnabled(task); + if(modTask.contains(name)) + delete modTask.find(name).value(); + + if(task) + modTask.insert(name, task); + else + modTask.remove(name); + + m_buttons->button(QDialogButtonBox::Ok)->setEnabled(!modTask.isEmpty()); +} + +void ModDownloadDialog::removeSelectedMod(const QString &name) +{ + if(modTask.contains(name)) + delete modTask.find(name).value(); + modTask.remove(name); +} + +bool ModDownloadDialog::isModSelected(const QString &name, const QString& filename) const +{ + // FIXME: Is there a way to check for versions without checking the filename + // as a heuristic, other than adding such info to ModDownloadTask itself? + auto iter = modTask.find(name); + return iter != modTask.end() && (iter.value()->getFilename() == filename); } ModDownloadDialog::~ModDownloadDialog() { } -ModDownloadTask *ModDownloadDialog::getTask() { - return modTask.release(); +const QList ModDownloadDialog::getTasks() { + return modTask.values(); } diff --git a/launcher/ui/dialogs/ModDownloadDialog.h b/launcher/ui/dialogs/ModDownloadDialog.h index ece8e328..02870c6c 100644 --- a/launcher/ui/dialogs/ModDownloadDialog.h +++ b/launcher/ui/dialogs/ModDownloadDialog.h @@ -29,9 +29,11 @@ public: QString dialogTitle() override; QList getPages() override; - void setSuggestedMod(const QString & name = QString(), ModDownloadTask * task = nullptr); + void addSelectedMod(const QString & name = QString(), ModDownloadTask * task = nullptr); + void removeSelectedMod(const QString & name = QString()); + bool isModSelected(const QString & name, const QString & filename) const; - ModDownloadTask * getTask(); + const QList getTasks(); const std::shared_ptr &mods; public slots: @@ -49,6 +51,6 @@ private: ModrinthPage *modrinthPage = nullptr; FlameModPage *flameModPage = nullptr; - std::unique_ptr modTask; + QHash modTask; BaseInstance *m_instance; }; diff --git a/launcher/ui/pages/instance/ModFolderPage.cpp b/launcher/ui/pages/instance/ModFolderPage.cpp index 494d32f0..b342accf 100644 --- a/launcher/ui/pages/instance/ModFolderPage.cpp +++ b/launcher/ui/pages/instance/ModFolderPage.cpp @@ -365,8 +365,7 @@ void ModFolderPage::on_actionInstall_mods_triggered() } ModDownloadDialog mdownload(m_mods, this, m_inst); if(mdownload.exec()) { - ModDownloadTask *task = mdownload.getTask(); - if (task) { + for(auto task : mdownload.getTasks()){ connect(task, &Task::failed, [this, task](QString reason) { task->deleteLater(); CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->show();