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