QNetworkProtocol Class

Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# QNetworkProtocol Class Reference
[network module]

The QNetworkProtocol class provides a common API for network protocols. More...

`#include <qnetworkprotocol.h>`

Inherits QObject.

Inherited by QFtp, QHttp, and QLocalFs.

List of all member functions.

## Public Members

- enum State { StWaiting = 0, StInProgress, StDone, StFailed, StStopped }
- enum Operation { OpListChildren = 1, OpMkDir = 2, OpMkdir = OpMkDir, OpRemove = 4, OpRename = 8, OpGet = 32, OpPut = 64 }
- enum ConnectionState { ConHostFound, ConConnected, ConClosed }
- enum Error { NoError = 0, ErrValid, ErrUnknownProtocol, ErrUnsupported, ErrParse, ErrLoginIncorrect, ErrHostNotFound, ErrListChildren, ErrListChlidren = ErrListChildren, ErrMkDir, ErrMkdir = 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 & data, QNetworkOperation * op )
- void connectionStateChanged ( int state, const QString & data )
- void finished ( QNetworkOperation * op )
- void start ( QNetworkOperation * op )
- void newChildren ( const QValueList<QUrlInfo> & i, QNetworkOperation * op )
- void newChild ( const QUrlInfo & i, QNetworkOperation * op )
- void createdDirectory ( const QUrlInfo & i, QNetworkOperation * op )
- void removed ( QNetworkOperation * op )
- void itemChanged ( QNetworkOperation * op )
- void dataTransferProgress ( int bytesDone, int bytesTotal, QNetworkOperation * op )

## Static Public Members

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

## Protected Members

- 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

The QNetworkProtocol class provides a common API for network protocols.

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 QUrlOperator.

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 QNetworkProtocol::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 and on failure). Qt relies on correctly emitted finished() signals.

For a detailed description of the Qt Network Architecture and how to implement and use network protocols in Qt, see the Qt Network Documentation.

See also Input/Output and Networking.

## 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 the following values:

- `QNetworkProtocol::ConHostFound` - Host has been found.
- `QNetworkProtocol::ConConnected` - Connection to the host has been established.
- `QNetworkProtocol::ConClosed` - Connection has been closed.

### QNetworkProtocol::Error

When an operation fails (finishes unsuccessfully), the QNetworkOperation of the operation returns an error code which has one of the following values:

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

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.

### QNetworkProtocol::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 QNetworkOperation is always one of these values.

- `QNetworkProtocol::OpListChildren` - List the children of a URL, e.g. of a directory.
- `QNetworkProtocol::OpMkDir` - Create a directory.
- `QNetworkProtocol::OpRemove` - Remove a child (e.g. a file).
- `QNetworkProtocol::OpRename` - Rename a child (e.g. a file).
- `QNetworkProtocol::OpGet` - Get data from a location.
- `QNetworkProtocol::OpPut` - Put data to a location.

### QNetworkProtocol::State

This enum contains the state that a QNetworkOperation can have.

- `QNetworkProtocol::StWaiting` - The operation is in the QNetworkProtocol's queue waiting to be prcessed.
- `QNetworkProtocol::StInProgress` - The operation is being processed.
- `QNetworkProtocol::StDone` - The operation has been processed succesfully.
- `QNetworkProtocol::StFailed` - The operation has been processed but an error occurred.
- `QNetworkProtocol::StStopped` - The operation has been processed but has been stopped before it finished, and is waiting to be processed.

## Member Function Documentation

### QNetworkProtocol::QNetworkProtocol ()

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

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

Destructor.

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

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

### bool QNetworkProtocol::autoDelete () const

Returns TRUE if auto-deleting is enabled; otherwise returns FALSE.

See also QNetworkProtocol::setAutoDelete().

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

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 op to QNetworkProtocol::StFailed and emit the finished() signal with this QNetworkOperation as argument.

op is the operation that needs an open connection.

Example: network/networkprotocol/nntp.cpp.

### 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 or 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 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, 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 calling get() or put(). 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 ). 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, 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]`

This signal is emitted during the transfer of data (using put() or get()). bytesDone is how many bytes of bytesTotal have been transferred. bytesTotal may be -1, which means that the total number of bytes is not known. 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, 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 finishes. This signal is always emitted, for both success and failure. 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, 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 the network protocol protocol. For example, if you need to do some FTP operations, do the following:

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

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 QUrlOperator, which makes working with network protocols much more convenient.

See also QUrlOperator.

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

