-
Notifications
You must be signed in to change notification settings - Fork 4
/
parparclient.cpp
70 lines (64 loc) · 2.01 KB
/
parparclient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "parparclient.h"
#include "settings.h"
#include <QJsonDocument>
#include <QJsonObject>
ParParClient::ParParClient(QObject *parent)
: QObject{parent}
{
connect(&parpar, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, &ParParClient::finished);
// treat failing to start, like a crash
connect(&parpar, &QProcess::errorOccurred, this, [this](QProcess::ProcessError err) {
if(err == QProcess::FailedToStart) {
this->finished(0, QProcess::ExitStatus::CrashExit);
}
// let other handler handle other errors
});
isRunning = false;
isCancelled = false;
timer.setSingleShot(true);
connect(&timer, &QTimer::timeout, this, [this]() {
this->parpar.kill();
});
}
void ParParClient::run(const QStringList& _args, int timeout)
{
auto cmd = Settings::getInstance().parparBin();
QStringList args{"--json"};
args.append(_args);
if(cmd.length() > 1)
args.prepend(cmd[1]);
isRunning = true;
isCancelled = false;
stdoutBuffer.clear();
if(timeout)
{
timer.setInterval(timeout);
timer.start();
}
parpar.start(cmd[0], args);
}
void ParParClient::finished(int exitCode, QProcess::ExitStatus exitStatus)
{
timer.stop();
isRunning = false;
if(isCancelled) {
emit failed(QString());
} else if(exitStatus != QProcess::ExitStatus::NormalExit) {
emit failed(tr("ParPar process crashed or failed to start"));
} else if(exitCode != 0) {
emit failed(tr("ParPar failure (exit code: %1)").arg(exitCode));
} else {
const auto data = QJsonDocument::fromJson(parpar.readAllStandardOutput());
if(data.isNull() || !data.isObject()) {
auto message = QString::fromUtf8(parpar.readAllStandardError());
if(message.isEmpty()) message = "No output received";
emit failed(message);
} else
emit output(data.object());
}
}
void ParParClient::kill()
{
isCancelled = true;
parpar.kill();
}