-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftpclientwindow.cpp
211 lines (178 loc) · 4.83 KB
/
ftpclientwindow.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
#include "ftpclientwindow.h"
#include "ui_ftpclientwindow.h"
#include <QPixmap>
#include <QListWidgetItem>
#include <QIcon>
#include <iostream>
FtpClientWindow::FtpClientWindow(QWidget *parent) :
QDialog(parent),
ui(new Ui::FtpClientWindow)
{
ui->setupUi(this);
ui->listWidget->clear();
ui->listWidget->setEnabled(true);
const QIcon icon = QIcon(":/images/cdtoparent.png");
ui->upwardBtn->setIcon(icon);
configWidget(); /* 初始化控件的属性 */
initConnection(); /* 初始化信号与槽 */
}
/* 初始化信号与槽 */
void FtpClientWindow::initConnection()
{
/* FTP主机、端口、用户名、密码 */
connect(ui->ftpHostLE, SIGNAL(editingFinished()), this, SLOT(ftpHostChanged()));
connect(ui->ftpPortLE, SIGNAL(editingFinished()), this, SLOT(ftpPortChanged()));
connect(ui->usernameLE, SIGNAL(editingFinished()), this, SLOT(ftpUserChanged()));
connect(ui->passwordLE, SIGNAL(editingFinished()), this, SLOT(ftpPwdChanged()));
/* 退出 */
connect(ui->exitBtn, SIGNAL(clicked()), this, SLOT(mainWindowQuit()));
/* FTP登陆 */
connect(ui->linkBtn, SIGNAL(clicked()), this, SLOT(ftpConnect()));
/* 接收列表项 */
connect(&ftpclient, SIGNAL(cmdAddToList(QUrlInfo)), this, SLOT(addToList(QUrlInfo)));
/* 文件或文件夹被双击 */
connect(ui->listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)),
this, SLOT(DoubleClicked(QListWidgetItem*)));
/* 下载按钮 */
connect(ui->downloadBtn, SIGNAL(clicked()), this, SLOT(callDownload()));
/* 状态转换 */
connect(ui->listWidget, SIGNAL(itemClicked(QListWidgetItem*)),
this, SLOT(switchDownloadBtn(QListWidgetItem*)));
/* 返回到上级目录 */
connect(ui->upwardBtn, SIGNAL(clicked()), this, SLOT(changeToParrent()));
}
/* 返回上一级目录 */
void FtpClientWindow::changeToParrent()
{
clearMemory(listItemCtn);
ui->listWidget->clear();
listItemCtn.clear();
ftpclient.changeDir("..");
}
void FtpClientWindow::clearMemory(QList<QListWidgetItem*> &listItemCtn)
{
QListWidgetItem *item;
QList<QListWidgetItem*>::iterator it;
it = listItemCtn.begin();
while (it != listItemCtn.end())
{
item = *it;
if (item != NULL)
{
delete item;
item = NULL;
}
++it;
}
static int a = 0;
qDebug() << "delete times: " << ++a << endl;
}
bool FtpClientWindow::isDir(const QString &str) const
{
return (ftpclient.isDir(str));
}
/* 初始化控件的属性 */
void FtpClientWindow::configWidget()
{
ui->passwordLE->setEchoMode(QLineEdit::Password);
ui->ftpHostLE->setText("ftp.ntu.edu.tw");
ui->ftpPortLE->setText("21");
ui->usernameLE->setText("anonymous");
ui->passwordLE->setText("anonymous");
}
void FtpClientWindow::switchDownloadBtn(QListWidgetItem *item)
{
QString filename;
filename = item->text();
if (this->isDir(filename))
{
ui->downloadBtn->setEnabled(false);
}
else
{
ui->downloadBtn->setEnabled(true);
}
}
/* 下载选中的文件 */
void FtpClientWindow::callDownload()
{
QString str;
str = ui->listWidget->currentItem()->text(); /* filename */
if (isDir(str) == false)
{
downLoadFile(str);
}
}
/* 通在ftpclient下载文件filename */
void FtpClientWindow::downLoadFile(const QString &filename)
{
ftpclient.downloadFile(filename);
}
/* 文件或文件夹被双击 */
void FtpClientWindow::DoubleClicked(QListWidgetItem *item)
{
QString dir, filename, str;
str = item->text();
if (isDir(str))
{
dir = str;
/* 清除列表前释放内存 */
clearMemory(listItemCtn);
ui->listWidget->clear(); /* 清除当前的列表, 以便显示新的 */
listItemCtn.clear();
ftpclient.changeDir(dir);
}
else
{
filename = str;
downLoadFile(filename);
}
}
/**
* @brief FTP调用list()时, 自动发出listInfo(QUrlInfo)信号
*
* @param urlInfo : 发送过来的Url信息
*/
void FtpClientWindow::addToList(const QUrlInfo &urlInfo)
{
QListWidgetItem *item = new QListWidgetItem(ui->listWidget);
item->setText(urlInfo.name()); /* 在listWidget中显示的名字 */
const QIcon icon = QIcon(urlInfo.isDir() ? ":/images/dir.png" : ":/images/file.png");
listItemCtn.append(item);
item->setIcon(icon);
ui->listWidget->addItem(item);
}
void FtpClientWindow::mainWindowQuit()
{
ftpclient.ftpLogout();
close();
}
/* 连接FTP */
void FtpClientWindow::ftpConnect()
{
ftpHostChanged();
ftpPortChanged();
ftpUserChanged();
ftpPwdChanged();
ftpclient.ftpConnect(); /* FTP登陆 */
}
void FtpClientWindow::ftpHostChanged()
{
ftpclient.setServerName(ui->ftpHostLE->text());
}
void FtpClientWindow::ftpPortChanged()
{
ftpclient.setServerPort(ui->ftpPortLE->text());
}
void FtpClientWindow::ftpUserChanged()
{
ftpclient.setUserName(ui->usernameLE->text());
}
void FtpClientWindow::ftpPwdChanged()
{
ftpclient.setPwd(ui->passwordLE->text());
}
FtpClientWindow::~FtpClientWindow()
{
delete ui;
}