-
Notifications
You must be signed in to change notification settings - Fork 7
/
mainwindow.cpp
274 lines (260 loc) · 9.58 KB
/
mainwindow.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
274
#include "qannconsole.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ui_dialog_setting.h"
#include <QSettings>
#include <QCompleter>
#define LOGIN_SHELL_CMD_STR "killall dt_ui;/tmp/dt_ui --platform vnc&sleep 1;ps | grep dt_ui | grep -v grep && echo connect2vnc"
#define LOGOUT_SHELL_CMD_STR "killall dt_ui;/tmp/dt_ui&sleep 1;ps | grep dt_ui | grep -v grep && echo remote dt_ui is running"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
cfg = new QSettings("./"+qAppName()+".ini",QSettings::IniFormat, this);
ssh = new QSshSocket(this);
ui->setupUi(this);
connect(ui->btnShowStyle,&QPushButton::pressed,[=](){
if(ui->btnShowStyle->text() == "FULL"){
ui->btnShowStyle->setText("CENTER");
ui->vncView->setFullScreen(true);
}
else {
ui->btnShowStyle->setText("FULL");
ui->vncView->setFullScreen(false);
}
});
if(ui->btnShowStyle->text() == "FULL"){
ui->btnShowStyle->setText("CENTER");
ui->vncView->setFullScreen(true);
}
else {
ui->btnShowStyle->setText("FULL");
ui->vncView->setFullScreen(false);
}
connect(ssh, &QSshSocket::error, this ,[&](QSshSocket::SshError se){
ui->statusBar->showMessage(tr("error :%1").arg(se));
});
connect(ui->btnSettings,&QPushButton::clicked,[=](){
QDialog dialog;
Ui::Dialog setting_ui;
setting_ui.setupUi(&dialog);
setting_ui.hostip->setText(cfg->value("hostip","192.168.200.166").toString());
setting_ui.port->setText(cfg->value("port","22").toString());
setting_ui.username->setText(cfg->value("username","root").toString());
setting_ui.passwd->setText(cfg->value("passwd","").toString());
setting_ui.uploadDir->setText(cfg->value("uploadDir","/tmp/").toString());
if(dialog.exec()==QDialog::Accepted){
cfg->setValue("hostip",setting_ui.hostip->text());
cfg->setValue("port",setting_ui.port->text());
cfg->setValue("username",setting_ui.username->text());
cfg->setValue("passwd",setting_ui.passwd->text());
cfg->setValue("uploadDir",setting_ui.uploadDir->text());
}
});
connect(ssh, &QSshSocket::commandExecuted, this, [=](QString s1,QString s2){
if(!s2.isEmpty()){
ui->console << s2;
}
});
connect(ssh,&QSshSocket::loginSuccessful,this,[=](){
ui->statusBar->showMessage(tr("已连接到 %1 , 登录用户: %2").arg(ssh->host()).arg(ssh->user()));
ui->dockWidget->setWindowTitle(tr("已连接到 %1 , 登录用户: %2").arg(ssh->host()).arg(ssh->user()));
ssh->enter2shell();
});
connect(ui->vncView,&QVNCClientWidget::connected,[&](bool b){
ui->btnRemoteCtrl->setText(b?"断开远程":"远程控制");
if(b){
setWindowTitle(ui->vncView->getServerMsg());
}
});
ui->dockWidget->hide();
ui->frame->hide();
ui->frameCmd->hide();
QStringList strs = cfg->value("cmdhistory","ls").toStringList();
auto cmdcompleter = new QCompleter(strs, ui->editCmd);
cmdcompleter->setCaseSensitivity(Qt::CaseInsensitive);
ui->editCmd->setCompleter(cmdcompleter);
ui->editCmd->installEventFilter(this);
ui->console->installEventFilter(ssh);
connect(ui->cbxCmd,QOverload<int>::of(&QComboBox::highlighted),[=](int index){
ui->statusBar->showMessage(ui->cbxCmd->itemData(index).toString(), 1000);
});
connect(ui->cbxCmd,QOverload<int>::of(&QComboBox::currentIndexChanged),[=](int index){
ui->editCmd->setText(ui->cbxCmd->itemData(index).toString());
});
QMap<QString, QVariant> cmdMap = cfg->value("cmdMap").toMap();
QMapIterator<QString, QVariant> i(cmdMap);
while (i.hasNext()) {
i.next();
ui->cbxCmd->addItem(i.key(),i.value());
}
}
MainWindow::~MainWindow()
{
cfg->setValue("cmdhistory",dynamic_cast<QStringListModel *>(ui->editCmd->completer()->model())->stringList());
ui->vncView->disconnectFromVncServer();
ssh->disconnectFromHost();
delete ui;
}
void MainWindow::on_btnConnect_pressed()
{
if(!ssh->isLoggedIn())
{
QString hostip,username,passwd;
username = cfg->value("username").toString();
passwd = cfg->value("passwd").toString();
hostip = cfg->value("hostip").toString();
if(hostip.isEmpty()||username.isEmpty()){
ui->statusBar->showMessage("温馨提示:请先设置连接参数");
return;
}
ssh->setConnectHost(cfg->value("hostip").toString());
ssh->login(username, passwd);
}
}
void MainWindow::on_btnDisconnect_pressed()
{
ui->statusBar->showMessage(tr("下线中,请稍等"));
if(ui->vncView->isConnectedToServer()){
ui->vncView->disconnectFromVncServer();
}
usleep(100000);// give some time to make a perfect execution;
ssh->disconnectFromHost();
ui->statusBar->showMessage(tr("已下线"));
}
void MainWindow::on_editCmd_returnPressed()
{
if(!ssh->isLoggedIn()){
ui->statusBar->showMessage("请先连接。");
return;
}
ssh->add2ShellCommand(ui->editCmd->text());
QStringList strs = dynamic_cast<QStringListModel *>(ui->editCmd->completer()->model())->stringList();
if(!strs.contains(ui->editCmd->text()))
{
if(strs.size()>100)
strs.pop_front();
strs << ui->editCmd->text();
dynamic_cast<QStringListModel *>(ui->editCmd->completer()->model())->setStringList(strs);
}
}
void MainWindow::on_toolBtnCmd_pressed()
{
bool show = ui->editCmd->isVisible();
ui->frameCmd->setHidden(show);
ui->dockWidget->setHidden(show);
}
void MainWindow::on_toolBtnSet_pressed()
{
ui->frame->setVisible(!ui->frame->isVisible());
}
void MainWindow::on_btnRemoteCtrl_clicked()
{
ui->btnRemoteCtrl->setEnabled(false);
QString str = ui->btnRemoteCtrl->text();
if(str == "断开远程"){
ui->vncView->disconnectFromVncServer();
}
else{
if(ui->vncView->connectToVncServer(cfg->value("hostip").toString(), "")) {
ui->vncView->startFrameBufferUpdate();
}
}
ui->btnRemoteCtrl->setEnabled(true);
}
void MainWindow::on_btnUpload_clicked()
{
if(!ssh->isLoggedIn()){
ui->statusBar->showMessage("请先连接上再传输.");
return;
}
QStringList files = QFileDialog::getOpenFileNames(this);
if(files.isEmpty())
return;
bool txOver=false;
int txDoneCnt = 0;
QColor defaultColor = ui->console->textColor();
QTextCharFormat defaultCFT = ui->console->currentCharFormat();
connect(ssh, &QSshSocket::pushSuccessful, this, [&](QString srcFile, QString dstFile){
if(!dstFile.isEmpty()){
ui->console->setTextColor(Qt::blue);
ui->console->append(srcFile + "--->" + dstFile + ":\t上传100%,上传成功!");
txDoneCnt++;
}
else{
ui->console->setTextColor(Qt::red);
ui->console->append(srcFile + "--->" + dstFile + ":\t上传错误,请检查!");
}
txOver = true;
ui->console->setTextColor(defaultColor);
});
foreach (auto file, files) {
txOver = false;
QFileInfo fi(file);
if(!file.isEmpty()){
ui->console->append("准备上传:" + fi.fileName() + ",请稍等...");
}
ssh->pushFile(file, cfg->value("uploadDir").toString()+"/"+fi.fileName());
while (!txOver && ssh->isLoggedIn()) {
qApp->processEvents();
usleep(1000);
}
}
this->disconnect(ssh, &QSshSocket::pushSuccessful, 0, 0);
ui->console->setTextColor(Qt::green);
ui->console->append(tr("======总共上传了 %1 个文件======\n").arg(txDoneCnt));
ui->console->setCurrentCharFormat(defaultCFT);
ssh->enter2shell();
}
bool MainWindow::eventFilter(QObject *obj, QEvent *e)
{
if(e->type() == QKeyEvent::KeyPress ){
QKeyEvent *keyevent = static_cast<QKeyEvent *>(e);
if(keyevent->modifiers() == Qt::ControlModifier){
switch (keyevent->key()) {
case Qt::Key_C:
ssh->add2ShellCommand("CTRL-C");
return true;
break;
case Qt::Key_L:
ssh->add2ShellCommand("clear");
return true;
break;
case Qt::Key_U:
ui->editCmd->cursorBackward(true, ui->editCmd->cursorPosition());
ui->editCmd->backspace();
return true;
break;
case Qt::Key_K:
ui->editCmd->cursorForward(true, ui->editCmd->text().size()-ui->editCmd->cursorPosition());
ui->editCmd->del();
return true;
break;
default:
break;
}
}
}
return QMainWindow::eventFilter(obj, e);
}
void MainWindow::on_btnAdd_clicked()
{
bool ok;
QString cmdname = QInputDialog::getText(this, "加入命令","请输入命令名", QLineEdit::Normal, "" , &ok);
if(ok&&!cmdname.isEmpty()){
ui->cbxCmd->addItem(cmdname, ui->editCmd->text());
QMap<QString, QVariant> cmdMap = cfg->value("cmdMap").toMap();
cmdMap.insert(cmdname, ui->editCmd->text());
cfg->setValue("cmdMap", cmdMap);
ui->cbxCmd->setCurrentText(cmdname);
}
}
void MainWindow::on_btnDel_clicked()
{
int index = ui->cbxCmd->currentIndex();
QString rmName = ui->cbxCmd->itemText(index);
ui->cbxCmd->removeItem(index);
QMap<QString, QVariant> cmdMap = cfg->value("cmdMap").toMap();
cmdMap.remove(rmName);
cfg->setValue("cmdMap", cmdMap);
}