~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/network-ftp-ftpwindow-cpp.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
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">
 
5
<head>
 
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>
 
13
</head>
 
14
<body>
 
15
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
16
<tr>
 
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">&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="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <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>&nbsp;   /****************************************************************************
 
21
    **
 
22
    ** Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
 
23
    **
 
24
    ** This file is part of the documentation of the Qt Toolkit.
 
25
    **
 
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.
 
29
**
 
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.
 
34
**
 
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.
 
39
**
 
40
** Contact info@trolltech.com if any conditions of this licensing are
 
41
** not clear to you.
 
42
    **
 
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.
 
45
    **
 
46
    ****************************************************************************/
 
47
 
 
48
    #include &lt;QtGui&gt;
 
49
    #include &lt;QtNetwork&gt;
 
50
 
 
51
    #include &quot;ftpwindow.h&quot;
 
52
 
 
53
    FtpWindow::FtpWindow(QWidget *parent)
 
54
        : QDialog(parent)
 
55
    {
 
56
        ftpServerLabel = new QLabel(tr(&quot;Ftp &amp;server:&quot;));
 
57
        ftpServerLineEdit = new QLineEdit(&quot;ftp.trolltech.com&quot;);
 
58
        ftpServerLabel-&gt;setBuddy(ftpServerLineEdit);
 
59
 
 
60
        statusLabel = new QLabel(tr(&quot;Please enter the name of an FTP server.&quot;));
 
61
 
 
62
        fileList = new QListWidget;
 
63
 
 
64
        connectButton = new QPushButton(tr(&quot;Connect&quot;));
 
65
        connectButton-&gt;setDefault(true);
 
66
 
 
67
        downloadButton = new QPushButton(tr(&quot;Download&quot;));
 
68
        downloadButton-&gt;setEnabled(false);
 
69
 
 
70
        cdToParentButton = new QPushButton;
 
71
        cdToParentButton-&gt;setIcon(QPixmap(&quot;:/images/cdtoparent.png&quot;));
 
72
        cdToParentButton-&gt;setEnabled(false);
 
73
 
 
74
        quitButton = new QPushButton(tr(&quot;Quit&quot;));
 
75
 
 
76
        ftp = new QFtp(this);
 
77
 
 
78
        progressDialog = new QProgressDialog(this);
 
79
 
 
80
        connect(ftpServerLineEdit, SIGNAL(textChanged(const QString &amp;)),
 
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 &amp;)),
 
91
                this, SLOT(addToList(const QUrlInfo &amp;)));
 
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()));
 
99
 
 
100
        QHBoxLayout *topLayout = new QHBoxLayout;
 
101
        topLayout-&gt;addWidget(ftpServerLabel);
 
102
        topLayout-&gt;addWidget(ftpServerLineEdit);
 
103
        topLayout-&gt;addWidget(cdToParentButton);
 
104
 
 
105
        QHBoxLayout *buttonLayout = new QHBoxLayout;
 
106
        buttonLayout-&gt;addStretch(1);
 
107
        buttonLayout-&gt;addWidget(downloadButton);
 
108
        buttonLayout-&gt;addWidget(connectButton);
 
109
        buttonLayout-&gt;addWidget(quitButton);
 
110
 
 
111
        QVBoxLayout *mainLayout = new QVBoxLayout;
 
112
        mainLayout-&gt;addLayout(topLayout);
 
113
        mainLayout-&gt;addWidget(fileList);
 
114
        mainLayout-&gt;addWidget(statusLabel);
 
115
        mainLayout-&gt;addLayout(buttonLayout);
 
116
        setLayout(mainLayout);
 
117
 
 
118
        setWindowTitle(tr(&quot;FTP&quot;));
 
119
    }
 
120
 
 
121
    void FtpWindow::connectToFtpServer()
 
122
    {
 
123
        QApplication::setOverrideCursor(Qt::WaitCursor);
 
124
        ftp-&gt;connectToHost(ftpServerLineEdit-&gt;text());
 
125
        ftp-&gt;login();
 
126
        ftp-&gt;list();
 
127
        statusLabel-&gt;setText(tr(&quot;Connecting to FTP server %1...&quot;)
 
128
                             .arg(ftpServerLineEdit-&gt;text()));
 
129
    }
 
130
 
 
131
    void FtpWindow::downloadFile()
 
132
    {
 
133
        QString fileName = fileList-&gt;currentItem()-&gt;text();
 
134
 
 
135
        if (QFile::exists(fileName)) {
 
136
            QMessageBox::information(this, tr(&quot;FTP&quot;),
 
137
                                     tr(&quot;There already exists a file called %1 in &quot;
 
138
                                        &quot;the current directory.&quot;)
 
139
                                     .arg(fileName));
 
140
            return;
 
141
        }
 
142
 
 
143
        file = new QFile(fileName);
 
144
        if (!file-&gt;open(QIODevice::WriteOnly)) {
 
145
            QMessageBox::information(this, tr(&quot;FTP&quot;),
 
146
                                     tr(&quot;Unable to save the file %1: %2.&quot;)
 
147
                                     .arg(fileName).arg(file-&gt;errorString()));
 
148
            delete file;
 
149
            return;
 
150
        }
 
151
 
 
152
        ftp-&gt;get(fileList-&gt;currentItem()-&gt;text(), file);
 
153
 
 
154
        progressDialog-&gt;setLabelText(tr(&quot;Downloading %1...&quot;).arg(fileName));
 
155
        progressDialog-&gt;show();
 
156
        downloadButton-&gt;setEnabled(false);
 
157
    }
 
