1 #include "Communication.h"
2 #include "ConnectionSettings.h"
3 #include <QAuthenticator>
5 Communication::Communication( const ConnectionSettings &aConnection ) :
9 iConnectionSettings = new ConnectionSettings( aConnection );
11 iHttp = new QHttp( iConnectionSettings->serverUrl().toString(), QHttp::ConnectionModeHttps );
14 SIGNAL( readyRead( const QHttpResponseHeader& ) ),
16 SLOT( processResponse( const QHttpResponseHeader& ) )
19 SIGNAL( requestStarted( int ) ),
21 SLOT( handleRequestStarted( int ) )
24 SIGNAL( requestFinished( int, bool ) ),
26 SLOT( handleResults( int, bool ) )
29 SIGNAL( dataReadProgress( int, int ) ),
31 SLOT( handleReadProgress( int, int ) )
34 SIGNAL( authenticationRequired( const QString&, quint16, QAuthenticator* ) ),
36 SLOT( handleAuthentication( const QString&, quint16, QAuthenticator* ) ) );
38 SIGNAL( sslErrors( const QList<QSslError>& ) ),
40 SLOT( ignoreSslErrors() )/*this, SLOT( notifySsl( const QList<QSslError>& ) )*/
44 Communication::~Communication()
46 delete iConnectionSettings;
48 QList<QByteArray*> responses = iResponses.values();
49 while(!responses.isEmpty())
50 delete responses.takeFirst();
53 void Communication::processResponse( const QHttpResponseHeader& aHeader )
55 if ( aHeader.statusCode() == 200 )
57 if( iResponses.contains( iCurrentRequest ) )
59 QByteArray* response = iResponses.value( iCurrentRequest, NULL );
60 response->append( iHttp->readAll() );
65 void Communication::handleRequestStarted( int aRequestId )
67 if( iResponses.contains( aRequestId ) )
69 iCurrentRequest = aRequestId;
70 emit requestStarted( aRequestId );
74 void Communication::handleResults( int aId, bool /*aError*/ )
76 if( iCurrentRequest != 0 && aId == iCurrentRequest )
80 int err = (int)iHttp->error();
81 if( iHttp->error() == QHttp::Aborted && iAuthFailCount > 1 )
83 emit requestFinished( aId, err );
87 void Communication::handleAuthentication( const QString& /*aHost*/, quint16 /*aPort*/, QAuthenticator* aAuthenticator )
89 aAuthenticator->setPassword( iConnectionSettings->password() );
90 aAuthenticator->setUser( iConnectionSettings->username() );
92 if( iAuthFailCount > 1 )
98 int Communication::request( const QString &aCommand, const QByteArray &aContent )
100 if( iAuthFailCount > 1 )
103 QHttpRequestHeader header( QString( "POST" ), QString( "/ews/exchange.asmx" ) );
104 header.setValue( "Host", iConnectionSettings->serverUrl().toString() );
105 QString command = aCommand;
106 header.setContentType( command );
108 int id = iHttp->request( header, aContent );
109 if( iResponses.contains( id ) )
111 QByteArray* response = iResponses.value( id, NULL );
116 iResponses.insert( id, new QByteArray() );
122 QByteArray* Communication::response( int aRequestId )
124 if( !iResponses.contains( aRequestId ) )
126 QByteArray* response = iResponses.take( aRequestId );
130 void Communication::handleReadProgress( int aDone, int aTotal )
132 emit readProgress( iCurrentRequest, aDone, aTotal );