5c3b9244f6466ccfe4b1063928250892eddc6d00
[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
13         connect( iHttp,
14                          SIGNAL( readyRead( const QHttpResponseHeader& ) ),
15                          this,
16                          SLOT( processResponse( const QHttpResponseHeader& ) )
17                         );
18         connect( iHttp,
19                          SIGNAL( requestStarted( int ) ),
20                          this,
21                          SLOT( handleRequestStarted( int ) )
22                         );
23         connect( iHttp,
24                          SIGNAL( requestFinished( int, bool ) ),
25                          this,
26                          SLOT( handleResults( int, bool ) )
27                         );
28         connect( iHttp,
29                          SIGNAL( dataReadProgress( int, int ) ),
30                          this,
31                          SLOT( handleReadProgress( int, int ) )
32                         );
33         connect( iHttp,
34                          SIGNAL( authenticationRequired( const QString&, quint16, QAuthenticator* ) ),
35                          this,
36                          SLOT( handleAuthentication( const QString&, quint16, QAuthenticator* ) ) );
37         connect( iHttp,
38                          SIGNAL( sslErrors( const QList<QSslError>& ) ),
39                          iHttp,
40                          SLOT( ignoreSslErrors() )/*this, SLOT( notifySsl( const QList<QSslError>& ) )*/
41                         );
42 }
43
44 Communication::~Communication()
45 {
46         delete iConnectionSettings;
47         delete iHttp;
48         QList<QByteArray*> responses = iResponses.values();
49         while(!responses.isEmpty())
50                 delete responses.takeFirst();
51 }
52
53 void Communication::processResponse( const QHttpResponseHeader& aHeader )
54 {
55         if ( aHeader.statusCode() == 200 )
56         {
57                 if( iResponses.contains( iCurrentRequest ) )
58                 {
59                         QByteArray* response = iResponses.value( iCurrentRequest, NULL );
60                         response->append( iHttp->readAll() );
61                 }
62         }
63 }
64
65 void Communication::handleRequestStarted( int aRequestId )
66 {
67         if( iResponses.contains( aRequestId ) )
68         {
69                 iCurrentRequest = aRequestId;
70                 emit requestStarted( aRequestId );      
71         }
72 }
73
74 void Communication::handleResults( int aId, bool /*aError*/ )
75 {
76         if( iCurrentRequest != 0 && aId == iCurrentRequest )
77         {
78                 iAuthFailCount = 0;
79                 iCurrentRequest = 0;
80                 int err = (int)iHttp->error();
81                 if( iHttp->error() == QHttp::Aborted && iAuthFailCount > 1 )
82                         err = 11;
83                 emit requestFinished( aId, err );
84         }
85 }
86
87 void Communication::handleAuthentication( const QString& /*aHost*/, quint16 /*aPort*/, QAuthenticator* aAuthenticator )
88 {
89         aAuthenticator->setPassword( iConnectionSettings->password() );
90         aAuthenticator->setUser( iConnectionSettings->username() );
91         iAuthFailCount++;
92         if( iAuthFailCount > 1 )
93         {
94                 iHttp->abort();
95         }
96 }
97
98 int Communication::request( const QString &aCommand, const QByteArray &aContent )
99 {
100         if( iAuthFailCount > 1 )
101                 return 0;
102         
103         QHttpRequestHeader header( QString( "POST" ), QString( "/ews/exchange.asmx" ) );
104         header.setValue( "Host", iConnectionSettings->serverUrl().toString() );
105         QString command = aCommand;
106         header.setContentType( command );
107
108         int id = iHttp->request( header, aContent );
109         if( iResponses.contains( id ) )
110         {
111                 QByteArray* response = iResponses.value( id, NULL );
112                 response->clear();
113         }
114         else
115         {
116                 iResponses.insert( id, new QByteArray() );
117         }
118         
119         return id;
120 }
121
122 QByteArray* Communication::response( int aRequestId )
123 {
124         if( !iResponses.contains( aRequestId ) )
125                 return NULL;
126         QByteArray* response = iResponses.take( aRequestId );
127         return response;
128 }
129
130 void Communication::handleReadProgress( int aDone, int aTotal )
131 {
132         emit readProgress( iCurrentRequest, aDone, aTotal );
133 }