1 #include "CommunicationManager.h"
2 #include "Communication.h"
3 #include "ConnectionSettings.h"
7 #include <QDomDocument>
10 static const int ERROR_BASE=100;
12 CommunicationManager::CommunicationManager( const ConnectionSettings &aConnection )
14 iConnectionSettings = new ConnectionSettings( aConnection );
15 iModifyingCommunication = NULL;
16 iFetchingCommunication = new Communication( aConnection );
18 if ( iFetchingCommunication )
20 connect( iFetchingCommunication,
21 SIGNAL( readProgress( int, int, int ) ),
23 SLOT( readProgress( int, int, int ) )
25 connect( iFetchingCommunication,
26 SIGNAL( requestFinished( int, QHttp::Error ) ),
28 SLOT( requestFinished( int, QHttp::Error ) )
33 CommunicationManager::~CommunicationManager()
35 delete iConnectionSettings;
36 delete iModifyingCommunication;
37 delete iFetchingCommunication;
38 while ( !iMeetings.isEmpty() )
39 delete iMeetings.takeFirst();
42 void CommunicationManager::fetchMeetings( const QDateTime &aFrom, const QDateTime &aUntil, const Room &aIn )
44 ReqMsgGetUserAvailability msg;
46 msg.setTimeWindow( aFrom, aUntil );
47 msg.addUser( aIn.address() );
49 int id = iFetchingCommunication->request( msg.getContentTypeForHeader(), msg.getMessage() );
50 qDebug() << "CommunicationManager::fetchMeetings: id: " << id;
53 addRequest( GetUserAvailability, id );
54 iRequestInfos.first()->room = new Room( aIn );
58 void CommunicationManager::getSecondaryIdForMeeting( Meeting &aMeeting )
60 ReqMsgConvertMeetingId msg( aMeeting.primaryId(), aMeeting.room().address() );
62 QByteArray arr = msg.getMessage();
64 int id = iFetchingCommunication->request( msg.getContentTypeForHeader(), arr );
65 qDebug() << "CommunicationManager::getSecondaryIdForMeeting: id: " << id;
68 addRequest( ConvertId, id );
69 iRequestInfos.first()->meeting = &aMeeting;
73 void CommunicationManager::fetchMeetingDetails( Meeting& aMeeting )
75 if( aMeeting.detailsAvailable() )
77 emit meetingDetailsFetched( aMeeting );
80 if( aMeeting.secondaryId().isEmpty() )
82 getSecondaryIdForMeeting( aMeeting );
86 getMeetingDetails( aMeeting );
90 void CommunicationManager::getMeetingDetails( Meeting &aMeeting )
92 ReqMsgGetCalendarItem msg( aMeeting.secondaryId() );
94 QByteArray arr = msg.getMessage();
96 int id = iFetchingCommunication->request( msg.getContentTypeForHeader(), arr );
97 qDebug() << "CommunicationManager::getMeetingDetails: id: " << id;
100 addRequest( GetCalendarItem, id );
101 iRequestInfos.first()->meeting = &aMeeting;
105 void CommunicationManager::requestFinished( int aRequestId, QHttp::Error aError )
107 RequestData* rd = takeRequest( aRequestId );
108 qDebug() << "CommunicationManager::requestFinished: id: " << aRequestId << " error: " << (int)aError;
110 if( aError != QHttp::NoError || rd == NULL )
112 int err = (int)aError;
116 emit error( ERROR_BASE+(int)err, CommunicationManager::FetchingCommunication );
120 const QByteArray& response = iFetchingCommunication->response( aRequestId );
124 case GetUserAvailability:
126 ResMsgGetUserAvailability msg( response );
128 while ( !iMeetings.isEmpty() )
129 delete iMeetings.takeFirst();
131 int err = msg.getMeetingsFromResponse( iMeetings, *(rd->room) );
133 emit error( ERROR_BASE+8, CommunicationManager::FetchingCommunication );
135 emit meetingsFetched( iMeetings );
140 ResponseMessage msg( response );
141 QString id = msg.getNodeValue( QString( "Id" ),
142 QDomNode::AttributeNode,
143 QString( "AlternateId" ) );
144 qDebug( "ID IS: %s", id.toStdString().data() );
145 rd->meeting->setSecondaryId( id );
146 getMeetingDetails( *(rd->meeting) );
149 case GetCalendarItem:
151 ResMsgGetCalendarItem msg( response );
152 int err = msg.getMeetingDetailsFromResponse( *(rd->meeting) );
154 emit error( ERROR_BASE+9, CommunicationManager::FetchingCommunication );
156 emit meetingDetailsFetched( *(rd->meeting) );
163 void CommunicationManager::readProgress( int /*aRequestId*/, int aDone, int aTotal )
165 emit readProgress( aDone, aTotal, CommunicationManager::FetchingCommunication );
168 void CommunicationManager::addRequest( RequestType aType, int aRequestId )
170 RequestData* rd = new RequestData( aType, aRequestId );
171 iRequestInfos.append( rd );
174 CommunicationManager::RequestData* CommunicationManager::takeRequest( int aRequestId )
176 if( aRequestId == 0 )
179 for( int i = iRequestInfos.count() - 1; i >= 0 ; i-- )
181 struct RequestData* rd = iRequestInfos[i];
182 if( rd->requestId == aRequestId )
184 iRequestInfos.removeAt( i );