source: trunk/doc/html/ftpclient-example.html

Last change on this file was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 15.4 KB
RevLine 
[190]1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/network/ftpclient/ftpclient.doc:5 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>An FTP client</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { background: #ffffff; color: black; }
12--></style>
13</head>
14<body>
15
16<table border="0" cellpadding="0" cellspacing="0" width="100%">
17<tr bgcolor="#E5E5E5">
18<td valign=center>
19 <a href="index.html">
20<font color="#004faf">Home</font></a>
21 | <a href="classes.html">
22<font color="#004faf">All&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;Classes</font></a>
25 | <a href="annotated.html">
26<font color="#004faf">Annotated</font></a>
27 | <a href="groups.html">
28<font color="#004faf">Grouped&nbsp;Classes</font></a>
29 | <a href="functions.html">
30<font color="#004faf">Functions</font></a>
31</td>
32<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>An FTP client</h1>
33
34
35<p>
36<p> This example implements a FTP client. It uses <a href="qftp.html">QFtp</a> to perform its FTP
37commands. The GUI parts are done in the Designer.
38<p> <hr>
39<p> The implementation of the FTP commands (ftpmainwindow.ui.h):
40<p> <pre>/****************************************************************************
41** $Id: ftpclient-example.html 2051 2007-02-21 10:04:20Z chehrlic $
42**
43** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
44**
45** This file is part of an example program for Qt. This example
46** program may be used, distributed and modified without limitation.
47**
48*****************************************************************************/
49
50/****************************************************************************
51**
52** ui.h extension file, included from the uic-generated form implementation.
53**
54** The init() function is used in place of a constructor.
55** The destroy() function is used in place of a destructor.
56** The slots uploadFile(), downloadFile(), removeFile() and connectToHost() are
57** connected with the resp. actions of the GUI.
58**
59*****************************************************************************/
60
61#include &lt;<a href="qftp-h.html">qftp.h</a>&gt;
62#include &lt;<a href="qlineedit-h.html">qlineedit.h</a>&gt;
63#include &lt;<a href="qspinbox-h.html">qspinbox.h</a>&gt;
64#include &lt;<a href="qstatusbar-h.html">qstatusbar.h</a>&gt;
65#include &lt;<a href="qmessagebox-h.html">qmessagebox.h</a>&gt;
66#include &lt;<a href="qfiledialog-h.html">qfiledialog.h</a>&gt;
67#include &lt;<a href="qprogressdialog-h.html">qprogressdialog.h</a>&gt;
68#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
69
70#include "connectdialog.h"
71#include "ftpviewitem.h"
72
73void FtpMainWindow::init()
74{
75 stateFtp = new <a href="qlabel.html">QLabel</a>( tr("Unconnected"), statusBar() );
76 statusBar()-&gt;addWidget( stateFtp, 0, TRUE );
77
78 ftp = new <a href="qftp.html">QFtp</a>( this );
79<a name="x748"></a> connect( ftp, SIGNAL(<a href="qftp.html#commandStarted">commandStarted</a>(int)),
80 SLOT(ftp_commandStarted()) );
81<a name="x747"></a> connect( ftp, SIGNAL(<a href="qftp.html#commandFinished">commandFinished</a>(int,bool)),
82 SLOT(ftp_commandFinished()) );
83<a name="x753"></a> connect( ftp, SIGNAL(<a href="qftp.html#done">done</a>(bool)),
84 SLOT(ftp_done(bool)) );
85<a name="x764"></a> connect( ftp, SIGNAL(<a href="qftp.html#stateChanged">stateChanged</a>(int)),
86 SLOT(ftp_stateChanged(int)) );
87<a name="x757"></a> connect( ftp, SIGNAL(<a href="qftp.html#listInfo">listInfo</a>(const <a href="qurlinfo.html">QUrlInfo</a> &amp;)),
88 SLOT(ftp_listInfo(const <a href="qurlinfo.html">QUrlInfo</a> &amp;)) );
89<a name="x761"></a> connect( ftp, SIGNAL(<a href="qftp.html#rawCommandReply">rawCommandReply</a>(int, const <a href="qstring.html">QString</a> &amp;)),
90 SLOT(ftp_rawCommandReply(int, const <a href="qstring.html">QString</a> &amp;)) );
91}
92
93void FtpMainWindow::destroy()
94{
95<a name="x763"></a> if ( ftp-&gt;<a href="qftp.html#state">state</a>() != QFtp::Unconnected )
96<a name="x746"></a> ftp-&gt;<a href="qftp.html#close">close</a>();
97}
98
99void FtpMainWindow::uploadFile()
100{
101<a name="x741"></a> <a href="qstring.html">QString</a> fileName = QFileDialog::<a href="qfiledialog.html#getOpenFileName">getOpenFileName</a>(
102 <a href="qstring.html#QString-null">QString::null</a>,
103 QString::null,
104 this,
105 "upload file dialog",
106 tr("Choose a file to upload") );
107<a name="x771"></a> if ( fileName.<a href="qstring.html#isNull">isNull</a>() )
108 return;
109
110 <a href="qfile.html">QFile</a> *file = new <a href="qfile.html">QFile</a>( fileName );
111 if ( !file-&gt;<a href="qfile.html#open">open</a>( <a href="qfile.html#open">IO_ReadOnly</a> ) ) {
112<a name="x767"></a> QMessageBox::<a href="qmessagebox.html#critical">critical</a>( this, tr("Upload error"),
113 tr("Can't open file '%1' for reading.").arg(fileName) );
114 delete file;
115 return;
116 }
117
118 <a href="qprogressdialog.html">QProgressDialog</a> progress(
119 tr("Uploading file..."),
120 tr("Cancel"),
121 0,
122 this,
123 "upload progress dialog",
124 TRUE );
125<a name="x752"></a> connect( ftp, SIGNAL(<a href="qftp.html#dataTransferProgress">dataTransferProgress</a>(int,int)),
126<a name="x770"></a> &amp;progress, SLOT(<a href="qprogressdialog.html#setProgress">setProgress</a>(int,int)) );
127 connect( ftp, SIGNAL(<a href="qftp.html#commandFinished">commandFinished</a>(int,bool)),
128<a name="x769"></a> &amp;progress, SLOT(<a href="qprogressdialog.html#reset">reset</a>()) );
129<a name="x768"></a> connect( &amp;progress, SIGNAL(<a href="qprogressdialog.html#cancelled">cancelled</a>()),
130<a name="x744"></a> ftp, SLOT(<a href="qftp.html#abort">abort</a>()) );
131
132 <a href="qfileinfo.html">QFileInfo</a> fi( fileName );
133<a name="x759"></a><a name="x743"></a> ftp-&gt;<a href="qftp.html#put">put</a>( file, fi.<a href="qfileinfo.html#fileName">fileName</a>() );
134<a name="x739"></a> progress.<a href="qdialog.html#exec">exec</a>(); // ### takes a lot of time!!!
135
136<a name="x756"></a> ftp-&gt;<a href="qftp.html#list">list</a>();
137}
138
139void FtpMainWindow::downloadFile()
140{
141 FtpViewItem *item = (FtpViewItem*)remoteView-&gt;selectedItem();
142 if ( !item || item-&gt;isDir() )
143 return;
144
145<a name="x742"></a> <a href="qstring.html">QString</a> fileName = QFileDialog::<a href="qfiledialog.html#getSaveFileName">getSaveFileName</a>(
146<a name="x766"></a> item-&gt;<a href="qlistviewitem.html#text">text</a>(0),
147 QString::null,
148 this,
149 "download file dialog",
150 tr("Save downloaded file as") );
151 if ( fileName.<a href="qstring.html#isNull">isNull</a>() )
152 return;
153
154 // create file on the heap because it has to be valid throughout the whole
155 // asynchronous download operation
156 <a href="qfile.html">QFile</a> *file = new <a href="qfile.html">QFile</a>( fileName );
157 if ( !file-&gt;<a href="qfile.html#open">open</a>( <a href="qfile.html#open">IO_WriteOnly</a> ) ) {
158 QMessageBox::<a href="qmessagebox.html#critical">critical</a>( this, tr("Download error"),
159 tr("Can't open file '%1' for writing.").arg(fileName) );
160 delete file;
161 return;
162 }
163
164 <a href="qprogressdialog.html">QProgressDialog</a> progress(
165 tr("Downloading file..."),
166 tr("Cancel"),
167 0,
168 this,
169 "download progress dialog",
170 TRUE );
171 connect( ftp, SIGNAL(<a href="qftp.html#dataTransferProgress">dataTransferProgress</a>(int,int)),
172 &amp;progress, SLOT(<a href="qprogressdialog.html#setProgress">setProgress</a>(int,int)) );
173 connect( ftp, SIGNAL(<a href="qftp.html#commandFinished">commandFinished</a>(int,bool)),
174 &amp;progress, SLOT(<a href="qprogressdialog.html#reset">reset</a>()) );
175 connect( &amp;progress, SIGNAL(<a href="qprogressdialog.html#cancelled">cancelled</a>()),
176 ftp, SLOT(<a href="qftp.html#abort">abort</a>()) );
177
178<a name="x755"></a> ftp-&gt;<a href="qftp.html#get">get</a>( item-&gt;<a href="qlistviewitem.html#text">text</a>(0), file );
179 progress.<a href="qdialog.html#exec">exec</a>(); // ### takes a lot of time!!!
180}
181
182void FtpMainWindow::removeFile()
183{
184 FtpViewItem *item = (FtpViewItem*)remoteView-&gt;selectedItem();
185 if ( !item || item-&gt;isDir() )
186 return;
187
188<a name="x762"></a> ftp-&gt;<a href="qftp.html#remove">remove</a>( item-&gt;<a href="qlistviewitem.html#text">text</a>(0) );
189 ftp-&gt;<a href="qftp.html#list">list</a>();
190}
191
192void FtpMainWindow::connectToHost()
193{
194 ConnectDialog connectDialog;
195 if ( connectDialog.exec() == QDialog::Rejected )
196 return;
197
198 remotePath-&gt;clear();
199 remoteView-&gt;clear();
200
201 if ( ftp-&gt;<a href="qftp.html#state">state</a>() != QFtp::Unconnected )
202 ftp-&gt;<a href="qftp.html#close">close</a>();
203
204<a name="x749"></a> ftp-&gt;<a href="qftp.html#connectToHost">connectToHost</a>( connectDialog.host-&gt;text(), connectDialog.port-&gt;value() );
205<a name="x758"></a> ftp-&gt;<a href="qftp.html#login">login</a>( connectDialog.username-&gt;text(), connectDialog.password-&gt;text() );
206<a name="x760"></a> ftp-&gt;<a href="qftp.html#rawCommand">rawCommand</a>( "PWD" );
207 ftp-&gt;<a href="qftp.html#list">list</a>();
208}
209
210// This slot is connected to the QComboBox::activated() signal of the
211// remotePath.
212void FtpMainWindow::changePath( const <a href="qstring.html">QString</a> &amp;newPath )
213{
214<a name="x745"></a> ftp-&gt;<a href="qftp.html#cd">cd</a>( newPath );
215 ftp-&gt;<a href="qftp.html#rawCommand">rawCommand</a>( "PWD" );
216 ftp-&gt;<a href="qftp.html#list">list</a>();
217}
218
219// This slot is connected to the QListView::doubleClicked() and
220// QListView::returnPressed() signals of the remoteView.
221void FtpMainWindow::changePathOrDownload( <a href="qlistviewitem.html">QListViewItem</a> *item )
222{
223 if ( ((FtpViewItem*)item)-&gt;isDir() )
224 changePath( item-&gt;<a href="qlistviewitem.html#text">text</a>(0) );
225 else
226 downloadFile();
227}
228
229/****************************************************************************
230**
231** Slots connected to signals of the QFtp class
232**
233*****************************************************************************/
234
235void FtpMainWindow::ftp_commandStarted()
236{
237<a name="x738"></a> QApplication::<a href="qapplication.html#setOverrideCursor">setOverrideCursor</a>( QCursor(Qt::WaitCursor) );
238<a name="x750"></a> if ( ftp-&gt;<a href="qftp.html#currentCommand">currentCommand</a>() == QFtp::List ) {
239 remoteView-&gt;clear();
240 if ( currentFtpDir != "/" )
241 new FtpViewItem( remoteView, FtpViewItem::Directory, "..", "", "" );
242 }
243}
244
245void FtpMainWindow::ftp_commandFinished()
246{
247<a name="x737"></a> QApplication::<a href="qapplication.html#restoreOverrideCursor">restoreOverrideCursor</a>();
248<a name="x751"></a> delete ftp-&gt;<a href="qftp.html#currentDevice">currentDevice</a>();
249}
250
251void FtpMainWindow::ftp_done( bool error )
252{
253 if ( error ) {
254<a name="x754"></a> QMessageBox::<a href="qmessagebox.html#critical">critical</a>( this, tr("FTP Error"), ftp-&gt;<a href="qftp.html#errorString">errorString</a>() );
255
256 // If we are connected, but not logged in, it is not meaningful to stay
257 // connected to the server since the error is a really fatal one (login
258 // failed).
259 if ( ftp-&gt;<a href="qftp.html#state">state</a>() == QFtp::Connected )
260 ftp-&gt;<a href="qftp.html#close">close</a>();
261 }
262}
263
264void FtpMainWindow::ftp_stateChanged( int state )
265{
266 switch ( (QFtp::State)state ) {
267 case QFtp::Unconnected:
268<a name="x765"></a> stateFtp-&gt;<a href="qlabel.html#setText">setText</a>( tr("Unconnected") );
269 break;
270 case QFtp::HostLookup:
271 stateFtp-&gt;<a href="qlabel.html#setText">setText</a>( tr("Host lookup") );
272 break;
273 case QFtp::Connecting:
274 stateFtp-&gt;<a href="qlabel.html#setText">setText</a>( tr("Connecting") );
275 break;
276 case QFtp::Connected:
277 stateFtp-&gt;<a href="qlabel.html#setText">setText</a>( tr("Connected") );
278 break;
279 case QFtp::LoggedIn:
280 stateFtp-&gt;<a href="qlabel.html#setText">setText</a>( tr("Logged in") );
281 break;
282 case QFtp::Closing:
283 stateFtp-&gt;<a href="qlabel.html#setText">setText</a>( tr("Closing") );
284 break;
285 }
286}
287
288void FtpMainWindow::ftp_listInfo( const <a href="qurlinfo.html">QUrlInfo</a> &amp;i )
289{
290 FtpViewItem::Type type;
291<a name="x773"></a> if ( i.<a href="qurlinfo.html#isDir">isDir</a>() )
292 type = FtpViewItem::Directory;
293 else
294 type = FtpViewItem::File;
295
296 new FtpViewItem( remoteView, type,
297<a name="x776"></a><a name="x775"></a><a name="x774"></a> i.<a href="qurlinfo.html#name">name</a>(), QString::number(i.<a href="qurlinfo.html#size">size</a>()), i.<a href="qurlinfo.html#lastModified">lastModified</a>().toString() );
298}
299
300void FtpMainWindow::ftp_rawCommandReply( int code, const <a href="qstring.html">QString</a> &amp;text )
301{
302 if ( code == 257 ) {
303<a name="x772"></a> currentFtpDir = text.<a href="qstring.html#section">section</a>( '"', 1, 1 );
304
305 for ( int i = 0; i&lt;remotePath-&gt;count(); i++ ) {
306 // make sure that we don't insert duplicates
307 if ( remotePath-&gt;text( i ) == currentFtpDir )
308 remotePath-&gt;removeItem( i );
309 }
310 remotePath-&gt;insertItem( currentFtpDir, 0 );
311 remotePath-&gt;setCurrentItem( 0 );
312 }
313}
314</pre>
315
316<p> <hr>
317<p> Main (main.cpp):
318<p> <pre>/****************************************************************************
319** $Id: ftpclient-example.html 2051 2007-02-21 10:04:20Z chehrlic $
320**
321** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
322**
323** This file is part of an example program for Qt. This example
324** program may be used, distributed and modified without limitation.
325**
326*****************************************************************************/
327
328#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
329#include "ftpmainwindow.h"
330
331int main( int argc, char **argv )
332{
333 <a href="qapplication.html">QApplication</a> a( argc, argv );
334
335 FtpMainWindow m;
336<a name="x779"></a> a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;m );
337 m.show();
338<a name="x778"></a> a.<a href="qapplication.html#processEvents">processEvents</a>();
339 m.connectToHost();
340<a name="x777"></a> return a.<a href="qapplication.html#exec">exec</a>();
341}
342</pre>
343
344<p>See also <a href="network-examples.html">Network Examples</a>.
345
346<!-- eof -->
347<p><address><hr><div align=center>
348<table width=100% cellspacing=0 border=0><tr>
349<td>Copyright &copy; 2007
350<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
351<td align=right><div align=right>Qt 3.3.8</div>
352</table></div></address></body>
353</html>
Note: See TracBrowser for help on using the repository browser.