41dedb534f0670bf46b59e69116cf6a0c77af6cd
[qtmeetings] / src / IO / Communication / Communication.h
1 #ifndef COMMUNICATION_H_
2 #define COMMUNICATION_H_
3
4 #include <QObject>
5 #include <QHttp>
6 #include <QHash>
7
8 class QByteArray;
9 class ConnectionSettings;
10
11 //! Class for handling HTTP requests and responses.
12 /*!
13  *  This class uses the QHttp class to make HTTP requests. HTTP responses are
14  *  returned as QByteArray.
15  */
16 class Communication : public QObject
17 {
18         Q_OBJECT
19
20 public:
21         //! Constructor.
22         /*! 
23          *  \param aConnection Reference to ConnectionSettings which holds
24          *  the server to connect to and authentication information.
25         */
26         Communication( const ConnectionSettings &aConnection );
27         
28         virtual ~Communication();
29         //! Returns the response of a request identified by aRequestId.
30         /*! 
31          *  \param aRequestId Request id number.
32         */
33         QByteArray* response( int aRequestId );
34
35         //! Makes a HTTP request to the server defined in the constructor.
36         /*! 
37          *  Returns a request id number if successful, otherwise zero.
38          *  \param aCommand Content type string of the HTTP header.
39          *  \param aContent Content body of the HTTP POST request.
40         */
41         int request( const QString &aCommand, const QByteArray &aContent );
42
43 signals:
44         //! Emitted when a request ongoing. Reports the bytes read from the server.
45         /*! 
46         *   \param aRequestId Request id number.
47         *   \param aDone Bytes read from the server.
48         *   \param aTotal Bytes total of the response.
49         */
50         void readProgress( int aRequestId, int aDone, int aTotal );
51         //! Emitted when a request is started.
52         /*! 
53         *   \param aRequestId Request id number.
54         */
55         void requestStarted( int aRequestId );
56         //! Emitted when a request is finished.
57         /*! 
58         *   \param aRequestId Request id number.
59         *   \param aError Error code of the request finished.
60         */
61         void requestFinished( int aRequestId, int aError );
62
63 protected slots:
64         //! Connected to QHttp::readyRead to read the response buffer.
65         void processResponse( const QHttpResponseHeader& aHeader );
66         //! Connected to QHttp::authenticationRequired to provide the user name and password defined in ConnectionSettings
67         void handleAuthentication( const QString& aHost, quint16 aPort, QAuthenticator* aAuthenticator );
68         //! Connected to QHttp::requestFinished 
69         void handleResults( int aId, bool aError );
70         //! Connected to QHttp::requestStarted
71         void handleRequestStarted( int aRequestId );
72         //! Connected to QHttp::dataReadProgress
73         void handleReadProgress( int aDone, int aTotal );
74
75 private:
76         /*!
77          * Instance of Connection settings of the communication
78         */
79         ConnectionSettings *iConnectionSettings;
80         /*!
81          * Instance of QHttp
82         */
83         QHttp* iHttp;
84         /*!
85          * QHash of requests and response buffers
86         */
87         QHash<int, QByteArray*> iResponses;
88         /*!
89          * ID number of a request currently ongoing
90         */
91         int iCurrentRequest;
92         /*!
93          * Counter how many times auhentication was failed to the server
94         */
95         int iAuthFailCount;
96 };
97
98 #endif /*COMMUNICATION_H_*/