Logo Search packages:      
Sourcecode: webissues version File versions  Download package

commandmanager.h

/**************************************************************************
* This file is part of the WebIssues program
* Copyright (C) 2006 Michał Męciński
* Copyright (C) 2007 WebIssues Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
**************************************************************************/

#ifndef COMMANDMANAGER_H
#define COMMANDMANAGER_H

#include <QObject>
#include <QUrl>
#include <QList>

class QHttp;
class QHttpResponseHeader;

namespace WebIssues
{

class AbstractBatch;
class Command;
class Reply;
class ReplyLine;

/**
* Class for communicating with the WebIssues server.
*
* This class contains a priority queue of AbstractBatch objects which provide
* commands to execute. Only a single command is executed at a time. If there are
* two or more batches, commands from the batch with the highest priority are
* executed first. Processing commands is asynchronous.
*
* The instance of this class is available using the commandManager global variable.
* It is created and owned by the ConnectionManager.
*/
00041 class CommandManager : public QObject
{
    Q_OBJECT
public:
    /**
    * Type of error that occurred while executing the command.
    */
00048     enum Error
    {
        /** No error. */
00051         NoError,
        /** Connection to the server failed. The errorCode() method returns a QHttp::Error flag. */
00053         ConnectionError,
        /** The server returned an HTTP error. The errorCode() method returns the HTTP status. */
00055         HttpError,
        /** No WebIssues server was found at the given URL. */
00057         InvalidServer,
        /** The server version is not supported. */
00059         InvalidVersion,
        /** The server returned an invalid response. */
00061         InvalidResponse,
        /** The server returned a WebIssues error. The errorCode() method returns the error code. */
00063         WebIssuesError,
        /** The request was aborted. */
00065         Aborted
    };

public:
    /**
    * Default constructor.
    */
    CommandManager();

    /**
    * Destructor.
    */
    ~CommandManager();

public:
    /**
    * Set the URL of the server to send commands to.
    *
    * @param url URL of the server.
    */
    void setServerUrl( const QUrl& url );

    /**
    * Return the URL of the server.
    */
00090     const QUrl& serverUrl() const { return m_url; }

    /**
    * Append the batch to the execution queue.
    *
    * The CommandManager takes ownership of the batch object.
    *
    * @param batch Batch to execute.
    */
    void execute( AbstractBatch* batch );

    /**
    * Abort processing the batch and remove it from the execution queue.
    *
    * The batch object is deleted immediately.
    *
    * @param batch Batch to abort.
    */
    void abort( AbstractBatch* batch );

    /**
    * Return the type of the last error.
    */
00113     Error error() const { return m_error; }

    /**
    * Return the code of the last error.
    */
00118     int errorCode() const { return m_errorCode; }

    /**
    * Return full formatted error message;
    */
    QString errorMessage( const QString& whatFailed );

    /**
    * Return the server version;
    */
00128     const QString& serverVersion() const { return m_serverVersion; }    

private:
    void checkPendingCommand();

    void sendSetHostRequest();
    void sendCommandRequest( Command* command );

    bool handleCommandResponse();
    bool handleCommandReply( const Reply& reply );

    bool parseReply( const QString& string, Reply& reply );
    bool validateReply( const Reply& reply );

    QString makeSignature( const ReplyLine& line );

    QString quoteString( const QString& string );
    QString unquoteString( const QString& string );

    void setError( Error error, int code, const QString& string );

private slots:
    void dataSendProgress( int done, int total );
    void dataReadProgress( int done, int total );

    void responseHeaderReceived( const QHttpResponseHeader& response );

    void readyRead( const QHttpResponseHeader& response );

    void requestFinished( int id, bool error );

private:
    QHttp* m_http;

    QList<AbstractBatch*> m_batches;

    QUrl m_url;
    QString m_cookie;

    int m_currentRequest;
    AbstractBatch* m_currentBatch;
    Command* m_currentCommand;

    int m_responseStatus;
    QString m_responseReason;

    QString m_protocolVersion;
    QString m_serverVersion;

    QString m_contentType;

    Error m_error;
    int m_errorCode;
    QString m_errorString;
};

/**
* Global pointer used to access the CommandManager.
*/
extern CommandManager* commandManager;

}

#endif

Generated by  Doxygen 1.6.0   Back to index