forked from gazelle-installer/gazelle-installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmprocess.cpp
executable file
·174 lines (162 loc) · 5.67 KB
/
mprocess.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
// MProcess class - Installer-specific extensions to QProcess.
//
// Copyright (C) 2019 by AK-47
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// This file is part of the gazelle-installer.
#include <QApplication>
#include <QDebug>
#include <QEventLoop>
#include <QTimer>
#include "mprocess.h"
MProcess::MProcess(QObject *parent)
: QProcess(parent)
{
}
void MProcess::setupUI(QListWidget *listLog, QProgressBar *progressBar)
{
logView = listLog;
progBar = progressBar;
QPalette pal = listLog->palette();
pal.setColor(QPalette::Base, Qt::black);
pal.setColor(QPalette::Text, Qt::white);
listLog->setPalette(pal);
}
bool MProcess::exec(const QString &cmd, const bool rawexec, const QByteArray *input, bool needRead)
{
if (halting) return false;
++execount;
qDebug().nospace() << "Exec #" << execount << ": " << cmd;
QListWidgetItem *logEntry = log(cmd, Exec);
QEventLoop eloop;
connect(this, static_cast<void (QProcess::*)(int)>(&QProcess::finished), &eloop, &QEventLoop::quit);
if (rawexec) start(cmd);
else start("/bin/bash", QStringList() << "-c" << cmd);
if (!debugUnusedOutput) {
if (!needRead) closeReadChannel(QProcess::StandardOutput);
closeReadChannel(QProcess::StandardError);
}
if (input && !(input->isEmpty())) write(*input);
closeWriteChannel();
eloop.exec();
disconnect(this, static_cast<void (QProcess::*)(int)>(&QProcess::finished), nullptr, nullptr);
if (debugUnusedOutput) {
bool hasOut = false;
if (!needRead) {
const QByteArray &StdOut = readAllStandardOutput();
if (!StdOut.isEmpty()) {
qDebug().nospace() << "SOut #" << execount << ": " << StdOut;
hasOut = 1;
}
}
const QByteArray &StdErr = readAllStandardError();
if (!StdErr.isEmpty()) {
qDebug().nospace() << "SErr #" << execount << ": " << StdErr;
hasOut = 1;
}
if(hasOut) {
QFont logFont = logEntry->font();
logFont.setItalic(true);
logEntry->setFont(logFont);
}
}
qDebug().nospace() << "Exit #" << execount << ": " << exitCode() << " " << exitStatus();
int status = 1;
if(exitStatus() != QProcess::NormalExit) status = -1;
else if(exitCode() != 0) status = 0;
log(logEntry, status);
return (status > 0);
}
QString MProcess::execOut(const QString &cmd, bool everything)
{
exec(cmd, false, nullptr, true);
QString strout(readAllStandardOutput().trimmed());
if (everything) return strout;
return strout.section("\n", 0, 0);
}
QStringList MProcess::execOutLines(const QString &cmd, const bool rawexec)
{
exec(cmd, rawexec, nullptr, true);
return QString(readAllStandardOutput().trimmed()).split('\n', QString::SkipEmptyParts);
}
void MProcess::sleep(const int msec, const bool silent)
{
if(!silent) {
++sleepcount;
qDebug().nospace() << "Sleep #" << sleepcount << ": " << msec << "ms";
}
QTimer cstimer(this);
QEventLoop eloop(this);
connect(&cstimer, &QTimer::timeout, &eloop, &QEventLoop::quit);
cstimer.start(msec);
int rc = eloop.exec();
if(!silent) qDebug().nospace() << "Sleep #" << sleepcount << ": exit " << rc;
}
void MProcess::halt()
{
halting = true;
terminate();
QTimer::singleShot(5000, this, &QProcess::kill);
}
void MProcess::unhalt()
{
QEventLoop eloop;
connect(this, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), &eloop, &QEventLoop::quit);
if (state() != QProcess::NotRunning) {
closeReadChannel(QProcess::StandardOutput);
closeReadChannel(QProcess::StandardError);
closeWriteChannel();
eloop.exec();
}
disconnect(this, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), nullptr, nullptr);
halting = false;
}
QListWidgetItem *MProcess::log(const QString &text, const LogType type)
{
if(type == Standard) qDebug().noquote() << text;
if(type == Section) qDebug().noquote() << "+++" << text << "+++";
else if(type == Status) qDebug().noquote() << "-" << text << "-";
if(!logView) return nullptr;
QListWidgetItem *entry = new QListWidgetItem(text, logView);
logView->addItem(entry);
if(type == Exec) entry->setTextColor(Qt::cyan);
else if(type != Standard) {
QFont font(entry->font());
if(type == Section) font.setBold(true);
else if(type == Status) font.setItalic(true);
entry->setFont(font);
}
logView->scrollToBottom();
return entry;
}
void MProcess::log(QListWidgetItem *entry, const int status)
{
if(!entry) return;
if(status > 0) entry->setTextColor(Qt::green);
else if(status < 0) entry->setTextColor(Qt::red);
else entry->setTextColor(Qt::yellow);
}
void MProcess::status(const QString &text, int progress)
{
if(!progBar) log(text, Status);
else {
QString fmt = "%p% - " + text;
if(progBar->format() != fmt) {
log(text, Status);
progBar->setFormat(fmt);
}
if(progress < 0) progress = progBar->value() + 1;
progBar->setValue(progress);
qApp->processEvents();
}
}