Classes - Annotated - Tree - Functions - Home - Structure

QUrlOperator Class Reference

The QUrlOperator class provides common operations on URLs (get() and more). More...

#include <qurloperator.h>

Inherits QObject and QUrl.

List of all member functions.

Public Members

Signals

Protected Members

Related Functions


Detailed Description

The QUrlOperator class provides common operations on URLs (get() and more).

This class operates on hirachical structures (such as filesystems) using URLs. Its API allows all the common operations (listing children, removing children, renaming, etc.). But the class itself contains no functionality for that. It uses the functionality of registered network protocols. Depending of the protocol of the URL, it uses a fitting network protocol class for the operations. In detail, each of the operation functions of QUrlOperator creates a QNetworkOperation object that describes the operation and puts it into the operation queue of the network protocol used. If no fitting protocol could be found (because no implementation of the needed network protocol is registered), the URL operator emits errors. Each protocol does not support every operation, but error handling deals with this problem.

A QUrlOperator can be used like this (for downloading a file):

  QUrlOperator op;
  op.copy( QString("ftp://ftp.trolltech.com/qt/source/qt-2.1.0.tar.gz"), "file:/tmp", FALSE );
  

Now, you will also need to connect to some signals of the QUrlOperator to be informed of success, errors, progress and more things.

Of course an implementation for the FTP protocol has to be registered for this example. There is an implementation of the FTP protocol in the Qt Network Extension Library. You can use the function qInitNetworkProtocols() to register all network protocols that are shipped with the Qt network extension (at the moment FTP and HTTP are supported).

For more information about the Qt Network Architecture see the Qt Network Documentation.

See also QNetworkProtocol, QNetworkOperation, Input/Output and Networking and Miscellaneous Classes.


Member Function Documentation

QUrlOperator::QUrlOperator ()

Constructs a QUrlOperator with an empty (i.e. invalid) URL.

QUrlOperator::QUrlOperator ( const QString & url )

Constructs a QUrlOperator using url and parses this string.

You can pass strings such as "/home/qt": in this case the protocol "file" is assumed.

QUrlOperator::QUrlOperator ( const QUrlOperator & url )

Constructs a copy of url.

QUrlOperator::QUrlOperator ( const QUrlOperator & url, const QString & relUrl, bool checkSlash = FALSE )

Constructs a QUrlOperator. The URL on which this QUrlOperator works on is constructed out of the arguments url, relUrl and checkSlash; the meaning of those arguments is the same as the corresponding QUrl constructor takes.

QUrlOperator::~QUrlOperator () [virtual]

Destructor.

void QUrlOperator::clearEntries () [virtual protected]

Clears the cache of children.

void QUrlOperator::connectionStateChanged ( int state, const QString & data ) [signal]

This signal is emitted whenever the state of the connection of the network protocol of the URL operator changes. state describes the new state, which QNetworkProtocol::ConHostFound, QNetworkProtocol::ConConnected or QNetworkProtocol::ConClosed. This enum is defined in QNetworkProtocol data.

QPtrList<QNetworkOperation> QUrlOperator::copy ( const QString & from, const QString & to, bool move = FALSE ) [virtual]

Copies the file from to to. If move is TRUE, the file is moved (copied and removed). from has to point to a file and to must point to a directory (into which from is copied). 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. Keep in mind that the get() and put() operations emit this signal through the QUrlOperator. The number of transferred and total bytes that you receive as argument in this signal does not relate to the the whole copy operation; it relates first to the get() and then to the put() operation. Always check what type of operation the signal comes from - the last argument of the signal tells you.

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/copy operation consists of multiple operations (get(), put() and maybe remove()), this function doesn't return a single QNetworkOperation, but rather a list of them. They are in order: get(), put() and (if applicable) remove().

See also get() and put().

void QUrlOperator::copy ( const QStringList & files, const QString & dest, bool move = FALSE ) [virtual]

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Copies files to the directory dest. If move is TRUE the files are moved, not copied. dest has to point to a directory.

