Qt Toolkit - QNetworkProtocol Class

| Classes - Annotated - Tree - Functions - Home - Structure

# QNetworkProtocol Class Reference

This is the base class for network protocols which provides a common API for network protocols. More...

`#include <qnetworkprotocol.h>`

Inherits QObject.

Inherited by QFtp and QLocalFs.

List of all member functions.

## Public Members

- enum State { StWaiting = 0, StInProgress, StDone, StFailed, StStopped }
- enum Operation { OpListChildren = 1, OpMkdir = 2, OpRemove = 4, OpRename = 8, OpGet = 32, OpPut = 64 }
- enum ConnectionState { ConHostFound, ConConnected, ConClosed }
- enum Error { NoError = 0, ErrValid, ErrUnknownProtocol, ErrUnsupported, ErrParse, ErrLoginIncorrect, ErrHostNotFound, ErrListChlidren, ErrMkdir, ErrRemove, ErrRename, ErrGet, ErrPut, ErrFileNotExisting, ErrPermissionDenied }
- QNetworkProtocol ()
- virtual ~QNetworkProtocol ()
- virtual void setUrl ( QUrlOperator * u )
- virtual void setAutoDelete ( bool b, int i = 10000 )
- bool autoDelete () const
- virtual int supportedOperations () const
- virtual void addOperation ( QNetworkOperation * op )
- QUrlOperator* url () const
- QNetworkOperation* operationInProgress () const
- virtual void clearOperationQueue ()
- virtual void stop ()

## Signals

- void data ( const QByteArray &, QNetworkOperation * res )
- void connectionStateChanged ( int state, const QString & data )
- void finished ( QNetworkOperation * res )
- void start ( QNetworkOperation * res )
- void newChildren ( const QValueList<QUrlInfo> &, QNetworkOperation * res )
- void newChild ( const QUrlInfo &, QNetworkOperation * res )
- void createdDirectory ( const QUrlInfo &, QNetworkOperation * res )
- void removed ( QNetworkOperation * res )
- void itemChanged ( QNetworkOperation * res )
- void dataTransferProgress ( int bytesDone, int bytesTotal, QNetworkOperation * res )

## Static Public Members

- void registerNetworkProtocol ( const QString & protocol, QNetworkProtocolFactoryBase * protocolFactory )
- QNetworkProtocol* getNetworkProtocol ( const QString & protocol )
- bool hasOnlyLocalFileSystem ()

## Protected Members

- virtual void processOperation ( QNetworkOperation * op ) (internal)
- virtual void operationListChildren ( QNetworkOperation * op )
- virtual void operationMkDir ( QNetworkOperation * op )
- virtual void operationRemove ( QNetworkOperation * op )
- virtual void operationRename ( QNetworkOperation * op )
- virtual void operationGet ( QNetworkOperation * op )
- virtual void operationPut ( QNetworkOperation * op )
- virtual bool checkConnection ( QNetworkOperation * op )

## Detailed Description

This is the base class for network protocols which provides a common API for network protocols.

This is a baseclass which should be used for implementations of network protocols which can then be used in Qt (e.g. in the filedialog) together with the QUrlOperator.

The easiest way to implement a new network protocol is, to reimplement the operation[something]( QNetworkOperation * ) methods. Of course only the ones, which are supported, should be reimplemented. To specify which operations are supported, also reimplement supportedOperations() and return an int there, which is ore'd together using the supported operations from the QNetworkProtocol::Operation enum.

When you implement a network protocol this way, be careful that you always emit the correct signals. Also, always emit the finished() signal when an operation is done (on failure or success!). The Qt Network Architecture relies on correctly emitted finished() signals.

For a detailed description about the Qt Network Architecture, and also how to implement and use network protocols in Qt, look at the Qt Network Documentation

## Member Type Documentation

### QNetworkProtocol::ConnectionState

When the connection state of a network protocol changes, it emits the signal connectionStateChanged(). The first argument is one of following values:

- `ConHostFound` - Host has been found
- `ConConnected` - Connection to the host has been established
- `ConClosed` - connection has been closed

### QNetworkProtocol::Error

When an operation failed (finished without success) the QNetworkOperation of the operation returns an error code, which is one of following values:

