Sophie

Sophie

distrib > CentOS > 5 > x86_64 > by-pkgid > ac91357d6caede925de099a02fced14e > files > 3212

qt4-doc-4.2.1-1.el5_7.1.x86_64.rpm

<?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">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <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 &lt;QtGui&gt;
 #include &lt;QtNetwork&gt;

 #include &quot;ftpwindow.h&quot;

 FtpWindow::FtpWindow(QWidget *parent)
     : QDialog(parent), ftp(0)
 {
     ftpServerLabel = new QLabel(tr(&quot;Ftp &amp;server:&quot;));
     ftpServerLineEdit = new QLineEdit(&quot;ftp.trolltech.com&quot;);
     ftpServerLabel-&gt;setBuddy(ftpServerLineEdit);

     statusLabel = new QLabel(tr(&quot;Please enter the name of an FTP server.&quot;));

     fileList = new QListWidget;
     fileList-&gt;setEnabled(false);

     connectButton = new QPushButton(tr(&quot;Connect&quot;));
     connectButton-&gt;setDefault(true);

     cdToParentButton = new QPushButton;
     cdToParentButton-&gt;setIcon(QPixmap(&quot;:/images/cdtoparent.png&quot;));
     cdToParentButton-&gt;setEnabled(false);

     downloadButton = new QPushButton(tr(&quot;Download&quot;));
     downloadButton-&gt;setEnabled(false);

     quitButton = new QPushButton(tr(&quot;Quit&quot;));

     buttonBox = new QDialogButtonBox;
     buttonBox-&gt;addButton(downloadButton, QDialogButtonBox::ActionRole);
     buttonBox-&gt;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-&gt;addWidget(ftpServerLabel);
     topLayout-&gt;addWidget(ftpServerLineEdit);
     topLayout-&gt;addWidget(cdToParentButton);
     topLayout-&gt;addWidget(connectButton);

     QVBoxLayout *mainLayout = new QVBoxLayout;
     mainLayout-&gt;addLayout(topLayout);
     mainLayout-&gt;addWidget(fileList);
     mainLayout-&gt;addWidget(statusLabel);
     mainLayout-&gt;addWidget(buttonBox);
     setLayout(mainLayout);

     setWindowTitle(tr(&quot;FTP&quot;));
 }

 void FtpWindow::connectOrDisconnect()
 {
     if (ftp) {
         ftp-&gt;abort();
         ftp-&gt;deleteLater();
         ftp = 0;
         fileList-&gt;setEnabled(false);
         cdToParentButton-&gt;setEnabled(false);
         downloadButton-&gt;setEnabled(false);
         connectButton-&gt;setEnabled(true);
         connectButton-&gt;setText(tr(&quot;Connect&quot;));
         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 &amp;)),
             this, SLOT(addToList(const QUrlInfo &amp;)));
     connect(ftp, SIGNAL(dataTransferProgress(qint64, qint64)),
             this, SLOT(updateDataTransferProgress(qint64, qint64)));

     fileList-&gt;clear();
     currentPath.clear();
     isDirectory.clear();

     ftp-&gt;connectToHost(ftpServerLineEdit-&gt;text());
     ftp-&gt;login();
     ftp-&gt;list();

     fileList-&gt;setEnabled(true);
     connectButton-&gt;setEnabled(false);
     connectButton-&gt;setText(tr(&quot;Disconnect&quot;));
     statusLabel-&gt;setText(tr(&quot;Connecting to FTP server %1...&quot;)
                          .arg(ftpServerLineEdit-&gt;text()));
 }

 void FtpWindow::downloadFile()
 {
     QString fileName = fileList-&gt;currentItem()-&gt;text();

     if (QFile::exists(fileName)) {
         QMessageBox::information(this, tr(&quot;FTP&quot;),
                                  tr(&quot;There already exists a file called %1 in &quot;
                                     &quot;the current directory.&quot;)
                                  .arg(fileName));
         return;
     }

     file = new QFile(fileName);
     if (!file-&gt;open(QIODevice::WriteOnly)) {
         QMessageBox::information(this, tr(&quot;FTP&quot;),
                                  tr(&quot;Unable to save the file %1: %2.&quot;)
                                  .arg(fileName).arg(file-&gt;errorString()));
         delete file;
         return;
     }

     ftp-&gt;get(fileList-&gt;currentItem()-&gt;text(), file);

     progressDialog-&gt;setLabelText(tr(&quot;Downloading %1...&quot;).arg(fileName));
     downloadButton-&gt;setEnabled(false);
     progressDialog-&gt;exec();
 }

 void FtpWindow::cancelDownload()
 {
     ftp-&gt;abort();
 }

 void FtpWindow::ftpCommandFinished(int, bool error)
 {
     setCursor(Qt::ArrowCursor);

     if (ftp-&gt;currentCommand() == QFtp::ConnectToHost) {
         if (error) {
             QMessageBox::information(this, tr(&quot;FTP&quot;),
                                      tr(&quot;Unable to connect to the FTP server &quot;
                                         &quot;at %1. Please check that the host &quot;
                                         &quot;name is correct.&quot;)
                                      .arg(ftpServerLineEdit-&gt;text()));
             connectOrDisconnect();
             return;
         }

         statusLabel-&gt;setText(tr(&quot;Logged onto %1.&quot;)
                              .arg(ftpServerLineEdit-&gt;text()));
         fileList-&gt;setFocus();
         downloadButton-&gt;setDefault(true);
         connectButton-&gt;setEnabled(true);
         return;
     }

     if (ftp-&gt;currentCommand() == QFtp::Get) {
         if (error) {
             statusLabel-&gt;setText(tr(&quot;Canceled download of %1.&quot;)
                                  .arg(file-&gt;fileName()));
             file-&gt;close();
             file-&gt;remove();
         } else {
             statusLabel-&gt;setText(tr(&quot;Downloaded %1 to current directory.&quot;)
                                  .arg(file-&gt;fileName()));
             file-&gt;close();
         }
         delete file;
         enableDownloadButton();
     } else if (ftp-&gt;currentCommand() == QFtp::List) {
         if (isDirectory.isEmpty()) {
             fileList-&gt;addItem(tr(&quot;&lt;empty&gt;&quot;));
             fileList-&gt;setEnabled(false);
         }
     }
 }

 void FtpWindow::addToList(const QUrlInfo &amp;urlInfo)
 {
     QListWidgetItem *item = new QListWidgetItem;
     item-&gt;setText(urlInfo.name());
     QPixmap pixmap(urlInfo.isDir() ? &quot;:/images/dir.png&quot; : &quot;:/images/file.png&quot;);
     item-&gt;setIcon(pixmap);

     isDirectory[urlInfo.name()] = urlInfo.isDir();
     fileList-&gt;addItem(item);
     if (!fileList-&gt;currentItem()) {
         fileList-&gt;setCurrentItem(fileList-&gt;item(0));
         fileList-&gt;setEnabled(true);
     }
 }

 void FtpWindow::processItem(QListWidgetItem *item)
 {
     QString name = item-&gt;text();
     if (isDirectory.value(name)) {
         fileList-&gt;clear();
         isDirectory.clear();
         currentPath += &quot;/&quot; + name;
         ftp-&gt;cd(name);
         ftp-&gt;list();
         cdToParentButton-&gt;setEnabled(true);
         setCursor(Qt::WaitCursor);
         return;
     }
 }

 void FtpWindow::cdToParent()
 {
     setCursor(Qt::WaitCursor);
     fileList-&gt;clear();
     isDirectory.clear();
     currentPath = currentPath.left(currentPath.lastIndexOf('/'));
     if (currentPath.isEmpty()) {
         cdToParentButton-&gt;setEnabled(false);
         ftp-&gt;cd(&quot;/&quot;);
     } else {
         ftp-&gt;cd(currentPath);
     }
     ftp-&gt;list();
 }

 void FtpWindow::updateDataTransferProgress(qint64 readBytes,
                                            qint64 totalBytes)
 {
     progressDialog-&gt;setMaximum(totalBytes);
     progressDialog-&gt;setValue(readBytes);
 }

 void FtpWindow::enableDownloadButton()
 {
     QListWidgetItem *current = fileList-&gt;currentItem();
     if (current) {
         QString currentFile = current-&gt;text();
         downloadButton-&gt;setEnabled(!isDirectory.value(currentFile));
     } else {
         downloadButton-&gt;setEnabled(false);
     }
 }</pre>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 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>