Returns TRUE if the only protocol registered is for working on the local filesystem; returns FALSE if other network protocols are also 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 file names in the first and second arguments, accessible with op->arg( 0 ) and op->arg( 1 ) respectively. 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, 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 (file) has been read. QNetworkProtocol automatically connects it to a slot which creates a list of QUrlInfo objects (with just one QUrlInfo i) and emits the newChildren() signal with this list. 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.

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

This signal is emitted after listChildren() was called and new children (files) have been read from the list of files. i holds the information about the new children. 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, QNetworkProtocol is smart enough to let the QUrlOperator, 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.

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

When implementing a new network protocol, this method should be reimplemented if the protocol supports getting data; this method should then process the QNetworkOperation.

When you reimplement this method it's very important that you emit the correct signals at the correct time (especially the finished() signal after processing an operation). Take a look at the Qt Network Documentation which describes in detail how to reimplement this method. You may also want to look at the example implementation in examples/network/networkprotocol/nntp.cpp.

op is the pointer to the operation object which contains all the information on the operation that has finished, including the state, etc.

Example: network/networkprotocol/nntp.cpp.

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

Returns the operation, which is being processed, or 0 of no operation is being processed at the moment.

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

When implementing a new network protocol, this method should be reimplemented if the protocol supports listing children (files); 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 (especially the finished() signal after processing an operation). Take a look at the Qt Network Documentation which describes in detail how to reimplement this method. You may also want to look at the example implementation in examples/network/networkprotocol/nntp.cpp.

op is the pointer to the operation object which contains all the information on the operation that has finished, including the state, etc.

Example: network/networkprotocol/nntp.cpp.

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

When implementing a new network protocol, this method should be reimplemented if the protocol supports making directories; 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 (especially the finished() signal after processing an operation). Take a look at the Qt Network Documentation which describes in detail how to reimplement this method. You may also want to look at the example implementation in examples/network/networkprotocol/nntp.cpp.

op is the pointer to the operation object which contains all the information on the operation that has finished, including the state, etc.

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

When implementing a new network protocol, this method should be reimplemented if the protocol supports putting (uploading) data; this method should then process the QNetworkOperation.

When you reimplement this method it's very important that you emit the correct signals at the correct time (especially the finished() signal after processing an operation). Take a look at the Qt Network Documentation which describes in detail how to reimplement this method. You may also want to look at the example implementation in examples/network/networkprotocol/nntp.cpp.

op is the pointer to the operation object which contains all the information on the operation that has finished, including the state, etc.

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

When implementing a new network protocol, this method should be reimplemented if the protocol supports removing children (files); 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 (especially the finished() signal after processing an operation). Take a look at the Qt Network Documentation which is describes in detail how to reimplement this method. You may also want to look at the example implementation in examples/network/networkprotocol/nntp.cpp.

op is the pointer to the operation object which contains all the information on the operation that has finished, including the state, etc.

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

When implementing a new newtork protocol, this method should be reimplemented if the protocol supports renaming children (files); 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 (especially the finished() signal after processing an operation). Take a look at the Qt Network Documentation which describes in detail how to reimplement this method. You may also want to look at the example implementation in examples/network/networkprotocol/nntp.cpp.

op is the pointer to the operation object which contains all the information on the operation that has finished, including the state, etc.

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

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

        QNetworkProtocol::registerNetworkProtocol( "nntp", new QNetworkProtocolFactory<Nntp> );

after which your implementation is registered for future nntp operations.

The name of the protocol is given in protocol and a pointer to the protocol factory is given in protocolFactory.

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

This signal is emitted when remove() has been succesful and the file has been removed. op holds the file name of the removed file in the first argument, accessible with op->arg( 0 ). 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, 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 take care of removing network protocol instances, QNetworkProtocol provides an auto-delete mechanism. If you set b to TRUE, the network protocol instance is removed after it has been inactive for i milliseconds (i.e. i milliseconds after the last operation has been processed). If you set b to FALSE the auto-delete mechanism is switched off.

If you switch on auto-delete, the QNetworkProtocol also deletes its QUrlOperator.

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

Sets the QUrlOperator, on which the protocol works, to u.

See also QUrlOperator.

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

Some operations (such as listChildren()) emit this signal when they start processing the operation. 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, 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 that is being processed and clears all waiting operations.

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

Returns an int that 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.

Example: network/networkprotocol/nntp.cpp.

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

Returns the QUrlOperator on which the protocol works.

This file is part of the Qt toolkit. Copyright © 1995-2005 Trolltech. All Rights Reserved.