📄 q3networkprotocol.cpp
字号:
url(), SIGNAL(createdDirectory(QUrlInfo,Q3NetworkOperation*)) ); connect( this, SIGNAL(removed(Q3NetworkOperation*)), url(), SIGNAL(removed(Q3NetworkOperation*)) ); connect( this, SIGNAL(itemChanged(Q3NetworkOperation*)), url(), SIGNAL(itemChanged(Q3NetworkOperation*)) ); connect( this, SIGNAL(dataTransferProgress(int,int,Q3NetworkOperation*)), url(), SIGNAL(dataTransferProgress(int,int,Q3NetworkOperation*)) ); connect( this, SIGNAL(connectionStateChanged(int,QString)), url(), SIGNAL(connectionStateChanged(int,QString)) ); } connect( this, SIGNAL(finished(Q3NetworkOperation*)), this, SLOT(processNextOperation(Q3NetworkOperation*)) ); connect( this, SIGNAL(newChild(QUrlInfo,Q3NetworkOperation*)), this, SLOT(emitNewChildren(QUrlInfo,Q3NetworkOperation*)) );}/*! Destructor.*/Q3NetworkProtocol::~Q3NetworkProtocol(){ delete d;}/*! Sets the Q3UrlOperator, on which the protocol works, to \a u. \sa Q3UrlOperator*/void Q3NetworkProtocol::setUrl( Q3UrlOperator *u ){ if ( url() ) { disconnect( this, SIGNAL(data(QByteArray,Q3NetworkOperation*)), url(), SIGNAL(data(QByteArray,Q3NetworkOperation*)) ); disconnect( this, SIGNAL(finished(Q3NetworkOperation*)), url(), SIGNAL(finished(Q3NetworkOperation*)) ); disconnect( this, SIGNAL(start(Q3NetworkOperation*)), url(), SIGNAL(start(Q3NetworkOperation*)) ); disconnect( this, SIGNAL(newChildren(Q3ValueList<QUrlInfo>,Q3NetworkOperation*)), url(), SIGNAL(newChildren(Q3ValueList<QUrlInfo>,Q3NetworkOperation*)) ); disconnect( this, SIGNAL(newChildren(Q3ValueList<QUrlInfo>,Q3NetworkOperation*)), url(), SLOT(addEntry(Q3ValueList<QUrlInfo>)) ); disconnect( this, SIGNAL(createdDirectory(QUrlInfo,Q3NetworkOperation*)), url(), SIGNAL(createdDirectory(QUrlInfo,Q3NetworkOperation*)) ); disconnect( this, SIGNAL(removed(Q3NetworkOperation*)), url(), SIGNAL(removed(Q3NetworkOperation*)) ); disconnect( this, SIGNAL(itemChanged(Q3NetworkOperation*)), url(), SIGNAL(itemChanged(Q3NetworkOperation*)) ); disconnect( this, SIGNAL(dataTransferProgress(int,int,Q3NetworkOperation*)), url(), SIGNAL(dataTransferProgress(int,int,Q3NetworkOperation*)) ); disconnect( this, SIGNAL(connectionStateChanged(int,QString)), url(), SIGNAL(connectionStateChanged(int,QString)) ); } // ### if autoDelete is true, we should delete the Q3UrlOperator (something // like below; but that is not possible since it would delete this, too). //if ( d->autoDelete && (d->url!=u) ) { // delete d->url; // destructor deletes the network protocol //} d->url = u; 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*)), url(), SIGNAL(createdDirectory(QUrlInfo,Q3NetworkOperation*)) ); connect( this, SIGNAL(removed(Q3NetworkOperation*)), url(), SIGNAL(removed(Q3NetworkOperation*)) ); connect( this, SIGNAL(itemChanged(Q3NetworkOperation*)), url(), SIGNAL(itemChanged(Q3NetworkOperation*)) ); connect( this, SIGNAL(dataTransferProgress(int,int,Q3NetworkOperation*)), url(), SIGNAL(dataTransferProgress(int,int,Q3NetworkOperation*)) ); connect( this, SIGNAL(connectionStateChanged(int,QString)), url(), SIGNAL(connectionStateChanged(int,QString)) ); } if ( !d->opInProgress && !d->operationQueue.isEmpty() ) d->opStartTimer->start( 0, true );}/*! For processing operations the network protocol base class calls this method quite often. This should be reimplemented by new network protocols. It should return true if the connection is OK (open); otherwise it should return false. If the connection is not open the protocol should open it. If the connection can't be opened (e.g. because you already tried but the host couldn't be found), set the state of \a op to Q3NetworkProtocol::StFailed and emit the finished() signal with this Q3NetworkOperation as argument. \a op is the operation that needs an open connection.*/bool Q3NetworkProtocol::checkConnection( Q3NetworkOperation * ){ return true;}/*! Returns an int that is OR'd together using the enum values of \l{Q3NetworkProtocol::Operation}, which describes which operations are supported by the network protocol. Should be reimplemented by new network protocols.*/int Q3NetworkProtocol::supportedOperations() const{ return 0;}/*! Adds the operation \a op to the operation queue. The operation will be processed as soon as possible. This method returns immediately.*/void Q3NetworkProtocol::addOperation( Q3NetworkOperation *op ){#ifdef Q3NETWORKPROTOCOL_DEBUG qDebug( "Q3NetworkOperation: addOperation: %p %d", op, op->operation() );#endif d->operationQueue.enqueue( op ); if ( !d->opInProgress ) d->opStartTimer->start( 0, true );}/*! Static method to register a network protocol for Qt. For example, if you have an implementation of NNTP (called Nntp) which is derived from Q3NetworkProtocol, call: \code Q3NetworkProtocol::registerNetworkProtocol( "nntp", new Q3NetworkProtocolFactory<Nntp> ); \endcode after which your implementation is registered for future nntp operations. The name of the protocol is given in \a protocol and a pointer to the protocol factory is given in \a protocolFactory.*/void Q3NetworkProtocol::registerNetworkProtocol( const QString &protocol, Q3NetworkProtocolFactoryBase *protocolFactory ){ if ( !q3networkProtocolRegister ) { q3networkProtocolRegister = new Q3NetworkProtocolDict; Q3NetworkProtocol::registerNetworkProtocol( QLatin1String("file"), new Q3NetworkProtocolFactory< Q3LocalFs > ); } q3networkProtocolRegister->insert( protocol, protocolFactory );}/*! Static method to get a new instance of the network protocol \a protocol. For example, if you need to do some FTP operations, do the following: \code Q3Ftp *ftp = Q3NetworkProtocol::getNetworkProtocol( "ftp" ); \endcode This returns a pointer to a new instance of an ftp implementation or null if no protocol for ftp was registered. The ownership of the pointer is transferred to you, so you must delete it if you don't need it anymore. Normally you should not work directly with network protocols, so you will not need to call this method yourself. Instead, use Q3UrlOperator, which makes working with network protocols much more convenient. \sa Q3UrlOperator*/Q3NetworkProtocol *Q3NetworkProtocol::getNetworkProtocol( const QString &protocol ){ if ( !q3networkProtocolRegister ) { q3networkProtocolRegister = new Q3NetworkProtocolDict; Q3NetworkProtocol::registerNetworkProtocol( QLatin1String("file"), new Q3NetworkProtocolFactory< Q3LocalFs > ); } if ( protocol.isNull() ) return 0; Q3NetworkProtocolFactoryBase *factory = q3networkProtocolRegister->find( protocol ); if ( factory ) return factory->createObject(); return 0;}/*! Returns true if the only protocol registered is for working on the local filesystem; returns false if other network protocols are also registered.*/bool Q3NetworkProtocol::hasOnlyLocalFileSystem(){ if ( !q3networkProtocolRegister ) return false; Q3DictIterator< Q3NetworkProtocolFactoryBase > it( *q3networkProtocolRegister ); for ( ; it.current(); ++it ) if ( it.currentKey() != QLatin1String("file") ) return false; return true;}/*! \internal Starts processing network operations.*/void Q3NetworkProtocol::startOps(){#ifdef Q3NETWORKPROTOCOL_DEBUG qDebug( "Q3NetworkOperation: start processing operations" );#endif processNextOperation( 0 );}/*! \internal Processes the operation \a op. It calls the corresponding operation[something]( Q3NetworkOperation * ) methods.*/void Q3NetworkProtocol::processOperation( Q3NetworkOperation *op ){ if ( !op ) return; switch ( op->operation() ) { case OpListChildren: operationListChildren( op ); break; case OpMkDir: operationMkDir( op ); break; case OpRemove: operationRemove( op ); break; case OpRename: operationRename( op ); break; case OpGet: operationGet( op ); break; case OpPut: operationPut( op ); break; }}/*! When implementing a new network protocol, this method should be reimplemented if the protocol supports listing children (files); this method should then process this Q3NetworkOperation. \a op is the pointer to the operation object which contains all the information on the operation that has finished, including the state, etc.*/void Q3NetworkProtocol::operationListChildren( Q3NetworkOperation * ){}/*! When implementing a new network protocol, this method should be reimplemented if the protocol supports making directories; this method should then process this Q3NetworkOperation. \a op is the pointer to the operation object which contains all the information on the operation that has finished, including the state, etc.*/void Q3NetworkProtocol::operationMkDir( Q3NetworkOperation * ){}/*! When implementing a new network protocol, this method should be reimplemented if the protocol supports removing children (files); this method should then process this Q3NetworkOperation. \a op is the pointer to the operation object which contains all the information on the operation that has finished, including the state, etc.*/void Q3NetworkProtocol::operationRemove( Q3NetworkOperation * ){}/*! When implementing a new network protocol, this method should be reimplemented if the protocol supports renaming children (files); this method should then process this Q3NetworkOperation. \a op is the pointer to the operation object which contains all the information on the operation that has finished, including the state, etc.*/void Q3NetworkProtocol::operationRename( Q3NetworkOperation * ){}/*! When implementing a new network protocol, this method should be reimplemented if the protocol supports getting data; this method should then process the Q3NetworkOperation. \a op is the pointer to the operation object which contains all the information on the operation that has finished, including the state, etc.*/void Q3NetworkProtocol::operationGet( Q3NetworkOperation * ){}/*! When implementing a new network protocol, this method should be reimplemented if the protocol supports putting (uploading) data; this method should then process the Q3NetworkOperation. \a op is the pointer to the operation object which contains all the information on the operation that has finished, including the state, etc.*/void Q3NetworkProtocol::operationPut( Q3NetworkOperation * ){}/*! \internal*/void Q3NetworkProtocol::operationPutChunk( Q3NetworkOperation * ){}/*! \internal Handles operations. Deletes the previous operation object and tries to process the next operation. It also checks the connection state and only processes the next operation, if the connection of the protocol is open. Otherwise it waits until the protocol opens the connection.*/void Q3NetworkProtocol::processNextOperation( Q3NetworkOperation *old ){#ifdef Q3NETWORKPROTOCOL_DEBUG qDebug( "Q3NetworkOperation: process next operation, old: %p", old );#endif d->removeTimer->stop(); if ( old ) d->oldOps.append( old ); if ( d->opInProgress && d->opInProgress!=old ) d->oldOps.append( d->opInProgress ); if ( d->operationQueue.isEmpty() ) { d->opInProgress = 0; if ( d->autoDelete ) d->removeTimer->start( d->removeInterval, true ); return; } Q3NetworkOperation *op = d->operationQueue.head(); d->opInProgress = op; if ( !checkConnection( op ) ) { if ( op->state() != Q3NetworkProtocol::StFailed ) { d->opStartTimer->start( 0, true ); } else { d->operationQueue.dequeue(); clearOperationQueue(); emit finished( op ); } return; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -