-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyDemo.cpp
200 lines (172 loc) · 5.1 KB
/
MyDemo.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
#include "MyDemo.h"
#include "FTPSocket.h"
MyDemo::MyDemo(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MyDemoClass())
{
ui->setupUi(this);
QTextCodec *codec = QTextCodec::codecForName("UTF8");
QNetworkProxy::setApplicationProxy(QNetworkProxy::NoProxy);
initTreeWidget();
sigAndSlotConnect();
}
MyDemo::~MyDemo()
{
release();
}
void MyDemo::initialize()
{
hostName = ui->le_host->text();
port = ui->le_port->text().toInt();
userName = ui->le_username->text();
password = ui->le_password->text();
// ftp
p_ftp = new FTPSocket;
p_ftp->setFtpUserNamePasswd(userName.toLatin1(), password.toLatin1());
p_ftp->connectFtpServer(hostName, port);
// rootPwd
lo_rootPwd = QStandardPaths::writableLocation(QStandardPaths::TempLocation) + "/downFile";
qInfo() << "rootPwd:" << lo_rootPwd;
}
void MyDemo::release()
{
delete ui;
delete p_ftp;
delete p_treeW_Remote;
delete p_treeW_Local;
}
void MyDemo::sigAndSlotConnect()
{
connect(ui->btn_login, &QPushButton::clicked, this, &MyDemo::on_login_clicked);
connect(ui->btn_exit, &QPushButton::clicked, this, &MyDemo::on_exit_clicked);
connect(ui->btn_download, &QPushButton::clicked, this, &MyDemo::on_download_clicked);
connect(ui->treew_local, &QTreeWidget::itemDoubleClicked, this, &MyDemo::double_local_changePath_clicked);
connect(ui->treew_remote, &QTreeWidget::itemDoubleClicked, this, &MyDemo::double_remote_changePath_clicked);
connect(ui->btn_louplevel, &QPushButton::clicked, this, &MyDemo::on_local_uplevel_clicked);
connect(ui->btn_reuplevel, &QPushButton::clicked, this, &MyDemo::on_remote_uplevel_clicked);
}
void MyDemo::on_download_clicked()
{
QTreeWidgetItem *current = p_treeW_Remote->currentItem();
QString strFile = current->text(0);
p_ftp->downloadFtpFile(strFile, localPwd);
QEventLoop loop;
QTimer::singleShot(10000, &loop, &QEventLoop::quit);
loop.exec();
p_ftp->saveDownloadFile();
ui->lb_message->setText("Download Complete");
}
void MyDemo::on_local_uplevel_clicked()
{
if (local_dir.path() == lo_rootPwd)
{
qInfo() << "目前已在根目录下,无法往上级移动";
return;
}
local_dir.cdUp();
list_local = local_dir.entryList(QDir::AllEntries);
list_local.removeOne(".");
list_local.removeOne("..");
p_treeW_Local->clear();
printTreeLocal(list_local);
}
void MyDemo::on_remote_uplevel_clicked()
{
if (p_ftp->currentDir == re_rootPwd)
{
qInfo() << "已在根目录下,无法往上级移动";
return;
}
p_ftp->FtpCWD("..");
list_ftp = p_ftp->listFtpFile();
p_treeW_Remote->clear();
printTreeRemote(list_ftp);
}
void MyDemo::double_local_changePath_clicked(QTreeWidgetItem* item, int column)
{
localPwd = local_dir.path();
localPwd = localPwd + "/" + p_treeW_Local->currentItem()->text(0);
qInfo() << "需要打开的文件夹:" << localPwd;
QFileInfo info(localPwd);
if (!info.isDir())
{
qInfo() << "非文件夹,无法打开";
return;
}
local_dir.cd(p_treeW_Local->currentItem()->text(0));
list_local = local_dir.entryList(QDir::AllEntries);
list_local.removeOne(".");
list_local.removeOne("..");
p_treeW_Local->clear();
printTreeLocal(list_local);
}
void MyDemo::double_remote_changePath_clicked(QTreeWidgetItem *item, int column)
{
QString dirName = p_treeW_Remote->currentItem()->text(0);
qInfo() << "dirName:" << dirName;
p_ftp->FtpCWD(dirName);
p_treeW_Remote->clear();
list_ftp = p_ftp->listFtpFile();
printTreeRemote(list_ftp);
}
QStringList MyDemo::listLocalFile()
{
localPwd = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
localPwd += "/downFile";
// 检测临时文件夹里是否存在downFile文件夹,如果没有则创建
local_dir.setPath(localPwd);
if (!local_dir.exists())
{
if (!local_dir.mkpath(localPwd))
{
qInfo() << "文件夹创建失败" << localPwd;
}
}
list_local = local_dir.entryList(QDir::AllEntries);
list_local.removeOne(".");
list_local.removeOne("..");
return list_local;
}
void MyDemo::initTreeWidget()
{
p_treeW_Remote = ui->treew_remote;
p_treeW_Local = ui->treew_local;
p_treeW_Remote->setHeaderLabel(QString("远程文件目录"));
p_treeW_Local->setHeaderLabel(QString("本地文件目录"));
}
void MyDemo::printTreeRemote(QStringList& list)
{
p_treeW_Remote->clear();
int index = 0;
for (auto str : list)
{
p_treeW_Remote->addTopLevelItem(new QTreeWidgetItem(QStringList() << str));
index++;
}
index = 0;
}
void MyDemo::printTreeLocal(QStringList& list)
{
p_treeW_Local->clear();
int index = 0;
for (auto str : list)
{
p_treeW_Local->addTopLevelItem(new QTreeWidgetItem(QStringList() << str));
index++;
}
index = 0;
}
void MyDemo::on_exit_clicked()
{
p_treeW_Remote->clear();
p_treeW_Local->clear();
p_ftp->closeFTPConnect();
}
void MyDemo::on_login_clicked()
{
initialize();
list_ftp = p_ftp->listFtpFile();
list_local = listLocalFile();
printTreeRemote(list_ftp);
printTreeLocal(list_local);
}