-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagprocessthread.cpp
228 lines (185 loc) · 6.38 KB
/
agprocessthread.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "agprocessthread.h"
#include <QDebug>
#include <QApplication>
#include <QWidget>
#ifdef Q_OS_WINxxx
#include <QWinTaskbarProgress>
#endif
#include <QTimer>
#ifdef Q_OS_MAC
#include <QApplication>
#endif
AGProcessAndThread::AGProcessAndThread(QObject *parent):
QObject(parent)
{
#ifdef Q_OS_WINxxx
taskbarButton = new QWinTaskbarButton(this);
taskbarButton->setWindow(((QWidget *)parent)->windowHandle());
// taskbarButton->setOverlayIcon(QIcon(":/loading.png"));
#endif
}
AGProcessAndThread::~AGProcessAndThread()
{
// qDebug()<<"AGProcessAndThread::~AGProcessAndThread"<<name;
// if (process != nullptr)
// {
// delete process;
// process = nullptr;
// }
// if (jobThread != nullptr)
// {
// delete jobThread;
// jobThread = nullptr;
// }
}
void AGProcessAndThread::kill()
{
if (process != nullptr && process->state() != QProcess::NotRunning)
{
qDebug()<<"AGProcessAndThread::kill"<<name<<" kill";
processStopped = true;
process->kill();
qDebug()<<" "<<name<<" killed";
}
if (jobThread != nullptr && jobThread->isRunning())
{
qDebug()<<"AGProcessAndThread::kill"<<name<<" stopThreadProcess";
processStopped = true;
emit stopThreadProcess();
}
}
void AGProcessAndThread::command(QString name, QString commandString, int timeOffsetMsec)
{
this->name = name;
this->timeOffsetMsec = timeOffsetMsec;
process = new QProcess(this);
process->setProcessChannelMode(QProcess::MergedChannels);
connect (process, &QProcess::readyReadStandardOutput, [=]()
{
QString output = process->readAllStandardOutput();
QStringList outputStringList = output.split(QRegExp("[\r\n]"),QString::SkipEmptyParts);
foreach (QString outputString, outputStringList)
{
addProcessLog("output", outputString);
}
});
connect (process, &QProcess::readyReadStandardError, [=]()
{
addProcessLog("output", process->readAllStandardError()); //no error...
});
connect(process, &QProcess::errorOccurred, [=](QProcess::ProcessError error)
{
errorMessage = "Error " + QString::number(error) + ": " + process->errorString();
addProcessLog("error", errorMessage);
});
connect(process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), [=](int exitCode, QProcess::ExitStatus exitStatus)
{
addProcessLog("finished", "Finished " + QString::number(exitCode) + ": " + exitStatus + " " + QTime::currentTime().toString());
});
log.clear();
// log << commandString;
QString execPath = "";
#ifdef Q_OS_MAC
execPath = qApp->applicationDirPath() + "/";
#endif
this->commandString = execPath + commandString;
// process->setProgram(execPath + commandString); //does not work in MacOS
// process->start(execPath + commandString);
addProcessLog("started", "started " + QTime::currentTime().toString());
}
void AGProcessAndThread::command(QString name, std::function<void ()> commandFunction)
{
jobThread = QThread::create(commandFunction);
//https://www.kdab.com/new-qt-5-10-qthread-create/
jobThread->setParent(this);
jobThread->setObjectName(name);
connect(jobThread, &QThread::started, [=]()
{
emit addProcessLog("started", "started " + QTime::currentTime().toString());
});
connect(jobThread, &QThread::finished, [=]()
{
QTimer::singleShot(0, this, [=]()->void //timer needed to process other addProcessLogs first
{
emit addProcessLog("finished", "finished " + QTime::currentTime().toString());
});
});
this->name = name;
log.clear();
}
void AGProcessAndThread::start()
{
if (process != nullptr)
{
process->start(commandString);
addProcessLog("started", commandString);
}
if (jobThread != nullptr)
jobThread->start();
}
void AGProcessAndThread::addProcessLog(QString event, QString outputString)
{
QTime time = QTime();
int timeIndex = outputString.indexOf("time="); //ffmpeg and derperview logging
if (timeIndex >= 0)
{
QString timeString = outputString.mid(timeIndex + 5, 11) + "0";
time = QTime::fromString(timeString,"HH:mm:ss.zzz");
}
timeIndex = outputString.indexOf("% of"); //youtube-dl logging
//[download] 0.0% of 17.66MiB at 503.03KiB/s ETA 00:35
if (timeIndex >= 0)
{
QString timeString = outputString.mid(timeIndex - 5, 5);
time = QTime::fromMSecsSinceStartOfDay(timeString.toDouble() * 1000);
totalTime = QTime::fromMSecsSinceStartOfDay(100 * 1000);
// qDebug()<<"timeString"<<timeString<<timeString.toDouble()<<time<<totalTime;
}
log << outputString;
// qDebug()<<"outputString"<<outputString;
#ifdef Q_OS_WINxxx
QWinTaskbarProgress *progress = taskbarButton->progress();
progress->setVisible(true);
progress->setValue(time.msecsSinceStartOfDay() / totalTime.msecsSinceStartOfDay());
if (event == "error")
{
progress->setVisible(false);
}
if (event == "finished")
{
progress->stop();
QTimer::singleShot(5000, this, [progress]()->void //timer needed to show first message
{
progress->resume();
progress->setVisible(false);
});
}
#endif
if (event == "error")
{
// qDebug()<<__func__<<outputString;
errorMessage = outputString;
}
emit processOutput(time.addMSecs(timeOffsetMsec), totalTime, event, outputString);
}
//void AGProcessAndThread::start(QString name, const QString &commandString)
//{
// this->name = name;
// log.clear();
// log << commandString;
// QString execPath = "";
//#ifdef Q_OS_MAC
// execPath = qApp->applicationDirPath() + "/";
//#endif
// process->start(execPath + commandString);
//}
//void AGProcessAndThread::start(QString name, std::function<void ()> commandFunction)
//{
// this->name = name;
// log.clear();
// QThread *ano = QThread::create(commandFunction);
// ano->setParent(this);
// ano->setObjectName(name);
// jobThread->commandFunction = commandFunction;
// jobThread->start();
//}