author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 3 | 41300fa6a67c |
child 5 | d3bac044e0f0 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the examples of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include <QtGui> |
|
43 |
#include <QtNetwork> |
|
44 |
||
45 |
#include "ftpwindow.h" |
|
46 |
||
47 |
#ifdef Q_OS_SYMBIAN |
|
48 |
#include "sym_iap_util.h" |
|
49 |
#endif |
|
50 |
||
51 |
FtpWindow::FtpWindow(QWidget *parent) |
|
52 |
: QDialog(parent), ftp(0) |
|
53 |
{ |
|
54 |
ftpServerLabel = new QLabel(tr("Ftp &server:")); |
|
55 |
ftpServerLineEdit = new QLineEdit("ftp.qt.nokia.com"); |
|
56 |
ftpServerLabel->setBuddy(ftpServerLineEdit); |
|
57 |
||
58 |
statusLabel = new QLabel(tr("Please enter the name of an FTP server.")); |
|
59 |
#ifdef Q_OS_SYMBIAN |
|
60 |
// Use word wrapping to fit the text on screen |
|
61 |
statusLabel->setWordWrap( true ); |
|
62 |
#endif |
|
63 |
||
64 |
fileList = new QTreeWidget; |
|
65 |
fileList->setEnabled(false); |
|
66 |
fileList->setRootIsDecorated(false); |
|
67 |
fileList->setHeaderLabels(QStringList() << tr("Name") << tr("Size") << tr("Owner") << tr("Group") << tr("Time")); |
|
68 |
fileList->header()->setStretchLastSection(false); |
|
69 |
||
70 |
connectButton = new QPushButton(tr("Connect")); |
|
71 |
connectButton->setDefault(true); |
|
72 |
||
73 |
cdToParentButton = new QPushButton; |
|
74 |
cdToParentButton->setIcon(QPixmap(":/images/cdtoparent.png")); |
|
75 |
cdToParentButton->setEnabled(false); |
|
76 |
||
77 |
downloadButton = new QPushButton(tr("Download")); |
|
78 |
downloadButton->setEnabled(false); |
|
79 |
||
80 |
quitButton = new QPushButton(tr("Quit")); |
|
81 |
||
82 |
buttonBox = new QDialogButtonBox; |
|
83 |
buttonBox->addButton(downloadButton, QDialogButtonBox::ActionRole); |
|
84 |
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole); |
|
85 |
||
86 |
progressDialog = new QProgressDialog(this); |
|
87 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
88 |
connect(fileList, SIGNAL(itemActivated(QTreeWidgetItem*,int)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
89 |
this, SLOT(processItem(QTreeWidgetItem*,int))); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
90 |
connect(fileList, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)), |
0 | 91 |
this, SLOT(enableDownloadButton())); |
92 |
connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload())); |
|
93 |
connect(connectButton, SIGNAL(clicked()), this, SLOT(connectOrDisconnect())); |
|
94 |
connect(cdToParentButton, SIGNAL(clicked()), this, SLOT(cdToParent())); |
|
95 |
connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile())); |
|
96 |
connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); |
|
97 |
||
98 |
QHBoxLayout *topLayout = new QHBoxLayout; |
|
99 |
topLayout->addWidget(ftpServerLabel); |
|
100 |
topLayout->addWidget(ftpServerLineEdit); |
|
101 |
#ifndef Q_OS_SYMBIAN |
|
102 |
topLayout->addWidget(cdToParentButton); |
|
103 |
topLayout->addWidget(connectButton); |
|
104 |
#else |
|
105 |
// Make app better lookin on small screen |
|
106 |
QHBoxLayout *topLayout2 = new QHBoxLayout; |
|
107 |
topLayout2->addWidget(cdToParentButton); |
|
108 |
topLayout2->addWidget(connectButton); |
|
109 |
#endif |
|
110 |
||
111 |
QVBoxLayout *mainLayout = new QVBoxLayout; |
|
112 |
mainLayout->addLayout(topLayout); |
|
113 |
#ifdef Q_OS_SYMBIAN |
|
114 |
// Make app better lookin on small screen |
|
115 |
mainLayout->addLayout(topLayout2); |
|
116 |
#endif |
|
117 |
mainLayout->addWidget(fileList); |
|
118 |
mainLayout->addWidget(statusLabel); |
|
119 |
mainLayout->addWidget(buttonBox); |
|
120 |
setLayout(mainLayout); |
|
121 |
||
122 |
#ifdef Q_OS_SYMBIAN |
|
123 |
bDefaultIapSet = false; |
|
124 |
#endif |
|
125 |
||
126 |
setWindowTitle(tr("FTP")); |
|
127 |
} |
|
128 |
||
129 |
QSize FtpWindow::sizeHint() const |
|
130 |
{ |
|
131 |
return QSize(500, 300); |
|
132 |
} |
|
133 |
||
134 |
//![0] |
|
135 |
void FtpWindow::connectOrDisconnect() |
|
136 |
{ |
|
137 |
#ifdef Q_OS_SYMBIAN |
|
138 |
if(!bDefaultIapSet) { |
|
139 |
qt_SetDefaultIap(); |
|
140 |
bDefaultIapSet = true; |
|
141 |
} |
|
142 |
#endif |
|
143 |
if (ftp) { |
|
144 |
ftp->abort(); |
|
145 |
ftp->deleteLater(); |
|
146 |
ftp = 0; |
|
147 |
//![0] |
|
148 |
fileList->setEnabled(false); |
|
149 |
cdToParentButton->setEnabled(false); |
|
150 |
downloadButton->setEnabled(false); |
|
151 |
connectButton->setEnabled(true); |
|
152 |
connectButton->setText(tr("Connect")); |
|
153 |
#ifndef QT_NO_CURSOR |
|
154 |
setCursor(Qt::ArrowCursor); |
|
155 |
#endif |
|
156 |
statusLabel->setText(tr("Please enter the name of an FTP server.")); |
|
157 |
return; |
|
158 |
} |
|
159 |
||
160 |
#ifndef QT_NO_CURSOR |
|
161 |
setCursor(Qt::WaitCursor); |
|
162 |
#endif |
|
163 |
||
164 |
//![1] |
|
165 |
ftp = new QFtp(this); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
166 |
connect(ftp, SIGNAL(commandFinished(int,bool)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
167 |
this, SLOT(ftpCommandFinished(int,bool))); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
168 |
connect(ftp, SIGNAL(listInfo(QUrlInfo)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
169 |
this, SLOT(addToList(QUrlInfo))); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
170 |
connect(ftp, SIGNAL(dataTransferProgress(qint64,qint64)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
171 |
this, SLOT(updateDataTransferProgress(qint64,qint64))); |
0 | 172 |
|
173 |
fileList->clear(); |
|
174 |
currentPath.clear(); |
|
175 |
isDirectory.clear(); |
|
176 |
//![1] |
|
177 |
||
178 |
//![2] |
|
179 |
QUrl url(ftpServerLineEdit->text()); |
|
180 |
if (!url.isValid() || url.scheme().toLower() != QLatin1String("ftp")) { |
|
181 |
ftp->connectToHost(ftpServerLineEdit->text(), 21); |
|
182 |
ftp->login(); |
|
183 |
} else { |
|
184 |
ftp->connectToHost(url.host(), url.port(21)); |
|
185 |
||
186 |
if (!url.userName().isEmpty()) |
|
187 |
ftp->login(QUrl::fromPercentEncoding(url.userName().toLatin1()), url.password()); |
|
188 |
else |
|
189 |
ftp->login(); |
|
190 |
if (!url.path().isEmpty()) |
|
191 |
ftp->cd(url.path()); |
|
192 |
} |
|
193 |
//![2] |
|
194 |
||
195 |
fileList->setEnabled(true); |
|
196 |
connectButton->setEnabled(false); |
|
197 |
connectButton->setText(tr("Disconnect")); |
|
198 |
statusLabel->setText(tr("Connecting to FTP server %1...") |
|
199 |
.arg(ftpServerLineEdit->text())); |
|
200 |
} |
|
201 |
||
202 |
//![3] |
|
203 |
void FtpWindow::downloadFile() |
|
204 |
{ |
|
205 |
QString fileName = fileList->currentItem()->text(0); |
|
206 |
//![3] |
|
207 |
// |
|
208 |
if (QFile::exists(fileName)) { |
|
209 |
QMessageBox::information(this, tr("FTP"), |
|
210 |
tr("There already exists a file called %1 in " |
|
211 |
"the current directory.") |
|
212 |
.arg(fileName)); |
|
213 |
return; |
|
214 |
} |
|
215 |
||
216 |
//![4] |
|
217 |
file = new QFile(fileName); |
|
218 |
if (!file->open(QIODevice::WriteOnly)) { |
|
219 |
QMessageBox::information(this, tr("FTP"), |
|
220 |
tr("Unable to save the file %1: %2.") |
|
221 |
.arg(fileName).arg(file->errorString())); |
|
222 |
delete file; |
|
223 |
return; |
|
224 |
} |
|
225 |
||
226 |
ftp->get(fileList->currentItem()->text(0), file); |
|
227 |
||
228 |
progressDialog->setLabelText(tr("Downloading %1...").arg(fileName)); |
|
229 |
downloadButton->setEnabled(false); |
|
230 |
progressDialog->exec(); |
|
231 |
} |
|
232 |
//![4] |
|
233 |
||
234 |
//![5] |
|
235 |
void FtpWindow::cancelDownload() |
|
236 |
{ |
|
237 |
ftp->abort(); |
|
238 |
} |
|
239 |
//![5] |
|
240 |
||
241 |
//![6] |
|
242 |
void FtpWindow::ftpCommandFinished(int, bool error) |
|
243 |
{ |
|
244 |
#ifndef QT_NO_CURSOR |
|
245 |
setCursor(Qt::ArrowCursor); |
|
246 |
#endif |
|
247 |
||
248 |
if (ftp->currentCommand() == QFtp::ConnectToHost) { |
|
249 |
if (error) { |
|
250 |
QMessageBox::information(this, tr("FTP"), |
|
251 |
tr("Unable to connect to the FTP server " |
|
252 |
"at %1. Please check that the host " |
|
253 |
"name is correct.") |
|
254 |
.arg(ftpServerLineEdit->text())); |
|
255 |
connectOrDisconnect(); |
|
256 |
return; |
|
257 |
} |
|
258 |
statusLabel->setText(tr("Logged onto %1.") |
|
259 |
.arg(ftpServerLineEdit->text())); |
|
260 |
fileList->setFocus(); |
|
261 |
downloadButton->setDefault(true); |
|
262 |
connectButton->setEnabled(true); |
|
263 |
return; |
|
264 |
} |
|
265 |
//![6] |
|
266 |
||
267 |
//![7] |
|
268 |
if (ftp->currentCommand() == QFtp::Login) |
|
269 |
ftp->list(); |
|
270 |
//![7] |
|
271 |
||
272 |
//![8] |
|
273 |
if (ftp->currentCommand() == QFtp::Get) { |
|
274 |
if (error) { |
|
275 |
statusLabel->setText(tr("Canceled download of %1.") |
|
276 |
.arg(file->fileName())); |
|
277 |
file->close(); |
|
278 |
file->remove(); |
|
279 |
} else { |
|
280 |
statusLabel->setText(tr("Downloaded %1 to current directory.") |
|
281 |
.arg(file->fileName())); |
|
282 |
file->close(); |
|
283 |
} |
|
284 |
delete file; |
|
285 |
enableDownloadButton(); |
|
286 |
progressDialog->hide(); |
|
287 |
//![8] |
|
288 |
//![9] |
|
289 |
} else if (ftp->currentCommand() == QFtp::List) { |
|
290 |
if (isDirectory.isEmpty()) { |
|
291 |
fileList->addTopLevelItem(new QTreeWidgetItem(QStringList() << tr("<empty>"))); |
|
292 |
fileList->setEnabled(false); |
|
293 |
} |
|
294 |
} |
|
295 |
//![9] |
|
296 |
} |
|
297 |
||
298 |
//![10] |
|
299 |
void FtpWindow::addToList(const QUrlInfo &urlInfo) |
|
300 |
{ |
|
301 |
QTreeWidgetItem *item = new QTreeWidgetItem; |
|
302 |
item->setText(0, urlInfo.name()); |
|
303 |
item->setText(1, QString::number(urlInfo.size())); |
|
304 |
item->setText(2, urlInfo.owner()); |
|
305 |
item->setText(3, urlInfo.group()); |
|
306 |
item->setText(4, urlInfo.lastModified().toString("MMM dd yyyy")); |
|
307 |
||
308 |
QPixmap pixmap(urlInfo.isDir() ? ":/images/dir.png" : ":/images/file.png"); |
|
309 |
item->setIcon(0, pixmap); |
|
310 |
||
311 |
isDirectory[urlInfo.name()] = urlInfo.isDir(); |
|
312 |
fileList->addTopLevelItem(item); |
|
313 |
if (!fileList->currentItem()) { |
|
314 |
fileList->setCurrentItem(fileList->topLevelItem(0)); |
|
315 |
fileList->setEnabled(true); |
|
316 |
} |
|
317 |
} |
|
318 |
//![10] |
|
319 |
||
320 |
//![11] |
|
321 |
void FtpWindow::processItem(QTreeWidgetItem *item, int /*column*/) |
|
322 |
{ |
|
323 |
QString name = item->text(0); |
|
324 |
if (isDirectory.value(name)) { |
|
325 |
fileList->clear(); |
|
326 |
isDirectory.clear(); |
|
327 |
currentPath += "/" + name; |
|
328 |
ftp->cd(name); |
|
329 |
ftp->list(); |
|
330 |
cdToParentButton->setEnabled(true); |
|
331 |
#ifndef QT_NO_CURSOR |
|
332 |
setCursor(Qt::WaitCursor); |
|
333 |
#endif |
|
334 |
return; |
|
335 |
} |
|
336 |
} |
|
337 |
//![11] |
|
338 |
||
339 |
//![12] |
|
340 |
void FtpWindow::cdToParent() |
|
341 |
{ |
|
342 |
#ifndef QT_NO_CURSOR |
|
343 |
setCursor(Qt::WaitCursor); |
|
344 |
#endif |
|
345 |
fileList->clear(); |
|
346 |
isDirectory.clear(); |
|
347 |
currentPath = currentPath.left(currentPath.lastIndexOf('/')); |
|
348 |
if (currentPath.isEmpty()) { |
|
349 |
cdToParentButton->setEnabled(false); |
|
350 |
ftp->cd("/"); |
|
351 |
} else { |
|
352 |
ftp->cd(currentPath); |
|
353 |
} |
|
354 |
ftp->list(); |
|
355 |
} |
|
356 |
//![12] |
|
357 |
||
358 |
//![13] |
|
359 |
void FtpWindow::updateDataTransferProgress(qint64 readBytes, |
|
360 |
qint64 totalBytes) |
|
361 |
{ |
|
362 |
progressDialog->setMaximum(totalBytes); |
|
363 |
progressDialog->setValue(readBytes); |
|
364 |
} |
|
365 |
//![13] |
|
366 |
||
367 |
//![14] |
|
368 |
void FtpWindow::enableDownloadButton() |
|
369 |
{ |
|
370 |
QTreeWidgetItem *current = fileList->currentItem(); |
|
371 |
if (current) { |
|
372 |
QString currentFile = current->text(0); |
|
373 |
downloadButton->setEnabled(!isDirectory.value(currentFile)); |
|
374 |
} else { |
|
375 |
downloadButton->setEnabled(false); |
|
376 |
} |
|
377 |
} |
|
378 |
//![14] |
|
379 |