📄 q3urloperator.cpp
字号:
op->setState( Q3NetworkProtocol::StFailed ); op->setProtocolDetail( msg ); op->setErrorCode( (int)Q3NetworkProtocol::ErrUnsupported ); emit finished( op ); deleteOperation( op ); return 0;}/*! Starts listing the children of this URL (e.g. the files in the directory). The start() signal is emitted before the first entry is listed and finished() is emitted after the last one. The newChildren() signal is emitted for each list of new entries. If an error occurs, the signal finished() is emitted, so be sure to check the state of the network operation pointer. Because the operation may not be executed immediately, a pointer to the Q3NetworkOperation object created by this function is returned. This object contains all the data about the operation and is used to refer to this operation later (e.g. in the signals that are emitted by the Q3UrlOperator). The return value can also be 0 if the operation object couldn't be created. The path of this Q3UrlOperator must to point to a directory (because the children of this directory will be listed), not to a file.*/const Q3NetworkOperation *Q3UrlOperator::listChildren(){ if ( !checkValid() ) return 0; Q3NetworkOperation *res = new Q3NetworkOperation( Q3NetworkProtocol::OpListChildren, QString(), QString(), QString() ); return startOperation( res );}/*! Tries to create a directory (child) with the name \a dirname. If it is successful, a newChildren() signal with the new child is emitted, and the createdDirectory() signal with the information about the new child is also emitted. The finished() signal (with success or failure) is emitted after the operation has been processed, so check the state of the network operation object to see whether or not the operation was successful. Because the operation will not be executed immediately, a pointer to the Q3NetworkOperation object created by this function is returned. This object contains all the data about the operation and is used to refer to this operation later (e.g. in the signals that are emitted by the Q3UrlOperator). The return value can also be 0 if the operation object couldn't be created. The path of this Q3UrlOperator must to point to a directory (not a file) because the new directory will be created in this path.*/const Q3NetworkOperation *Q3UrlOperator::mkdir( const QString &dirname ){ if ( !checkValid() ) return 0; Q3NetworkOperation *res = new Q3NetworkOperation( Q3NetworkProtocol::OpMkDir, dirname, QString(), QString() ); return startOperation( res );}/*! Tries to remove the file (child) \a filename. If it succeeds the removed() signal is emitted. finished() (with success or failure) is also emitted after the operation has been processed, so check the state of the network operation object to see whether or not the operation was successful. Because the operation will not be executed immediately, a pointer to the Q3NetworkOperation object created by this function is returned. This object contains all the data about the operation and is used to refer to this operation later (e.g. in the signals that are emitted by the Q3UrlOperator). The return value can also be 0 if the operation object couldn't be created. The path of this Q3UrlOperator must point to a directory; because if \a filename is relative, it will try to remove it in this directory.*/const Q3NetworkOperation *Q3UrlOperator::remove( const QString &filename ){ if ( !checkValid() ) return 0; Q3NetworkOperation *res = new Q3NetworkOperation( Q3NetworkProtocol::OpRemove, filename, QString(), QString() ); return startOperation( res );}/*! Tries to rename the file (child) called \a oldname to \a newname. If it succeeds, the itemChanged() signal is emitted. finished() (with success or failure) is also emitted after the operation has been processed, so check the state of the network operation object to see whether or not the operation was successful. Because the operation may not be executed immediately, a pointer to the Q3NetworkOperation object created by this function is returned. This object contains all the data about the operation and is used to refer to this operation later (e.g. in the signals that are emitted by the Q3UrlOperator). The return value can also be 0 if the operation object couldn't be created. This path of this Q3UrlOperator must to point to a directory because \a oldname and \a newname are handled relative to this directory.*/const Q3NetworkOperation *Q3UrlOperator::rename( const QString &oldname, const QString &newname ){ if ( !checkValid() ) return 0; Q3NetworkOperation *res = new Q3NetworkOperation( Q3NetworkProtocol::OpRename, oldname, newname, QString() ); return startOperation( res );}/*! Copies the file \a from to \a to. If \a move is true, the file is moved (copied and removed). \a from must point to a file and \a to must point to a directory (into which \a from is copied) unless \a toPath is set to false. If \a toPath is set to false then the \a to variable is assumed to be the absolute file path (destination file path + file name). The copying is done using the get() and put() operations. If you want to be notified about the progress of the operation, connect to the dataTransferProgress() signal. Bear in mind that the get() and put() operations emit this signal through the Q3UrlOperator. The number of transferred bytes and the total bytes that you receive as arguments in this signal do not relate to the the whole copy operation; they relate first to the get() and then to the put() operation. Always check what type of operation the signal comes from; this is given in the signal's last argument. At the end, finished() (with success or failure) is emitted, so check the state of the network operation object to see whether or not the operation was successful. Because a move or copy operation consists of multiple operations (get(), put() and maybe remove()), this function doesn't return a single Q3NetworkOperation, but rather a list of them. They are in the order: get(), put() and (if applicable) remove(). \sa get(), put()*/Q3PtrList<Q3NetworkOperation> Q3UrlOperator::copy( const QString &from, const QString &to, bool move, bool toPath ){#ifdef Q3URLOPERATOR_DEBUG qDebug( "Q3UrlOperator: copy %s %s %d", from.latin1(), to.latin1(), move );#endif Q3PtrList<Q3NetworkOperation> ops; ops.setAutoDelete( false ); Q3UrlOperator *uFrom = new Q3UrlOperator( *this, from ); Q3UrlOperator *uTo = new Q3UrlOperator( to ); // prepare some string for later usage QString frm = *uFrom; QString file = uFrom->fileName(); if (frm == to + file) return ops; file.prepend( QLatin1String("/") ); // uFrom and uTo are deleted when the Q3NetworkProtocol deletes itself via // autodelete uFrom->getNetworkProtocol(); uTo->getNetworkProtocol(); Q3NetworkProtocol *gProt = uFrom->d->networkProtocol; Q3NetworkProtocol *pProt = uTo->d->networkProtocol; uFrom->setPath( uFrom->dirPath() ); if ( gProt && (gProt->supportedOperations()&Q3NetworkProtocol::OpGet) && pProt && (pProt->supportedOperations()&Q3NetworkProtocol::OpPut) ) { connect( gProt, SIGNAL(data(QByteArray,Q3NetworkOperation*)), this, SLOT(copyGotData(QByteArray,Q3NetworkOperation*)) ); connect( gProt, SIGNAL(dataTransferProgress(int,int,Q3NetworkOperation*)), this, SIGNAL(dataTransferProgress(int,int,Q3NetworkOperation*)) ); connect( gProt, SIGNAL(finished(Q3NetworkOperation*)), this, SLOT(continueCopy(Q3NetworkOperation*)) ); connect( gProt, SIGNAL(finished(Q3NetworkOperation*)), this, SIGNAL(finished(Q3NetworkOperation*)) ); connect( gProt, SIGNAL(connectionStateChanged(int,QString)), this, SIGNAL(connectionStateChanged(int,QString)) ); connect( pProt, SIGNAL(dataTransferProgress(int,int,Q3NetworkOperation*)), this, SIGNAL(dataTransferProgress(int,int,Q3NetworkOperation*)) ); connect( pProt, SIGNAL(finished(Q3NetworkOperation*)), this, SIGNAL(finished(Q3NetworkOperation*)) ); connect( pProt, SIGNAL(finished(Q3NetworkOperation*)), this, SLOT(finishedCopy()) ); Q3NetworkOperation *opGet = new Q3NetworkOperation( Q3NetworkProtocol::OpGet, frm, QString(), QString() ); ops.append( opGet ); gProt->addOperation( opGet ); QString toFile = to + file; if (!toPath) toFile = to; Q3NetworkOperation *opPut = new Q3NetworkOperation( Q3NetworkProtocol::OpPut, toFile, QString(), QString() ); ops.append( opPut ); d->getOpPutProtMap.insert( (void*)opGet, pProt ); d->getOpGetProtMap.insert( (void*)opGet, gProt ); d->getOpPutOpMap.insert( (void*)opGet, opPut ); if ( move && (gProt->supportedOperations()&Q3NetworkProtocol::OpRemove) ) { gProt->setAutoDelete( false ); Q3NetworkOperation *opRm = new Q3NetworkOperation( Q3NetworkProtocol::OpRemove, frm, QString(), QString() ); ops.append( opRm ); d->getOpRemoveOpMap.insert( (void*)opGet, opRm ); } else { gProt->setAutoDelete( true ); }#ifdef Q3URLOPERATOR_DEBUG qDebug( "Q3UrlOperator: copy operation should start now..." );#endif return ops; } else { QString msg; if ( !gProt ) { msg = tr( "The protocol `%1' is not supported" ).arg( uFrom->protocol() ); } else if ( gProt->supportedOperations() & Q3NetworkProtocol::OpGet ) { msg = tr( "The protocol `%1' does not support copying or moving files or directories" ).arg( uFrom->protocol() ); } else if ( !pProt ) { msg = tr( "The protocol `%1' is not supported" ).arg( uTo->protocol() ); } else { msg = tr( "The protocol `%1' does not support copying or moving files or directories" ).arg( uTo->protocol() ); } delete uFrom; delete uTo; Q3NetworkOperation *res = new Q3NetworkOperation( Q3NetworkProtocol::OpGet, frm, to, QString() ); res->setState( Q3NetworkProtocol::StFailed ); res->setProtocolDetail( msg ); res->setErrorCode( (int)Q3NetworkProtocol::ErrUnsupported ); emit finished( res ); deleteOperation( res ); } return ops;}/*! \overload Copies the \a files to the directory \a dest. If \a move is true the files are moved, not copied. \a dest must point to a directory. This function calls copy() for each entry in \a files in turn. You don't get a result from this function; each time a new copy begins, startedNextCopy() is emitted, with a list of Q3NetworkOperations that describe the new copy operation.*/void Q3UrlOperator::copy( const QStringList &files, const QString &dest, bool move ){ d->waitingCopies = files; d->waitingCopiesDest = dest; d->waitingCopiesMove = move; finishedCopy();}/*! Returns true if the URL is a directory; otherwise returns false. This may not always work correctly, if the protocol of the URL is something other than file (local filesystem). If you pass a bool pointer as the \a ok argument, *\a ok is set to true if the result of this function is known to be correct, and to false otherwise.*/bool Q3UrlOperator::isDir( bool *ok ){ if ( ok ) *ok = true; if ( isLocalFile() ) { if ( QFileInfo( path() ).isDir() ) return true; else return false; } if ( d->entryMap.contains( QLatin1String(".") ) ) { return d->entryMap[ QLatin1String(".") ].isDir(); } // #### can assume that we are a directory? if ( ok ) *ok = false; return true;}/*! Tells the network protocol to get data from \a location or, if it is empty, to get data from the location to which this URL points (see Q3Url::fileName() and Q3Url::encodedPathAndQuery()). What happens then depends on the network protocol. The data() signal is emitted when data comes in. Because it's unlikely that all data will come in at once, it is common for multiple data() signals to be emitted. The dataTransferProgress() signal is emitted while processing the operation. At the end, finished() (with success or failure) is emitted, so check the state of the network operation object to see whether or not the operation was successful. If \a location is empty, the path of this Q3UrlOperator should point to a file when you use this operation. If \a location is not empty, it can be a relative URL (a child of the path to which the Q3UrlOperator points) or an absolute URL. For example, to get a web page you might do something like this: \code Q3UrlOperator op( "http://www.whatever.org/cgi-bin/search.pl?cmd=Hello" ); op.get(); \endcode For most other operations, the path of the Q3UrlOperator must point to a directory. If you want to download a file you could do the following: \code Q3UrlOperator op( "ftp://ftp.whatever.org/pub" ); // do some other stuff like op.listChildren() or op.mkdir( "new_dir" ) op.get( "a_file.txt" ); \endcode This will get the data of ftp://ftp.whatever.org/pub/a_file.txt. \e Never do anything like this: \code Q3UrlOperator op( "http://www.whatever.org/cgi-bin" ); op.get( "search.pl?cmd=Hello" ); // WRONG! \endcode If \a location is not empty and relative it must not contain any queries or references, just the name of a child. So if you need to specify a query or reference, do it as shown in the first example or specify the full URL (such as http://www.whatever.org/cgi-bin/search.pl?cmd=Hello) as \a location. \sa copy()*/const Q3NetworkOperation *Q3UrlOperator::get( const QString &location ){ Q3Url u( *this ); if ( !location.isEmpty() ) u = Q3Url( *this, location ); if ( !u.isValid() ) return 0; if ( !d->networkProtocol ) { setProtocol( u.protocol() ); getNetworkProtocol(); } Q3NetworkOperation *res = new Q3NetworkOperation( Q3NetworkProtocol::OpGet, u, QString(), QString() ); return startOperation( res );}/*! This function tells the network protocol to put \a data in \a location. If \a location is empty, it puts the \a data in the location to which the URL points. What happens depends on the network protocol. Depending on the network protocol, some data might come back after putting data, in which case the data() signal is emitted. The dataTransferProgress() signal is emitted during processing of the operation. At the end, finished() (with success or failure) is emitted, so check the state of the network operation object to see whether or not the operation was successful. If \a location is empty, the path of this Q3UrlOperator should point to a file when you use this operation. If \a location is not empty, it can be a relative (a child of the path to which the Q3UrlOperator points) or an absolute URL. For putting some data to a file you can do the following: \code Q3UrlOperator op( "ftp://ftp.whatever.com/home/me/filename.dat" ); op.put( data ); \endcode For most other operations, the path of the Q3UrlOperator must point to a directory. If you want to upload data to a file you could do the following: \code Q3UrlOperator op( "ftp://ftp.whatever.com/home/me" ); // do some other stuff like op.listChildren() or op.mkdir( "new_dir" ) op.put( data, "filename.dat" );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -