<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <!-- /tmp/qt-3.0-reggie-28534/qt-x11-free-3.0.2/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"><!-- h3.fn,span.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 Classes</font></a> | <a href="mainclasses.html"> <font color="#004faf">Main Classes</font></a> | <a href="annotated.html"> <font color="#004faf">Annotated</font></a> | <a href="groups.html"> <font color="#004faf">Grouped 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="qurloperator.html">QUrlOperator</a> (which in turn uses <a href="qftp.html">QFtp</a>) to perform its FTP commands. <p> <hr> <p> The API of the FtpMainWindow class (ftpmainwindow.h): <p> <pre>/**************************************************************************** ** $Id: qt/ftpmainwindow.h 3.0.2 edited Oct 12 12:18 $ ** ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved. ** ** This file is part of an example program for Qt. This example ** program may be used, distributed and modified without limitation. ** *****************************************************************************/ #ifndef FTPMAINWINDOW_H #define FTPMAINWINDOW_H #include <<a href="qmainwindow-h.html">qmainwindow.h</a>> #include <<a href="qurloperator-h.html">qurloperator.h</a>> class FtpView; class QSplitter; class QVBox; class QSpinBox; class QComboBox; class QLineEdit; class QNetworkOperation; class QLabel; class QProgressBar; class FtpMainWindow : public <a href="qmainwindow.html">QMainWindow</a> { <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a> public: FtpMainWindow(); <a href="qsplitter.html">QSplitter</a> *mainSplitter() const { return splitter; } private: void setupLeftSide(); void setupRightSide(); void setupCenterCommandBar(); void setup(); private slots: void slotLocalDirChanged( const <a href="qstring.html">QString</a> &path ); void slotLocalDirChanged( const <a href="qurlinfo.html">QUrlInfo</a> &info ); void slotRemoteDirChanged( const <a href="qstring.html">QString</a> &path ); void slotRemoteDirChanged( const <a href="qurlinfo.html">QUrlInfo</a> &info ); void slotConnect(); void slotUpload(); void slotDownload(); void slotLocalStart( <a href="qnetworkoperation.html">QNetworkOperation</a> * ); void slotLocalFinished( <a href="qnetworkoperation.html">QNetworkOperation</a> * ); void slotRemoteStart( <a href="qnetworkoperation.html">QNetworkOperation</a> * ); void slotRemoteFinished( <a href="qnetworkoperation.html">QNetworkOperation</a> * ); void slotLocalDataTransferProgress( int, int, QNetworkOperation * ); void slotRemoteDataTransferProgress( int, int, QNetworkOperation * ); void slotLocalMkDir(); void slotLocalRemove(); void slotRemoteMkDir(); void slotRemoteRemove(); void slotConnectionStateChanged( int, const <a href="qstring.html">QString</a> &msg ); private: <a href="qsplitter.html">QSplitter</a> *splitter; <a href="qvbox.html">QVBox</a> *mainWidget; FtpView *leftView, *rightView; <a href="qcombobox.html">QComboBox</a> *localCombo, *remoteHostCombo, *remotePathCombo, *userCombo; <a href="qlineedit.html">QLineEdit</a> *passLined; <a href="qspinbox.html">QSpinBox</a> *portSpin; <a href="qurloperator.html">QUrlOperator</a> localOperator, remoteOperator, oldLocal, oldRemote; <a href="qlabel.html">QLabel</a> *progressLabel1, *progressLabel2; <a href="qprogressbar.html">QProgressBar</a> *progressBar1, *progressBar2; }; #endif </pre> <p> <hr> <p> The Implementation of the FtpMainWindow class (ftpmainwindow.cpp): <p> <pre>/**************************************************************************** ** $Id: qt/ftpmainwindow.cpp 3.0.2 edited Oct 12 12:18 $ ** ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved. ** ** This file is part of an example program for Qt. This example ** program may be used, distributed and modified without limitation. ** *****************************************************************************/ #include "ftpmainwindow.h" #include "ftpview.h" #include <<a href="qvbox-h.html">qvbox.h</a>> #include <<a href="qhbox-h.html">qhbox.h</a>> #include <<a href="qsplitter-h.html">qsplitter.h</a>> #include <<a href="qcombobox-h.html">qcombobox.h</a>> #include <<a href="qlabel-h.html">qlabel.h</a>> #include <<a href="qspinbox-h.html">qspinbox.h</a>> #include <<a href="qlineedit-h.html">qlineedit.h</a>> #include <<a href="qpushbutton-h.html">qpushbutton.h</a>> #include <<a href="qmessagebox-h.html">qmessagebox.h</a>> #include <<a href="qprogressbar-h.html">qprogressbar.h</a>> #include <<a href="qdir-h.html">qdir.h</a>> #include <<a href="qinputdialog-h.html">qinputdialog.h</a>> #include <<a href="qapplication-h.html">qapplication.h</a>> #include <<a href="qstatusbar-h.html">qstatusbar.h</a>> <a name="f310"></a>FtpMainWindow::FtpMainWindow() : <a href="qmainwindow.html">QMainWindow</a>(), localOperator( "/" ) { setup(); // connect to the signals of the local QUrlOperator - this will be used to // work on the local file system (listing dirs, etc.) and to copy files // TO the local filesystem (downloading) <a href="qobject.html#connect">connect</a>( &localOperator, SIGNAL( newChildren( const <a href="qvaluelist.html">QValueList</a><QUrlInfo> &, QNetworkOperation * ) ), leftView, SLOT( slotInsertEntries( const <a href="qvaluelist.html">QValueList</a><QUrlInfo> & ) ) ); <a href="qobject.html#connect">connect</a>( &localOperator, SIGNAL( start( <a href="qnetworkoperation.html">QNetworkOperation</a> * ) ), this, SLOT( slotLocalStart( <a href="qnetworkoperation.html">QNetworkOperation</a> *) ) ); <a href="qobject.html#connect">connect</a>( &localOperator, SIGNAL( finished( <a href="qnetworkoperation.html">QNetworkOperation</a> * ) ), this, SLOT( slotLocalFinished( <a href="qnetworkoperation.html">QNetworkOperation</a> *) ) ); <a href="qobject.html#connect">connect</a>( leftView, SIGNAL( itemSelected( const <a href="qurlinfo.html">QUrlInfo</a> & ) ), this, SLOT( slotLocalDirChanged( const <a href="qurlinfo.html">QUrlInfo</a> & ) ) ); <a href="qobject.html#connect">connect</a>( &localOperator, SIGNAL( dataTransferProgress( int, int, QNetworkOperation * ) ), this, SLOT( slotLocalDataTransferProgress( int, int, QNetworkOperation * ) ) ); // connect to the signals of the remote QUrlOperator - this will be used to // work on the remote file system (on the FTP Server) and to copy files // TO the ftp server (uploading) <a href="qobject.html#connect">connect</a>( &remoteOperator, SIGNAL( newChildren( const <a href="qvaluelist.html">QValueList</a><QUrlInfo> &, QNetworkOperation * ) ), rightView, SLOT( slotInsertEntries( const <a href="qvaluelist.html">QValueList</a><QUrlInfo> & ) ) ); <a href="qobject.html#connect">connect</a>( &remoteOperator, SIGNAL( start( <a href="qnetworkoperation.html">QNetworkOperation</a> * ) ), this, SLOT( slotRemoteStart( <a href="qnetworkoperation.html">QNetworkOperation</a> *) ) ); <a href="qobject.html#connect">connect</a>( &remoteOperator, SIGNAL( finished( <a href="qnetworkoperation.html">QNetworkOperation</a> * ) ), this, SLOT( slotRemoteFinished( <a href="qnetworkoperation.html">QNetworkOperation</a> *) ) ); <a href="qobject.html#connect">connect</a>( rightView, SIGNAL( itemSelected( const <a href="qurlinfo.html">QUrlInfo</a> & ) ), this, SLOT( slotRemoteDirChanged( const <a href="qurlinfo.html">QUrlInfo</a> & ) ) ); <a href="qobject.html#connect">connect</a>( &remoteOperator, SIGNAL( dataTransferProgress( int, int, QNetworkOperation * ) ), this, SLOT( slotRemoteDataTransferProgress( int, int, QNetworkOperation * ) ) ); <a href="qobject.html#connect">connect</a>( &remoteOperator, SIGNAL( connectionStateChanged( int, const <a href="qstring.html">QString</a> & ) ), this, SLOT( slotConnectionStateChanged( int, const <a href="qstring.html">QString</a> & ) ) ); // read the local filesystem at the beginning once localOperator.listChildren(); // create status bar (void)<a href="qmainwindow.html#statusBar">statusBar</a>(); } void <a name="f311"></a>FtpMainWindow::setupLeftSide() { // Setup the left side of the GUI, this is the listview // of the local filesystem <a href="qvbox.html">QVBox</a> *layout = new <a href="qvbox.html">QVBox</a>( splitter ); <a name="x1009"></a> layout-><a href="qhbox.html#setSpacing">setSpacing</a>( 5 ); <a name="x1008"></a> layout-><a href="qframe.html#setMargin">setMargin</a>( 5 ); <a href="qhbox.html">QHBox</a> *h = new <a href="qhbox.html">QHBox</a>( layout ); h-><a href="qhbox.html#setSpacing">setSpacing</a>( 5 ); <a href="qlabel.html">QLabel</a> *l = new <a href="qlabel.html">QLabel</a>( <a href="qobject.html#tr">tr</a>( "Local Path:" ), h ); <a name="x1038"></a><a name="x1036"></a> l-><a href="qwidget.html#setFixedWidth">setFixedWidth</a>( l-><a href="qwidget.html#sizeHint">sizeHint</a>().width() ); localCombo = new <a href="qcombobox.html">QComboBox</a>( TRUE, h ); <a name="x1005"></a> localCombo-><a href="qcombobox.html#insertItem">insertItem</a>( "/" ); <a name="x1003"></a> <a href="qobject.html#connect">connect</a>( localCombo, SIGNAL( <a href="qcombobox.html#activated">activated</a>( const <a href="qstring.html">QString</a> & ) ), this, SLOT( slotLocalDirChanged( const <a href="qstring.html">QString</a> & ) ) ); leftView = new FtpView( layout ); <a href="qhbox.html">QHBox</a> *bottom = new <a href="qhbox.html">QHBox</a>( layout ); bottom-><a href="qhbox.html#setSpacing">setSpacing</a>( 5 ); <a href="qpushbutton.html">QPushButton</a> *bMkDir = new <a href="qpushbutton.html">QPushButton</a>( <a href="qobject.html#tr">tr</a>( "New Directory" ), bottom ); <a href="qpushbutton.html">QPushButton</a> *bRemove = new <a href="qpushbutton.html">QPushButton</a>( <a href="qobject.html#tr">tr</a>( "Remove" ), bottom ); <a name="x1002"></a> <a href="qobject.html#connect">connect</a>( bMkDir, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( slotLocalMkDir() ) ); <a href="qobject.html#connect">connect</a>( bRemove, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( slotLocalRemove() ) ); <a name="x1028"></a> splitter-><a href="qsplitter.html#setResizeMode">setResizeMode</a>( layout, QSplitter::Stretch ); } void <a name="f312"></a>FtpMainWindow::setupRightSide() { // Setup the right side of the GUI, this is the listview // of the remote filesystem (FTP), needs also lineedits/combos // for username, password, etc. <a href="qvbox.html">QVBox</a> *layout = new <a href="qvbox.html">QVBox</a>( splitter ); layout-><a href="qhbox.html#setSpacing">setSpacing</a>( 5 ); layout-><a href="qframe.html#setMargin">setMargin</a>( 5 ); <a href="qhbox.html">QHBox</a> *h = new <a href="qhbox.html">QHBox</a>( layout ); h-><a href="qhbox.html#setSpacing">setSpacing</a>( 5 ); <a href="qlabel.html">QLabel</a> *l = new <a href="qlabel.html">QLabel</a>( <a href="qobject.html#tr">tr</a>( "Remote Host:" ), h ); l-><a href="qwidget.html#setFixedWidth">setFixedWidth</a>( l-><a href="qwidget.html#sizeHint">sizeHint</a>().width() ); remoteHostCombo = new <a href="qcombobox.html">QComboBox</a>( TRUE, h ); l = new <a href="qlabel.html">QLabel</a>( <a href="qobject.html#tr">tr</a>( "Port:" ), h ); l-><a href="qwidget.html#setFixedWidth">setFixedWidth</a>( l-><a href="qwidget.html#sizeHint">sizeHint</a>().width() ); portSpin = new <a href="qspinbox.html">QSpinBox</a>( 0, 32767, 1, h ); <a name="x1026"></a> portSpin-><a href="qspinbox.html#setValue">setValue</a>( 21 ); portSpin-><a href="qwidget.html#setFixedWidth">setFixedWidth</a>( portSpin-><a href="qwidget.html#sizeHint">sizeHint</a>().width() ); <a name="x1027"></a> remoteOperator.setPort( portSpin-><a href="qspinbox.html#value">value</a>() ); h = new <a href="qhbox.html">QHBox</a>( layout ); h-><a href="qhbox.html#setSpacing">setSpacing</a>( 5 ); l = new <a href="qlabel.html">QLabel</a>( <a href="qobject.html#tr">tr</a>( "Remote Path:" ), h ); l-><a href="qwidget.html#setFixedWidth">setFixedWidth</a>( l-><a href="qwidget.html#sizeHint">sizeHint</a>().width() ); remotePathCombo = new <a href="qcombobox.html">QComboBox</a>( TRUE, h ); h = new <a href="qhbox.html">QHBox</a>( layout ); h-><a href="qhbox.html#setSpacing">setSpacing</a>( 5 ); l = new <a href="qlabel.html">QLabel</a>( <a href="qobject.html#tr">tr</a>( "Username:" ), h ); l-><a href="qwidget.html#setFixedWidth">setFixedWidth</a>( l-><a href="qwidget.html#sizeHint">sizeHint</a>().width() ); userCombo = new <a href="qcombobox.html">QComboBox</a>( TRUE, h ); l = new <a href="qlabel.html">QLabel</a>( <a href="qobject.html#tr">tr</a>( "Password:" ), h ); l-><a href="qwidget.html#setFixedWidth">setFixedWidth</a>( l-><a href="qwidget.html#sizeHint">sizeHint</a>().width() ); passLined = new <a href="qlineedit.html">QLineEdit</a>( h ); <a name="x1012"></a> passLined-><a href="qlineedit.html#setEchoMode">setEchoMode</a>( QLineEdit::Password ); rightView = new FtpView( layout ); <a href="qhbox.html">QHBox</a> *bottom = new <a href="qhbox.html">QHBox</a>( layout ); bottom-><a href="qhbox.html#setSpacing">setSpacing</a>( 5 ); <a href="qpushbutton.html">QPushButton</a> *bMkDir = new <a href="qpushbutton.html">QPushButton</a>( <a href="qobject.html#tr">tr</a>( "New Directory" ), bottom ); <a href="qpushbutton.html">QPushButton</a> *bRemove = new <a href="qpushbutton.html">QPushButton</a>( <a href="qobject.html#tr">tr</a>( "Remove" ), bottom ); <a href="qobject.html#connect">connect</a>( bMkDir, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( slotRemoteMkDir() ) ); <a href="qobject.html#connect">connect</a>( bRemove, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( slotRemoteRemove() ) ); splitter-><a href="qsplitter.html#setResizeMode">setResizeMode</a>( layout, QSplitter::Stretch ); <a href="qobject.html#connect">connect</a>( remotePathCombo, SIGNAL( <a href="qcombobox.html#activated">activated</a>( const <a href="qstring.html">QString</a> & ) ), this, SLOT( slotRemoteDirChanged( const <a href="qstring.html">QString</a> & ) ) ); } void <a name="f313"></a>FtpMainWindow::setupCenterCommandBar() { // Setup the command bar in the middle between the two views <a href="qvbox.html">QVBox</a> *w = new <a href="qvbox.html">QVBox</a>( splitter ); splitter-><a href="qsplitter.html#setResizeMode">setResizeMode</a>( w, QSplitter::FollowSizeHint ); w-><a href="qhbox.html#setSpacing">setSpacing</a>( 5 ); w-><a href="qframe.html#setMargin">setMargin</a>( 5 ); <a href="qpushbutton.html">QPushButton</a> *bConnect = new <a href="qpushbutton.html">QPushButton</a>( <a href="qobject.html#tr">tr</a>( "&Connect" ), w ); (void)new <a href="qwidget.html">QWidget</a>( w ); <a href="qpushbutton.html">QPushButton</a> *bUpload = new <a href="qpushbutton.html">QPushButton</a>( <a href="qobject.html#tr">tr</a>( "== &Upload ==>" ), w ); <a href="qpushbutton.html">QPushButton</a> *bDownload = new <a href="qpushbutton.html">QPushButton</a>( <a href="qobject.html#tr">tr</a>( "<== &Download ==" ), w ); (void)new <a href="qwidget.html">QWidget</a>( w ); <a href="qobject.html#connect">connect</a>( bConnect, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( slotConnect() ) ); <a href="qobject.html#connect">connect</a>( bUpload, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( slotUpload() ) ); <a href="qobject.html#connect">connect</a>( bDownload, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ), this, SLOT( slotDownload() ) ); } void <a name="f314"></a>FtpMainWindow::setup() { // Setup the GUI mainWidget = new <a href="qvbox.html">QVBox</a>( this ); splitter = new <a href="qsplitter.html">QSplitter</a>( mainWidget ); setupLeftSide(); setupCenterCommandBar(); setupRightSide(); progressLabel1 = new <a href="qlabel.html">QLabel</a>( <a href="qobject.html#tr">tr</a>( "No Operation in Progress" ), mainWidget ); progressBar1 = new <a href="qprogressbar.html">QProgressBar</a>( mainWidget ); progressLabel2 = new <a href="qlabel.html">QLabel</a>( <a href="qobject.html#tr">tr</a>( "No Operation in Progress" ), mainWidget ); progressBar2 = new <a href="qprogressbar.html">QProgressBar</a>( mainWidget ); <a name="x1034"></a> progressLabel1-><a href="qwidget.html#hide">hide</a>(); progressBar1-><a href="qwidget.html#hide">hide</a>(); progressLabel2-><a href="qwidget.html#hide">hide</a>(); progressBar2-><a href="qwidget.html#hide">hide</a>(); <a href="qmainwindow.html#setCentralWidget">setCentralWidget</a>( mainWidget ); } void <a name="f315"></a>FtpMainWindow::slotLocalDirChanged( const <a href="qstring.html">QString</a> &path ) { // The user changed the path on the left side oldLocal = localOperator; localOperator.setPath( path ); localOperator.listChildren(); } void FtpMainWindow::slotLocalDirChanged( const <a href="qurlinfo.html">QUrlInfo</a> &info ) { // The user changed the path on the left side oldLocal = localOperator; <a name="x1030"></a> localOperator.addPath( info.<a href="qurlinfo.html#name">name</a>() ); localOperator.listChildren(); localCombo-><a href="qcombobox.html#insertItem">insertItem</a>( localOperator.path(), 0 ); <a name="x1006"></a> localCombo-><a href="qcombobox.html#setCurrentItem">setCurrentItem</a>( 0 ); } void <a name="f316"></a>FtpMainWindow::slotRemoteDirChanged( const <a href="qstring.html">QString</a> &path ) { // The user changed the path on the right side if ( !remoteOperator.isValid() ) return; oldRemote = remoteOperator; remoteOperator.setPath( path ); remoteOperator.listChildren(); } void FtpMainWindow::slotRemoteDirChanged( const <a href="qurlinfo.html">QUrlInfo</a> &info ) { // The user changed the path on the right side oldRemote = remoteOperator; remoteOperator.addPath( info.<a href="qurlinfo.html#name">name</a>() ); remoteOperator.listChildren(); remotePathCombo-><a href="qcombobox.html#insertItem">insertItem</a>( remoteOperator.path(), 0 ); remotePathCombo-><a href="qcombobox.html#setCurrentItem">setCurrentItem</a>( 0 ); } void <a name="f317"></a>FtpMainWindow::slotConnect() { // The user pressed the connect button, so let's connect to the // FTP server // First we need to set stuff (host, path, etc.) which the user // entered on the right side to the remote QUrlOperator // protocol + hostname <a name="x1004"></a> <a href="qstring.html">QString</a> s = "ftp://" + remoteHostCombo-><a href="qcombobox.html#currentText">currentText</a>(); oldRemote = remoteOperator; remoteOperator = s; // path on the server if ( !remotePathCombo-><a href="qcombobox.html#currentText">currentText</a>().isEmpty() ) remoteOperator.setPath( remotePathCombo-><a href="qcombobox.html#currentText">currentText</a>() ); else remoteOperator.setPath( "/" ); // if nothing or "ftp" or "anonymous" has been entered into the username combo, // let's connect anonymous, else private with password if ( !userCombo-><a href="qcombobox.html#currentText">currentText</a>().isEmpty() && userCombo-><a href="qcombobox.html#currentText">currentText</a>().lower() != "anonymous" && userCombo-><a href="qcombobox.html#currentText">currentText</a>().lower() != "ftp" ) { remoteOperator.setUser( userCombo-><a href="qcombobox.html#currentText">currentText</a>() ); <a name="x1014"></a> remoteOperator.setPassword( passLined-><a href="qlineedit.html#text">text</a>() ); } // set the port remoteOperator.setPort( portSpin-><a href="qspinbox.html#value">value</a>() ); // finally read the directory on the ftp server remoteOperator.listChildren(); } void <a name="f318"></a>FtpMainWindow::slotUpload() { // the user pressed the upload button // if files have been selected on the left side (local filesystem) <a href="qvaluelist.html">QValueList</a><QUrlInfo> files = leftView->selectedItems(); <a name="x1033"></a> if ( files.<a href="qvaluelist.html#isEmpty">isEmpty</a>() ) return; // create a list of the URLs which should be copied <a href="qstringlist.html">QStringList</a> lst; QValueList<QUrlInfo>::Iterator it = files.<a href="qvaluelist.html#begin">begin</a>(); for ( ; it != files.<a href="qvaluelist.html#end">end</a>(); ++it ) lst << QUrl( localOperator, ( *it ).name() ); // copy the list of selected files to the directory in which the // remoteOperator currently is (upload) remoteOperator.copy( lst, remoteOperator, FALSE ); } void <a name="f319"></a>FtpMainWindow::slotDownload() { // if the user pressed the download button // if files have been selected on the right side (remote filesystem) <a href="qvaluelist.html">QValueList</a><QUrlInfo> files = rightView->selectedItems(); if ( files.<a href="qvaluelist.html#isEmpty">isEmpty</a>() ) return; // create a list of the URLs which should be downloaded <a href="qstringlist.html">QStringList</a> lst; QValueList<QUrlInfo>::Iterator it = files.<a href="qvaluelist.html#begin">begin</a>(); for ( ; it != files.<a href="qvaluelist.html#end">end</a>(); ++it ) lst << QUrl( remoteOperator, ( *it ).name() ); // copy the list of selected files to the directory in which the // localOperator currently is (download) localOperator.copy( lst, localOperator, FALSE ); } void <a name="f320"></a>FtpMainWindow::slotLocalStart( <a href="qnetworkoperation.html">QNetworkOperation</a> *op ) { // this slot is always called if the local QUrlOperator starts // listing a directory or dowloading a file if ( !op ) return; <a name="x1019"></a> if ( op-><a href="qnetworkoperation.html#operation">operation</a>() == QNetworkProtocol::OpListChildren ) { // start listing a dir? clear the left view! <a name="x1015"></a> leftView-><a href="qlistview.html#clear">clear</a>(); } else if ( op-><a href="qnetworkoperation.html#operation">operation</a>() == QNetworkProtocol::OpGet ) { // start downloading a file? reset the progress bar! <a name="x1024"></a> progressBar1-><a href="qprogressbar.html#setTotalSteps">setTotalSteps</a>( 0 ); <a name="x1022"></a> progressBar1-><a href="qprogressbar.html#reset">reset</a>(); } } void <a name="f321"></a>FtpMainWindow::slotLocalFinished( <a href="qnetworkoperation.html">QNetworkOperation</a> *op ) { // this slot is always called if the local QUrlOperator finished // an operation if ( !op ) return; <a name="x1021"></a> if ( op && op-><a href="qnetworkoperation.html#state">state</a>() == QNetworkProtocol::StFailed ) { // an error happend, let the user know that <a name="x1020"></a><a name="x1016"></a> QMessageBox::<a href="qmessagebox.html#critical">critical</a>( this, tr( "ERROR" ), op-><a href="qnetworkoperation.html#protocolDetail">protocolDetail</a>() ); // do something depending in the error code <a name="x1018"></a> int ecode = op-><a href="qnetworkoperation.html#errorCode">errorCode</a>(); if ( ecode == QNetworkProtocol::ErrListChildren || ecode == QNetworkProtocol::ErrParse || ecode == QNetworkProtocol::ErrUnknownProtocol || ecode == QNetworkProtocol::ErrLoginIncorrect || ecode == QNetworkProtocol::ErrValid || ecode == QNetworkProtocol::ErrHostNotFound || ecode == QNetworkProtocol::ErrFileNotExisting ) { localOperator = oldLocal; <a name="x1007"></a> localCombo-><a href="qcombobox.html#setEditText">setEditText</a>( localOperator.path() ); localOperator.listChildren(); } } else if ( op-><a href="qnetworkoperation.html#operation">operation</a>() == QNetworkProtocol::OpPut ) { // finished saving the downloaded file? reread the dir and hide the progress bar localOperator.listChildren(); progressLabel1-><a href="qwidget.html#hide">hide</a>(); progressBar1-><a href="qwidget.html#hide">hide</a>(); } else if ( op-><a href="qnetworkoperation.html#operation">operation</a>() == QNetworkProtocol::OpGet ) { // finished reading a file from the ftp server? reset the progress bar progressBar1-><a href="qprogressbar.html#setTotalSteps">setTotalSteps</a>( 0 ); progressBar1-><a href="qprogressbar.html#reset">reset</a>(); } } void <a name="f322"></a>FtpMainWindow::slotRemoteStart( <a href="qnetworkoperation.html">QNetworkOperation</a> *op ) { // this slot is always called if the remote QUrlOperator starts // listing a directory or uploading a file if ( !op ) return; if ( op-><a href="qnetworkoperation.html#operation">operation</a>() == QNetworkProtocol::OpListChildren ) { // start listing a dir? clear the right view! rightView-><a href="qlistview.html#clear">clear</a>(); } else if ( op-><a href="qnetworkoperation.html#operation">operation</a>() == QNetworkProtocol::OpGet ) { // start downloading a file? reset the progress bar! progressBar2-><a href="qprogressbar.html#setTotalSteps">setTotalSteps</a>( 0 ); progressBar2-><a href="qprogressbar.html#reset">reset</a>(); } } void <a name="f323"></a>FtpMainWindow::slotRemoteFinished( <a href="qnetworkoperation.html">QNetworkOperation</a> *op ) { // this slot is always called if the remote QUrlOperator finished // an operation if ( !op ) return; if ( op && op-><a href="qnetworkoperation.html#state">state</a>() == QNetworkProtocol::StFailed ) { // an error happend, let the user know that QMessageBox::<a href="qmessagebox.html#critical">critical</a>( this, tr( "ERROR" ), op-><a href="qnetworkoperation.html#protocolDetail">protocolDetail</a>() ); // do something depending in the error code int ecode = op-><a href="qnetworkoperation.html#errorCode">errorCode</a>(); if ( ecode == QNetworkProtocol::ErrListChildren || ecode == QNetworkProtocol::ErrParse || ecode == QNetworkProtocol::ErrUnknownProtocol || ecode == QNetworkProtocol::ErrLoginIncorrect || ecode == QNetworkProtocol::ErrValid || ecode == QNetworkProtocol::ErrHostNotFound || ecode == QNetworkProtocol::ErrFileNotExisting ) { remoteOperator = oldRemote; remoteHostCombo-><a href="qcombobox.html#setEditText">setEditText</a>( remoteOperator.host() ); remotePathCombo-><a href="qcombobox.html#setEditText">setEditText</a>( remoteOperator.path() ); <a name="x1013"></a> passLined-><a href="qlineedit.html#setText">setText</a>( remoteOperator.password() ); userCombo-><a href="qcombobox.html#setEditText">setEditText</a>( remoteOperator.user() ); portSpin-><a href="qspinbox.html#setValue">setValue</a>( remoteOperator.port() ); remoteOperator.listChildren(); } } else if ( op-><a href="qnetworkoperation.html#operation">operation</a>() == QNetworkProtocol::OpListChildren ) { // finished reading a dir? set the correct path to the pth combo of the right view remotePathCombo-><a href="qcombobox.html#setEditText">setEditText</a>( remoteOperator.path() ); } else if ( op-><a href="qnetworkoperation.html#operation">operation</a>() == QNetworkProtocol::OpPut ) { // finished saving the uploaded file? reread the dir and hide the progress bar remoteOperator.listChildren(); progressLabel2-><a href="qwidget.html#hide">hide</a>(); progressBar2-><a href="qwidget.html#hide">hide</a>(); } else if ( op-><a href="qnetworkoperation.html#operation">operation</a>() == QNetworkProtocol::OpGet ) { // finished reading a file from the local filesystem? reset the progress bar progressBar2-><a href="qprogressbar.html#setTotalSteps">setTotalSteps</a>( 0 ); progressBar2-><a href="qprogressbar.html#reset">reset</a>(); } } void <a name="f324"></a>FtpMainWindow::slotLocalDataTransferProgress( int bytesDone, int bytesTotal, <a href="qnetworkoperation.html">QNetworkOperation</a> *op ) { // Show the progress here of the local QUrlOperator reads or writes data if ( !op ) return; <a name="x1035"></a> if ( !progressBar1-><a href="qwidget.html#isVisible">isVisible</a>() ) { if ( bytesDone < bytesTotal) { <a name="x1037"></a> progressLabel1-><a href="qwidget.html#show">show</a>(); progressBar1-><a href="qwidget.html#show">show</a>(); progressBar1-><a href="qprogressbar.html#setTotalSteps">setTotalSteps</a>( bytesTotal ); <a name="x1023"></a> progressBar1-><a href="qprogressbar.html#setProgress">setProgress</a>( 0 ); progressBar1-><a href="qprogressbar.html#reset">reset</a>(); } else return; } <a name="x1025"></a> if ( progressBar1-><a href="qprogressbar.html#totalSteps">totalSteps</a>() == bytesTotal ) progressBar1-><a href="qprogressbar.html#setTotalSteps">setTotalSteps</a>( bytesTotal ); if ( op-><a href="qnetworkoperation.html#operation">operation</a>() == QNetworkProtocol::OpGet ) <a name="x1017"></a><a name="x1011"></a> progressLabel1-><a href="qlabel.html#setText">setText</a>( <a href="qobject.html#tr">tr</a>( "Read: %1" ).arg( op-><a href="qnetworkoperation.html#arg">arg</a>( 0 ) ) ); else if ( op-><a href="qnetworkoperation.html#operation">operation</a>() == QNetworkProtocol::OpPut ) progressLabel1-><a href="qlabel.html#setText">setText</a>( <a href="qobject.html#tr">tr</a>( "Write: %1" ).arg( op-><a href="qnetworkoperation.html#arg">arg</a>( 0 ) ) ); else return; progressBar1-><a href="qprogressbar.html#setProgress">setProgress</a>( bytesDone ); } void <a name="f325"></a>FtpMainWindow::slotRemoteDataTransferProgress( int bytesDone, int bytesTotal, <a href="qnetworkoperation.html">QNetworkOperation</a> *op ) { // Show the progress here of the remote QUrlOperator reads or writes data if ( !op ) return; if ( !progressBar2-><a href="qwidget.html#isVisible">isVisible</a>() ) { if ( bytesDone < bytesTotal) { progressLabel2-><a href="qwidget.html#show">show</a>(); progressBar2-><a href="qwidget.html#show">show</a>(); progressBar2-><a href="qprogressbar.html#setTotalSteps">setTotalSteps</a>( bytesTotal ); progressBar2-><a href="qprogressbar.html#setProgress">setProgress</a>( 0 ); progressBar2-><a href="qprogressbar.html#reset">reset</a>(); } else return; } if ( progressBar2-><a href="qprogressbar.html#totalSteps">totalSteps</a>() != bytesTotal ) progressBar2-><a href="qprogressbar.html#setTotalSteps">setTotalSteps</a>( bytesTotal ); if ( op-><a href="qnetworkoperation.html#operation">operation</a>() == QNetworkProtocol::OpGet ) progressLabel2-><a href="qlabel.html#setText">setText</a>( <a href="qobject.html#tr">tr</a>( "Read: %1" ).arg( op-><a href="qnetworkoperation.html#arg">arg</a>( 0 ) ) ); else if ( op-><a href="qnetworkoperation.html#operation">operation</a>() == QNetworkProtocol::OpPut ) progressLabel2-><a href="qlabel.html#setText">setText</a>( <a href="qobject.html#tr">tr</a>( "Write: %1" ).arg( op-><a href="qnetworkoperation.html#arg">arg</a>( 0 ) ) ); else return; progressBar2-><a href="qprogressbar.html#setProgress">setProgress</a>( bytesDone ); } void <a name="f326"></a>FtpMainWindow::slotLocalMkDir() { // create a dir on the local filesystem bool ok = FALSE; <a name="x1010"></a> <a href="qstring.html">QString</a> name = QInputDialog::<a href="qinputdialog.html#getText">getText</a>( <a href="qobject.html#tr">tr</a>( "Directory Name:" ), <a href="qstring.html#QString-null">QString::null</a>, QLineEdit::Normal, QString::null, &ok, this ); if ( !name.<a href="qstring.html#isEmpty">isEmpty</a>() && ok ) localOperator.mkdir( name ); } void <a name="f327"></a>FtpMainWindow::slotLocalRemove() { } void <a name="f328"></a>FtpMainWindow::slotRemoteMkDir() { // create a dir on the remote filesystem (FTP server) bool ok = FALSE; <a href="qstring.html">QString</a> name = QInputDialog::<a href="qinputdialog.html#getText">getText</a>( <a href="qobject.html#tr">tr</a>( "Directory Name:" ), QString::null, QLineEdit::Normal, QString::null, &ok, this ); if ( !name.<a href="qstring.html#isEmpty">isEmpty</a>() && ok ) remoteOperator.mkdir( name ); } void <a name="f329"></a>FtpMainWindow::slotRemoteRemove() { } void <a name="f330"></a>FtpMainWindow::slotConnectionStateChanged( int, const <a href="qstring.html">QString</a> &msg ) { <a href="qmainwindow.html#statusBar">statusBar</a>()->message( msg ); } </pre> <p> <hr> <p> The API of the FtpViewItem and FtpView classes (ftpview.h): <p> <pre>/**************************************************************************** ** $Id: qt/ftpview.h 3.0.2 edited Oct 12 12:18 $ ** ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved. ** ** This file is part of an example program for Qt. This example ** program may be used, distributed and modified without limitation. ** *****************************************************************************/ #ifndef FTPVIEW_H #define FTPVIEW_H #include <<a href="qlistview-h.html">qlistview.h</a>> #include <<a href="qvaluelist-h.html">qvaluelist.h</a>> #include <<a href="qurlinfo-h.html">qurlinfo.h</a>> class FtpViewItem : public <a href="qlistviewitem.html">QListViewItem</a> { public: FtpViewItem( <a href="qlistview.html">QListView</a> *parent, const <a href="qurlinfo.html">QUrlInfo</a> &i ); int compare( <a href="qlistviewitem.html">QListViewItem</a> * i, int col, bool ascending ) const; <a href="qstring.html">QString</a> text( int c ) const; const <a href="qpixmap.html">QPixmap</a>* pixmap( int c ) const; <a href="qurlinfo.html">QUrlInfo</a> entryInfo() { return info; } private: <a href="qurlinfo.html">QUrlInfo</a> info; }; class FtpView : public <a href="qlistview.html">QListView</a> { Q_OBJECT public: FtpView( <a href="qwidget.html">QWidget</a> *parent ); <a href="qvaluelist.html">QValueList</a><QUrlInfo> selectedItems() const; public slots: void slotInsertEntries( const <a href="qvaluelist.html">QValueList</a><QUrlInfo> &info ); signals: void itemSelected( const <a href="qurlinfo.html">QUrlInfo</a> &info ); private slots: void slotSelected( <a href="qlistviewitem.html">QListViewItem</a> *item ); }; #endif </pre> <p> <hr> <p> Their Implementation (ftpview.cpp): <p> <pre>/**************************************************************************** ** $Id: qt/ftpview.cpp 3.0.2 edited Oct 12 12:18 $ ** ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved. ** ** This file is part of an example program for Qt. This example ** program may be used, distributed and modified without limitation. ** *****************************************************************************/ #include "ftpview.h" #include <<a href="qpixmap-h.html">qpixmap.h</a>> #include <<a href="qvaluelist-h.html">qvaluelist.h</a>> /* XPM */ static const char* closed_xpm[]={ "15 15 6 1", ". c None", "b c #ffff00", "d c #000000", "* c #999999", "a c #cccccc", "c c #ffffff", "...............", "..*****........", ".*ababa*.......", "*abababa******.", "*cccccccccccc*d", "*cbababababab*d", "*cabababababa*d", "*cbababababab*d", "*cabababababa*d", "*cbababababab*d", "*cabababababa*d", "*cbababababab*d", "**************d", ".dddddddddddddd", "..............."}; /* XPM */ static const char* file_xpm[]={ "13 15 5 1", ". c #7f7f7f", "# c None", "c c #000000", "b c #bfbfbf", "a c #ffffff", "..........###", ".aaaaaaaab.##", ".aaaaaaaaba.#", ".aaaaaaaacccc", ".aaaaaaaaaabc", ".aaaaaaaaaabc", ".aaaaaaaaaabc", ".aaaaaaaaaabc", ".aaaaaaaaaabc", ".aaaaaaaaaabc", ".aaaaaaaaaabc", ".aaaaaaaaaabc", ".aaaaaaaaaabc", ".bbbbbbbbbbbc", "ccccccccccccc"}; QPixmap *folderIcon = 0; QPixmap *fileIcon = 0; <a name="f335"></a>FtpViewItem::FtpViewItem( <a href="qlistview.html">QListView</a> *parent, const <a href="qurlinfo.html">QUrlInfo</a> &i ) <a name="x1044"></a> : <a href="qlistviewitem.html">QListViewItem</a>( parent, i.<a href="qurlinfo.html#name">name</a>() ), info( i ) { } <a name="x1039"></a>int FtpViewItem::<a href="qlistviewitem.html#compare">compare</a>( <a href="qlistviewitem.html">QListViewItem</a> * i, int col, bool ascending ) const { FtpViewItem *other = (FtpViewItem*)i; switch ( col ) { case 1: <a name="x1047"></a> if ( info.<a href="qvaluelist.html#size">size</a>() == other->info.<a href="qvaluelist.html#size">size</a>() ) return 0; else return info.<a href="qvaluelist.html#size">size</a>() < other->info.<a href="qvaluelist.html#size">size</a>() ? -1 : 1; case 2: if ( info.lastModified() == other->info.lastModified() ) return 0; else return info.lastModified() < other->info.lastModified() ? -1 : 1; default: // use default method for colum 0 and others added in the future return QListViewItem::compare( i, col, ascending ); }; } <a name="x1042"></a>QString FtpViewItem::<a href="qlistviewitem.html#text">text</a>( int c ) const { switch ( c ) { case 0: return info.name(); case 1: return QString::number( info.<a href="qvaluelist.html#size">size</a>() ); case 2: return info.lastModified().toString(); } return "????"; } <a name="x1040"></a>const QPixmap *FtpViewItem::<a href="qlistviewitem.html#pixmap">pixmap</a>( int c ) const { if ( !folderIcon ) folderIcon = new <a href="qpixmap.html">QPixmap</a>( closed_xpm ); if ( !fileIcon ) fileIcon = new <a href="qpixmap.html">QPixmap</a>( file_xpm ); if ( info.isDir() && c == 0 ) return folderIcon; else if ( info.isFile() && c == 0 ) return fileIcon; return 0; } <a name="f331"></a>FtpView::FtpView( <a href="qwidget.html">QWidget</a> *parent ) : <a href="qlistview.html">QListView</a>( parent ) { <a href="qlistview.html#addColumn">addColumn</a>( <a href="qobject.html#tr">tr</a>( "Name" ) ); <a href="qlistview.html#addColumn">addColumn</a>( <a href="qobject.html#tr">tr</a>( "Size" ) ); <a href="qlistview.html#addColumn">addColumn</a>( <a href="qobject.html#tr">tr</a>( "Last Modified" ) ); <a href="qlistview.html#setColumnAlignment">setColumnAlignment</a>( 1, Qt::AlignRight ); <a href="qlistview.html#setShowSortIndicator">setShowSortIndicator</a>( TRUE ); <a href="qlistview.html#setAllColumnsShowFocus">setAllColumnsShowFocus</a>( TRUE ); <a href="qlistview.html#setSelectionMode">setSelectionMode</a>( Extended ); <a href="qobject.html#connect">connect</a>( this, SIGNAL( <a href="qlistview.html#doubleClicked">doubleClicked</a>( <a href="qlistviewitem.html">QListViewItem</a> * ) ), this, SLOT( slotSelected( <a href="qlistviewitem.html">QListViewItem</a> * ) ) ); <a href="qobject.html#connect">connect</a>( this, SIGNAL( <a href="qlistview.html#returnPressed">returnPressed</a>( <a href="qlistviewitem.html">QListViewItem</a> * ) ), this, SLOT( slotSelected( <a href="qlistviewitem.html">QListViewItem</a> * ) ) ); } void <a name="f332"></a>FtpView::slotInsertEntries( const <a href="qvaluelist.html">QValueList</a><QUrlInfo> &info ) { QValueList<QUrlInfo>::ConstIterator it; <a name="x1046"></a><a name="x1045"></a> for( it = info.<a href="qvaluelist.html#begin">begin</a>(); it != info.<a href="qvaluelist.html#end">end</a>(); ++it ) { if ( (*it).name() != ".." && (*it).name() != "." && (*it).name()[ 0 ] == '.' ) continue; FtpViewItem *item = new FtpViewItem( this, (*it) ); if ( (*it).isDir() ) <a name="x1041"></a> item-><a href="qlistviewitem.html#setSelectable">setSelectable</a>( FALSE ); } } void <a name="f333"></a>FtpView::slotSelected( <a href="qlistviewitem.html">QListViewItem</a> *item ) { if ( !item ) return; FtpViewItem *i = (FtpViewItem*)item; if ( i->entryInfo().isDir() ) emit itemSelected( i->entryInfo() ); } QValueList<QUrlInfo> <a name="f334"></a>FtpView::selectedItems() const { <a href="qvaluelist.html">QValueList</a><QUrlInfo> lst; <a href="qlistviewitemiterator.html">QListViewItemIterator</a> it( (QListView*)this ); <a name="x1043"></a> for ( ; it.<a href="qlistviewitemiterator.html#current">current</a>(); ++it ) { if ( it.<a href="qlistviewitemiterator.html#current">current</a>()->isSelected() ) { lst << ( (FtpViewItem*)it.<a href="qlistviewitemiterator.html#current">current</a>() )->entryInfo(); } } return lst; } </pre> <p> <hr> <p> Main (main.cpp): <p> <pre>/**************************************************************************** ** $Id: qt/main.cpp 3.0.2 edited Nov 6 19:46 $ ** ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved. ** ** This file is part of an example program for Qt. This example ** program may be used, distributed and modified without limitation. ** *****************************************************************************/ #include <<a href="qapplication-h.html">qapplication.h</a>> #include <<a href="qnetwork-h.html">qnetwork.h</a>> #include <<a href="qsplitter-h.html">qsplitter.h</a>> #include "ftpmainwindow.h" int main( int argc, char **argv ) { <a href="qapplication.html">QApplication</a> a( argc, argv ); // call this to register the built-in network protocols, e.g. FTP // and HTTP. <a href="qurloperator.html#qInitNetworkProtocols">qInitNetworkProtocols</a>(); FtpMainWindow m; a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &m ); <a href="qvaluelist.html">QValueList</a><int> sizes; sizes << 300 << 70 << 300; m.mainSplitter()->setSizes( sizes ); m.<a href="qwidget.html#resize">resize</a>( 800, 600 ); m.<a href="qwidget.html#show">show</a>(); return a.<a href="qapplication.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 © 2001 <a href="http://www.trolltech.com">Trolltech</a><td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a> <td align=right><div align=right>Qt version 3.0.2</div> </table></div></address></body> </html>