You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tqt3/doc/html/ftpclient-example.html

354 lines
15 KiB

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/network/ftpclient/ftpclient.doc:5 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>An FTP client</title>
<style type="text/css"><!--
fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
<a href="index.html">
<font color="#004faf">Home</font></a>
| <a href="classes.html">
<font color="#004faf">All&nbsp;Classes</font></a>
| <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
| <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
| <a href="groups.html">
<font color="#004faf">Grouped&nbsp;Classes</font></a>
| <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<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>
<p>
<p> This example implements a FTP client. It uses <a href="ntqftp.html">TQFtp</a> to perform its FTP
commands. The GUI parts are done in the Designer.
<p> <hr>
<p> The implementation of the FTP commands (ftpmainwindow.ui.h):
<p> <pre>/****************************************************************************
** $Id: qt/ftpmainwindow.ui.h 3.3.8 edited Jan 11 14:37 $
**
** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
**
** This file is part of an example program for TQt. This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/
/****************************************************************************
**
** ui.h extension file, included from the uic-generated form implementation.
**
** The init() function is used in place of a constructor.
** The destroy() function is used in place of a destructor.
** The slots uploadFile(), downloadFile(), removeFile() and connectToHost() are
** connected with the resp. actions of the GUI.
**
*****************************************************************************/
#include &lt;<a href="qftp-h.html">ntqftp.h</a>&gt;
#include &lt;<a href="qlineedit-h.html">ntqlineedit.h</a>&gt;
#include &lt;<a href="qspinbox-h.html">ntqspinbox.h</a>&gt;
#include &lt;<a href="qstatusbar-h.html">ntqstatusbar.h</a>&gt;
#include &lt;<a href="qmessagebox-h.html">ntqmessagebox.h</a>&gt;
#include &lt;<a href="qfiledialog-h.html">ntqfiledialog.h</a>&gt;
#include &lt;<a href="qprogressdialog-h.html">ntqprogressdialog.h</a>&gt;
#include &lt;<a href="qapplication-h.html">ntqapplication.h</a>&gt;
#include "connectdialog.h"
#include "ftpviewitem.h"
void FtpMainWindow::init()
{
stateFtp = new <a href="ntqlabel.html">TQLabel</a>( tr("Unconnected"), statusBar() );
statusBar()-&gt;addWidget( stateFtp, 0, TRUE );
ftp = new <a href="ntqftp.html">TQFtp</a>( this );
<a name="x748"></a> connect( ftp, TQ_SIGNAL(<a href="ntqftp.html#commandStarted">commandStarted</a>(int)),
TQ_SLOT(ftp_commandStarted()) );
<a name="x747"></a> connect( ftp, TQ_SIGNAL(<a href="ntqftp.html#commandFinished">commandFinished</a>(int,bool)),
TQ_SLOT(ftp_commandFinished()) );
<a name="x753"></a> connect( ftp, TQ_SIGNAL(<a href="ntqftp.html#done">done</a>(bool)),
TQ_SLOT(ftp_done(bool)) );
<a name="x764"></a> connect( ftp, TQ_SIGNAL(<a href="ntqftp.html#stateChanged">stateChanged</a>(int)),
TQ_SLOT(ftp_stateChanged(int)) );
<a name="x757"></a> connect( ftp, TQ_SIGNAL(<a href="ntqftp.html#listInfo">listInfo</a>(const <a href="ntqurlinfo.html">TQUrlInfo</a> &amp;)),
TQ_SLOT(ftp_listInfo(const <a href="ntqurlinfo.html">TQUrlInfo</a> &amp;)) );
<a name="x761"></a> connect( ftp, TQ_SIGNAL(<a href="ntqftp.html#rawCommandReply">rawCommandReply</a>(int, const <a href="ntqstring.html">TQString</a> &amp;)),
TQ_SLOT(ftp_rawCommandReply(int, const <a href="ntqstring.html">TQString</a> &amp;)) );
}
void FtpMainWindow::destroy()
{
<a name="x763"></a> if ( ftp-&gt;<a href="ntqftp.html#state">state</a>() != TQFtp::Unconnected )
<a name="x746"></a> ftp-&gt;<a href="ntqftp.html#close">close</a>();
}
void FtpMainWindow::uploadFile()
{
<a name="x741"></a> <a href="ntqstring.html">TQString</a> fileName = TQFileDialog::<a href="ntqfiledialog.html#getOpenFileName">getOpenFileName</a>(
<a href="ntqstring.html#TQString-null">TQString::null</a>,
TQString::null,
this,
"upload file dialog",
tr("Choose a file to upload") );
<a name="x771"></a> if ( fileName.<a href="ntqstring.html#isNull">isNull</a>() )
return;
<a href="ntqfile.html">TQFile</a> *file = new <a href="ntqfile.html">TQFile</a>( fileName );
if ( !file-&gt;<a href="ntqfile.html#open">open</a>( <a href="ntqfile.html#open">IO_ReadOnly</a> ) ) {
<a name="x767"></a> TQMessageBox::<a href="ntqmessagebox.html#critical">critical</a>( this, tr("Upload error"),
tr("Can't open file '%1' for reading.").arg(fileName) );
delete file;
return;
}
<a href="ntqprogressdialog.html">TQProgressDialog</a> progress(
tr("Uploading file..."),
tr("Cancel"),
0,
this,
"upload progress dialog",
TRUE );
<a name="x752"></a> connect( ftp, TQ_SIGNAL(<a href="ntqftp.html#dataTransferProgress">dataTransferProgress</a>(int,int)),
<a name="x770"></a> &amp;progress, TQ_SLOT(<a href="ntqprogressdialog.html#setProgress">setProgress</a>(int,int)) );
connect( ftp, TQ_SIGNAL(<a href="ntqftp.html#commandFinished">commandFinished</a>(int,bool)),
<a name="x769"></a> &amp;progress, TQ_SLOT(<a href="ntqprogressdialog.html#reset">reset</a>()) );
<a name="x768"></a> connect( &amp;progress, TQ_SIGNAL(<a href="ntqprogressdialog.html#cancelled">cancelled</a>()),
<a name="x744"></a> ftp, TQ_SLOT(<a href="ntqftp.html#abort">abort</a>()) );
<a href="ntqfileinfo.html">TQFileInfo</a> fi( fileName );
<a name="x759"></a><a name="x743"></a> ftp-&gt;<a href="ntqftp.html#put">put</a>( file, fi.<a href="ntqfileinfo.html#fileName">fileName</a>() );
<a name="x739"></a> progress.<a href="ntqdialog.html#exec">exec</a>(); // ### takes a lot of time!!!
<a name="x756"></a> ftp-&gt;<a href="ntqftp.html#list">list</a>();
}
void FtpMainWindow::downloadFile()
{
FtpViewItem *item = (FtpViewItem*)remoteView-&gt;selectedItem();
if ( !item || item-&gt;isDir() )
return;
<a name="x742"></a> <a href="ntqstring.html">TQString</a> fileName = TQFileDialog::<a href="ntqfiledialog.html#getSaveFileName">getSaveFileName</a>(
<a name="x766"></a> item-&gt;<a href="qlistviewitem.html#text">text</a>(0),
TQString::null,
this,
"download file dialog",
tr("Save downloaded file as") );
if ( fileName.<a href="ntqstring.html#isNull">isNull</a>() )
return;
// create file on the heap because it has to be valid throughout the whole
// asynchronous download operation
<a href="ntqfile.html">TQFile</a> *file = new <a href="ntqfile.html">TQFile</a>( fileName );
if ( !file-&gt;<a href="ntqfile.html#open">open</a>( <a href="ntqfile.html#open">IO_WriteOnly</a> ) ) {
TQMessageBox::<a href="ntqmessagebox.html#critical">critical</a>( this, tr("Download error"),
tr("Can't open file '%1' for writing.").arg(fileName) );
delete file;
return;
}
<a href="ntqprogressdialog.html">TQProgressDialog</a> progress(
tr("Downloading file..."),
tr("Cancel"),
0,
this,
"download progress dialog",
TRUE );
connect( ftp, TQ_SIGNAL(<a href="ntqftp.html#dataTransferProgress">dataTransferProgress</a>(int,int)),
&amp;progress, TQ_SLOT(<a href="ntqprogressdialog.html#setProgress">setProgress</a>(int,int)) );
connect( ftp, TQ_SIGNAL(<a href="ntqftp.html#commandFinished">commandFinished</a>(int,bool)),
&amp;progress, TQ_SLOT(<a href="ntqprogressdialog.html#reset">reset</a>()) );
connect( &amp;progress, TQ_SIGNAL(<a href="ntqprogressdialog.html#cancelled">cancelled</a>()),
ftp, TQ_SLOT(<a href="ntqftp.html#abort">abort</a>()) );
<a name="x755"></a> ftp-&gt;<a href="ntqftp.html#get">get</a>( item-&gt;<a href="qlistviewitem.html#text">text</a>(0), file );
progress.<a href="ntqdialog.html#exec">exec</a>(); // ### takes a lot of time!!!
}
void FtpMainWindow::removeFile()
{
FtpViewItem *item = (FtpViewItem*)remoteView-&gt;selectedItem();
if ( !item || item-&gt;isDir() )
return;
<a name="x762"></a> ftp-&gt;<a href="ntqftp.html#remove">remove</a>( item-&gt;<a href="qlistviewitem.html#text">text</a>(0) );
ftp-&gt;<a href="ntqftp.html#list">list</a>();
}
void FtpMainWindow::connectToHost()
{
ConnectDialog connectDialog;
if ( connectDialog.exec() == TQDialog::Rejected )
return;
remotePath-&gt;clear();
remoteView-&gt;clear();
if ( ftp-&gt;<a href="ntqftp.html#state">state</a>() != TQFtp::Unconnected )
ftp-&gt;<a href="ntqftp.html#close">close</a>();
<a name="x749"></a> ftp-&gt;<a href="ntqftp.html#connectToHost">connectToHost</a>( connectDialog.host-&gt;text(), connectDialog.port-&gt;value() );
<a name="x758"></a> ftp-&gt;<a href="ntqftp.html#login">login</a>( connectDialog.username-&gt;text(), connectDialog.password-&gt;text() );
<a name="x760"></a> ftp-&gt;<a href="ntqftp.html#rawCommand">rawCommand</a>( "PWD" );
ftp-&gt;<a href="ntqftp.html#list">list</a>();
}
// This slot is connected to the TQComboBox::activated() signal of the
// remotePath.
void FtpMainWindow::changePath( const <a href="ntqstring.html">TQString</a> &amp;newPath )
{
<a name="x745"></a> ftp-&gt;<a href="ntqftp.html#cd">cd</a>( newPath );
ftp-&gt;<a href="ntqftp.html#rawCommand">rawCommand</a>( "PWD" );
ftp-&gt;<a href="ntqftp.html#list">list</a>();
}
// This slot is connected to the TQListView::doubleClicked() and
// TQListView::returnPressed() signals of the remoteView.
void FtpMainWindow::changePathOrDownload( <a href="qlistviewitem.html">TQListViewItem</a> *item )
{
if ( ((FtpViewItem*)item)-&gt;isDir() )
changePath( item-&gt;<a href="qlistviewitem.html#text">text</a>(0) );
else
downloadFile();
}
/****************************************************************************
**
** Slots connected to signals of the TQFtp class
**
*****************************************************************************/
void FtpMainWindow::ftp_commandStarted()
{
<a name="x738"></a> TQApplication::<a href="ntqapplication.html#setOverrideCursor">setOverrideCursor</a>( TQCursor(TQt::WaitCursor) );
<a name="x750"></a> if ( ftp-&gt;<a href="ntqftp.html#currentCommand">currentCommand</a>() == TQFtp::List ) {
remoteView-&gt;clear();
if ( currentFtpDir != "/" )
new FtpViewItem( remoteView, FtpViewItem::Directory, "..", "", "" );
}
}
void FtpMainWindow::ftp_commandFinished()
{
<a name="x737"></a> TQApplication::<a href="ntqapplication.html#restoreOverrideCursor">restoreOverrideCursor</a>();
<a name="x751"></a> delete ftp-&gt;<a href="ntqftp.html#currentDevice">currentDevice</a>();
}
void FtpMainWindow::ftp_done( bool error )
{
if ( error ) {
<a name="x754"></a> TQMessageBox::<a href="ntqmessagebox.html#critical">critical</a>( this, tr("FTP Error"), ftp-&gt;<a href="ntqftp.html#errorString">errorString</a>() );
// If we are connected, but not logged in, it is not meaningful to stay
// connected to the server since the error is a really fatal one (login
// failed).
if ( ftp-&gt;<a href="ntqftp.html#state">state</a>() == TQFtp::Connected )
ftp-&gt;<a href="ntqftp.html#close">close</a>();
}
}
void FtpMainWindow::ftp_stateChanged( int state )
{
switch ( (TQFtp::State)state ) {
case TQFtp::Unconnected:
<a name="x765"></a> stateFtp-&gt;<a href="ntqlabel.html#setText">setText</a>( tr("Unconnected") );
break;
case TQFtp::HostLookup:
stateFtp-&gt;<a href="ntqlabel.html#setText">setText</a>( tr("Host lookup") );
break;
case TQFtp::Connecting:
stateFtp-&gt;<a href="ntqlabel.html#setText">setText</a>( tr("Connecting") );
break;
case TQFtp::Connected:
stateFtp-&gt;<a href="ntqlabel.html#setText">setText</a>( tr("Connected") );
break;
case TQFtp::LoggedIn:
stateFtp-&gt;<a href="ntqlabel.html#setText">setText</a>( tr("Logged in") );
break;
case TQFtp::Closing:
stateFtp-&gt;<a href="ntqlabel.html#setText">setText</a>( tr("Closing") );
break;
}
}
void FtpMainWindow::ftp_listInfo( const <a href="ntqurlinfo.html">TQUrlInfo</a> &amp;i )
{
FtpViewItem::Type type;
<a name="x773"></a> if ( i.<a href="ntqurlinfo.html#isDir">isDir</a>() )
type = FtpViewItem::Directory;
else
type = FtpViewItem::File;
new FtpViewItem( remoteView, type,
<a name="x776"></a><a name="x775"></a><a name="x774"></a> i.<a href="ntqurlinfo.html#name">name</a>(), TQString::number(i.<a href="ntqurlinfo.html#size">size</a>()), i.<a href="ntqurlinfo.html#lastModified">lastModified</a>().toString() );
}
void FtpMainWindow::ftp_rawCommandReply( int code, const <a href="ntqstring.html">TQString</a> &amp;text )
{
if ( code == 257 ) {
<a name="x772"></a> currentFtpDir = text.<a href="ntqstring.html#section">section</a>( '"', 1, 1 );
for ( int i = 0; i&lt;remotePath-&gt;count(); i++ ) {
// make sure that we don't insert duplicates
if ( remotePath-&gt;text( i ) == currentFtpDir )
remotePath-&gt;removeItem( i );
}
remotePath-&gt;insertItem( currentFtpDir, 0 );
remotePath-&gt;setCurrentItem( 0 );
}
}
</pre>
<p> <hr>
<p> Main (main.cpp):
<p> <pre>/****************************************************************************
** $Id: qt/main.cpp 3.3.8 edited Jan 11 14:37 $
**
** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
**
** This file is part of an example program for TQt. This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/
#include &lt;<a href="qapplication-h.html">ntqapplication.h</a>&gt;
#include "ftpmainwindow.h"
int main( int argc, char **argv )
{
<a href="ntqapplication.html">TQApplication</a> a( argc, argv );
FtpMainWindow m;
<a name="x779"></a> a.<a href="ntqapplication.html#setMainWidget">setMainWidget</a>( &amp;m );
m.show();
<a name="x778"></a> a.<a href="ntqapplication.html#processEvents">processEvents</a>();
m.connectToHost();
<a name="x777"></a> return a.<a href="ntqapplication.html#exec">exec</a>();
}
</pre>
<p>See also <a href="network-examples.html">Network Examples</a>.
<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>TQt 3.3.8</div>
</table></div></address></body>
</html>