2016-09-03 19:39:43 +02:00
|
|
|
#include "./syncthingprocess.h"
|
|
|
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
2018-04-02 20:26:00 +02:00
|
|
|
using namespace ChronoUtilities;
|
|
|
|
|
2016-09-03 19:39:43 +02:00
|
|
|
namespace Data {
|
|
|
|
|
2018-04-11 23:15:15 +02:00
|
|
|
SyncthingProcess *SyncthingProcess::s_mainInstance = nullptr;
|
|
|
|
|
2017-05-01 03:34:43 +02:00
|
|
|
SyncthingProcess::SyncthingProcess(QObject *parent)
|
|
|
|
: QProcess(parent)
|
2018-04-02 20:26:00 +02:00
|
|
|
, m_manuallyStopped(false)
|
2016-09-03 19:39:43 +02:00
|
|
|
{
|
2018-04-02 23:29:39 +02:00
|
|
|
m_killTimer.setInterval(3000);
|
|
|
|
m_killTimer.setSingleShot(true);
|
2016-09-03 19:39:43 +02:00
|
|
|
setProcessChannelMode(QProcess::MergedChannels);
|
2018-04-02 20:26:00 +02:00
|
|
|
connect(this, &SyncthingProcess::started, this, &SyncthingProcess::handleStarted);
|
2017-05-01 03:34:43 +02:00
|
|
|
connect(this, static_cast<void (SyncthingProcess::*)(int exitCode, QProcess::ExitStatus exitStatus)>(&SyncthingProcess::finished), this,
|
|
|
|
&SyncthingProcess::handleFinished);
|
2018-04-02 23:29:39 +02:00
|
|
|
connect(&m_killTimer, &QTimer::timeout, this, &SyncthingProcess::confirmKill);
|
2016-09-03 19:39:43 +02:00
|
|
|
}
|
|
|
|
|
2018-04-18 23:27:45 +02:00
|
|
|
QStringList SyncthingProcess::splitArguments(const QString &arguments)
|
|
|
|
{
|
|
|
|
enum { Any, Quote, Slash, Space } lastInput = Any;
|
|
|
|
bool inQuotes = false;
|
|
|
|
QStringList result;
|
|
|
|
QString currentArg;
|
|
|
|
for (const auto c : arguments) {
|
|
|
|
switch (c.unicode()) {
|
|
|
|
case '\"':
|
|
|
|
case '\'':
|
|
|
|
switch (lastInput) {
|
|
|
|
case Slash:
|
|
|
|
currentArg += c;
|
|
|
|
lastInput = Any;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
inQuotes = !inQuotes;
|
|
|
|
lastInput = Quote;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
switch (lastInput) {
|
|
|
|
case Slash:
|
|
|
|
currentArg += c;
|
|
|
|
lastInput = Any;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
lastInput = Slash;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ' ':
|
|
|
|
switch (lastInput) {
|
|
|
|
case Slash:
|
2018-04-18 23:46:17 +02:00
|
|
|
if (!currentArg.isEmpty()) {
|
|
|
|
currentArg += c;
|
|
|
|
}
|
2018-04-18 23:27:45 +02:00
|
|
|
lastInput = Any;
|
|
|
|
break;
|
|
|
|
case Space:
|
|
|
|
if (inQuotes) {
|
|
|
|
currentArg += c;
|
|
|
|
lastInput = Any;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (inQuotes) {
|
|
|
|
currentArg += c;
|
|
|
|
lastInput = Any;
|
|
|
|
} else {
|
2018-04-18 23:46:17 +02:00
|
|
|
if (!currentArg.isEmpty()) {
|
|
|
|
result << currentArg;
|
|
|
|
currentArg.clear();
|
|
|
|
}
|
2018-04-18 23:27:45 +02:00
|
|
|
lastInput = Space;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
currentArg += c;
|
|
|
|
lastInput = Any;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!currentArg.isEmpty()) {
|
|
|
|
result << currentArg;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SyncthingProcess::restartSyncthing(const QString &program, const QStringList &arguments)
|
2016-09-03 19:39:43 +02:00
|
|
|
{
|
2018-04-02 20:26:00 +02:00
|
|
|
if (!isRunning()) {
|
2018-04-18 23:27:45 +02:00
|
|
|
startSyncthing(program, arguments);
|
2018-04-02 20:26:00 +02:00
|
|
|
return;
|
2016-09-03 19:39:43 +02:00
|
|
|
}
|
2018-04-18 23:27:45 +02:00
|
|
|
m_program = program;
|
|
|
|
m_arguments = arguments;
|
2018-04-02 20:26:00 +02:00
|
|
|
m_manuallyStopped = true;
|
2018-04-02 23:29:39 +02:00
|
|
|
m_killTimer.start();
|
2018-04-02 20:26:00 +02:00
|
|
|
terminate();
|
2016-09-03 19:39:43 +02:00
|
|
|
}
|
|
|
|
|
2018-04-18 23:27:45 +02:00
|
|
|
void SyncthingProcess::startSyncthing(const QString &program, const QStringList &arguments)
|
2016-09-03 19:39:43 +02:00
|
|
|
{
|
2018-04-02 20:26:00 +02:00
|
|
|
if (isRunning()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_manuallyStopped = false;
|
2018-04-02 23:29:39 +02:00
|
|
|
m_killTimer.stop();
|
2018-04-18 23:27:45 +02:00
|
|
|
start(program, arguments, QProcess::ReadOnly);
|
2016-09-03 19:39:43 +02:00
|
|
|
}
|
|
|
|
|
2017-03-17 00:38:42 +01:00
|
|
|
void SyncthingProcess::stopSyncthing()
|
|
|
|
{
|
2018-04-02 20:26:00 +02:00
|
|
|
if (!isRunning()) {
|
|
|
|
return;
|
2017-03-17 00:38:42 +01:00
|
|
|
}
|
2018-04-02 20:26:00 +02:00
|
|
|
m_manuallyStopped = true;
|
2018-04-02 23:29:39 +02:00
|
|
|
m_killTimer.start();
|
2018-04-02 20:26:00 +02:00
|
|
|
terminate();
|
|
|
|
}
|
|
|
|
|
2018-04-02 23:29:39 +02:00
|
|
|
void SyncthingProcess::killSyncthing()
|
|
|
|
{
|
|
|
|
if (!isRunning()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_manuallyStopped = true;
|
|
|
|
m_killTimer.stop();
|
|
|
|
kill();
|
|
|
|
}
|
|
|
|
|
2018-04-02 20:26:00 +02:00
|
|
|
void SyncthingProcess::handleStarted()
|
|
|
|
{
|
|
|
|
m_activeSince = DateTime::gmtNow();
|
2017-03-17 00:38:42 +01:00
|
|
|
}
|
|
|
|
|
2016-09-03 19:39:43 +02:00
|
|
|
void SyncthingProcess::handleFinished(int exitCode, QProcess::ExitStatus exitStatus)
|
|
|
|
{
|
|
|
|
Q_UNUSED(exitCode)
|
|
|
|
Q_UNUSED(exitStatus)
|
2018-04-02 20:26:00 +02:00
|
|
|
m_activeSince = DateTime();
|
2018-04-02 23:29:39 +02:00
|
|
|
m_killTimer.stop();
|
2018-04-18 23:27:45 +02:00
|
|
|
if (!m_program.isEmpty()) {
|
|
|
|
startSyncthing(m_program, m_arguments);
|
|
|
|
m_program.clear();
|
|
|
|
m_arguments.clear();
|
2016-09-03 19:39:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SyncthingProcess::killToRestart()
|
|
|
|
{
|
2018-04-18 23:27:45 +02:00
|
|
|
if (!m_program.isEmpty()) {
|
2016-09-03 19:39:43 +02:00
|
|
|
kill();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Data
|