From 64041a84a2da2d0ba07d6eb663ff7cbc3b351b7b Mon Sep 17 00:00:00 2001 From: Trial97 Date: Mon, 17 Jul 2023 16:55:26 +0300 Subject: [PATCH] handle file removal in ExportToZipTask Signed-off-by: Trial97 --- launcher/MMCZip.cpp | 11 +++++++++++ launcher/MMCZip.h | 12 +++++++++++- launcher/modplatform/flame/FlamePackExportTask.cpp | 6 +----- .../modplatform/modrinth/ModrinthPackExportTask.cpp | 6 +----- launcher/ui/dialogs/ExportInstanceDialog.cpp | 7 ++----- 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/launcher/MMCZip.cpp b/launcher/MMCZip.cpp index cc7491971..84d357874 100644 --- a/launcher/MMCZip.cpp +++ b/launcher/MMCZip.cpp @@ -480,4 +480,15 @@ void ExportToZipTask::exportZip() emitSucceeded(); } +void ExportToZipTask::emitAborted() +{ + QFile::remove(m_output_path); + Task::emitAborted(); +} +void ExportToZipTask::emitFailed(QString reason) +{ + QFile::remove(m_output_path); + Task::emitFailed(reason); +} + } // namespace MMCZip \ No newline at end of file diff --git a/launcher/MMCZip.h b/launcher/MMCZip.h index 60b3490fe..cb49e0c0f 100644 --- a/launcher/MMCZip.h +++ b/launcher/MMCZip.h @@ -148,7 +148,12 @@ bool collectFileListRecursively(const QString& rootDir, const QString& subDir, Q class ExportToZipTask : public Task { public: ExportToZipTask(QString outputPath, QDir dir, QFileInfoList files, QString destinationPrefix = "", bool followSymlinks = false) - : m_output(outputPath), m_dir(dir), m_files(files), m_destination_prefix(destinationPrefix), m_follow_symlinks(followSymlinks) + : m_output_path(outputPath) + , m_output(outputPath) + , m_dir(dir) + , m_files(files) + , m_destination_prefix(destinationPrefix) + , m_follow_symlinks(followSymlinks) { setAbortable(true); }; @@ -164,7 +169,12 @@ class ExportToZipTask : public Task { virtual void executeTask() override; void exportZip(); + protected slots: + virtual void emitAborted() override; + virtual void emitFailed(QString reason = "") override; + private: + QString m_output_path; QuaZip m_output; QDir m_dir; QFileInfoList m_files; diff --git a/launcher/modplatform/flame/FlamePackExportTask.cpp b/launcher/modplatform/flame/FlamePackExportTask.cpp index fc2cbc36d..a4efaf7cf 100644 --- a/launcher/modplatform/flame/FlamePackExportTask.cpp +++ b/launcher/modplatform/flame/FlamePackExportTask.cpp @@ -344,14 +344,10 @@ void FlamePackExportTask::buildZip() }); connect(zipTask.get(), &Task::succeeded, this, &FlamePackExportTask::emitSucceeded); - connect(zipTask.get(), &Task::aborted, this, [this]() { - QFile::remove(output); - emitAborted(); - }); + connect(zipTask.get(), &Task::aborted, this, &FlamePackExportTask::emitAborted); connect(zipTask.get(), &Task::failed, this, [this, progressStep](QString reason) { progressStep->state = TaskStepState::Failed; stepProgress(*progressStep); - QFile::remove(output); emitFailed(reason); }); connect(zipTask.get(), &Task::stepProgress, this, &FlamePackExportTask::propogateStepProgress); diff --git a/launcher/modplatform/modrinth/ModrinthPackExportTask.cpp b/launcher/modplatform/modrinth/ModrinthPackExportTask.cpp index 0470f8319..7290f4542 100644 --- a/launcher/modplatform/modrinth/ModrinthPackExportTask.cpp +++ b/launcher/modplatform/modrinth/ModrinthPackExportTask.cpp @@ -208,14 +208,10 @@ void ModrinthPackExportTask::buildZip() }); connect(zipTask.get(), &Task::succeeded, this, &ModrinthPackExportTask::emitSucceeded); - connect(zipTask.get(), &Task::aborted, this, [this]() { - QFile::remove(output); - emitAborted(); - }); + connect(zipTask.get(), &Task::aborted, this, &ModrinthPackExportTask::emitAborted); connect(zipTask.get(), &Task::failed, this, [this, progressStep](QString reason) { progressStep->state = TaskStepState::Failed; stepProgress(*progressStep); - QFile::remove(output); emitFailed(reason); }); connect(zipTask.get(), &Task::stepProgress, this, &ModrinthPackExportTask::propogateStepProgress); diff --git a/launcher/ui/dialogs/ExportInstanceDialog.cpp b/launcher/ui/dialogs/ExportInstanceDialog.cpp index 1a3f8cd41..d1a69b932 100644 --- a/launcher/ui/dialogs/ExportInstanceDialog.cpp +++ b/launcher/ui/dialogs/ExportInstanceDialog.cpp @@ -146,11 +146,8 @@ void ExportInstanceDialog::doExport() auto task = makeShared(output, m_instance->instanceRoot(), files, "", true); - connect(task.get(), &Task::failed, this, [this, output](QString reason) { - CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->show(); - QFile::remove(output); - }); - connect(task.get(), &Task::aborted, this, [output] { QFile::remove(output); }); + connect(task.get(), &Task::failed, this, + [this, output](QString reason) { CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->show(); }); connect(task.get(), &Task::finished, this, [task] { task->deleteLater(); }); ProgressDialog progress(this);