This repository has been archived by the owner on Mar 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
terminaldialog.cpp
273 lines (248 loc) · 9.18 KB
/
terminaldialog.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include "terminaldialog.h"
#include "ui_terminaldialog.h"
#include <QTreeWidgetItem>
#include <QScrollBar>
#include <QProcess>
//#include <QSizeF>
#include <QTimer>
#include <QStringList>
#include <mainwindow.h>
#include <QTextCodec>
#include <QTextBrowser>
#include "inputdialog.h"
#include <QDesktopWidget>
TerminalDialog::TerminalDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::TerminalDialog)
{
ui->setupUi(this);
FUI = (MainWindow*)parent;//((MainWindow*)FUI)->octonetwork.SendGCode(_GCode_Setting);
cmd = new QProcess(this);
/* 将QProcess的输出错误打印到界面 */
connect(cmd,&QProcess::readyReadStandardError,this,[=](){
if(!SendCMD_argu.contains("-e")) ui->Receiver->append("->Receive->->-> Standard Error");
QString outStr = QString::fromLocal8Bit(cmd->readAllStandardError());
if(!SendCMD_argu.contains("-e")) ui->Receiver->append("<<StandardError:");
if(!SendCMD_argu.contains("-e")) ui->Receiver->append(outStr.trimmed());
if(!SendCMD_argu.contains("-e")) ui->Receiver->horizontalScrollBar()->setValue(ui->Receiver->horizontalScrollBar()->maximum());
// QStringList _output;
// _output<<"&&StandardError"<<outStr;
// emit CMD_Reply(_output);//抛出槽触发
// if(outStr.trimmed().contains("sudo") && outStr.trimmed().contains("askpass") && command.contains("sudo "))
// {
// int startlocal = command.indexOf("sudo ");
// command = command.replace(startlocal,5,"echo \"" + CMD_Password + "\" | sudo -S ");//片段阔幅代码未处理
// command = command.remove(0,startlocal);
// SendCMD(command);
// }
});
/* 将QProcess的输出打印到界面 */
connect(cmd,&QProcess::readyReadStandardOutput,this,[=](){
if(!SendCMD_argu.contains("-q")) ui->Receiver->append("->Receive->->-> Standard Output");
QString outStr = QString::fromLocal8Bit(cmd->readAllStandardOutput());
if(!SendCMD_argu.contains("-q")) ui->Receiver->append("<<Receiver:");
// ui->Receiver->append(outStr.trimmed());
// ui->Receiver->horizontalScrollBar()->setValue(ui->Receiver->horizontalScrollBar()->maximum());
// cmd->disconnect(SIGNAL(readyReadStandardOutput()));
QStringList _output;
foreach(QString item,outStr.trimmed().split("\n",QString::SkipEmptyParts))
{
if(item.trimmed() != "")
{
QString Data = item.trimmed();
while (Data.indexOf("\\x")>=0)//字符处理
{
int index = Data.indexOf("\\x");
QString Block = Data.mid(index,12);
Block = Block.replace("\\x", QString());
QString Blocks ;
Blocks.append(Block.mid(0,2).toUShort(0,16));
Blocks.append(Block.mid(2,2).toUShort(0,16));
Blocks.append(Block.mid(4,2).toUShort(0,16));
// QTextCodec *codec = QTextCodec::codecForName("UTF-8");
// QString desStr = codec->fromUnicode(Blocks);
Data = Data.replace(index, 12, tr(Blocks.toLatin1().data()));
// Data = unicodeToUtf8(Data);
}
// while (Data.indexOf("\\u")>=0)
// {
// int index = Data.indexOf("\\u");
// QString Block = Data.mid(index,6);
// Block = Block.replace("\\u", QString());
// int nHex = Block.toInt(0, 16);
// Data.replace(index, 6, QChar(nHex));
// }
_output.append(Data);//创建序列
if(!SendCMD_argu.contains("-q")) ui->Receiver->append(Data);
if(!SendCMD_argu.contains("-q")) ui->Receiver->horizontalScrollBar()->setValue(ui->Receiver->horizontalScrollBar()->maximum());
}
}
emit CMD_Reply(_output);//抛出槽触发
// CMD_Reply *CMD_reply = new CMD_Reply(_output);
// QCoreApplication::postEvent(this, CMD_reply);//抛出事件
});
/* 将QProcess的 "启动/写入/结束" 打印到界面 */
connect(cmd,&QProcess::started,this,[=](){
ui->Receiver->append("->Started->->->");
ui->Receiver->horizontalScrollBar()->setValue(ui->Receiver->horizontalScrollBar()->maximum());
});
connect(cmd, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),[=](int exitCode, QProcess::ExitStatus exitStatus){
Q_UNUSED(exitCode);
Q_UNUSED(exitStatus);
if(!CloseDialog)
{
ui->Receiver->append("->Finished->->->");
ui->Receiver->horizontalScrollBar()->setValue(ui->Receiver->horizontalScrollBar()->maximum());
cmd->start("/bin/bash");
}
});
// cmd->setReadChannelMode(QProcess::ForwardedChannels);
cmd->start("/bin/bash");
ui->Sender->setFocus();
// SendCMD("export DISPLAY=\":0.0\"");
this->resize((int)(SizePercent.width()*800),(int)(SizePercent.height()*480));
this->setMaximumSize((int)(SizePercent.width()*800),(int)(SizePercent.height()*480));
this->setFixedSize((int)(SizePercent.width()*800),(int)(SizePercent.height()*480));
}
//QString TerminalDialog::unicodeToUtf8(const QString resStr)
//{
// QString temp;
// for(int i=0;i<resStr.length();)
// {
// if(resStr.at(i)=='\\' && resStr.at(i+1)=='x')
// {
// QString str = resStr.mid(i+2,i+4);
// temp.append(str.mid(0,2).toUShort(0,16));
// i=i+4;
// }
// else
// {
// temp.append(resStr.at(i));
// ++i;
// }
// }
// QTextCodec *codec = QTextCodec::codecForName("UTF-8");
// QString desStr = codec->fromUnicode(temp);
// return tr(desStr.toLatin1().data());
//}
TerminalDialog::~TerminalDialog()
{
delete ui;
CloseDialog = true;
if (cmd->state() == QProcess::Running)
{
cmd->terminate();
cmd->close();
cmd->waitForFinished();
}
}
void TerminalDialog::showEvent(QShowEvent *event)
{
Q_UNUSED(event);
if(FirstLoad)
{
FirstLoad = !FirstLoad;
}
}
void TerminalDialog::on_Btn_Back_clicked()
{
this->close();
}
void TerminalDialog::on_Btn_Send_clicked()
{
if(ui->Sender->text().isEmpty())
{
// ui->Sender->setFocus();
return;
}
ui->Sender->setText(ui->Sender->text().trimmed());
SendCMD(ui->Sender->text());
// ui->Sender->setFocus();
}
void TerminalDialog::on_pushButton_clicked()
{
ui->Receiver->clear();
}
void TerminalDialog::Show_debug(QString msg)
{
ui->Receiver->append("->System MSG->->->" + msg);
}
void TerminalDialog::SendCMD(QString CommandLine , QString argu)
{
SendCMD_argu = argu;
command = CommandLine.trimmed();
if(command.isEmpty() || command == "")
{
return;
}
if(!cmd->isOpen())
{
cmd->open();
}
if(command.toLower() == "exit app" || command.toLower() == "exitapp")
qApp->exit(Base_OnlyExitApp);
// else if(command.toLower().startsWith("qdebug "))
// {
// qDebug() << command.remove(0,6);
// }
else if(cmd->isOpen() && cmd->waitForStarted())
{
if(!SendCMD_argu.contains("-q")) ui->Receiver->append(">>Sender:");
if(!SendCMD_argu.contains("-q")) ui->Receiver->append(command);
if(command.contains("sudo "))
{
int startlocal = command.indexOf("sudo ");
command = command.replace(startlocal,5,"echo \"" + CMD_Password + "\" | sudo -S ");//片段阔幅代码未处理
}
cmd->write(command.toLocal8Bit()+ "\n");
if(!SendCMD_argu.contains("-q")) ui->Receiver->horizontalScrollBar()->setValue(ui->Receiver->horizontalScrollBar()->maximum());
}
else if(!cmd->isOpen())
{
ui->Receiver->append("->System Busy->->->");
}
}
//QEvent::Type CMD_Reply::m_EventType = QEvent::None;
//CMD_Reply::CMD_Reply(QStringList _value) : QEvent(eventType())
//{
// m_StringList = _value;
//}
//CMD_Reply::~CMD_Reply()
//{
//}
//QEvent::Type CMD_Reply::eventType()
//{
// // 创建事件Type
// if (m_EventType == QEvent::None)
// m_EventType = (QEvent::Type)QEvent::registerEventType();
// return m_EventType;
//}
//QStringList CMD_Reply::getValue(void)
//{
// return m_StringList;
//}
void TerminalDialog::on_Btn_Logo_clicked()
{
QStringList btnName = {"Yes", "No"} ;
CustomDialog *newDialog = new CustomDialog();
QObject::connect(newDialog, &CustomDialog::OutputEvent, newDialog,[=](QString instruct)
{
if(instruct == "Yes")
{
InputDialog *inputdialog = new InputDialog(this);
connect(inputdialog,&InputDialog::closedialog,this,[=](QString value,bool SettingMark){
if(SettingMark)
{
CMD_Password = value;
}
delete sender();
});
inputdialog->setvalue("Please Input Password","",CMD_Password);
}
});
newDialog->showCustomDialog("Warning",":/assets/terminal-box-line.svg",btnName,"Do you want to change the incoming password from the console ?",this);
}
void TerminalDialog::on_Btn_Kill_clicked()
{
cmd->terminate();
}