-
-
Notifications
You must be signed in to change notification settings - Fork 226
/
Copy pathtbackgroundprocess.cpp
103 lines (81 loc) · 3.25 KB
/
tbackgroundprocess.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
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
100
101
102
103
#include "tbackgroundprocess.h"
#include <QMetaType>
#include <QObject>
#include <TBackgroundProcessHandler>
#include <TWebApplication>
/*!
\class TBackgroundProcess
\brief The TBackgroundProcess class is used to start external programs in background.
*/
TBackgroundProcess::TBackgroundProcess(QObject *parent) :
QProcess(parent)
{
moveToThread(Tf::app()->databaseContextMainThread());
}
void TBackgroundProcess::connectToSlots(TBackgroundProcessHandler *handler)
{
if (handler) {
QObject::connect(this, SIGNAL(finished(int, QProcess::ExitStatus)), handler, SLOT(handleFinished(int, QProcess::ExitStatus)));
QObject::connect(this, SIGNAL(readyReadStandardError()), handler, SLOT(handleReadyReadStandardError()));
QObject::connect(this, SIGNAL(readyReadStandardOutput()), handler, SLOT(handleReadyReadStandardOutput()));
QObject::connect(this, SIGNAL(started()), handler, SLOT(handleStarted()));
QObject::connect(this, SIGNAL(stateChanged(QProcess::ProcessState)), handler, SLOT(handleStateChanged(QProcess::ProcessState)));
QObject::connect(this, SIGNAL(errorOccurred(QProcess::ProcessError)), handler, SLOT(handleErrorOccurred(QProcess::ProcessError)));
QObject::connect(this, SIGNAL(finished(int, QProcess::ExitStatus)), handler, SLOT(deleteAutoDeleteHandler()));
QObject::connect(this, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(handleFinished()), Qt::QueuedConnection);
handler->_process = this;
}
}
void TBackgroundProcess::start(const QString &program, const QStringList &arguments, OpenMode mode, TBackgroundProcessHandler *handler)
{
connectToSlots(handler);
QMetaObject::invokeMethod(this, "callStart", Qt::QueuedConnection,
Q_ARG(QString, program),
Q_ARG(QStringList, arguments),
Q_ARG(int, mode));
}
void TBackgroundProcess::start(const QString &command, OpenMode mode, TBackgroundProcessHandler *handler)
{
connectToSlots(handler);
QMetaObject::invokeMethod(this, "callStart", Qt::QueuedConnection,
Q_ARG(QString, command),
Q_ARG(QStringList, QStringList()),
Q_ARG(int, mode));
}
void TBackgroundProcess::start(OpenMode mode, TBackgroundProcessHandler *handler)
{
connectToSlots(handler);
QMetaObject::invokeMethod(this, "callStart", Qt::QueuedConnection,
Q_ARG(QString, QString()),
Q_ARG(QStringList, QStringList()),
Q_ARG(int, mode));
}
bool TBackgroundProcess::autoDelete() const
{
return _autoDelete;
}
void TBackgroundProcess::setAutoDelete(bool autoDelete)
{
_autoDelete = autoDelete;
}
void TBackgroundProcess::handleFinished()
{
if (_autoDelete) {
deleteLater();
}
}
void TBackgroundProcess::callStart(const QString &program, const QStringList &arguments, int mode)
{
if (program.isEmpty()) {
QProcess::start((OpenMode)mode);
} else {
QProcess::start(program, arguments, (OpenMode)mode);
}
}
// RegisterMetaType - QProcess::ProcessState
Q_DECLARE_METATYPE(QProcess::ProcessState)
class StaticQProcessProcessStateRegisterMetaType {
public:
StaticQProcessProcessStateRegisterMetaType() { qRegisterMetaType<QProcess::ProcessState>(); }
};
static StaticQProcessProcessStateRegisterMetaType _staticQProcessProcessStateRegisterMetaType;