Skip to content

Commit

Permalink
GUI: confirm and abort running job on quit
Browse files Browse the repository at this point in the history
  • Loading branch information
mbunkus committed Jun 1, 2015
1 parent f8fc44d commit 7d2d016
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
2015-06-01 Moritz Bunkus <moritz@bunkus.org>

* 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.

Expand Down
1 change: 1 addition & 0 deletions src/mkvtoolnix-gui/jobs/job.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Job::Job(Status status)
, m_exitCode{std::numeric_limits<unsigned int>::max()}
, m_warningsAcknowledged{}
, m_errorsAcknowledged{}
, m_quitAfterFinished{}
, m_mutex{QMutex::Recursive}
{
connect(this, &Job::lineRead, this, &Job::addLineToInternalLogs);
Expand Down
1 change: 1 addition & 0 deletions src/mkvtoolnix-gui/jobs/job.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
19 changes: 19 additions & 0 deletions src/mkvtoolnix-gui/jobs/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<QStandardItem *> const &items,
Job const &job)
Expand Down Expand Up @@ -148,6 +159,14 @@ Model::withSelectedJobs(QAbstractItemView *view,
worker(*job);
}

void
Model::withAllJobs(std::function<void(Job &)> const &worker) {
QMutexLocker locked{&m_mutex};

for (auto const &job : m_jobsById)
worker(*job);
}

void
Model::withJob(uint64_t id,
std::function<void(Job &)> const &worker) {
Expand Down
2 changes: 2 additions & 0 deletions src/mkvtoolnix-gui/jobs/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<void(Job &)> const &worker);
void withAllJobs(std::function<void(Job &)> const &worker);
void withJob(uint64_t id, std::function<void(Job &)> const &worker);

void removeJobsIf(std::function<bool(Job const &)> predicate);
Expand Down
5 changes: 5 additions & 0 deletions src/mkvtoolnix-gui/jobs/mux_job.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
#include <QSettings>
#include <QStringList>
#include <QTemporaryFile>
#include <QTimer>

#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"
Expand Down Expand Up @@ -127,6 +129,9 @@ MuxJob::processFinished(int exitCode,
: Job::Failed;

setStatus(status);

if (m_quitAfterFinished)
QTimer::singleShot(0, MainWindow::get(), SLOT(close()));
}

void
Expand Down
32 changes: 32 additions & 0 deletions src/mkvtoolnix-gui/main_window/main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <QDesktopServices>
#include <QIcon>
#include <QLabel>
#include <QMessageBox>
#include <QSettings>
#include <QStaticText>
#include <QVBoxLayout>
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/mkvtoolnix-gui/main_window/main_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 7d2d016

Please sign in to comment.