📄 q3urloperator.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the Qt3Support module 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://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** 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 "q3urloperator.h"#ifndef QT_NO_NETWORKPROTOCOL#include "qurlinfo.h"#include "q3networkprotocol.h"#include "qmap.h"#include "qdir.h"#include "q3ptrdict.h"#include "qpointer.h"#include "q3valuelist.h"#include "qapplication.h"//#define Q3URLOPERATOR_DEBUGclass Q3UrlOperatorPrivate{public: Q3UrlOperatorPrivate() { oldOps.setAutoDelete( false ); networkProtocol = 0; nameFilter = QLatin1String("*"); currPut = 0; } ~Q3UrlOperatorPrivate() { delete networkProtocol; while ( oldOps.first() ) { oldOps.first()->free(); oldOps.removeFirst(); } } QMap<QString, QUrlInfo> entryMap; Q3NetworkProtocol *networkProtocol; QString nameFilter; QDir dir; // maps needed for copy/move operations Q3PtrDict<Q3NetworkOperation> getOpPutOpMap; Q3PtrDict<Q3NetworkProtocol> getOpPutProtMap; Q3PtrDict<Q3NetworkProtocol> getOpGetProtMap; Q3PtrDict<Q3NetworkOperation> getOpRemoveOpMap; QPointer<Q3NetworkProtocol> currPut; QStringList waitingCopies; QString waitingCopiesDest; bool waitingCopiesMove; Q3PtrList< Q3NetworkOperation > oldOps;};/*! \class Q3UrlOperator q3urloperator.h \brief The Q3UrlOperator class provides common operations on URLs. \compat \module network This class operates on hierarchical structures (such as filesystems) using URLs. Its API facilitates all the common operations: \table \header \i Operation \i Function \row \i List files \i \l listChildren() \row \i Make a directory \i \l mkdir() \row \i Remove a file \i \l remove() \row \i Rename a file \i \l rename() \row \i Get a file \i \l get() \row \i Put a file \i \l put() \row \i Copy a file \i \l copy() \endtable You can obtain additional information about the URL with isDir() and info(). If a directory is to be traversed using listChildren(), a name filter can be set with setNameFilter(). A Q3UrlOperator can be used like this, for example to download a file (and assuming that the FTP protocol is registered): \code Q3UrlOperator *op = new Q3UrlOperator(); op->copy( QString("ftp://ftp.trolltech.com/qt/source/qt-2.1.0.tar.gz"), "file:///tmp" ); \endcode If you want to be notified about success/failure, progress, etc., you can connect to Q3UrlOperator's signals, e.g. to start(), newChildren(), createdDirectory(), removed(), data(), dataTransferProgress(), startedNextCopy(), connectionStateChanged(), finished(), etc. A network operation can be stopped with stop(). The class uses the functionality of registered network protocols to perform these operations. Depending of the protocol of the URL, it uses an appropriate network protocol class for the operations. Each of the operation functions of Q3UrlOperator creates a Q3NetworkOperation object that describes the operation and puts it into the operation queue for the network protocol used. If no suitable protocol could be found (because no implementation of the necessary network protocol is registered), the URL operator emits errors. Not every protocol supports every operation, but error handling deals with this problem. To register the available network protocols, use the qInitNetworkProtocols() function. The protocols currently supported are: \list \i \link Q3Ftp FTP\endlink, \i \link Q3Http HTTP\endlink, \i \link Q3LocalFs local file system\endlink. \endlist \sa Q3NetworkProtocol, Q3NetworkOperation*//*! \fn void Q3UrlOperator::newChildren( const Q3ValueList<QUrlInfo> &i, Q3NetworkOperation *op ) This signal is emitted after listChildren() was called and new children (i.e. files) have been read from a list of files. \a i holds the information about the new files. \a op is a pointer to the operation object which contains all the information about the operation, including the state. \sa Q3NetworkOperation, Q3NetworkProtocol*//*! \fn void Q3UrlOperator::finished( Q3NetworkOperation *op ) This signal is emitted when an operation of some sort finishes, whether with success or failure. \a op is a pointer to the operation object, which contains all the information, including the state, of the operation which has been finished. Check the state and error code of the operation object to see whether or not the operation was successful. \sa Q3NetworkOperation, Q3NetworkProtocol*//*! \fn void Q3UrlOperator::start( Q3NetworkOperation *op ) Some operations (such as listChildren()) emit this signal when they start processing the operation. \a op is a pointer to the operation object which contains all the information about the operation, including the state. \sa Q3NetworkOperation, Q3NetworkProtocol*//*! \fn void Q3UrlOperator::createdDirectory( const QUrlInfo &i, Q3NetworkOperation *op ) This signal is emitted when mkdir() succeeds and the directory has been created. \a i holds the information about the new directory. \a op is a pointer to the operation object, which contains all the information about the operation, including the state. \c op->arg(0) holds the new directory's name. \sa Q3NetworkOperation, Q3NetworkProtocol*//*! \fn void Q3UrlOperator::removed( Q3NetworkOperation *op ) This signal is emitted when remove() has been successful and the file has been removed. \a op is a pointer to the operation object which contains all the information about the operation, including the state. \c op->arg(0) holds the name of the file that was removed. \sa Q3NetworkOperation, Q3NetworkProtocol*//*! \fn void Q3UrlOperator::itemChanged( Q3NetworkOperation *op ) This signal is emitted whenever a file which is a child of the URL has been changed, for example by successfully calling rename(). \a op is a pointer to the operation object which contains all the information about the operation, including the state. \c op->arg(0) holds the original file name and \c op->arg(1) holds the new file name (if it was changed). \sa Q3NetworkOperation, Q3NetworkProtocol*//*! \fn void Q3UrlOperator::data( const QByteArray &data, Q3NetworkOperation *op ) This signal is emitted when new \a data has been received after calling get() or put(). \a op is a pointer to the operation object which contains all the information about the operation, including the state. \c op->arg(0) holds the name of the file whose data is retrieved and op->rawArg(1) holds the (raw) data. \sa Q3NetworkOperation, Q3NetworkProtocol*//*! \fn void Q3UrlOperator::dataTransferProgress( int bytesDone, int bytesTotal, Q3NetworkOperation *op ) This signal is emitted during data transfer (using put() or get()). \a bytesDone specifies how many bytes of \a bytesTotal have been transferred. More information about the operation is stored in \a op, a pointer to the network operation that is processed. \a bytesTotal may be -1, which means that the total number of bytes is not known. \sa Q3NetworkOperation, Q3NetworkProtocol*//*! \fn void Q3UrlOperator::startedNextCopy( const Q3PtrList<Q3NetworkOperation> &lst ) This signal is emitted if copy() starts a new copy operation. \a lst contains all Q3NetworkOperations related to this copy operation. \sa copy()*//*! \fn void Q3UrlOperator::connectionStateChanged( int state, const QString &data ) This signal is emitted whenever the URL operator's connection state changes. \a state describes the new state, which is a \l{Q3NetworkProtocol::ConnectionState} value. \a data is a string that describes the change of the connection. This can be used to display a message to the user.*//*! Constructs a Q3UrlOperator with an empty (i.e. invalid) URL.*/Q3UrlOperator::Q3UrlOperator() : Q3Url(){#ifdef Q3URLOPERATOR_DEBUG qDebug( "Q3UrlOperator: cstr 1" );#endif d = new Q3UrlOperatorPrivate;}/*! Constructs a Q3UrlOperator using \a url and parses this string. If you pass strings like "/home/qt" the "file" protocol is assumed.*/Q3UrlOperator::Q3UrlOperator( const QString &url ) : Q3Url( url ){#ifdef Q3URLOPERATOR_DEBUG qDebug( "Q3UrlOperator: cstr 2" );#endif d = new Q3UrlOperatorPrivate; getNetworkProtocol();}/*! Constructs a copy of \a url.*/Q3UrlOperator::Q3UrlOperator( const Q3UrlOperator& url ) : QObject(), Q3Url( url ){#ifdef Q3URLOPERATOR_DEBUG qDebug( "Q3UrlOperator: cstr 3" );#endif d = new Q3UrlOperatorPrivate; *d = *url.d; d->networkProtocol = 0; getNetworkProtocol(); d->nameFilter = QLatin1String("*"); d->currPut = 0;}/*! Constructs a Q3UrlOperator. The URL on which this Q3UrlOperator operates is constructed out of the arguments \a url, \a relUrl and \a checkSlash: see the corresponding Q3Url constructor for an explanation of these arguments.*/Q3UrlOperator::Q3UrlOperator( const Q3UrlOperator& url, const QString& relUrl, bool checkSlash ) : Q3Url( url, relUrl, checkSlash ){#ifdef Q3URLOPERATOR_DEBUG qDebug( "Q3UrlOperator: cstr 4" );#endif d = new Q3UrlOperatorPrivate; if ( relUrl == QLatin1String(".") ) *d = *url.d; d->networkProtocol = 0; getNetworkProtocol(); d->currPut = 0;}/*! Destructor.*/Q3UrlOperator::~Q3UrlOperator(){#ifdef Q3URLOPERATOR_DEBUG qDebug( "Q3UrlOperator: dstr" );#endif delete d;}/*! This private function is used by the simple operation functions, i.e. listChildren(), mkdir(), remove(), rename(), get() and put(), to really start the operation. \a op is a pointer to the network operation that should be started. Returns \a op on success; otherwise returns 0.*/const Q3NetworkOperation *Q3UrlOperator::startOperation( Q3NetworkOperation *op ){ if ( !d->networkProtocol ) getNetworkProtocol(); if ( d->networkProtocol && (d->networkProtocol->supportedOperations()&op->operation()) ) { d->networkProtocol->addOperation( op ); if ( op->operation() == Q3NetworkProtocol::OpListChildren ) clearEntries(); return op; } // error QString msg; if ( !d->networkProtocol ) { msg = tr( "The protocol `%1' is not supported" ).arg( protocol() ); } else { switch ( op->operation() ) { case Q3NetworkProtocol::OpListChildren: msg = tr( "The protocol `%1' does not support listing directories" ).arg( protocol() ); break; case Q3NetworkProtocol::OpMkDir: msg = tr( "The protocol `%1' does not support creating new directories" ).arg( protocol() ); break; case Q3NetworkProtocol::OpRemove: msg = tr( "The protocol `%1' does not support removing files or directories" ).arg( protocol() ); break; case Q3NetworkProtocol::OpRename: msg = tr( "The protocol `%1' does not support renaming files or directories" ).arg( protocol() ); break; case Q3NetworkProtocol::OpGet: msg = tr( "The protocol `%1' does not support getting files" ).arg( protocol() ); break; case Q3NetworkProtocol::OpPut: msg = tr( "The protocol `%1' does not support putting files" ).arg( protocol() ); break; default: // this should never happen break; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -