82b847e084096b1518e04192f310d54887b0e2f7
[qtmeetings] / src / IO / Communication / Communication.cpp
1 #include "Communication.h"
2 #include "ConnectionSettings.h"
3 #include <QAuthenticator>
4
5 Communication::Communication( const ConnectionSettings &aConnection ) :
6         iCurrentRequest(0),
7         iAuthFailCount(0)
8 {
9         iConnectionSettings = new ConnectionSettings( aConnection );
10
11         iHttp = new QHttp( iConnectionSettings->serverUrl().toString(), QHttp::ConnectionModeHttps );
12         iResponse = new QByteArray();
13
14         connect( iHttp,
15                          SIGNAL( readyRead( const QHttpResponseHeader& ) ),
16                          this,
17                          SLOT( processResponse( const QHttpResponseHeader& ) )
18                         );
19         connect( iHttp,
20                          SIGNAL( requestStarted( int ) ),
21                          this,
22                          SLOT( handleRequestStarted( int ) )
23                         );
24         connect( iHttp,
25                          SIGNAL( requestFinished( int, bool ) ),
26                          this,
27                          SLOT( handleResults( int, bool ) )
28                         );
29         connect( iHttp,
30                          SIGNAL( dataReadProgress( int, int ) ),
31                          this,
32                          SLOT( handleReadProgress( int, int ) )
33                         );
34         connect( iHttp,
35                          SIGNAL( authenticationRequired( const QString&, quint16, QAuthenticator* ) ),
36                          this,
37                          SLOT( handleAuthentication( const QString&, quint16, QAuthenticator* ) ) );
38         connect( iHttp,
39                          SIGNAL( sslErrors( const QList<QSslError>& ) ),
40                          iHttp,
41                          SLOT( ignoreSslErrors() )/*this, SLOT( notifySsl( const QList<QSslError>& ) )*/
42                         );
43 }
44
45 Communication::~Communication()
46 {
47         delete iConnectionSettings;
48         delete iHttp;
49         delete iResponse;
50 }
51
52 void Communication::processResponse( const QHttpResponseHeader& aHeader )
53 {
54         if ( aHeader.statusCode() == 200 )
55         {
56                 iResponse->append( iHttp->readAll() );
57         }
58 }
59
60 void Communication::handleRequestStarted( int aRequestId )
61 {
62         if( aRequestId != 0 && iCurrentRequest != 0 &&
63             aRequestId == iCurrentRequest )
64         {
65                 emit requestStarted( iCurrentRequest ); 
66         }
67 }
68
69 void Communication::handleResults( int aId, bool /*aError*/ )
70 {
71         if( aId == iCurrentRequest )
72         {
73                 iCurrentRequest = 0;
74                 emit requestFinished( aId, iHttp->error() );
75         }
76 }
77
78 void Communication::handleAuthentication( const QString& /*aHost*/, quint16 /*aPort*/, QAuthenticator* aAuthenticator )
79 {
80         aAuthenticator->setPassword( iConnectionSettings->password() );
81         aAuthenticator->setUser( iConnectionSettings->username() );
82         iAuthFailCount++;
83         if( iAuthFailCount > 1 )
84         {
85                 iHttp->abort();
86         }
87 }
88
89 int Communication::request( const QString &aCommand, const QByteArray &aContent )
90 {
91         //TODO: This is just temporarily here to implement multiple request support     
92         if( iCurrentRequest )
93                 return 0;
94         QHttpRequestHeader header( QString( "POST" ), QString( "/ews/exchange.asmx" ) );
95         header.setValue( "Host", iConnectionSettings->serverUrl().toString() );
96         QString command = aCommand;
97         header.setContentType( command );
98
99         iAuthFailCount = 0;
100         iResponse->clear();
101         iCurrentRequest = iHttp->request( header, aContent );
102         
103         return iCurrentRequest;
104 }
105
106 const QByteArray& Communication::response( int /*aRequestId*/ )
107 {
108         return *iResponse;
109 }
110
111 void Communication::handleReadProgress( int aDone, int aTotal )
112 {
113         emit readProgress( iCurrentRequest, aDone, aTotal );
114 }