syncthingtray/connector/syncthingprocess.cpp

71 lines
1.6 KiB
C++
Raw Normal View History

2016-09-03 19:39:43 +02:00
#include "./syncthingprocess.h"
#include <QTimer>
namespace Data {
2017-05-01 03:34:43 +02:00
SyncthingProcess::SyncthingProcess(QObject *parent)
: QProcess(parent)
2016-09-03 19:39:43 +02:00
{
setProcessChannelMode(QProcess::MergedChannels);
2017-05-01 03:34:43 +02:00
connect(this, static_cast<void (SyncthingProcess::*)(int exitCode, QProcess::ExitStatus exitStatus)>(&SyncthingProcess::finished), this,
&SyncthingProcess::handleFinished);
2016-09-03 19:39:43 +02:00
}
void SyncthingProcess::restartSyncthing(const QString &cmd)
2016-09-03 19:39:43 +02:00
{
2017-05-01 03:34:43 +02:00
if (state() == QProcess::Running) {
m_cmd = cmd;
2016-09-03 19:39:43 +02:00
// give Syncthing 5 seconds to terminate, otherwise kill it
QTimer::singleShot(5000, this, &SyncthingProcess::killToRestart);
2016-09-03 19:39:43 +02:00
terminate();
} else {
startSyncthing(cmd);
2016-09-03 19:39:43 +02:00
}
}
void SyncthingProcess::startSyncthing(const QString &cmd)
2016-09-03 19:39:43 +02:00
{
2017-05-01 03:34:43 +02:00
if (state() == QProcess::NotRunning) {
if (cmd.isEmpty()) {
start(QProcess::ReadOnly);
} else {
start(cmd, QProcess::ReadOnly);
}
}
2016-09-03 19:39:43 +02:00
}
void SyncthingProcess::stopSyncthing()
{
2017-05-01 03:34:43 +02:00
if (state() == QProcess::Running) {
// give Syncthing 5 seconds to terminate, otherwise kill it
QTimer::singleShot(5000, this, &SyncthingProcess::kill);
terminate();
}
}
2016-09-03 19:39:43 +02:00
void SyncthingProcess::handleFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
Q_UNUSED(exitCode)
Q_UNUSED(exitStatus)
2017-05-01 03:34:43 +02:00
if (!m_cmd.isEmpty()) {
startSyncthing(m_cmd);
m_cmd.clear();
2016-09-03 19:39:43 +02:00
}
}
void SyncthingProcess::killToRestart()
{
2017-05-01 03:34:43 +02:00
if (!m_cmd.isEmpty()) {
2016-09-03 19:39:43 +02:00
kill();
}
}
SyncthingProcess &syncthingProcess()
{
static SyncthingProcess process;
return process;
}
} // namespace Data