This function calls the copy for each entry in files one after the other. You don't get a result from this function; each time a new copy begins, startedNextCopy() is emitted, with a list of QNetworkOperations that describe the new copy operation.

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

This signal is emitted when mkdir() succeeds 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 information about the operation, including the state. Using op->arg(0) you also get the file name of the new directory.

See also QNetworkOperation and QNetworkProtocol.

void QUrlOperator::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 whose data is retrieved in the first argument and the (raw) data in the second argument. You get them with op->arg( 0 ) and op->rawArg( 1 ).

op is the pointer to the operation object which contains all information about the operation, including the state.

See also QNetworkOperation and QNetworkProtocol.

void QUrlOperator::dataTransferProgress ( int bytesDone, int bytesTotal, QNetworkOperation * op ) [signal]

This signal is emitted during data transfer (using put() or get()). 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 that is processed. bytesTotal may be -1, which means that the number of total bytes is not known.

See also QNetworkOperation and QNetworkProtocol.

void QUrlOperator::deleteNetworkProtocol () [protected]

Deletes the currently used network protocol.

void QUrlOperator::finished ( QNetworkOperation * op ) [signal]

This signal is emitted when an operation of some sort finishes, whether with success or failure. op is the pointer to the operation object, which contains all information, including the state, of the operation which has been finished. Check the state and error code of the operation object to see whether or not the operation was successful.

See also QNetworkOperation and QNetworkProtocol.

const QNetworkOperation * QUrlOperator::get ( const QString & location = QString::null ) [virtual]

Tells the network protocol to get data from location or, if this is QString::null, to get data from the location to which this URL points (see QUrl::fileName() and QUrl::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, multiple data() signals will most likely be emitted. The dataTransferProgress() 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.

Now, if location is QString::null, the path of this QUrlOperator should point to a file when you use this operation. If location is not empty, it can be a relative URL (a child of the path to which the QUrlOperator points) or an absolute URL.

For example, to get a web page you might do something like this:

  QUrlOperator op( "http://www.whatever.org/cgi-bin/search.pl?cmd=Hallo" );
  op.get();
  

For most other operations, however, the path of the QUrlOperator must point to a directory. If you want to download a file you could do the following:

  QUrlOperator op( "ftp://ftp.whatever.org/pub" );
  // do some other stuff like op.listChildren() or op.mkdir( "new Dir" )
  op.get( "a_file.txt" );
  

This will get the data of ftp://ftp.whatever.org/pub/a_file.txt.

Never do anything like this:

  QUrlOperator op( "http://www.whatever.org/cgi-bin" );
  op.get( "search.pl?cmd=Hallo" );
  

If 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=Hallo) as location.

See also copy().

void QUrlOperator::getNetworkProtocol () [protected]

Finds a network protocol for the URL and deletes the old network protocol.

QUrlInfo QUrlOperator::info ( const QString & entry ) const [virtual]

Returns the URL information for the child entry, or returns an empty QUrlInfo object if there is no information available about entry.

bool QUrlOperator::isDir ( bool * ok = 0 ) [virtual]

Returns TRUE if the URL is a directory; otherwise it 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 as ok argument, this is set to TRUE if the result of this function is known to be correct; otherwise ok is set to FALSE.

void QUrlOperator::itemChanged ( QNetworkOperation * op ) [signal]

This signal is emitted whenever a file which is a child of this URL has been changed, for example by successfully calling rename(). op holds the original and new file names 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 information about the operation, including the state.

See also QNetworkOperation and QNetworkProtocol.

const QNetworkOperation * QUrlOperator::listChildren () [virtual]

Starts listing the children of this URL (e.g., of a directory). The signal start() 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 QNetworkOperation object created by this function is returned. This object contains all data about the operation and is used to refer to this operation later (e.g., in the signals that are emitted by the QUrlOperator). The return value can also be 0 if the operation object couldn't be created.

The path of this QUrlOperator has to point to a directory (because the children of this directory will be listed), not to a file. Otherwise this operation might not work!

