From 7d2d0164cfd7e4fa7281fdc122789202c978fa61 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Mon, 1 Jun 2015 22:49:46 +0200 Subject: [PATCH] GUI: confirm and abort running job on quit --- ChangeLog | 4 +++ src/mkvtoolnix-gui/jobs/job.cpp | 1 + src/mkvtoolnix-gui/jobs/job.h | 1 + src/mkvtoolnix-gui/jobs/model.cpp | 19 +++++++++++ src/mkvtoolnix-gui/jobs/model.h | 2 ++ src/mkvtoolnix-gui/jobs/mux_job.cpp | 5 +++ .../main_window/main_window.cpp | 32 +++++++++++++++++++ src/mkvtoolnix-gui/main_window/main_window.h | 1 + 8 files changed, 65 insertions(+) diff --git a/ChangeLog b/ChangeLog index d582f15da..cfc0bd1c9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2015-06-01 Moritz Bunkus + * MKVToolNix GUI: bug fix: if a job is running when the user wants + to quit requires confirmation from the user that the running job + should be aborted. Fixes #1219. + * MKVToolNix GUI: bug fix: fixed the initial status display when viewing a job's output from the queue. diff --git a/src/mkvtoolnix-gui/jobs/job.cpp b/src/mkvtoolnix-gui/jobs/job.cpp index 2a9b015ed..f150d014c 100644 --- a/src/mkvtoolnix-gui/jobs/job.cpp +++ b/src/mkvtoolnix-gui/jobs/job.cpp @@ -17,6 +17,7 @@ Job::Job(Status status) , m_exitCode{std::numeric_limits::max()} , m_warningsAcknowledged{} , m_errorsAcknowledged{} + , m_quitAfterFinished{} , m_mutex{QMutex::Recursive} { connect(this, &Job::lineRead, this, &Job::addLineToInternalLogs); diff --git a/src/mkvtoolnix-gui/jobs/job.h b/src/mkvtoolnix-gui/jobs/job.h index 612e50d50..1f2a4422b 100644 --- a/src/mkvtoolnix-gui/jobs/job.h +++ b/src/mkvtoolnix-gui/jobs/job.h @@ -48,6 +48,7 @@ class Job: public QObject { QStringList m_output, m_warnings, m_errors, m_fullOutput; unsigned int m_progress, m_exitCode, m_warningsAcknowledged, m_errorsAcknowledged; QDateTime m_dateAdded, m_dateStarted, m_dateFinished; + bool m_quitAfterFinished; QMutex m_mutex; diff --git a/src/mkvtoolnix-gui/jobs/model.cpp b/src/mkvtoolnix-gui/jobs/model.cpp index 65ac261bd..efc01563c 100644 --- a/src/mkvtoolnix-gui/jobs/model.cpp +++ b/src/mkvtoolnix-gui/jobs/model.cpp @@ -91,6 +91,17 @@ Model::hasJobs() return !!rowCount(); } +bool +Model::hasRunningJobs() { + QMutexLocker locked{&m_mutex}; + + for (auto const &job : m_jobsById) + if (Job::Running == job->m_status) + return true; + + return false; +} + void Model::setRowText(QList const &items, Job const &job) @@ -148,6 +159,14 @@ Model::withSelectedJobs(QAbstractItemView *view, worker(*job); } +void +Model::withAllJobs(std::function const &worker) { + QMutexLocker locked{&m_mutex}; + + for (auto const &job : m_jobsById) + worker(*job); +} + void Model::withJob(uint64_t id, std::function const &worker) { diff --git a/src/mkvtoolnix-gui/jobs/model.h b/src/mkvtoolnix-gui/jobs/model.h index 2a2a6a23b..1e5549048 100644 --- a/src/mkvtoolnix-gui/jobs/model.h +++ b/src/mkvtoolnix-gui/jobs/model.h @@ -48,8 +48,10 @@ class Model: public QStandardItemModel { Job *fromId(uint64_t id) const; int rowFromId(uint64_t id) const; bool hasJobs() const; + bool hasRunningJobs(); void withSelectedJobs(QAbstractItemView *view, std::function const &worker); + void withAllJobs(std::function const &worker); void withJob(uint64_t id, std::function const &worker); void removeJobsIf(std::function predicate); diff --git a/src/mkvtoolnix-gui/jobs/mux_job.cpp b/src/mkvtoolnix-gui/jobs/mux_job.cpp index e60bfe7a7..21e3ac99d 100644 --- a/src/mkvtoolnix-gui/jobs/mux_job.cpp +++ b/src/mkvtoolnix-gui/jobs/mux_job.cpp @@ -6,9 +6,11 @@ #include #include #include +#include #include "common/qt.h" #include "mkvtoolnix-gui/jobs/mux_job.h" +#include "mkvtoolnix-gui/main_window/main_window.h" #include "mkvtoolnix-gui/merge/mux_config.h" #include "mkvtoolnix-gui/util/option_file.h" #include "mkvtoolnix-gui/util/settings.h" @@ -127,6 +129,9 @@ MuxJob::processFinished(int exitCode, : Job::Failed; setStatus(status); + + if (m_quitAfterFinished) + QTimer::singleShot(0, MainWindow::get(), SLOT(close())); } void diff --git a/src/mkvtoolnix-gui/main_window/main_window.cpp b/src/mkvtoolnix-gui/main_window/main_window.cpp index dcb057ad8..a0d0ddf1f 100644 --- a/src/mkvtoolnix-gui/main_window/main_window.cpp +++ b/src/mkvtoolnix-gui/main_window/main_window.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -274,8 +275,39 @@ MainWindow::retranslateUi() { ui->tool->setUpdatesEnabled(true); } +bool +MainWindow::beforeCloseCheckRunningJobs() { + auto tool = jobTool(); + if (!tool) + return true; + + auto model = tool->model(); + if (!model->hasRunningJobs()) + return true; + + if (QMessageBox::question(this, QY("Abort running jobs"), Q("%1 %2").arg(QY("There is currently a job running.")).arg(QY("Do you want to abort that job and quit?"))) == QMessageBox::No) + return false; + + model->stop(); + model->withAllJobs([](Jobs::Job &job) { + if (Jobs::Job::Running == job.m_status) { + job.m_quitAfterFinished = true; + job.abort(); + } + }); + + return false; +} + void MainWindow::closeEvent(QCloseEvent *event) { + auto ok = beforeCloseCheckRunningJobs(); + + if (!ok) { + event->ignore(); + return; + } + QSettings reg; auto tool = jobTool(); diff --git a/src/mkvtoolnix-gui/main_window/main_window.h b/src/mkvtoolnix-gui/main_window/main_window.h index 4193dad08..75400786b 100644 --- a/src/mkvtoolnix-gui/main_window/main_window.h +++ b/src/mkvtoolnix-gui/main_window/main_window.h @@ -112,6 +112,7 @@ public slots: virtual QWidget *createNotImplementedWidget(); virtual void closeEvent(QCloseEvent *event); + virtual bool beforeCloseCheckRunningJobs(); #if defined(HAVE_CURL_EASY_H) virtual void silentlyCheckForUpdates();