From 61716687c96947f86736072b76ac4fe23c949cf3 Mon Sep 17 00:00:00 2001 From: Martchus Date: Sun, 7 Jul 2019 14:31:09 +0200 Subject: [PATCH] Handle QProcess::errorOccurred --- widgets/misc/syncthinglauncher.cpp | 1 + widgets/misc/syncthinglauncher.h | 1 + widgets/settings/settingsdialog.cpp | 63 ++++++++++++++++++++++++++--- widgets/settings/settingsdialog.h | 1 + 4 files changed, 61 insertions(+), 5 deletions(-) diff --git a/widgets/misc/syncthinglauncher.cpp b/widgets/misc/syncthinglauncher.cpp index 8f86494..f81f840 100644 --- a/widgets/misc/syncthinglauncher.cpp +++ b/widgets/misc/syncthinglauncher.cpp @@ -20,6 +20,7 @@ SyncthingLauncher::SyncthingLauncher(QObject *parent) connect(&m_process, &SyncthingProcess::readyRead, this, &SyncthingLauncher::handleProcessReadyRead); connect(&m_process, static_cast(&SyncthingProcess::finished), this, &SyncthingLauncher::handleProcessFinished); + connect(&m_process, &SyncthingProcess::errorOccurred, this, &SyncthingLauncher::errorOccurred); connect(&m_process, &SyncthingProcess::confirmKill, this, &SyncthingLauncher::confirmKill); } diff --git a/widgets/misc/syncthinglauncher.h b/widgets/misc/syncthinglauncher.h index 0a82586..813fffa 100644 --- a/widgets/misc/syncthinglauncher.h +++ b/widgets/misc/syncthinglauncher.h @@ -38,6 +38,7 @@ Q_SIGNALS: void runningChanged(bool isRunning); void outputAvailable(const QByteArray &data); void exited(int exitCode, QProcess::ExitStatus exitStatus); + void errorOccurred(QProcess::ProcessError error); public Q_SLOTS: void setUseLibSyncthing(bool useLibSyncthing); diff --git a/widgets/settings/settingsdialog.cpp b/widgets/settings/settingsdialog.cpp index 01ffe0d..6e4cbb5 100644 --- a/widgets/settings/settingsdialog.cpp +++ b/widgets/settings/settingsdialog.cpp @@ -51,6 +51,7 @@ #include #include #include +#include #include #include @@ -793,10 +794,13 @@ QWidget *LauncherOptionPage::setupWidget() m_connections << connect(m_process, static_cast(&SyncthingProcess::finished), this, &LauncherOptionPage::handleSyncthingExited, Qt::QueuedConnection); + m_connections << connect(m_process, &SyncthingProcess::errorOccurred, this, &LauncherOptionPage::handleSyncthingError, Qt::QueuedConnection); } else if (m_launcher) { m_connections << connect( m_launcher, &SyncthingLauncher::outputAvailable, this, &LauncherOptionPage::handleSyncthingOutputAvailable, Qt::QueuedConnection); m_connections << connect(m_launcher, &SyncthingLauncher::exited, this, &LauncherOptionPage::handleSyncthingExited, Qt::QueuedConnection); + m_connections << connect( + m_launcher, &SyncthingLauncher::errorOccurred, this, &LauncherOptionPage::handleSyncthingError, Qt::QueuedConnection); } QObject::connect(ui()->launchNowPushButton, &QPushButton::clicked, this, &LauncherOptionPage::launch); QObject::connect(ui()->stopPushButton, &QPushButton::clicked, this, &LauncherOptionPage::stop); @@ -856,6 +860,7 @@ void LauncherOptionPage::handleSyncthingOutputAvailable(const QByteArray &output cursor.movePosition(QTextCursor::End); cursor.insertText(QString::fromUtf8(output)); if (ui()->ensureCursorVisibleCheckBox->isChecked()) { + ui()->logTextEdit->moveCursor(QTextCursor::End); ui()->logTextEdit->ensureCursorVisible(); } } @@ -865,25 +870,73 @@ void LauncherOptionPage::handleSyncthingExited(int exitCode, QProcess::ExitStatu if (!hasBeenShown()) { return; } + QTextCursor cursor(ui()->logTextEdit->textCursor()); cursor.movePosition(QTextCursor::End); - if (cursor.positionInBlock()) { - cursor.insertBlock(); - } + cursor.insertBlock(); + switch (exitStatus) { case QProcess::NormalExit: - cursor.insertText(QCoreApplication::translate("QtGui::LauncherOptionPage", "%1 exited with exit code %2\n") + cursor.insertText(QCoreApplication::translate("QtGui::LauncherOptionPage", "%1 exited with exit code %2") .arg(m_tool.isEmpty() ? QStringLiteral("Syncthing") : m_tool, QString::number(exitCode))); break; case QProcess::CrashExit: - cursor.insertText(QCoreApplication::translate("QtGui::LauncherOptionPage", "%1 crashed with exit code %2\n") + cursor.insertText(QCoreApplication::translate("QtGui::LauncherOptionPage", "%1 crashed with exit code %2") .arg(m_tool.isEmpty() ? QStringLiteral("Syncthing") : m_tool, QString::number(exitCode))); break; } + cursor.insertBlock(); + + if (ui()->ensureCursorVisibleCheckBox->isChecked()) { + ui()->logTextEdit->moveCursor(QTextCursor::End); + ui()->logTextEdit->ensureCursorVisible(); + } + ui()->stopPushButton->hide(); ui()->launchNowPushButton->show(); } +void LauncherOptionPage::handleSyncthingError(QProcess::ProcessError error) +{ + if (!hasBeenShown()) { + return; + } + + QTextCursor cursor(ui()->logTextEdit->textCursor()); + cursor.movePosition(QTextCursor::End); + cursor.insertBlock(); + + QString errorString; + switch (error) { + case QProcess::FailedToStart: + errorString + = QCoreApplication::translate("QtGui::LauncherOptionPage", "failed to start (e.g. executable does not exist or not permission error)"); + break; + case QProcess::Crashed: + errorString = QCoreApplication::translate("QtGui::LauncherOptionPage", "process crashed"); + break; + case QProcess::Timedout: + errorString = QCoreApplication::translate("QtGui::LauncherOptionPage", "timeout error"); + break; + case QProcess::ReadError: + errorString = QCoreApplication::translate("QtGui::LauncherOptionPage", "read error"); + break; + case QProcess::WriteError: + errorString = QCoreApplication::translate("QtGui::LauncherOptionPage", "write error"); + break; + default: + errorString = QCoreApplication::translate("QtGui::LauncherOptionPage", "unknown process error"); + } + cursor.insertText(QCoreApplication::translate("QtGui::LauncherOptionPage", "An error occurred when running %1: %2") + .arg(m_tool.isEmpty() ? QStringLiteral("Syncthing") : m_tool, errorString)); + cursor.insertBlock(); + + if ((m_launcher && !m_launcher->isRunning()) || (m_process && !m_process->isRunning())) { + ui()->stopPushButton->hide(); + ui()->launchNowPushButton->show(); + } +} + bool LauncherOptionPage::isRunning() const { return (m_process && m_process->isRunning()) || (m_launcher && m_launcher->isRunning()); diff --git a/widgets/settings/settingsdialog.h b/widgets/settings/settingsdialog.h index 3ec6687..41c0d07 100644 --- a/widgets/settings/settingsdialog.h +++ b/widgets/settings/settingsdialog.h @@ -109,6 +109,7 @@ private slots: void handleSyncthingReadyRead(); void handleSyncthingOutputAvailable(const QByteArray &output); void handleSyncthingExited(int exitCode, QProcess::ExitStatus exitStatus); + void handleSyncthingError(QProcess::ProcessError error); bool isRunning() const; void launch(); void stop();