- `NoError` - No error occurred
- `ErrValid` - The URL you are operating on is not valid
- `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)
- `ErrUnsupported` - The operation is not supported by the protocol
- `ErrParse` - Parse error of the URL
- `ErrLoginIncorrect` - You needed to login but the username and or password are wrong
- `ErrHostNotFound` - The specified host (in the URL) couldn´t be found
- `ErrListChlidren` - An error occurred while listing the children
- `ErrMkdir` - An error occurred when creating a directory
- `ErrRemove` - An error occurred while removing a child
- `ErrRename` - An error occurred while renaming a child
- `ErrGet` - An error occurred while getting (retrieving) data
- `ErrPut` - An error occurred while putting (uploading) data
- `ErrFileNotExisting` - A file which is needed by the operation doesn't exist
- `ErrPermissionDenied` - The permission for doing the operation has been denied

When implementing custom network protocols, you should also use these values of error codes. If this is not possible, you can define your own ones by using an integer value which doesn't conflict with one of these vales.

### QNetworkProtocol::Operation

This enum lists all possible operations which a network protocol can support. supportedOperations() returns an int which is or'd together of these values, also the type() or a QNetworkOperation is always one of these values.

- `OpListChildren` - Listing the children of a URL, e.g. of a directory
- `OpMkdir` - Create a directory
- `OpRemove` - remove a child (e.g. file)
- `OpRename` - rename a child (e.g. file )
- `OpGet` - get data from a location
- `OpPut` - put data to a location

### QNetworkProtocol::State

This enum contains the state which a QNetworkOperation can have:

- `StWaiting` - The operation is in the queue of the QNetworkProtocol and is waiting for being prcessed
- `StInProgress` - The operation is just processed
- `StDone` - The operation has been processed succesfully
- `StFailed` - The operation has been processed but an error occurred
- `StStopped` - The operation has been processed but has been stopped before it finished

## Member Function Documentation

### QNetworkProtocol::QNetworkProtocol ()

Constructor of the network protocol baseclass. Does some initialization and connecting of signals and slots.

### QNetworkProtocol::~QNetworkProtocol () `[virtual]`

Destructor.

### void QNetworkProtocol::addOperation ( QNetworkOperation * op ) `[virtual]`

Adds the operation op the operation queue. The operation will be processed as soon as possible. This method returns immediately.

### bool QNetworkProtocol::autoDelete () const

Returns TRUE, of autodeleting is enabled, else FALSE.

See also QNetworkProtocol::setAutoDelete().

### bool QNetworkProtocol::checkConnection ( QNetworkOperation * ) `[virtual protected]`

For processing operations the newtork protocol baseclass calls this method quite often. This should be reimplemented by new network protocols. It should return TRUE, if the connection is ok (open), else 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 it, but the host couldn't be found or something like that), set the state of op to QNetworkProtocol::StFailed and emit the finished() signal with this QNetworkOperation as argument.

op is the operation which needs an open connection.

Reimplemented in QFtp.

### void QNetworkProtocol::clearOperationQueue () `[virtual]`

Clears the operation queue.

### void QNetworkProtocol::connectionStateChanged ( int state, const QString & data ) `[signal]`

This signal is emitted whenever the state of the connection of the network protocol is changed. state describes the new state, which is one of ConHostFound, ConConnected, ConClosed data is a message text.

### void QNetworkProtocol::createdDirectory ( const QUrlInfo & i, QNetworkOperation * op ) `[signal]`

This signal is emitted when mkdir() has been succesful and the directory has been created. i holds the information about the new directory. op is the pointer to the operation object, which contains all infos of the operation, including the state and so on and using op->arg( 0 ) you also get the filename of the new directory.

When a protocol emits this signal, QNetworkProtocol is smart enough to let the QUrlOperator, which is used by the network protocol, emit its corresponding signal.

### void QNetworkProtocol::data ( const QByteArray & data, QNetworkOperation * op ) `[signal]`

This signal is emitted when new data has been received after e.g. calling get() or put(). op holds the name of the file which data is retrieved in the first argument and the data in the second argument (raw). You get them with op->arg( 0 ) and op->rawArg( 1 ).

op is the pointer to the operation object, which contains all infos of the operation, including the state and so on.

When a protocol emits this signal, QNetworkProtocol is smart enough to let the QUrlOperator, which is used by the network protocol, emit its corresponding signal.

### void QNetworkProtocol::dataTransferProgress ( int bytesDone, int bytesTotal, QNetworkOperation * op ) `[signal]`

When transferring data (using put() or get()) this signal is emitted during the progress. bytesDone tells how many bytes of bytesTotal are transferred. More information about the operation is stored in the op, the pointer to the network operation which is processed. bytesTotal may be -1, which means that the number of total bytes is not known.

When a protocol emits this signal, QNetworkProtocol is smart enough to let the QUrlOperator, which is used by the network protocol, emit its corresponding signal.

### void QNetworkProtocol::finished ( QNetworkOperation * op ) `[signal]`

This signal is emitted when an operation of some sort finished. This signal is emitted always, this means on success and on failure. op is the pointer to the operation object, which contains all infos of the operation which has been finished, including the state and so on. To check if the operation was successful or not, check the state and error code of the operation object.

When a protocol emits this signal, QNetworkProtocol is smart enough to let the QUrlOperator, which is used by the network protocol, emit its corresponding signal.

### QNetworkProtocol * QNetworkProtocol::getNetworkProtocol ( const QString & protocol ) `[static]`

Static method to get a new instance of a network protocol. E.g. if you need to do some FTP operations, do

QFtp *ftp = QNetworkProtocol::getNetworkProtocol( "ftp" );

This returns now either NULL, if no protocol for ftp was registered, or a pointer to a new instance of an FTP implementation. The ownership of the pointer is transferred to you, so you have to 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. Rather use the QUrlOperator, which makes working with network protocols much more convenient.

See also QUrlOperator.

### bool QNetworkProtocol::hasOnlyLocalFileSystem () `[static]`

Returns TRUE, if only a protocol for working on the local filesystem is registered, or FALSE if also other network protocols are registered.

### void QNetworkProtocol::itemChanged ( QNetworkOperation * op ) `[signal]`

This signal is emitted whenever a file, which is a child of this URL, has been changed e.g. by successfully calling rename(). op holds the original and the new filenames in the first and second arguments. You get them with op->arg( 0 ) and op->arg( 1 ).

op is the pointer to the operation object, which contains all infos of the operation, including the state and so on.

When a protocol emits this signal, QNetworkProtocol is smart enough to let the QUrlOperator, which is used by the network protocol, emit its corresponding signal.

### void QNetworkProtocol::newChild ( const QUrlInfo & i, QNetworkOperation * op ) `[signal]`

This signal is emitted if a new child has been read. QNetworkProtocol automatically connects that to a slot which creates a list of QUrlInfo objects (with just the one QUrlInfo i) and emits then newChildren() signal with this created list.

So this is just a convenience signal when implementing an own network protocol. In all other cases just care about the newChildren() signal with the list of QUrlInfo objects.

### void QNetworkProtocol::newChildren ( const QValueList<QUrlInfo> & i, QNetworkOperation * op ) `[signal]`

This signal is emitted after listChildren() was called and new children (e.g. files) have been read from list of files. i holds the information about the new children. op is the pointer to the operation object, which contains all infos of the operation, including the state and so on.

When a protocol emits this signal, QNetworkProtocol is smart enough to let the QUrlOperator, which is used by the network protocol, emit its corresponding signal.

When implementing an own network protocol and reading children in most cases you don't read one child at once, but a list of them. That's why this signal takes a list of QUrlInfo objects. But if you read only one child at once, you can use the convenience signal newChild(), which takes only a single QUrlInfo object.

### void QNetworkProtocol::operationGet ( QNetworkOperation * ) `[virtual protected]`

When implemeting a new newtork protocol this method should be reimplemented, if the protocol supports getting data, and process this QNetworkOperation.

When you reimplement this method, it's very important that you emit the correct signals at the correct time (esp. the finished() signal after processing an operation). So have a look at the Qt Network Documentation, there it is described in detail how to reimplement this method. Also you may look at the example implementation of qt/extenstions/network/examples/networkprotocol/nntp.cpp.

### QNetworkOperation * QNetworkProtocol::operationInProgress () const

Returns the operation, which is just processed, or NULL of none is processed at the moment.

### void QNetworkProtocol::operationListChildren ( QNetworkOperation * ) `[virtual protected]`

When implemeting a new newtork protocol this method should be reimplemented, if the protocol supports listing children, and this method should then process this QNetworkOperation.

When you reimplement this method, it's very important that you emit the correct signals at the correct time (esp. the finished() signal after processing an operation). So have a look at the Qt Network Documentation, there it is described in detail how to reimplement this method. Also you may look at the example implementation of qt/extenstions/network/examples/networkprotocol/nntp.cpp.

### void QNetworkProtocol::operationMkDir ( QNetworkOperation * ) `[virtual protected]`

When implemeting a new newtork protocol this method should be reimplemented, if the protocol supports making directories, and this method should then process this QNetworkOperation.

When you reimplement this method, it's very important that you emit the correct signals at the correct time (esp. the finished() signal after processing an operation). So have a look at the Qt Network Documentation, there it is described in detail how to reimplement this method. Also you may look at the example implementation of qt/extenstions/network/examples/networkprotocol/nntp.cpp.

### void QNetworkProtocol::operationPut ( QNetworkOperation * ) `[virtual protected]`

When implemeting a new newtork protocol this method should be reimplemented, if the protocol supports putting data, and this method should then process this QNetworkOperation.

When you reimplement this method, it's very important that you emit the correct signals at the correct time (esp. the finished() signal after processing an operation). So have a look at the Qt Network Documentation, there it is described in detail how to reimplement this method. Also you may look at the example implementation of qt/extenstions/network/examples/networkprotocol/nntp.cpp.

### void QNetworkProtocol::operationRemove ( QNetworkOperation * ) `[virtual protected]`

When implemeting a new newtork protocol this method should be reimplemented, if the protocol supports removing children, and this method should then process this QNetworkOperation.

When you reimplement this method, it's very important that you emit the correct signals at the correct time (esp. the finished() signal after processing an operation). So have a look at the Qt Network Documentation, there it is described in detail how to reimplement this method. Also you may look at the example implementation of qt/extenstions/network/examples/networkprotocol/nntp.cpp.

### void QNetworkProtocol::operationRename ( QNetworkOperation * ) `[virtual protected]`

When implemeting a new newtork protocol this method should be reimplemented, if the protocol supports renaming children, and this method should then process this QNetworkOperation.

When you reimplement this method, it's very important that you emit the correct signals at the correct time (esp. the finished() signal after processing an operation). So have a look at the Qt Network Documentation, there it is described in detail how to reimplement this method. Also you may look at the example implementation of qt/extenstions/network/examples/networkprotocol/nntp.cpp.

### void QNetworkProtocol::registerNetworkProtocol ( const QString & protocol, QNetworkProtocolFactoryBase * protocolFactory ) `[static]`

Static method to register a network protocol for Qt. E.g. if you have a implementation of NNTP (called Nntp), which is derived from QNetworkProtocol, call

QNetworkProtocol::registerNetworkProtocol( "nntp", new QNetworkProtocolFactory );

After that, this implementation is registered for nntp operations.

### void QNetworkProtocol::removed ( QNetworkOperation * op ) `[signal]`

This signal is emitted when remove() has been succesful and the file has been removed. op holds the filename of the removed file in the first argument, you get it with op->arg( 0 ).

op is the pointer to the operation object, which contains all infos of the operation, including the state and so on.

When a protocol emits this signal, QNetworkProtocol is smart enough to let the QUrlOperator, which is used by the network protocol, emit its corresponding signal.

### void QNetworkProtocol::setAutoDelete ( bool b, int i = 10000 ) `[virtual]`

Because it's sometimes hard to care about removing network protocol instances, QNetworkProtocol provides an autodelete mechanism. If you set b to TRUE, this network protocol instance gets removed after it has been i milliseconds inactive (this means i ms after the last operation has been processed). If you set b to FALSE, the autodelete mechanism is switched off.

NOTE: If you switch on autodeleting, the QNetworkProtocol also deletes its QUrlOperator!.

### void QNetworkProtocol::setUrl ( QUrlOperator * u ) `[virtual]`

Sets the QUrlOperator, on which the protocol works.

See also QUrlOperator.

### void QNetworkProtocol::start ( QNetworkOperation * op ) `[signal]`

Some operations (like listChildren()) emit this signal when they start processing the operation. op is the pointer to the operation object, which contains all infos of the operation, including the state and so on.

When a protocol emits this signal, QNetworkProtocol is smart enough to let the QUrlOperator, which is used by the network protocol, emit its corresponding signal.

### void QNetworkProtocol::stop () `[virtual]`

Stops the current operation which is just processed and clears all waiting operations.

### int QNetworkProtocol::supportedOperations () const `[virtual]`

Returns an int, which is or'd together using the enum values of `QNetworkProtocol::Operation,` which describes which operations are supported by the network protocol. Should be reimplemented by new network protocols.

### QUrlOperator * QNetworkProtocol::url () const

Returns the QUrlOperator on which the protocol works.

### void QNetworkProtocol::processOperation ( QNetworkOperation * op ) `[virtual protected]`

For internal use only.

Search the documentation, FAQ, qt-interest archive and more (uses www.trolltech.com):

This file is part of the Qt toolkit, copyright © 1995-2000 Trolltech, all rights reserved.