#include "./syncthingprocess.h" #include namespace Data { SyncthingProcess::SyncthingProcess(QObject *parent) : QProcess(parent) { setProcessChannelMode(QProcess::MergedChannels); connect(this, static_cast(&SyncthingProcess::finished), this, &SyncthingProcess::handleFinished); } void SyncthingProcess::restartSyncthing(const QString &cmd) { if(state() == QProcess::Running) { m_cmd = cmd; // give Syncthing 5 seconds to terminate, otherwise kill it QTimer::singleShot(5000, this, SLOT(killToRestart())); terminate(); } else { startSyncthing(cmd); } } void SyncthingProcess::startSyncthing(const QString &cmd) { if(state() == QProcess::NotRunning) { if(cmd.isEmpty()) { start(QProcess::ReadOnly); } else { start(cmd, QProcess::ReadOnly); } } } void SyncthingProcess::handleFinished(int exitCode, QProcess::ExitStatus exitStatus) { Q_UNUSED(exitCode) Q_UNUSED(exitStatus) if(!m_cmd.isEmpty()) { startSyncthing(m_cmd); m_cmd.clear(); } } void SyncthingProcess::killToRestart() { if(!m_cmd.isEmpty()) { kill(); } } SyncthingProcess &syncthingProcess() { static SyncthingProcess process; return process; } } // namespace Data