📄 q3networkprotocol.cpp
字号:
d->opInProgress = op; d->operationQueue.dequeue(); processOperation( op );}/*! Returns the Q3UrlOperator on which the protocol works.*/Q3UrlOperator *Q3NetworkProtocol::url() const{ return d->url;}/*! Returns the operation, which is being processed, or 0 of no operation is being processed at the moment.*/Q3NetworkOperation *Q3NetworkProtocol::operationInProgress() const{ return d->opInProgress;}/*! Clears the operation queue.*/void Q3NetworkProtocol::clearOperationQueue(){ d->operationQueue.dequeue(); d->operationQueue.setAutoDelete( true ); d->operationQueue.clear();}/*! Stops the current operation that is being processed and clears all waiting operations.*/void Q3NetworkProtocol::stop(){ Q3NetworkOperation *op = d->opInProgress; clearOperationQueue(); if ( op ) { op->setState( StStopped ); op->setProtocolDetail( tr( "Operation stopped by the user" ) ); emit finished( op ); setUrl( 0 ); op->free(); }}/*! Because it's sometimes hard to take care of removing network protocol instances, Q3NetworkProtocol provides an auto-delete mechanism. If you set \a b to true, the network protocol instance is removed after it has been inactive for \a i milliseconds (i.e. \a i milliseconds after the last operation has been processed). If you set \a b to false the auto-delete mechanism is switched off. If you switch on auto-delete, the Q3NetworkProtocol also deletes its Q3UrlOperator.*/void Q3NetworkProtocol::setAutoDelete( bool b, int i ){ d->autoDelete = b; d->removeInterval = i;}/*! Returns true if auto-deleting is enabled; otherwise returns false. \sa Q3NetworkProtocol::setAutoDelete()*/bool Q3NetworkProtocol::autoDelete() const{ return d->autoDelete;}/*! \internal*/void Q3NetworkProtocol::removeMe(){ if ( d->autoDelete ) {#ifdef Q3NETWORKPROTOCOL_DEBUG qDebug( "Q3NetworkOperation: autodelete of Q3NetworkProtocol %p", this );#endif delete d->url; // destructor deletes the network protocol }}void Q3NetworkProtocol::emitNewChildren( const QUrlInfo &i, Q3NetworkOperation *op ){ Q3ValueList<QUrlInfo> lst; lst << i; emit newChildren( lst, op );}class Q3NetworkOperationPrivate{public: Q3NetworkProtocol::Operation operation; Q3NetworkProtocol::State state; QMap<int, QString> args; QMap<int, QByteArray> rawArgs; QString protocolDetail; int errorCode; QTimer *deleteTimer;};/*! \class Q3NetworkOperation \brief The Q3NetworkOperation class provides common operations for network protocols. \compat An object is created to describe the operation and the current state for each operation that a network protocol should process. \sa Q3NetworkProtocol*//*! Constructs a network operation object. \a operation is the type of the operation, and \a arg0, \a arg1 and \a arg2 are the first three arguments of the operation. The state is initialized to Q3NetworkProtocol::StWaiting. \sa Q3NetworkProtocol::Operation Q3NetworkProtocol::State*/Q3NetworkOperation::Q3NetworkOperation( Q3NetworkProtocol::Operation operation, const QString &arg0, const QString &arg1, const QString &arg2 ){ d = new Q3NetworkOperationPrivate; d->deleteTimer = new QTimer( this ); connect( d->deleteTimer, SIGNAL(timeout()), this, SLOT(deleteMe()) ); d->operation = operation; d->state = Q3NetworkProtocol::StWaiting; d->args[ 0 ] = arg0; d->args[ 1 ] = arg1; d->args[ 2 ] = arg2; d->rawArgs[ 0 ] = QByteArray( 0 ); d->rawArgs[ 1 ] = QByteArray( 0 ); d->rawArgs[ 2 ] = QByteArray( 0 ); d->protocolDetail.clear(); d->errorCode = (int)Q3NetworkProtocol::NoError;}/*! Constructs a network operation object. \a operation is the type of the operation, and \a arg0, \a arg1 and \a arg2 are the first three raw data arguments of the operation. The state is initialized to Q3NetworkProtocol::StWaiting. \sa Q3NetworkProtocol::Operation Q3NetworkProtocol::State*/Q3NetworkOperation::Q3NetworkOperation( Q3NetworkProtocol::Operation operation, const QByteArray &arg0, const QByteArray &arg1, const QByteArray &arg2 ){ d = new Q3NetworkOperationPrivate; d->deleteTimer = new QTimer( this ); connect( d->deleteTimer, SIGNAL(timeout()), this, SLOT(deleteMe()) ); d->operation = operation; d->state = Q3NetworkProtocol::StWaiting; d->args[ 0 ].clear(); d->args[ 1 ].clear(); d->args[ 2 ].clear(); d->rawArgs[ 0 ] = arg0; d->rawArgs[ 1 ] = arg1; d->rawArgs[ 2 ] = arg2; d->protocolDetail.clear(); d->errorCode = (int)Q3NetworkProtocol::NoError;}/*! Destructor.*/Q3NetworkOperation::~Q3NetworkOperation(){ delete d;}/*! Sets the \a state of the operation object. This should be done by the network protocol during processing; at the end it should be set to Q3NetworkProtocol::StDone or Q3NetworkProtocol::StFailed, depending on success or failure. \sa Q3NetworkProtocol::State*/void Q3NetworkOperation::setState( Q3NetworkProtocol::State state ){ if ( d->deleteTimer->isActive() ) { d->deleteTimer->stop(); d->deleteTimer->start( NETWORK_OP_DELAY ); } d->state = state;}/*! If the operation failed, the error message can be specified as \a detail.*/void Q3NetworkOperation::setProtocolDetail( const QString &detail ){ if ( d->deleteTimer->isActive() ) { d->deleteTimer->stop(); d->deleteTimer->start( NETWORK_OP_DELAY ); } d->protocolDetail = detail;}/*! Sets the error code to \a ec. If the operation failed, the protocol should set an error code to describe the error in more detail. If possible, one of the error codes defined in Q3NetworkProtocol should be used. \sa setProtocolDetail() Q3NetworkProtocol::Error*/void Q3NetworkOperation::setErrorCode( int ec ){ if ( d->deleteTimer->isActive() ) { d->deleteTimer->stop(); d->deleteTimer->start( NETWORK_OP_DELAY ); } d->errorCode = ec;}/*! Sets the network operation's \a{num}-th argument to \a arg.*/void Q3NetworkOperation::setArg( int num, const QString &arg ){ if ( d->deleteTimer->isActive() ) { d->deleteTimer->stop(); d->deleteTimer->start( NETWORK_OP_DELAY ); } d->args[ num ] = arg;}/*! Sets the network operation's \a{num}-th raw data argument to \a arg.*/void Q3NetworkOperation::setRawArg( int num, const QByteArray &arg ){ if ( d->deleteTimer->isActive() ) { d->deleteTimer->stop(); d->deleteTimer->start( NETWORK_OP_DELAY ); } d->rawArgs[ num ] = arg;}/*! Returns the type of the operation.*/Q3NetworkProtocol::Operation Q3NetworkOperation::operation() const{ if ( d->deleteTimer->isActive() ) { d->deleteTimer->stop(); d->deleteTimer->start( NETWORK_OP_DELAY ); } return d->operation;}/*! Returns the state of the operation. You can determine whether an operation is still waiting to be processed, is being processed, has been processed successfully, or failed.*/Q3NetworkProtocol::State Q3NetworkOperation::state() const{ if ( d->deleteTimer->isActive() ) { d->deleteTimer->stop(); d->deleteTimer->start( NETWORK_OP_DELAY ); } return d->state;}/*! Returns the operation's \a{num}-th argument. If this argument was not already set, an empty string is returned.*/QString Q3NetworkOperation::arg( int num ) const{ if ( d->deleteTimer->isActive() ) { d->deleteTimer->stop(); d->deleteTimer->start( NETWORK_OP_DELAY ); } return d->args[ num ];}/*! Returns the operation's \a{num}-th raw data argument. If this argument was not already set, an empty bytearray is returned.*/QByteArray Q3NetworkOperation::rawArg( int num ) const{ if ( d->deleteTimer->isActive() ) { d->deleteTimer->stop(); d->deleteTimer->start( NETWORK_OP_DELAY ); } return d->rawArgs[ num ];}/*! Returns a detailed error message for the last error. This must have been set using setProtocolDetail().*/QString Q3NetworkOperation::protocolDetail() const{ if ( d->deleteTimer->isActive() ) { d->deleteTimer->stop(); d->deleteTimer->start( NETWORK_OP_DELAY ); } return d->protocolDetail;}/*! Returns the error code for the last error that occurred.*/int Q3NetworkOperation::errorCode() const{ if ( d->deleteTimer->isActive() ) { d->deleteTimer->stop(); d->deleteTimer->start( NETWORK_OP_DELAY ); } return d->errorCode;}/*! \internal*/QByteArray& Q3NetworkOperation::raw( int num ) const{ if ( d->deleteTimer->isActive() ) { d->deleteTimer->stop(); d->deleteTimer->start( NETWORK_OP_DELAY ); } return d->rawArgs[ num ];}/*! Sets this object to delete itself when it hasn't been used for one second. Because Q3NetworkOperation pointers are passed around a lot the Q3NetworkProtocol generally does not have enough knowledge to delete these at the correct time. If a Q3NetworkProtocol doesn't need an operation any more it will call this function instead. Note: you should never need to call the method yourself.*/void Q3NetworkOperation::free(){ d->deleteTimer->start( NETWORK_OP_DELAY );}/*! \internal Internal slot for auto-deletion.*/void Q3NetworkOperation::deleteMe(){ delete this;}#include "moc_q3networkprotocol.cpp"#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -