Repository navigation
Expand file tree
/
Copy pathsftpdialog.cpp
More file actions
374 lines (329 loc) · 11.9 KB
/
Copy pathsftpdialog.cpp
File metadata and controls
374 lines (329 loc) · 11.9 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include "sftpdialog.h"
#ifdef WIN32
#pragma execution_character_set("utf-8")
#endif
#include <QHBoxLayout>
#include <QPointer>
#include <QResizeEvent>
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include "components/connectionstatusoverlay.h"
#include "components/folderitemwidget.h"
#include "sftpclient.h"
#include "components/messagedialog.h"
SftpDialog::SftpDialog(QWidget *parent, const ConnectInfo &info)
: QWidget(parent), connectInfo(info) {
setAttribute(Qt::WA_DeleteOnClose);
initUi();
statusOverlay_ = new ConnectionStatusOverlay(this);
statusOverlay_->setGeometry(rect());
statusOverlay_->raise();
connect(statusOverlay_, &ConnectionStatusOverlay::retryRequested, this,
[this]() { sftpConnect(); });
connect(statusOverlay_, &ConnectionStatusOverlay::closeRequested, this,
[this]() { emit closeRequested(); });
sftpConnect();
}
SftpDialog::~SftpDialog() {
if (sftpClient) {
sftpClient->stop();
delete sftpClient;
sftpClient = nullptr;
}
}
void SftpDialog::resizeEvent(QResizeEvent *event) {
QWidget::resizeEvent(event);
if (statusOverlay_) {
statusOverlay_->setGeometry(rect());
}
}
QString SftpDialog::connectionLabel() const {
const QString host = connectInfo.hostName.trimmed();
if (connectInfo.username.trimmed().isEmpty()) {
return QStringLiteral("%1:%2").arg(host).arg(connectInfo.port);
}
return QStringLiteral("%1@%2:%3")
.arg(connectInfo.username.trimmed())
.arg(host)
.arg(connectInfo.port);
}
void SftpDialog::initUi() {
hBoxLayout = new QHBoxLayout(this);
treeView = new QTreeWidget(this);
treeView->setColumnCount(1);
treeView->setHeaderLabels(QStringList() << tr("目录"));
treeView->setItemsExpandable(true);
treeView->setColumnWidth(0, 220);
treeView->setIconSize(QSize(18, 18));
QFont font;
font.setPointSize(10);
treeView->setFont(font);
rootItem = new QTreeWidgetItem(treeView);
rootItem->setText(0, QStringLiteral("/"));
rootItem->setIcon(0, QIcon(QStringLiteral(":/icons/folder.png")));
rootItem->setExpanded(true);
FileInfo_S rootInfo;
rootInfo.fileName = QStringLiteral("/");
rootInfo.filePath = QStringLiteral("/");
rootInfo.fileType = RemoteFileDirectory;
rootItem->setData(0, Qt::UserRole + 1, QVariant::fromValue(rootInfo));
dirItems.insert(QStringLiteral("/"), rootItem);
connect(treeView, &QTreeWidget::itemClicked, this,
[this](QTreeWidgetItem *item, int) {
if (!item || !folderItemWidget) {
return;
}
const FileInfo_S info =
item->data(0, Qt::UserRole + 1).value<FileInfo_S>();
if (info.fileType == RemoteFileDirectory ||
(info.fileType == RemoteFileSymlink &&
info.targetFileType == RemoteFileDirectory)) {
currentPath = info.filePath;
treeView->setCurrentItem(item);
item->setExpanded(true);
folderItemWidget->navigateTo(info.filePath);
}
});
// 用户直接展开左树节点时也按需加载子目录,不必先让右侧进入该目录。
connect(treeView, &QTreeWidget::itemExpanded, this,
[this](QTreeWidgetItem *item) {
if (!item) {
return;
}
const FileInfo_S info =
item->data(0, Qt::UserRole + 1).value<FileInfo_S>();
if (info.fileType == RemoteFileDirectory ||
(info.fileType == RemoteFileSymlink &&
info.targetFileType == RemoteFileDirectory)) {
requestTreeChildren(info.filePath);
}
});
hBoxLayout->addWidget(treeView);
hBoxLayout->setStretch(0, 1);
setLayout(hBoxLayout);
}
void SftpDialog::sftpConnect() {
const QString previousDir =
folderItemWidget ? folderItemWidget->currentDir() : currentPath;
// 先销毁旧界面,避免其继续接收旧客户端排队中的信号。
if (folderItemWidget) {
hBoxLayout->removeWidget(folderItemWidget);
delete folderItemWidget;
folderItemWidget = nullptr;
}
if (sftpClient) {
sftpClient->stop();
delete sftpClient;
sftpClient = nullptr;
}
resetTree();
currentPath = previousDir;
sftpClient = new SFTPClient(connectInfo);
folderItemWidget = new FolderItemWidget(this, sftpClient);
hBoxLayout->addWidget(folderItemWidget);
hBoxLayout->setStretch(1, 4);
// 等 directoryLoaded 的其他槽(addDirectoryItems)先跑完再同步目录树。
connect(folderItemWidget, &FolderItemWidget::directoryChanged, this,
&SftpDialog::syncTreeToPath, Qt::QueuedConnection);
statusOverlay_->raise();
statusOverlay_->showConnecting(connectionLabel(),
tr("正在建立 SFTP 会话,请稍候…"));
SFTPClient *client = sftpClient;
QPointer<SFTPClient> clientGuard(client);
connect(client, &SFTPClient::disconnected, this,
[this, clientGuard]() {
if (!clientGuard || sftpClient != clientGuard.data()) {
return;
}
statusOverlay_->showDisconnected(
connectionLabel(),
QStringLiteral("网络连接已中断,SFTP 会话已结束。"));
},
Qt::QueuedConnection);
connect(client, &SFTPClient::initSftpSessionSuccess, this,
[this, clientGuard]() {
if (!clientGuard || sftpClient != clientGuard.data()) {
return;
}
statusOverlay_->hideOverlay();
if (folderItemWidget) {
folderItemWidget->navigateTo(currentPath);
}
},
Qt::QueuedConnection);
connect(client, &SFTPClient::connectionFailed, this,
[this, clientGuard](const QString &reason, const QString &hint,
const QString &detail) {
if (!clientGuard || sftpClient != clientGuard.data()) {
return;
}
statusOverlay_->showFailure(connectionLabel(), reason, hint, detail);
},
Qt::QueuedConnection);
// 已连接后的目录/文件操作错误仍用具体的提示框,不占用连接状态卡片。
connect(client, &SFTPClient::errorMsg, this,
[this, clientGuard](const QString &message) {
if (!clientGuard || sftpClient != clientGuard.data()) {
return;
}
MessageDialog::warning(this, tr("错误提示"), message);
},
Qt::QueuedConnection);
connect(client, &SFTPClient::successMsg, this,
[this, clientGuard](const QString &message) {
if (!clientGuard || sftpClient != clientGuard.data()) {
return;
}
MessageDialog::success(this, tr("提示"), message);
},
Qt::QueuedConnection);
connect(client, &SFTPClient::directoryLoaded, this,
[this, clientGuard](const QString &dirPath,
const QList<FileInfo_S> &entries) {
if (!clientGuard || sftpClient != clientGuard.data()) {
return;
}
treeLoadingDirs.remove(normalizeRemotePath(dirPath));
addDirectoryItems(dirPath, entries);
if (!treeSyncPath.isEmpty()) {
continueTreeSync();
}
},
Qt::QueuedConnection);
connect(client, &SFTPClient::directoryLoadFailed, this,
[this, clientGuard](const QString &dirPath, const QString &) {
if (!clientGuard || sftpClient != clientGuard.data()) {
return;
}
const QString normalized = normalizeRemotePath(dirPath);
treeLoadingDirs.remove(normalized);
// 为了展开目录树而补加载的祖先目录失败时,放弃本次同步,
// 不要无限等待;右侧文件面板会自行提示当前目录的错误。
if (!treeSyncPath.isEmpty() &&
(treeSyncPath == normalized ||
treeSyncPath.startsWith(normalized + QLatin1Char('/')))) {
treeSyncPath.clear();
}
},
Qt::QueuedConnection);
client->start();
}
void SftpDialog::resetTree() {
clearChildren(rootItem);
dirItems.clear();
dirItems.insert(QStringLiteral("/"), rootItem);
loadedTreeDirs.clear();
treeLoadingDirs.clear();
treeSyncPath.clear();
rootItem->setExpanded(true);
}
void SftpDialog::addDirectoryItems(const QString &dirPath,
const QList<FileInfo_S> &entries) {
const QString normalized = normalizeRemotePath(dirPath);
QTreeWidgetItem *parentItem = dirItems.value(normalized, nullptr);
if (!parentItem) {
// 父目录还没进树,子项无处可挂;等父目录加载完再重新请求。
loadedTreeDirs.remove(normalized);
return;
}
clearChildren(parentItem);
for (const FileInfo_S &info : entries) {
QTreeWidgetItem *item = FolderItemWidget::createFileItem(parentItem, info);
if (!item) {
continue;
}
if (info.fileType == RemoteFileDirectory ||
(info.fileType == RemoteFileSymlink &&
info.targetFileType == RemoteFileDirectory)) {
dirItems.insert(info.filePath, item);
}
}
loadedTreeDirs.insert(normalized);
if (parentItem == rootItem) {
parentItem->setExpanded(true);
}
}
void SftpDialog::clearChildren(QTreeWidgetItem *item) {
while (item->childCount() > 0) {
QTreeWidgetItem *child = item->takeChild(0);
removeFromIndex(child);
delete child;
}
}
void SftpDialog::removeFromIndex(QTreeWidgetItem *item) {
const FileInfo_S info =
item->data(0, Qt::UserRole + 1).value<FileInfo_S>();
if (!info.filePath.isEmpty()) {
dirItems.remove(info.filePath);
loadedTreeDirs.remove(info.filePath);
}
for (int i = 0; i < item->childCount(); ++i) {
removeFromIndex(item->child(i));
}
}
void SftpDialog::syncTreeToPath(const QString &dirPath) {
treeSyncPath = normalizeRemotePath(dirPath);
currentPath = treeSyncPath;
continueTreeSync();
}
void SftpDialog::continueTreeSync() {
if (treeSyncPath.isEmpty() || !rootItem || !sftpClient) {
return;
}
QTreeWidgetItem *item = rootItem;
QString current = QStringLiteral("/");
if (treeSyncPath == current) {
item->setExpanded(true);
treeView->setCurrentItem(item);
treeView->scrollToItem(item);
treeSyncPath.clear();
requestTreeChildren(current);
return;
}
// 逐级向下走:节点不存在且父目录还没加载过时,先请求父目录,等
// directoryLoaded 回来后再继续(continueTreeSync 会被再次调用)。
const QString relative = treeSyncPath.mid(1);
int start = 0;
while (start < relative.size()) {
int end = relative.indexOf(QLatin1Char('/'), start);
if (end < 0) {
end = relative.size();
}
const QString part = relative.mid(start, end - start);
const QString nextPath = joinRemotePath(current, part);
QTreeWidgetItem *child = dirItems.value(nextPath, nullptr);
if (!child) {
if (loadedTreeDirs.contains(current)) {
// 父目录已经完整加载过却没有这一级:目标在远端不存在或不是目录。
treeSyncPath.clear();
return;
}
requestTreeChildren(current);
return;
}
item = child;
current = nextPath;
item->setExpanded(true);
start = end + 1;
}
// 找到目标:展开并选中,滚动到可见位置。
item->setExpanded(true);
treeView->setCurrentItem(item);
treeView->scrollToItem(item);
treeSyncPath.clear();
// 从地址栏直接跳到深层目录时,目标目录的子项可能还没进树,补一次加载,
// 这样左侧展开后能立刻看到子目录。
requestTreeChildren(current);
}
void SftpDialog::requestTreeChildren(const QString &dirPath) {
if (!sftpClient || !sftpClient->isReady()) {
return;
}
const QString normalized = normalizeRemotePath(dirPath);
if (loadedTreeDirs.contains(normalized) ||
treeLoadingDirs.contains(normalized)) {
return;
}
treeLoadingDirs.insert(normalized);
sftpClient->asyncOpendir(normalized);
}