1 #ifndef COMMUNICATION_H_
2 #define COMMUNICATION_H_
9 class ConnectionSettings;
11 //! Class for handling HTTP requests and responses.
13 * This class uses the QHttp class to make HTTP requests. HTTP responses are
14 * returned as QByteArray.
16 class Communication : public QObject
23 * \param aConnection Reference to ConnectionSettings which holds
24 * the server to connect to and authentication information.
26 Communication( const ConnectionSettings &aConnection );
28 virtual ~Communication();
29 //! Returns the response of a request identified by aRequestId.
31 * \param aRequestId Request id number.
33 QByteArray* response( int aRequestId );
35 //! Makes a HTTP request to the server defined in the constructor.
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.
41 int request( const QString &aCommand, const QByteArray &aContent );
44 //! Emitted when a request ongoing. Reports the bytes read from the server.
46 * \param aRequestId Request id number.
47 * \param aDone Bytes read from the server.
48 * \param aTotal Bytes total of the response.
50 void readProgress( int aRequestId, int aDone, int aTotal );
51 //! Emitted when a request is started.
53 * \param aRequestId Request id number.
55 void requestStarted( int aRequestId );
56 //! Emitted when a request is finished.
58 * \param aRequestId Request id number.
59 * \param aError Error code of the request finished.
61 void requestFinished( int aRequestId, int aError );
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 );
77 * Instance of Connection settings of the communication
79 ConnectionSettings *iConnectionSettings;
85 * QHash of requests and response buffers
87 QHash<int, QByteArray*> iResponses;
89 * ID number of a request currently ongoing
93 * Counter how many times auhentication was failed to the server
98 #endif /*COMMUNICATION_H_*/