syncthingtray/connector/syncthingprocess.cpp

70 lines
1.6 KiB
C++
Raw Normal View History

2016-09-03 19:39:43 +02:00
#include "./syncthingprocess.h"
#include <QTimer>
namespace Data {
SyncthingProcess::SyncthingProcess(QObject *parent) :
QProcess(parent)
2016-09-03 19:39:43 +02:00
{
setProcessChannelMode(QProcess::MergedChannels);
connect(this, static_cast<void(SyncthingProcess::*)(int exitCode, QProcess::ExitStatus exitStatus)>(&SyncthingProcess::finished), this, &SyncthingProcess::handleFinished);
}
void SyncthingProcess::restartSyncthing(const QString &cmd)
2016-09-03 19:39: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
{
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()
{
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)
if(!m_cmd.isEmpty()) {
startSyncthing(m_cmd);
m_cmd.clear();
2016-09-03 19:39:43 +02:00
}
}
void SyncthingProcess::killToRestart()
{
if(!m_cmd.isEmpty()) {
2016-09-03 19:39:43 +02:00
kill();
}
}
SyncthingProcess &syncthingProcess()
{
static SyncthingProcess process;
return process;
}
} // namespace Data