📄 q3networkprotocol.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 "q3networkprotocol.h"#ifndef QT_NO_NETWORKPROTOCOL#include "q3localfs.h"#include "q3urloperator.h"#include "qtimer.h"#include "qmap.h"#include "q3ptrqueue.h"#include "q3valuelist.h"#include "qurlinfo.h"//#define Q3NETWORKPROTOCOL_DEBUG#define NETWORK_OP_DELAY 1000extern Q_COMPAT_EXPORT Q3NetworkProtocolDict *q3networkProtocolRegister;Q3NetworkProtocolDict *q3networkProtocolRegister = 0;class Q3NetworkProtocolPrivate{public: Q3NetworkProtocolPrivate( Q3NetworkProtocol *p ) { url = 0; opInProgress = 0; opStartTimer = new QTimer( p ); removeTimer = new QTimer( p ); operationQueue.setAutoDelete( false ); autoDelete = false; removeInterval = 10000; oldOps.setAutoDelete( false ); } ~Q3NetworkProtocolPrivate() { removeTimer->stop(); if ( opInProgress ) { if ( opInProgress == operationQueue.head() ) operationQueue.dequeue(); opInProgress->free(); } while ( operationQueue.head() ) { operationQueue.head()->free(); operationQueue.dequeue(); } while ( oldOps.first() ) { oldOps.first()->free(); oldOps.removeFirst(); } delete opStartTimer; } Q3UrlOperator *url; Q3PtrQueue< Q3NetworkOperation > operationQueue; Q3NetworkOperation *opInProgress; QTimer *opStartTimer, *removeTimer; int removeInterval; bool autoDelete; Q3PtrList< Q3NetworkOperation > oldOps;};/*! \class Q3NetworkProtocol q3networkprotocol.h \brief The Q3NetworkProtocol class provides a common API for network protocols. \compat This is a base class which should be used for network protocols implementations that can then be used in Qt (e.g. in the file dialog) together with the Q3UrlOperator. The easiest way to implement a new network protocol is to reimplement the operation*() methods, e.g. operationGet(), etc. Only the supported operations should be reimplemented. To specify which operations are supported, also reimplement supportedOperations() and return an int that is OR'd together using the supported operations from the \l Q3NetworkProtocol::Operation enum. When you implement a network protocol this way, it is important to emit the correct signals. Also, always emit the finished() signal when an operation is done (on success \e and on failure). Qt relies on correctly emitted finished() signals.*//*! \fn void Q3NetworkProtocol::newChildren( const Q3ValueList<QUrlInfo> &i, Q3NetworkOperation *op ) This signal is emitted after listChildren() was called and new children (files) have been read from the list of files. \a i holds the information about the new children. \a op is the pointer to the operation object which contains all the information about the operation, including the state, etc. When a protocol emits this signal, Q3NetworkProtocol is smart enough to let the Q3UrlOperator, which is used by the network protocol, emit its corresponding signal. When implementing your own network protocol and reading children, you usually don't read one child at once, but rather a list of them. That's why this signal takes a list of QUrlInfo objects. If you prefer to read just one child at a time you can use the convenience signal newChild(), which takes a single QUrlInfo object.*//*! \fn void Q3NetworkProtocol::newChild( const QUrlInfo &i, Q3NetworkOperation *op ) This signal is emitted if a new child (file) has been read. Q3NetworkProtocol automatically connects it to a slot which creates a list of QUrlInfo objects (with just one QUrlInfo \a i) and emits the newChildren() signal with this list. \a op is the pointer to the operation object which contains all the information about the operation that has finished, including the state, etc. This is just a convenience signal useful for implementing your own network protocol. In all other cases connect to the newChildren() signal with its list of QUrlInfo objects.*//*! \fn void Q3NetworkProtocol::finished( Q3NetworkOperation *op ) This signal is emitted when an operation finishes. This signal is always emitted, for both success and failure. \a op is the pointer to the operation object which contains all the information about the operation, including the state, etc. Check the state and error code of the operation object to determine whether or not the operation was successful. When a protocol emits this signal, Q3NetworkProtocol is smart enough to let the Q3UrlOperator, which is used by the network protocol, emit its corresponding signal.*//*! \fn void Q3NetworkProtocol::start( Q3NetworkOperation *op ) Some operations (such as listChildren()) emit this signal when they start processing the operation. \a op is the pointer to the operation object which contains all the information about the operation, including the state, etc. When a protocol emits this signal, Q3NetworkProtocol is smart enough to let the Q3UrlOperator, which is used by the network protocol, emit its corresponding signal.*//*! \fn void Q3NetworkProtocol::createdDirectory( const QUrlInfo &i, Q3NetworkOperation *op ) This signal is emitted when mkdir() has been successful and the directory has been created. \a i holds the information about the new directory. \a op is the pointer to the operation object which contains all the information about the operation, including the state, etc. Using op->arg( 0 ), you can get the file name of the new directory. When a protocol emits this signal, Q3NetworkProtocol is smart enough to let the Q3UrlOperator, which is used by the network protocol, emit its corresponding signal.*//*! \fn void Q3NetworkProtocol::removed( Q3NetworkOperation *op ) This signal is emitted when remove() has been succesiisful and the file has been removed. \a op holds the file name of the removed file in the first argument, accessible with op->arg( 0 ). \a op is the pointer to the operation object which contains all the information about the operation, including the state, etc. When a protocol emits this signal, Q3NetworkProtocol is smart enough to let the Q3UrlOperator, which is used by the network protocol, emit its corresponding signal.*//*! \fn void Q3NetworkProtocol::itemChanged( Q3NetworkOperation *op ) This signal is emitted whenever a file which is a child of this URL has been changed, e.g. by successfully calling rename(). \a op holds the original and the new file names in the first and second arguments, accessible with op->arg( 0 ) and op->arg( 1 ) respectively. \a op is the pointer to the operation object which contains all the information about the operation, including the state, etc. When a protocol emits this signal, Q3NetworkProtocol is smart enough to let the Q3UrlOperator, which is used by the network protocol, emit its corresponding signal.*//*! \fn void Q3NetworkProtocol::data( const QByteArray &data, Q3NetworkOperation *op ) This signal is emitted when new \a data has been received after calling get() or put(). \a op holds the name of the file from which data is retrieved or uploaded in its first argument, and the (raw) data in its second argument. You can get them with op->arg( 0 ) and op->rawArg( 1 ). \a op is the pointer to the operation object, which contains all the information about the operation, including the state, etc. When a protocol emits this signal, Q3NetworkProtocol is smart enough to let the Q3UrlOperator (which is used by the network protocol) emit its corresponding signal.*//*! \fn void Q3NetworkProtocol::dataTransferProgress( int bytesDone, int bytesTotal, Q3NetworkOperation *op ) This signal is emitted during the transfer of data (using put() or get()). \a bytesDone is how many bytes of \a bytesTotal have been transferred. \a bytesTotal may be -1, which means that the total number of bytes is not known. \a op is the pointer to the operation object which contains all the information about the operation, including the state, etc. When a protocol emits this signal, Q3NetworkProtocol is smart enough to let the Q3UrlOperator, which is used by the network protocol, emit its corresponding signal.*//*! \fn void Q3NetworkProtocol::connectionStateChanged( int state, const QString &data ) This signal is emitted whenever the state of the connection of the network protocol is changed. \a state describes the new state, which is one of, \c ConHostFound, \c ConConnected or \c ConClosed. \a data is a message text.*//*! \enum Q3NetworkProtocol::State This enum contains the state that a Q3NetworkOperation can have. \value StWaiting The operation is in the Q3NetworkProtocol's queue waiting to be prcessed. \value StInProgress The operation is being processed. \value StDone The operation has been processed successfully. \value StFailed The operation has been processed but an error occurred. \value StStopped The operation has been processed but has been stopped before it finished, and is waiting to be processed.*//*! \enum Q3NetworkProtocol::Operation This enum lists the possible operations that a network protocol can support. supportedOperations() returns an int of these that is OR'd together. Also, the type() of a Q3NetworkOperation is always one of these values. \value OpListChildren List the children of a URL, e.g. of a directory. \value OpMkDir Create a directory. \value OpRemove Remove a child (e.g. a file). \value OpRename Rename a child (e.g. a file). \value OpGet Get data from a location. \value OpPut Put data to a location. \omitvalue OpMkdir*//*! \enum Q3NetworkProtocol::ConnectionState When the connection state of a network protocol changes it emits the signal connectionStateChanged(). The first argument is one of the following values: \value ConHostFound Host has been found. \value ConConnected Connection to the host has been established. \value ConClosed Connection has been closed.*//*! \enum Q3NetworkProtocol::Error When an operation fails (finishes unsuccessfully), the Q3NetworkOperation of the operation returns an error code which has one of the following values: \value NoError No error occurred. \value ErrValid The URL you are operating on is not valid. \value ErrUnknownProtocol There is no protocol implementation available for the protocol of the URL you are operating on (e.g. if the protocol is http and no http implementation has been registered). \value ErrUnsupported The operation is not supported by the protocol. \value ErrParse The URL could not be parsed correctly. \value ErrLoginIncorrect You needed to login but the username or password is wrong. \value ErrHostNotFound The specified host (in the URL) couldn't be found. \value ErrListChildren An error occurred while listing the children (files). \value ErrMkDir An error occurred when creating a directory. \value ErrRemove An error occurred when removing a child (file). \value ErrRename An error occurred when renaming a child (file). \value ErrGet An error occurred while getting (retrieving) data. \value ErrPut An error occurred while putting (uploading) data. \value ErrFileNotExisting A file which is needed by the operation doesn't exist. \value ErrPermissionDenied Permission for doing the operation has been denied. \omitvalue ErrMkdir \omitvalue ErrListChlidren You should also use these error codes when implementing custom network protocols. If this is not possible, you can define your own error codes by using integer values that don't conflict with any of these values.*//*! Constructor of the network protocol base class. Does some initialization and connecting of signals and slots.*/Q3NetworkProtocol::Q3NetworkProtocol() : QObject(){ d = new Q3NetworkProtocolPrivate( this ); connect( d->opStartTimer, SIGNAL(timeout()), this, SLOT(startOps()) ); connect( d->removeTimer, SIGNAL(timeout()), this, SLOT(removeMe()) ); if ( url() ) { connect( this, SIGNAL(data(QByteArray,Q3NetworkOperation*)), url(), SIGNAL(data(QByteArray,Q3NetworkOperation*)) ); connect( this, SIGNAL(finished(Q3NetworkOperation*)), url(), SIGNAL(finished(Q3NetworkOperation*)) ); connect( this, SIGNAL(start(Q3NetworkOperation*)), url(), SIGNAL(start(Q3NetworkOperation*)) ); connect( this, SIGNAL(newChildren(Q3ValueList<QUrlInfo>,Q3NetworkOperation*)), url(), SIGNAL(newChildren(Q3ValueList<QUrlInfo>,Q3NetworkOperation*)) ); connect( this, SIGNAL(newChildren(Q3ValueList<QUrlInfo>,Q3NetworkOperation*)), url(), SLOT(addEntry(Q3ValueList<QUrlInfo>)) ); connect( this, SIGNAL(createdDirectory(QUrlInfo,Q3NetworkOperation*)),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -