1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QtGui>
|
---|
42 | #include <QtNetwork>
|
---|
43 |
|
---|
44 | #include "ftpwindow.h"
|
---|
45 |
|
---|
46 | FtpWindow::FtpWindow(QWidget *parent)
|
---|
47 | : QDialog(parent), ftp(0), networkSession(0)
|
---|
48 | {
|
---|
49 | ftpServerLabel = new QLabel(tr("Ftp &server:"));
|
---|
50 | ftpServerLineEdit = new QLineEdit("ftp.qt.nokia.com");
|
---|
51 | ftpServerLabel->setBuddy(ftpServerLineEdit);
|
---|
52 |
|
---|
53 | statusLabel = new QLabel(tr("Please enter the name of an FTP server."));
|
---|
54 | #ifdef Q_OS_SYMBIAN
|
---|
55 | // Use word wrapping to fit the text on screen
|
---|
56 | statusLabel->setWordWrap( true );
|
---|
57 | #endif
|
---|
58 |
|
---|
59 | fileList = new QTreeWidget;
|
---|
60 | fileList->setEnabled(false);
|
---|
61 | fileList->setRootIsDecorated(false);
|
---|
62 | fileList->setHeaderLabels(QStringList() << tr("Name") << tr("Size") << tr("Owner") << tr("Group") << tr("Time"));
|
---|
63 | fileList->header()->setStretchLastSection(false);
|
---|
64 |
|
---|
65 | connectButton = new QPushButton(tr("Connect"));
|
---|
66 | connectButton->setDefault(true);
|
---|
67 |
|
---|
68 | cdToParentButton = new QPushButton;
|
---|
69 | cdToParentButton->setIcon(QPixmap(":/images/cdtoparent.png"));
|
---|
70 | cdToParentButton->setEnabled(false);
|
---|
71 |
|
---|
72 | downloadButton = new QPushButton(tr("Download"));
|
---|
73 | downloadButton->setEnabled(false);
|
---|
74 |
|
---|
75 | quitButton = new QPushButton(tr("Quit"));
|
---|
76 |
|
---|
77 | buttonBox = new QDialogButtonBox;
|
---|
78 | buttonBox->addButton(downloadButton, QDialogButtonBox::ActionRole);
|
---|
79 | buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
|
---|
80 |
|
---|
81 | progressDialog = new QProgressDialog(this);
|
---|
82 |
|
---|
83 | connect(fileList, SIGNAL(itemActivated(QTreeWidgetItem*,int)),
|
---|
84 | this, SLOT(processItem(QTreeWidgetItem*,int)));
|
---|
85 | connect(fileList, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
|
---|
86 | this, SLOT(enableDownloadButton()));
|
---|
87 | connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload()));
|
---|
88 | connect(connectButton, SIGNAL(clicked()), this, SLOT(connectOrDisconnect()));
|
---|
89 | connect(cdToParentButton, SIGNAL(clicked()), this, SLOT(cdToParent()));
|
---|
90 | connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile()));
|
---|
91 | connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
|
---|
92 |
|
---|
93 | QHBoxLayout *topLayout = new QHBoxLayout;
|
---|
94 | topLayout->addWidget(ftpServerLabel);
|
---|
95 | topLayout->addWidget(ftpServerLineEdit);
|
---|
96 | #ifndef Q_OS_SYMBIAN
|
---|
97 | topLayout->addWidget(cdToParentButton);
|
---|
98 | topLayout->addWidget(connectButton);
|
---|
99 | #else
|
---|
100 | // Make app better lookin on small screen
|
---|
101 | QHBoxLayout *topLayout2 = new QHBoxLayout;
|
---|
102 | topLayout2->addWidget(cdToParentButton);
|
---|
103 | topLayout2->addWidget(connectButton);
|
---|
104 | #endif
|
---|
105 |
|
---|
106 | QVBoxLayout *mainLayout = new QVBoxLayout;
|
---|
107 | mainLayout->addLayout(topLayout);
|
---|
108 | #ifdef Q_OS_SYMBIAN
|
---|
109 | // Make app better lookin on small screen
|
---|
110 | mainLayout->addLayout(topLayout2);
|
---|
111 | #endif
|
---|
112 | mainLayout->addWidget(fileList);
|
---|
113 | mainLayout->addWidget(statusLabel);
|
---|
114 | mainLayout->addWidget(buttonBox);
|
---|
115 | setLayout(mainLayout);
|
---|
116 |
|
---|
117 | QNetworkConfigurationManager manager;
|
---|
118 | if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) {
|
---|
119 | // Get saved network configuration
|
---|
120 | QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
|
---|
121 | settings.beginGroup(QLatin1String("QtNetwork"));
|
---|
122 | const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString();
|
---|
123 | settings.endGroup();
|
---|
124 |
|
---|
125 | // If the saved network configuration is not currently discovered use the system default
|
---|
126 | QNetworkConfiguration config = manager.configurationFromIdentifier(id);
|
---|
127 | if ((config.state() & QNetworkConfiguration::Discovered) !=
|
---|
128 | QNetworkConfiguration::Discovered) {
|
---|
129 | config = manager.defaultConfiguration();
|
---|
130 | }
|
---|
131 |
|
---|
132 | networkSession = new QNetworkSession(config, this);
|
---|
133 | connect(networkSession, SIGNAL(opened()), this, SLOT(enableConnectButton()));
|
---|
134 |
|
---|
135 | connectButton->setEnabled(false);
|
---|
136 | statusLabel->setText(tr("Opening network session."));
|
---|
137 | networkSession->open();
|
---|
138 | }
|
---|
139 |
|
---|
140 | setWindowTitle(tr("FTP"));
|
---|
141 | }
|
---|
142 |
|
---|
143 | QSize FtpWindow::sizeHint() const
|
---|
144 | {
|
---|
145 | return QSize(500, 300);
|
---|
146 | }
|
---|
147 |
|
---|
148 | //![0]
|
---|
149 | void FtpWindow::connectOrDisconnect()
|
---|
150 | {
|
---|
151 | if (ftp) {
|
---|
152 | ftp->abort();
|
---|
153 | ftp->deleteLater();
|
---|
154 | ftp = 0;
|
---|
155 | //![0]
|
---|
156 | fileList->setEnabled(false);
|
---|
157 | cdToParentButton->setEnabled(false);
|
---|
158 | downloadButton->setEnabled(false);
|
---|
159 | connectButton->setEnabled(true);
|
---|
160 | connectButton->setText(tr("Connect"));
|
---|
161 | #ifndef QT_NO_CURSOR
|
---|
162 | setCursor(Qt::ArrowCursor);
|
---|
163 | #endif
|
---|
164 | statusLabel->setText(tr("Please enter the name of an FTP server."));
|
---|
165 | return;
|
---|
166 | }
|
---|
167 |
|
---|
168 | #ifndef QT_NO_CURSOR
|
---|
169 | setCursor(Qt::WaitCursor);
|
---|
170 | #endif
|
---|
171 |
|
---|
172 | //![1]
|
---|
173 | ftp = new QFtp(this);
|
---|
174 | connect(ftp, SIGNAL(commandFinished(int,bool)),
|
---|
175 | this, SLOT(ftpCommandFinished(int,bool)));
|
---|
176 | connect(ftp, SIGNAL(listInfo(QUrlInfo)),
|
---|
177 | this, SLOT(addToList(QUrlInfo)));
|
---|
178 | connect(ftp, SIGNAL(dataTransferProgress(qint64,qint64)),
|
---|
179 | this, SLOT(updateDataTransferProgress(qint64,qint64)));
|
---|
180 |
|
---|
181 | fileList->clear();
|
---|
182 | currentPath.clear();
|
---|
183 | isDirectory.clear();
|
---|
184 | //![1]
|
---|
185 |
|
---|
186 | //![2]
|
---|
187 | QUrl url(ftpServerLineEdit->text());
|
---|
188 | if (!url.isValid() || url.scheme().toLower() != QLatin1String("ftp")) {
|
---|
189 | ftp->connectToHost(ftpServerLineEdit->text(), 21);
|
---|
190 | ftp->login();
|
---|
191 | } else {
|
---|
192 | ftp->connectToHost(url.host(), url.port(21));
|
---|
193 |
|
---|
194 | if (!url.userName().isEmpty())
|
---|
195 | ftp->login(QUrl::fromPercentEncoding(url.userName().toLatin1()), url.password());
|
---|
196 | else
|
---|
197 | ftp->login();
|
---|
198 | if (!url.path().isEmpty())
|
---|
199 | ftp->cd(url.path());
|
---|
200 | }
|
---|
201 | //![2]
|
---|
202 |
|
---|
203 | fileList->setEnabled(true);
|
---|
204 | connectButton->setEnabled(false);
|
---|
205 | connectButton->setText(tr("Disconnect"));
|
---|
206 | statusLabel->setText(tr("Connecting to FTP server %1...")
|
---|
207 | .arg(ftpServerLineEdit->text()));
|
---|
208 | }
|
---|
209 |
|
---|
210 | //![3]
|
---|
211 | void FtpWindow::downloadFile()
|
---|
212 | {
|
---|
213 | QString fileName = fileList->currentItem()->text(0);
|
---|
214 | //![3]
|
---|
215 | //
|
---|
216 | if (QFile::exists(fileName)) {
|
---|
217 | QMessageBox::information(this, tr("FTP"),
|
---|
218 | tr("There already exists a file called %1 in "
|
---|
219 | "the current directory.")
|
---|
220 | .arg(fileName));
|
---|
221 | return;
|
---|
222 | }
|
---|
223 |
|
---|
224 | //![4]
|
---|
225 | file = new QFile(fileName);
|
---|
226 | if (!file->open(QIODevice::WriteOnly)) {
|
---|
227 | QMessageBox::information(this, tr("FTP"),
|
---|
228 | tr("Unable to save the file %1: %2.")
|
---|
229 | .arg(fileName).arg(file->errorString()));
|
---|
230 | delete file;
|
---|
231 | return;
|
---|
232 | }
|
---|
233 |
|
---|
234 | ftp->get(fileList->currentItem()->text(0), file);
|
---|
235 |
|
---|
236 | progressDialog->setLabelText(tr("Downloading %1...").arg(fileName));
|
---|
237 | downloadButton->setEnabled(false);
|
---|
238 | progressDialog->exec();
|
---|
239 | }
|
---|
240 | //![4]
|
---|
241 |
|
---|
242 | //![5]
|
---|
243 | void FtpWindow::cancelDownload()
|
---|
244 | {
|
---|
245 | ftp->abort();
|
---|
246 | }
|
---|
247 | //![5]
|
---|
248 |
|
---|
249 | //![6]
|
---|
250 | void FtpWindow::ftpCommandFinished(int, bool error)
|
---|
251 | {
|
---|
252 | #ifndef QT_NO_CURSOR
|
---|
253 | setCursor(Qt::ArrowCursor);
|
---|
254 | #endif
|
---|
255 |
|
---|
256 | if (ftp->currentCommand() == QFtp::ConnectToHost) {
|
---|
257 | if (error) {
|
---|
258 | QMessageBox::information(this, tr("FTP"),
|
---|
259 | tr("Unable to connect to the FTP server "
|
---|
260 | "at %1. Please check that the host "
|
---|
261 | "name is correct.")
|
---|
262 | .arg(ftpServerLineEdit->text()));
|
---|
263 | connectOrDisconnect();
|
---|
264 | return;
|
---|
265 | }
|
---|
266 | statusLabel->setText(tr("Logged onto %1.")
|
---|
267 | .arg(ftpServerLineEdit->text()));
|
---|
268 | fileList->setFocus();
|
---|
269 | downloadButton->setDefault(true);
|
---|
270 | connectButton->setEnabled(true);
|
---|
271 | return;
|
---|
272 | }
|
---|
273 | //![6]
|
---|
274 |
|
---|
275 | //![7]
|
---|
276 | if (ftp->currentCommand() == QFtp::Login)
|
---|
277 | ftp->list();
|
---|
278 | //![7]
|
---|
279 |
|
---|
280 | //![8]
|
---|
281 | if (ftp->currentCommand() == QFtp::Get) {
|
---|
282 | if (error) {
|
---|
283 | statusLabel->setText(tr("Canceled download of %1.")
|
---|
284 | .arg(file->fileName()));
|
---|
285 | file->close();
|
---|
286 | file->remove();
|
---|
287 | } else {
|
---|
288 | statusLabel->setText(tr("Downloaded %1 to current directory.")
|
---|
289 | .arg(file->fileName()));
|
---|
290 | file->close();
|
---|
291 | }
|
---|
292 | delete file;
|
---|
293 | enableDownloadButton();
|
---|
294 | progressDialog->hide();
|
---|
295 | //![8]
|
---|
296 | //![9]
|
---|
297 | } else if (ftp->currentCommand() == QFtp::List) {
|
---|
298 | if (isDirectory.isEmpty()) {
|
---|
299 | fileList->addTopLevelItem(new QTreeWidgetItem(QStringList() << tr("<empty>")));
|
---|
300 | fileList->setEnabled(false);
|
---|
301 | }
|
---|
302 | }
|
---|
303 | //![9]
|
---|
304 | }
|
---|
305 |
|
---|
306 | //![10]
|
---|
307 | void FtpWindow::addToList(const QUrlInfo &urlInfo)
|
---|
308 | {
|
---|
309 | QTreeWidgetItem *item = new QTreeWidgetItem;
|
---|
310 | item->setText(0, urlInfo.name());
|
---|
311 | item->setText(1, QString::number(urlInfo.size()));
|
---|
312 | item->setText(2, urlInfo.owner());
|
---|
313 | item->setText(3, urlInfo.group());
|
---|
314 | item->setText(4, urlInfo.lastModified().toString("MMM dd yyyy"));
|
---|
315 |
|
---|
316 | QPixmap pixmap(urlInfo.isDir() ? ":/images/dir.png" : ":/images/file.png");
|
---|
317 | item->setIcon(0, pixmap);
|
---|
318 |
|
---|
319 | isDirectory[urlInfo.name()] = urlInfo.isDir();
|
---|
320 | fileList->addTopLevelItem(item);
|
---|
321 | if (!fileList->currentItem()) {
|
---|
322 | fileList->setCurrentItem(fileList->topLevelItem(0));
|
---|
323 | fileList->setEnabled(true);
|
---|
324 | }
|
---|
325 | }
|
---|
326 | //![10]
|
---|
327 |
|
---|
328 | //![11]
|
---|
329 | void FtpWindow::processItem(QTreeWidgetItem *item, int /*column*/)
|
---|
330 | {
|
---|
331 | QString name = item->text(0);
|
---|
332 | if (isDirectory.value(name)) {
|
---|
333 | fileList->clear();
|
---|
334 | isDirectory.clear();
|
---|
335 | currentPath += '/';
|
---|
336 | currentPath += name;
|
---|
337 | ftp->cd(name);
|
---|
338 | ftp->list();
|
---|
339 | cdToParentButton->setEnabled(true);
|
---|
340 | #ifndef QT_NO_CURSOR
|
---|
341 | setCursor(Qt::WaitCursor);
|
---|
342 | #endif
|
---|
343 | return;
|
---|
344 | }
|
---|
345 | }
|
---|
346 | //![11]
|
---|
347 |
|
---|
348 | //![12]
|
---|
349 | void FtpWindow::cdToParent()
|
---|
350 | {
|
---|
351 | #ifndef QT_NO_CURSOR
|
---|
352 | setCursor(Qt::WaitCursor);
|
---|
353 | #endif
|
---|
354 | fileList->clear();
|
---|
355 | isDirectory.clear();
|
---|
356 | currentPath = currentPath.left(currentPath.lastIndexOf('/'));
|
---|
357 | if (currentPath.isEmpty()) {
|
---|
358 | cdToParentButton->setEnabled(false);
|
---|
359 | ftp->cd("/");
|
---|
360 | } else {
|
---|
361 | ftp->cd(currentPath);
|
---|
362 | }
|
---|
363 | ftp->list();
|
---|
364 | }
|
---|
365 | //![12]
|
---|
366 |
|
---|
367 | //![13]
|
---|
368 | void FtpWindow::updateDataTransferProgress(qint64 readBytes,
|
---|
369 | qint64 totalBytes)
|
---|
370 | {
|
---|
371 | progressDialog->setMaximum(totalBytes);
|
---|
372 | progressDialog->setValue(readBytes);
|
---|
373 | }
|
---|
374 | //![13]
|
---|
375 |
|
---|
376 | //![14]
|
---|
377 | void FtpWindow::enableDownloadButton()
|
---|
378 | {
|
---|
379 | QTreeWidgetItem *current = fileList->currentItem();
|
---|
380 | if (current) {
|
---|
381 | QString currentFile = current->text(0);
|
---|
382 | downloadButton->setEnabled(!isDirectory.value(currentFile));
|
---|
383 | } else {
|
---|
384 | downloadButton->setEnabled(false);
|
---|
385 | }
|
---|
386 | }
|
---|
387 | //![14]
|
---|
388 |
|
---|
389 | void FtpWindow::enableConnectButton()
|
---|
390 | {
|
---|
391 | // Save the used configuration
|
---|
392 | QNetworkConfiguration config = networkSession->configuration();
|
---|
393 | QString id;
|
---|
394 | if (config.type() == QNetworkConfiguration::UserChoice)
|
---|
395 | id = networkSession->sessionProperty(QLatin1String("UserChoiceConfiguration")).toString();
|
---|
396 | else
|
---|
397 | id = config.identifier();
|
---|
398 |
|
---|
399 | QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
|
---|
400 | settings.beginGroup(QLatin1String("QtNetwork"));
|
---|
401 | settings.setValue(QLatin1String("DefaultNetworkConfiguration"), id);
|
---|
402 | settings.endGroup();
|
---|
403 |
|
---|
404 | connectButton->setEnabled(networkSession->isOpen());
|
---|
405 | statusLabel->setText(tr("Please enter the name of an FTP server."));
|
---|
406 | }
|
---|
407 |
|
---|