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