1
/****************************************************************************
3
** Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
5
** This file is part of the example classes of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
32
#include "ftpwindow.h"
34
FtpWindow::FtpWindow(QWidget *parent)
37
ftpServerLabel = new QLabel(tr("Ftp &server:"));
38
ftpServerLineEdit = new QLineEdit("ftp.trolltech.com");
39
ftpServerLabel->setBuddy(ftpServerLineEdit);
41
statusLabel = new QLabel(tr("Please enter the name of an FTP server."));
43
fileList = new QListWidget;
45
connectButton = new QPushButton(tr("Connect"));
46
connectButton->setDefault(true);
48
downloadButton = new QPushButton(tr("Download"));
49
downloadButton->setEnabled(false);
51
cdToParentButton = new QPushButton;
52
cdToParentButton->setIcon(QPixmap(":/images/cdtoparent.png"));
53
cdToParentButton->setEnabled(false);
55
quitButton = new QPushButton(tr("Quit"));
59
progressDialog = new QProgressDialog(this);
61
connect(ftpServerLineEdit, SIGNAL(textChanged(const QString &)),
62
this, SLOT(enableConnectButton()));
63
connect(fileList, SIGNAL(itemDoubleClicked(QListWidgetItem *)),
64
this, SLOT(processItem(QListWidgetItem *)));
65
connect(fileList, SIGNAL(itemEntered(QListWidgetItem *)),
66
this, SLOT(processItem(QListWidgetItem *)));
67
connect(fileList, SIGNAL(itemSelectionChanged()),
68
this, SLOT(enableDownloadButton()));
69
connect(ftp, SIGNAL(commandFinished(int, bool)),
70
this, SLOT(ftpCommandFinished(int, bool)));
71
connect(ftp, SIGNAL(listInfo(const QUrlInfo &)),
72
this, SLOT(addToList(const QUrlInfo &)));
73
connect(ftp, SIGNAL(dataTransferProgress(qint64, qint64)),
74
this, SLOT(updateDataTransferProgress(qint64, qint64)));
75
connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload()));
76
connect(connectButton, SIGNAL(clicked()), this, SLOT(connectToFtpServer()));
77
connect(cdToParentButton, SIGNAL(clicked()), this, SLOT(cdToParent()));
78
connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile()));
79
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
81
QHBoxLayout *topLayout = new QHBoxLayout;
82
topLayout->addWidget(ftpServerLabel);
83
topLayout->addWidget(ftpServerLineEdit);
84
topLayout->addWidget(cdToParentButton);
86
QHBoxLayout *buttonLayout = new QHBoxLayout;
87
buttonLayout->addStretch(1);
88
buttonLayout->addWidget(downloadButton);
89
buttonLayout->addWidget(connectButton);
90
buttonLayout->addWidget(quitButton);
92
QVBoxLayout *mainLayout = new QVBoxLayout;
93
mainLayout->addLayout(topLayout);
94
mainLayout->addWidget(fileList);
95
mainLayout->addWidget(statusLabel);
96
mainLayout->addLayout(buttonLayout);
97
setLayout(mainLayout);
99
setWindowTitle(tr("FTP"));
102
void FtpWindow::connectToFtpServer()
104
QApplication::setOverrideCursor(Qt::WaitCursor);
105
ftp->connectToHost(ftpServerLineEdit->text());
108
statusLabel->setText(tr("Connecting to FTP server %1...")
109
.arg(ftpServerLineEdit->text()));
112
void FtpWindow::downloadFile()
114
QString fileName = fileList->currentItem()->text();
116
if (QFile::exists(fileName)) {
117
QMessageBox::information(this, tr("FTP"),
118
tr("There already exists a file called %1 in "
119
"the current directory.")
124
file = new QFile(fileName);
125
if (!file->open(QIODevice::WriteOnly)) {
126
QMessageBox::information(this, tr("FTP"),
127
tr("Unable to save the file %1: %2.")
128
.arg(fileName).arg(file->errorString()));
133
ftp->get(fileList->currentItem()->text(), file);
135
progressDialog->setLabelText(tr("Downloading %1...").arg(fileName));
136
progressDialog->show();
137
downloadButton->setEnabled(false);
140
void FtpWindow::cancelDownload()
145
void FtpWindow::ftpCommandFinished(int, bool error)
147
if (ftp->currentCommand() == QFtp::ConnectToHost) {
149
QApplication::restoreOverrideCursor();
150
QMessageBox::information(this, tr("FTP"),
151
tr("Unable to connect to the FTP server "
152
"at %1. Please check that the host "
154
.arg(ftpServerLineEdit->text()));
158
statusLabel->setText(tr("Logged onto %1.")
159
.arg(ftpServerLineEdit->text()));
160
fileList->setFocus();
161
connectButton->setEnabled(false);
162
downloadButton->setDefault(true);
166
if (ftp->currentCommand() == QFtp::Get) {
167
QApplication::restoreOverrideCursor();
169
statusLabel->setText(tr("Canceled download of %1.")
170
.arg(file->fileName()));
174
statusLabel->setText(tr("Downloaded %1 to current directory.")
175
.arg(file->fileName()));
179
enableDownloadButton();
180
} else if (ftp->currentCommand() == QFtp::List) {
181
QApplication::restoreOverrideCursor();
182
if (isDirectory.isEmpty()) {
183
fileList->addItem(tr("<empty>"));
184
fileList->setEnabled(false);
189
void FtpWindow::addToList(const QUrlInfo &urlInfo)
191
QListWidgetItem *item = new QListWidgetItem;
192
item->setText(urlInfo.name());
193
QPixmap pixmap(urlInfo.isDir() ? ":/images/dir.png" : ":/images/file.png");
194
item->setIcon(pixmap);
196
isDirectory[urlInfo.name()] = urlInfo.isDir();
197
fileList->addItem(item);
198
if (!fileList->currentItem()) {
199
fileList->setCurrentItem(fileList->item(0));
200
fileList->setEnabled(true);
204
void FtpWindow::processItem(QListWidgetItem *item)
206
QString name = item->text();
207
if (isDirectory.value(name)) {
210
currentPath += "/" + name;
213
cdToParentButton->setEnabled(true);
214
QApplication::setOverrideCursor(Qt::WaitCursor);
219
void FtpWindow::cdToParent()
221
QApplication::setOverrideCursor(Qt::WaitCursor);
224
currentPath = currentPath.left(currentPath.lastIndexOf('/'));
225
if (currentPath.isEmpty()) {
226
cdToParentButton->setEnabled(false);
229
ftp->cd(currentPath);
234
void FtpWindow::updateDataTransferProgress(qint64 readBytes,
237
progressDialog->setMaximum(totalBytes);
238
progressDialog->setValue(readBytes);
241
void FtpWindow::enableConnectButton()
243
connectButton->setEnabled(!ftpServerLineEdit->text().isEmpty());
246
void FtpWindow::enableDownloadButton()
248
QListWidgetItem *current = fileList->currentItem();
250
QString currentFile = current->text();
251
downloadButton->setEnabled(!isDirectory.value(currentFile));
253
downloadButton->setEnabled(false);