forked from AdrianTM/xdelta3-gui
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcmd.cpp
More file actions
63 lines (57 loc) · 1.92 KB
/
cmd.cpp
File metadata and controls
63 lines (57 loc) · 1.92 KB
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
#include "cmd.h"
#include <QDebug>
#include <QEventLoop>
Cmd::Cmd(QObject *parent)
: QProcess(parent)
{
connect(this, &Cmd::readyReadStandardOutput, [this] { emit outputAvailable(readAllStandardOutput()); });
connect(this, &Cmd::readyReadStandardError, [this] { emit errorAvailable(readAllStandardError()); });
connect(this, &Cmd::outputAvailable, [this](const QString &out) { out_buffer += out; });
connect(this, &Cmd::errorAvailable, [this](const QString &out) { out_buffer += out; });
connect(this, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, &Cmd::done);
}
bool Cmd::run(const QString &cmd, Output output)
{
QString out;
return run(cmd, &out, output);
}
QString Cmd::getCmdOut(const QString &cmd, Output output)
{
QString out;
run(cmd, &out, output);
return out;
}
bool Cmd::run(const QString &cmd, QString *out, Output output)
{
out_buffer.clear();
if (state() != QProcess::NotRunning) {
qDebug() << "Process already running:" << program() << arguments();
return false;
}
if (output == Verbose) {
qDebug().noquote() << cmd;
}
QEventLoop loop;
connect(this, &Cmd::done, &loop, &QEventLoop::quit);
start("/bin/bash", {"-c", cmd});
loop.exec();
*out = out_buffer.trimmed();
return (exitStatus() == QProcess::NormalExit && exitCode() == 0);
}
bool Cmd::run(const QString &program, const QStringList &args, QString *out, Output output)
{
out_buffer.clear();
if (state() != QProcess::NotRunning) {
qDebug() << "Process already running:" << this->program() << arguments();
return false;
}
if (output == Verbose) {
qDebug().noquote() << program << args;
}
QEventLoop loop;
connect(this, &Cmd::done, &loop, &QEventLoop::quit);
start(program, args);
loop.exec();
*out = out_buffer.trimmed();
return (exitStatus() == QProcess::NormalExit && exitCode() == 0);
}