-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBeaconExternalProgram.hpp
99 lines (95 loc) · 2.86 KB
/
BeaconExternalProgram.hpp
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#ifndef BF_EXTERNALPROGRAM
#define BF_EXTERNALPROGRAM
#define BF_EXTERNALPROGRAMVERSION "1.0.0"
#include <QObject>
#include <QFile>
#include <QProcess>
#include <QDebug>
class BeaconExternalProgram : public QObject
{
Q_OBJECT
public:
explicit BeaconExternalProgram(QObject *parent = nullptr)
{
connect(&program,SIGNAL(readyReadStandardOutput()),this,SLOT(programOutputUpdatedTriggered()));
connect(&program,SIGNAL(readyReadStandardError()),this,SLOT(programErrorUpdatedTriggered()));
connect(&program,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(sendProgramExited(int,QProcess::ExitStatus)));
}
QProcess program;
QFile programFile;
QString programPath;
QStringList arguments;
QByteArray standardOut,errorOut,logOut;
QByteArray standardOutDelta,errorOutDelta,logOutDelta;
bool setProgram(QString path,QStringList argu=QString("").split('.'))
{
programPath=path;
arguments=argu;
program.setProgram(programPath);
program.setArguments(arguments);
programFile.setFileName(path);
if(!programFile.exists())return false;
return true;
}
void setArguments(QStringList arg)
{
program.setArguments(arg);
}
bool startProgram()
{
program.start();
if (!program.waitForStarted()){
return false;
}
return true;
}
bool startProgramDetached()
{
program.startDetached();
if (!program.waitForStarted()){
return false;
}
return true;
}
void clearLog()
{
this->logOut.clear();
this->logOutDelta.clear();
this->errorOut.clear();
this->errorOutDelta.clear();
this->standardOut.clear();
this->standardOutDelta.clear();
}
signals:
void programOutputUpdated(QString content);
void programErrorUpdated(QString content);
void programLogUpdated(QString content);
void programExited(int result,QProcess::ExitStatus);
public slots:
void sendProgramExited(int result,QProcess::ExitStatus status)
{
emit programExited(result,status);
}
void programOutputUpdatedTriggered()
{
QByteArray ba = program.readAllStandardOutput();
standardOut.append(ba);
logOut.append(ba);
standardOutDelta=ba;
logOutDelta=ba;
qDebug() << "[BEP]update:" << QString(ba);
emit programOutputUpdated(ba);
emit programLogUpdated(ba);
}
void programErrorUpdatedTriggered()
{
QByteArray ba = program.readAllStandardError();
errorOut.append(ba);
logOut.append(ba);
errorOutDelta=ba;
logOutDelta=ba;
emit programErrorUpdated(ba);
emit programLogUpdated(ba);
}
};
#endif // BF_EXTERNALPROGRAM