158
 
 
159
    void FtpWindow::cancelDownload()
 
160
    {
 
161
        ftp-&gt;abort();
 
162
    }
 
163
 
 
164
    void FtpWindow::ftpCommandFinished(int, bool error)
 
165
    {
 
166
        if (ftp-&gt;currentCommand() == QFtp::ConnectToHost) {
 
167
            if (error) {
 
168
                QApplication::restoreOverrideCursor();
 
169
                QMessageBox::information(this, tr(&quot;FTP&quot;),
 
170
                                         tr(&quot;Unable to connect to the FTP server &quot;
 
171
                                            &quot;at %1. Please check that the host &quot;
 
172
                                            &quot;name is correct.&quot;)
 
173
                                         .arg(ftpServerLineEdit-&gt;text()));
 
174
                return;
 
175
            }
 
176
 
 
177
            statusLabel-&gt;setText(tr(&quot;Logged onto %1.&quot;)
 
178
                                 .arg(ftpServerLineEdit-&gt;text()));
 
179
            fileList-&gt;setFocus();
 
180
            connectButton-&gt;setEnabled(false);
 
181
            downloadButton-&gt;setDefault(true);
 
182
            return;
 
183
        }
 
184
 
 
185
        if (ftp-&gt;currentCommand() == QFtp::Get) {
 
186
            QApplication::restoreOverrideCursor();
 
187
            if (error) {
 
188
                statusLabel-&gt;setText(tr(&quot;Canceled download of %1.&quot;)
 
189
                                     .arg(file-&gt;fileName()));
 
190
                file-&gt;close();
 
191
                file-&gt;remove();
 
192
            } else {
 
193
                statusLabel-&gt;setText(tr(&quot;Downloaded %1 to current directory.&quot;)
 
194
                                     .arg(file-&gt;fileName()));
 
195
                file-&gt;close();
 
196
            }
 
197
            delete file;
 
198
            enableDownloadButton();
 
199
        } else if (ftp-&gt;currentCommand() == QFtp::List) {
 
200
            QApplication::restoreOverrideCursor();
 
201
            if (isDirectory.isEmpty()) {
 
202
                fileList-&gt;addItem(tr(&quot;&lt;empty&gt;&quot;));
 
203
                fileList-&gt;setEnabled(false);
 
204
            }
 
205
        }
 
206
    }
 
207
 
 
208
    void FtpWindow::addToList(const QUrlInfo &amp;urlInfo)
 
209
    {
 
210
        QListWidgetItem *item = new QListWidgetItem;
 
211
        item-&gt;setText(urlInfo.name());
 
212
        QPixmap pixmap(urlInfo.isDir() ? &quot;:/images/dir.png&quot; : &quot;:/images/file.png&quot;);
 
213
        item-&gt;setIcon(pixmap);
 
214
 
 
215
        isDirectory[urlInfo.name()] = urlInfo.isDir();
 
216
        fileList-&gt;addItem(item);
 
217
        if (!fileList-&gt;currentItem()) {
 
218
            fileList-&gt;setCurrentItem(fileList-&gt;item(0));
 
219
            fileList-&gt;setEnabled(true);
 
220
        }
 
221
    }
 
222
 
 
223
    void FtpWindow::processItem(QListWidgetItem *item)
 
224
    {
 
225
        QString name = item-&gt;text();
 
226
        if (isDirectory.value(name)) {
 
227
            fileList-&gt;clear();
 
228
            isDirectory.clear();
 
229
            currentPath += &quot;/&quot; + name;
 
230
            ftp-&gt;cd(name);
 
231
            ftp-&gt;list();
 
232
            cdToParentButton-&gt;setEnabled(true);
 
233
            QApplication::setOverrideCursor(Qt::WaitCursor);
 
234
            return;
 
235
        }
 
236
    }
 
237
 
 
238
    void FtpWindow::cdToParent()
 
239
    {
 
240
        QApplication::setOverrideCursor(Qt::WaitCursor);
 
241
        fileList-&gt;clear();
 
242
        isDirectory.clear();
 
243
        currentPath = currentPath.left(currentPath.lastIndexOf('/'));
 
244
        if (currentPath.isEmpty()) {
 
245
            cdToParentButton-&gt;setEnabled(false);
 
246
            ftp-&gt;cd(&quot;/&quot;);
 
247
        } else {
 
248
            ftp-&gt;cd(currentPath);
 
249
        }
 
250
        ftp-&gt;list();
 
251
    }
 
252
 
 
253
    void FtpWindow::updateDataTransferProgress(qint64 readBytes,
 
254
                                               qint64 totalBytes)
 
255
    {
 
256
        progressDialog-&gt;setMaximum(totalBytes);
 
257
        progressDialog-&gt;setValue(readBytes);
 
258
    }
 
259
 
 
260
    void FtpWindow::enableConnectButton()
 
261
    {
 
262
        connectButton-&gt;setEnabled(!ftpServerLineEdit-&gt;text().isEmpty());
 
263
    }
 
264
 
 
265
    void FtpWindow::enableDownloadButton()
 
266
    {
 
267
        QListWidgetItem *current = fileList-&gt;currentItem();
 
268
        if (current) {
 
269
            QString currentFile = current-&gt;text();
 
270
            downloadButton-&gt;setEnabled(!isDirectory.value(currentFile));
 
271
        } else {
 
272
            downloadButton-&gt;setEnabled(false);
 
273
        }
 
274
    }</pre>
 
275
<p /><address><hr /><div align="center">
 
276
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
277
<td width="30%">Copyright &copy; 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>
 
281
</html>