1
<?xml version="1.0" encoding="iso-8859-1"?>
3
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
6
<title>Qt 4.0: ftpwindow.cpp Example File (network/ftp/ftpwindow.cpp)</title>
7
<style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
8
a:link { color: #004faf; text-decoration: none }
9
a:visited { color: #672967; text-decoration: none }
10
td.postheader { font-family: sans-serif }
11
tr.address { font-family: sans-serif }
12
body { background: #ffffff; color: black; }</style>
15
<table border="0" cellpadding="0" cellspacing="0" width="100%">
17
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
18
<td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> · <a href="annotated.html"><font color="#004faf">Annotated</font></a> · <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> · <a href="functions.html"><font color="#004faf">Functions</font></a></td>
19
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">ftpwindow.cpp Example File<br /><small><small>network/ftp/ftpwindow.cpp</small></small></h1>
20
<pre> /****************************************************************************
22
** Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
24
** This file is part of the documentation of the Qt Toolkit.
26
** This file may be distributed under the terms of the Q Public License
27
** as defined by Trolltech AS of Norway and appearing in the file
28
** LICENSE.QPL included in the packaging of this file.
30
** This file may be distributed and/or modified under the terms of the
31
** GNU General Public License version 2 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file.
35
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
36
** information about Qt Commercial License Agreements.
37
** See http://www.trolltech.com/qpl/ for QPL licensing information.
38
** See http://www.trolltech.com/gpl/ for GPL licensing information.
40
** Contact info@trolltech.com if any conditions of this licensing are
43
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
44
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
46
****************************************************************************/
48
#include <QtGui>
49
#include <QtNetwork>
51
#include "ftpwindow.h"
53
FtpWindow::FtpWindow(QWidget *parent)
56
ftpServerLabel = new QLabel(tr("Ftp &server:"));
57
ftpServerLineEdit = new QLineEdit("ftp.trolltech.com");
58
ftpServerLabel->setBuddy(ftpServerLineEdit);
60
statusLabel = new QLabel(tr("Please enter the name of an FTP server."));
62
fileList = new QListWidget;
64
connectButton = new QPushButton(tr("Connect"));
65
connectButton->setDefault(true);
67
downloadButton = new QPushButton(tr("Download"));
68
downloadButton->setEnabled(false);
70
cdToParentButton = new QPushButton;
71
cdToParentButton->setIcon(QPixmap(":/images/cdtoparent.png"));
72
cdToParentButton->setEnabled(false);
74
quitButton = new QPushButton(tr("Quit"));
78
progressDialog = new QProgressDialog(this);
80
connect(ftpServerLineEdit, SIGNAL(textChanged(const QString &)),
81
this, SLOT(enableConnectButton()));
82
connect(fileList, SIGNAL(itemDoubleClicked(QListWidgetItem *)),
83
this, SLOT(processItem(QListWidgetItem *)));
84
connect(fileList, SIGNAL(itemEntered(QListWidgetItem *)),
85
this, SLOT(processItem(QListWidgetItem *)));
86
connect(fileList, SIGNAL(itemSelectionChanged()),
87
this, SLOT(enableDownloadButton()));
88
connect(ftp, SIGNAL(commandFinished(int, bool)),
89
this, SLOT(ftpCommandFinished(int, bool)));
90
connect(ftp, SIGNAL(listInfo(const QUrlInfo &)),
91
this, SLOT(addToList(const QUrlInfo &)));
92
connect(ftp, SIGNAL(dataTransferProgress(qint64, qint64)),
93
this, SLOT(updateDataTransferProgress(qint64, qint64)));
94
connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload()));
95
connect(connectButton, SIGNAL(clicked()), this, SLOT(connectToFtpServer()));
96
connect(cdToParentButton, SIGNAL(clicked()), this, SLOT(cdToParent()));
97
connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile()));
98
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
100
QHBoxLayout *topLayout = new QHBoxLayout;
101
topLayout->addWidget(ftpServerLabel);
102
topLayout->addWidget(ftpServerLineEdit);
103
topLayout->addWidget(cdToParentButton);
105
QHBoxLayout *buttonLayout = new QHBoxLayout;
106
buttonLayout->addStretch(1);
107
buttonLayout->addWidget(downloadButton);
108
buttonLayout->addWidget(connectButton);
109
buttonLayout->addWidget(quitButton);
111
QVBoxLayout *mainLayout = new QVBoxLayout;
112
mainLayout->addLayout(topLayout);
113
mainLayout->addWidget(fileList);
114
mainLayout->addWidget(statusLabel);
115
mainLayout->addLayout(buttonLayout);
116
setLayout(mainLayout);
118
setWindowTitle(tr("FTP"));
121
void FtpWindow::connectToFtpServer()
123
QApplication::setOverrideCursor(Qt::WaitCursor);
124
ftp->connectToHost(ftpServerLineEdit->text());
127
statusLabel->setText(tr("Connecting to FTP server %1...")
128
.arg(ftpServerLineEdit->text()));
131
void FtpWindow::downloadFile()
133
QString fileName = fileList->currentItem()->text();
135
if (QFile::exists(fileName)) {
136
QMessageBox::information(this, tr("FTP"),
137
tr("There already exists a file called %1 in "
138
"the current directory.")
143
file = new QFile(fileName);
144
if (!file->open(QIODevice::WriteOnly)) {
145
QMessageBox::information(this, tr("FTP"),
146
tr("Unable to save the file %1: %2.")
147
.arg(fileName).arg(file->errorString()));
152
ftp->get(fileList->currentItem()->text(), file);
154
progressDialog->setLabelText(tr("Downloading %1...").arg(fileName));
155
progressDialog->show();
156
downloadButton->setEnabled(false);
159
void FtpWindow::cancelDownload()
164
void FtpWindow::ftpCommandFinished(int, bool error)
166
if (ftp->currentCommand() == QFtp::ConnectToHost) {
168
QApplication::restoreOverrideCursor();
169
QMessageBox::information(this, tr("FTP"),
170
tr("Unable to connect to the FTP server "
171
"at %1. Please check that the host "
172
"name is correct.")
173
.arg(ftpServerLineEdit->text()));
177
statusLabel->setText(tr("Logged onto %1.")
178
.arg(ftpServerLineEdit->text()));
179
fileList->setFocus();
180
connectButton->setEnabled(false);
181
downloadButton->setDefault(true);
185
if (ftp->currentCommand() == QFtp::Get) {
186
QApplication::restoreOverrideCursor();
188
statusLabel->setText(tr("Canceled download of %1.")
189
.arg(file->fileName()));
193
statusLabel->setText(tr("Downloaded %1 to current directory.")
194
.arg(file->fileName()));
198
enableDownloadButton();
199
} else if (ftp->currentCommand() == QFtp::List) {
200
QApplication::restoreOverrideCursor();
201
if (isDirectory.isEmpty()) {
202
fileList->addItem(tr("<empty>"));
203
fileList->setEnabled(false);
208
void FtpWindow::addToList(const QUrlInfo &urlInfo)
210
QListWidgetItem *item = new QListWidgetItem;
211
item->setText(urlInfo.name());
212
QPixmap pixmap(urlInfo.isDir() ? ":/images/dir.png" : ":/images/file.png");
213
item->setIcon(pixmap);
215
isDirectory[urlInfo.name()] = urlInfo.isDir();
216
fileList->addItem(item);
217
if (!fileList->currentItem()) {
218
fileList->setCurrentItem(fileList->item(0));
219
fileList->setEnabled(true);
223
void FtpWindow::processItem(QListWidgetItem *item)
225
QString name = item->text();
226
if (isDirectory.value(name)) {
227
fileList->clear();
229
currentPath += "/" + name;
232
cdToParentButton->setEnabled(true);
233
QApplication::setOverrideCursor(Qt::WaitCursor);
238
void FtpWindow::cdToParent()
240
QApplication::setOverrideCursor(Qt::WaitCursor);
241
fileList->clear();
243
currentPath = currentPath.left(currentPath.lastIndexOf('/'));
244
if (currentPath.isEmpty()) {
245
cdToParentButton->setEnabled(false);
246
ftp->cd("/");
248
ftp->cd(currentPath);
253
void FtpWindow::updateDataTransferProgress(qint64 readBytes,
256
progressDialog->setMaximum(totalBytes);
257
progressDialog->setValue(readBytes);
260
void FtpWindow::enableConnectButton()
262
connectButton->setEnabled(!ftpServerLineEdit->text().isEmpty());
265
void FtpWindow::enableDownloadButton()
267
QListWidgetItem *current = fileList->currentItem();
269
QString currentFile = current->text();
270
downloadButton->setEnabled(!isDirectory.value(currentFile));
272
downloadButton->setEnabled(false);
275
<p /><address><hr /><div align="center">
276
<table width="100%" cellspacing="0" border="0"><tr class="address">
277
<td width="30%">Copyright © 2005 <a href="trolltech.html">Trolltech</a></td>
278
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
279
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
280
</tr></table></div></address></body>