<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Qt 4.2: ftpwindow.cpp Example File (network/ftp/ftpwindow.cpp)</title> <link href="classic.css" rel="stylesheet" type="text/css" /> </head> <body> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tr> <td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td> <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="groups.html"><font color="#004faf">Grouped Classes</font></a> · <a href="modules.html"><font color="#004faf">Modules</font></a> · <a href="functions.html"><font color="#004faf">Functions</font></a></td> <td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">ftpwindow.cpp Example File<br /><sup><sup>network/ftp/ftpwindow.cpp</sup></sup></h1> <pre> /**************************************************************************** ** ** Copyright (C) 2004-2006 Trolltech ASA. All rights reserved. ** ** This file is part of the example classes of the Qt Toolkit. ** ** This file may be used under the terms of the GNU General Public ** License version 2.0 as published by the Free Software Foundation ** and appearing in the file LICENSE.GPL included in the packaging of ** this file. Please review the following information to ensure GNU ** General Public Licensing requirements will be met: ** http:<span class="comment">//www.trolltech.com/products/qt/opensource.html</span> ** ** If you are unsure which license is appropriate for your use, please ** review the following information: ** http:<span class="comment">//www.trolltech.com/products/qt/licensing.html or contact the</span> ** sales department at sales@trolltech.com. ** ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** ****************************************************************************/ #include <QtGui> #include <QtNetwork> #include "ftpwindow.h" FtpWindow::FtpWindow(QWidget *parent) : QDialog(parent), ftp(0) { ftpServerLabel = new QLabel(tr("Ftp &server:")); ftpServerLineEdit = new QLineEdit("ftp.trolltech.com"); ftpServerLabel->setBuddy(ftpServerLineEdit); statusLabel = new QLabel(tr("Please enter the name of an FTP server.")); fileList = new QListWidget; fileList->setEnabled(false); connectButton = new QPushButton(tr("Connect")); connectButton->setDefault(true); cdToParentButton = new QPushButton; cdToParentButton->setIcon(QPixmap(":/images/cdtoparent.png")); cdToParentButton->setEnabled(false); downloadButton = new QPushButton(tr("Download")); downloadButton->setEnabled(false); quitButton = new QPushButton(tr("Quit")); buttonBox = new QDialogButtonBox; buttonBox->addButton(downloadButton, QDialogButtonBox::ActionRole); buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole); progressDialog = new QProgressDialog(this); connect(fileList, SIGNAL(itemActivated(QListWidgetItem *)), this, SLOT(processItem(QListWidgetItem *))); connect(fileList, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)), this, SLOT(enableDownloadButton())); connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload())); connect(connectButton, SIGNAL(clicked()), this, SLOT(connectOrDisconnect())); connect(cdToParentButton, SIGNAL(clicked()), this, SLOT(cdToParent())); connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile())); connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); QHBoxLayout *topLayout = new QHBoxLayout; topLayout->addWidget(ftpServerLabel); topLayout->addWidget(ftpServerLineEdit); topLayout->addWidget(cdToParentButton); topLayout->addWidget(connectButton); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addLayout(topLayout); mainLayout->addWidget(fileList); mainLayout->addWidget(statusLabel); mainLayout->addWidget(buttonBox); setLayout(mainLayout); setWindowTitle(tr("FTP")); } void FtpWindow::connectOrDisconnect() { if (ftp) { ftp->abort(); ftp->deleteLater(); ftp = 0; fileList->setEnabled(false); cdToParentButton->setEnabled(false); downloadButton->setEnabled(false); connectButton->setEnabled(true); connectButton->setText(tr("Connect")); setCursor(Qt::ArrowCursor); return; } setCursor(Qt::WaitCursor); ftp = new QFtp(this); connect(ftp, SIGNAL(commandFinished(int, bool)), this, SLOT(ftpCommandFinished(int, bool))); connect(ftp, SIGNAL(listInfo(const QUrlInfo &)), this, SLOT(addToList(const QUrlInfo &))); connect(ftp, SIGNAL(dataTransferProgress(qint64, qint64)), this, SLOT(updateDataTransferProgress(qint64, qint64))); fileList->clear(); currentPath.clear(); isDirectory.clear(); ftp->connectToHost(ftpServerLineEdit->text()); ftp->login(); ftp->list(); fileList->setEnabled(true); connectButton->setEnabled(false); connectButton->setText(tr("Disconnect")); statusLabel->setText(tr("Connecting to FTP server %1...") .arg(ftpServerLineEdit->text())); } void FtpWindow::downloadFile() { QString fileName = fileList->currentItem()->text(); if (QFile::exists(fileName)) { QMessageBox::information(this, tr("FTP"), tr("There already exists a file called %1 in " "the current directory.") .arg(fileName)); return; } file = new QFile(fileName); if (!file->open(QIODevice::WriteOnly)) { QMessageBox::information(this, tr("FTP"), tr("Unable to save the file %1: %2.") .arg(fileName).arg(file->errorString())); delete file; return; } ftp->get(fileList->currentItem()->text(), file); progressDialog->setLabelText(tr("Downloading %1...").arg(fileName)); downloadButton->setEnabled(false); progressDialog->exec(); } void FtpWindow::cancelDownload() { ftp->abort(); } void FtpWindow::ftpCommandFinished(int, bool error) { setCursor(Qt::ArrowCursor); if (ftp->currentCommand() == QFtp::ConnectToHost) { if (error) { QMessageBox::information(this, tr("FTP"), tr("Unable to connect to the FTP server " "at %1. Please check that the host " "name is correct.") .arg(ftpServerLineEdit->text())); connectOrDisconnect(); return; } statusLabel->setText(tr("Logged onto %1.") .arg(ftpServerLineEdit->text())); fileList->setFocus(); downloadButton->setDefault(true); connectButton->setEnabled(true); return; } if (ftp->currentCommand() == QFtp::Get) { if (error) { statusLabel->setText(tr("Canceled download of %1.") .arg(file->fileName())); file->close(); file->remove(); } else { statusLabel->setText(tr("Downloaded %1 to current directory.") .arg(file->fileName())); file->close(); } delete file; enableDownloadButton(); } else if (ftp->currentCommand() == QFtp::List) { if (isDirectory.isEmpty()) { fileList->addItem(tr("<empty>")); fileList->setEnabled(false); } } } void FtpWindow::addToList(const QUrlInfo &urlInfo) { QListWidgetItem *item = new QListWidgetItem; item->setText(urlInfo.name()); QPixmap pixmap(urlInfo.isDir() ? ":/images/dir.png" : ":/images/file.png"); item->setIcon(pixmap); isDirectory[urlInfo.name()] = urlInfo.isDir(); fileList->addItem(item); if (!fileList->currentItem()) { fileList->setCurrentItem(fileList->item(0)); fileList->setEnabled(true); } } void FtpWindow::processItem(QListWidgetItem *item) { QString name = item->text(); if (isDirectory.value(name)) { fileList->clear(); isDirectory.clear(); currentPath += "/" + name; ftp->cd(name); ftp->list(); cdToParentButton->setEnabled(true); setCursor(Qt::WaitCursor); return; } } void FtpWindow::cdToParent() { setCursor(Qt::WaitCursor); fileList->clear(); isDirectory.clear(); currentPath = currentPath.left(currentPath.lastIndexOf('/')); if (currentPath.isEmpty()) { cdToParentButton->setEnabled(false); ftp->cd("/"); } else { ftp->cd(currentPath); } ftp->list(); } void FtpWindow::updateDataTransferProgress(qint64 readBytes, qint64 totalBytes) { progressDialog->setMaximum(totalBytes); progressDialog->setValue(readBytes); } void FtpWindow::enableDownloadButton() { QListWidgetItem *current = fileList->currentItem(); if (current) { QString currentFile = current->text(); downloadButton->setEnabled(!isDirectory.value(currentFile)); } else { downloadButton->setEnabled(false); } }</pre> <p /><address><hr /><div align="center"> <table width="100%" cellspacing="0" border="0"><tr class="address"> <td width="30%">Copyright © 2006 <a href="trolltech.html">Trolltech</a></td> <td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td> <td width="30%" align="right"><div align="right">Qt 4.2.1</div></td> </tr></table></div></address></body> </html>