-
Notifications
You must be signed in to change notification settings - Fork 2
/
sftpdialog.cpp
172 lines (153 loc) · 5.89 KB
/
sftpdialog.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
#include "sftpdialog.h"
#include <QFileDialog>
#include <QFileInfo>
#include <QMenu>
#include <QMessageBox>
#include <QTreeWidgetItem>
#include <QtConcurrent>
#include "components/confirmdialog.h"
#include "fileinfo.h"
SftpDialog::SftpDialog(QWidget *parent, ConnectInfo connectInfo)
: QWidget(parent) {
this->connectInfo = connectInfo;
// setWindowTitle(connectInfo->name + "-文件管理");
// setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
initUI();
sftpConnect();
}
void SftpDialog::initUI() {
hBoxLayout = new QHBoxLayout;
treeView = new QTreeWidget(this);
treeView->setContextMenuPolicy(Qt::CustomContextMenu);
treeView->sortByColumn(0, Qt::SortOrder::AscendingOrder);
treeView->setItemsExpandable(true);
QFont font;
font.setPointSize(12);
treeView->setFont(font);
rootItem = new QTreeWidgetItem(treeView);
rootItem->setText(0, "/");
rootItem->setText(6, "/");
rootItem->setIcon(0, QIcon(":/icons/folder.png"));
rootItem->setExpanded(true);
connect(treeView, &QTreeWidget::itemClicked, this,
[=](QTreeWidgetItem *item, int column) {
folderItemWidget->clearTreeWidget();
QString currentPath = item->text(6);
qDebug() << "打开路径:" << currentPath;
currentPath = currentPath.replace("//", "/");
folderItemWidget->setCurrentDirEdit(currentPath);
sftpClient->asyncOpendir(currentPath);
});
connect(treeView, &QTreeWidget::itemActivated,
[&](QTreeWidgetItem *item, int column) { qDebug() << "回车"; });
treeView->setColumnCount(1); // 设置列
treeView->hideColumn(6);
treeView->setHeaderLabels(QStringList() << "文件名");
hBoxLayout->addWidget(treeView);
// folderItemWidget = new FolderItemWidget(this, sftpClient);
// hBoxLayout->addWidget(folderItemWidget);
// hBoxLayout->setStretch(1, 4);
setLayout(hBoxLayout);
}
void SftpDialog::sftpConnect() {
if (sftpClient) {
disconnect(sftpClient);
sftpClient->stop();
delete sftpClient;
sftpClient = NULL;
}
if (folderItemWidget) {
hBoxLayout->removeWidget(folderItemWidget);
}
sftpClient = new SFTPClient(connectInfo);
folderItemWidget = new FolderItemWidget(this, sftpClient);
hBoxLayout->addWidget(folderItemWidget);
hBoxLayout->setStretch(1, 4);
qDebug() << "SftpDialog ThreadId is" << QThread::currentThreadId();
connect(sftpClient, &SFTPClient::disconnected, this, [=]() {
AlertWindow *alertWindow = new AlertWindow(this, true);
alertWindow->setTitleText("错误提示");
alertWindow->setContentText("当前连接已断开\n是否重新连接");
alertWindow->setConfirmButtonText("重连");
connect(alertWindow, &AlertWindow::confirmEvent, this,
[=]() { this->sftpConnect(); });
alertWindow->show();
this->installEventFilter(alertWindow);
});
connect(sftpClient, &SFTPClient::initSftpSessionSuccess, this, [=]() {
qDebug() << "initSftpSessionSuccess ThreadId is"
<< QThread::currentThreadId();
folderItemWidget->setCurrentDirEdit(currentPath);
sftpClient->asyncOpendir(currentPath);
});
connect(sftpClient, &SFTPClient::errorMsg, this, [=](QString msg) {
AlertWindow *alertWindow = new AlertWindow(this, true);
alertWindow->setTitleText("错误提示");
alertWindow->setContentText(msg);
alertWindow->confirmButtonHide();
alertWindow->setCancelButtonText("关闭");
connect(alertWindow, &AlertWindow::cancelEvent, this,
[=]() { this->close(); });
alertWindow->show();
});
connect(sftpClient, &SFTPClient::successMsg, this,
[=](QString msg) { QMessageBox::information(this, "提示", msg); });
connect(sftpClient, &SFTPClient::opendirEvent, this,
[=](QString currentPath) {
this->currentPath = currentPath;
if (currentPath == "/") {
refresh = true;
int count = rootItem->childCount();
for (int i = 0; i < count; i++) {
rootItem->removeChild(rootItem->takeChild(0));
}
} else {
refresh = false;
}
});
connect(sftpClient, &SFTPClient::opendirInfoCallBack, this,
[=](QString currentPath, QString data) {
if (!refresh) {
return;
}
FileInfo_S info = parseBySftpData(data);
if (info.fileName == "." || info.fileName == "..") {
return;
}
QTreeWidgetItem *item = new QTreeWidgetItem(rootItem);
item->setText(0, info.fileName);
item->setText(1, info.fileSize);
item->setText(2, info.fileNum);
if (info.fileType == 1) {
item->setIcon(0, QIcon(":/icons/folder.png"));
item->setText(3, "目录");
} else if (info.fileType == 2) {
item->setIcon(0, QIcon(":/icons/file.png"));
item->setText(3, "文件");
} else if (info.fileType == 3) {
item->setIcon(0, QIcon(":/icons/folder.png"));
item->setText(3, "符号链接");
}
item->setText(4, info.permission);
item->setText(5, info.userOrGroup);
item->setText(6, currentPath + "/" + info.fileName);
info.filePath = currentPath + "/" + info.fileName;
item->setData(0, Qt::UserRole + 1, QVariant::fromValue(info));
item->setText(7, info.updateTime);
});
sftpClient->start();
}
void SftpDialog::treeWidgetItemRefresh(QTreeWidgetItem *item) {
int count = item->childCount();
for (int i = 0; i < count; i++) {
item->removeChild(item->takeChild(0));
}
QString currentPath = item->text(6);
item->setExpanded(true);
sftpClient->asyncOpendir(currentPath);
}
void SftpDialog::closeEvent(QCloseEvent *event) {
sftpClient->stop();
QTimer::singleShot(100, [&]() { delete this; });
}
SftpDialog::~SftpDialog() { delete sftpClient; }