forked from MX-Linux/mx-packageinstaller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.cpp
57 lines (50 loc) · 1.65 KB
/
cmd.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
#include <QDebug>
#include <QEventLoop>
#include "cmd.h"
Cmd::Cmd(QObject *parent)
: QProcess(parent)
{
connect(this, &Cmd::readyReadStandardOutput, [=]() { emit outputAvailable(readAllStandardOutput()); });
connect(this, &Cmd::readyReadStandardError, [=]() { emit errorAvailable(readAllStandardError()); });
connect(this, &Cmd::outputAvailable, [=](const QString &out) { out_buffer += out; });
connect(this, &Cmd::errorAvailable, [=](const QString &out) { out_buffer += out; });
}
void Cmd::halt()
{
if (state() != QProcess::NotRunning) {
terminate();
waitForFinished(5000);
kill();
waitForFinished(1000);
}
}
bool Cmd::run(const QString &cmd, bool quiet)
{
out_buffer.clear();
QString output;
return run(cmd, output, quiet);
}
// util function for getting bash command output
QString Cmd::getCmdOut(const QString &cmd, bool quiet)
{
out_buffer.clear();
QString output;
run(cmd, output, quiet);
return output;
}
bool Cmd::run(const QString &cmd, QString &output, bool quiet)
{
out_buffer.clear();
connect(this, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, &Cmd::finished, Qt::UniqueConnection);
if (this->state() != QProcess::NotRunning) {
qDebug() << "Process already running:" << this->program() << this->arguments();
return false;
}
if (!quiet) qDebug().noquote() << cmd;
QEventLoop loop;
connect(this, &Cmd::finished, &loop, &QEventLoop::quit);
start("/bin/bash", QStringList() << "-c" << cmd);
loop.exec();
output = out_buffer.trimmed();
return (exitStatus() == QProcess::NormalExit && exitCode() == 0);
}