const QNetworkOperation * QUrlOperator::mkdir ( const QString & dirname ) [virtual]

Tries to create a directory (child) with the name 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 emitted, too. 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 QNetworkOperation object created by this function is returned. This object contains all data about the operation and is used to refer to this operation later (e.g., in the signals that are emitted by the QUrlOperator). The return value can also be 0 if the operation object couldn't be created.

The path of this QUrlOperator has to point to a directory because the new directory will be created in this path, not to a file. Otherwise this operation might not work.

QString QUrlOperator::nameFilter () const

Returns the name filter of the URL.

See also QUrlOperator::setNameFilter() and QDir::nameFilter().

void QUrlOperator::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 a list of files. i holds the information about the new children.op is the pointer to the operation object which contains all information about the operation, including the state.

See also QNetworkOperation and QNetworkProtocol.

const QNetworkOperation * QUrlOperator::put ( const QByteArray & data, const QString & location = QString::null ) [virtual]

Tells the network protocol to put data in location. If it is empty (QString::null), it puts the 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() 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.

Now, if location is QString::null, the path of this QUrlOperator should point to a file when you use this operation. If location is not empty, it can be a relative (a child of the path to which the QUrlOperator points) or an absolute URL.

For putting some data to a file you can do the following:

  QUrlOperator op( "ftp://ftp.whatever.com/home/me/filename" );
  op.put( data );
  

For most other operations, however, the path of the QUrlOperator must point to a directory. If you want to upload data to a file you could do the following:

  QUrlOperator op( "ftp://ftp.whatever.com/home/me" );
  // do some other stuff like op.listChildren() or op.mkdir( "new Dir" )
  op.put( data, "filename" );
  

This will upload the data to ftp://ftp.whatever.com/home/me/filename.

See also copy().

const QNetworkOperation * QUrlOperator::remove ( const QString & filename ) [virtual]

Tries to remove the file (child) filename. If it succeeds the signal removed() 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 QNetworkOperation object created by this function is returned. This object contains all data about the operation and is used to refer to this operation later (e.g., in the signals that are emitted by the QUrlOperator). The return value can also be 0 if the operation object couldn't be created.

The path of this QUrlOperator has to point to a directory; because if filename is relative, it will try to remove it in this directory, not to a file. Otherwise this operation might not work.

void QUrlOperator::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; you get it with op->arg( 0 ).

op is the pointer to the operation object which contains all information about the operation, including the state.

See also QNetworkOperation and QNetworkProtocol.

const QNetworkOperation * QUrlOperator::rename ( const QString & oldname, const QString & newname ) [virtual]

Tries to rename the file (child) oldname by newname. If it succeeds, the signal itemChanged() 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 QNetworkOperation object created by this function is returned. This object contains all data about the operation and is used to refer to this operation later (e.g., in the signals that are emitted by the QUrlOperator). The return value can also be 0 if the operation object couldn't be created.

This path of this QUrlOperator has to point to a directory because oldname and newname are handled relative to this directory, not to a file. Otherwise this operation might not work!

void QUrlOperator::setNameFilter ( const QString & nameFilter ) [virtual]

Sets the name filter of the URL to nameFilter.

See also QDir::setNameFilter().

void QUrlOperator::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 information about the operation, including the state.

See also QNetworkOperation and QNetworkProtocol.

void QUrlOperator::startedNextCopy ( const QPtrList<QNetworkOperation> & lst ) [signal]

This signal is emitted if copy() starts a new copy operation. lst contains all QNetworkOperations related to this copy operation.

See also copy().

void QUrlOperator::stop () [virtual]

Stops the current network operation that was just processed and removes all waiting network operations of this QUrlOperator.

Related Functions

void qInitNetworkProtocols ()

This function registers the network protocols for FTP and HTTP. You have to call this function before you use QUrlOperator for these protocols.

Include the header qnetwork.h if you want to use this function.


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


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


Copyright © 2001 TrolltechTrademarks
Qt version 3.0.0-beta3