qtmeetings sources to Maemo garage
[qtmeetings] / src / IO / Communication / CommunicationManager.cpp
1 #include "CommunicationManager.h"
2 #include "Communication.h"
3 #include "ConnectionSettings.h"
4 #include "Meeting.h"
5 #include "Room.h"
6 #include <QDateTime>
7 #include <QDomDocument>
8 #include <QDebug>
9
10 static const int ERROR_BASE=100;
11
12 CommunicationManager::CommunicationManager( const ConnectionSettings &aConnection )
13 {
14         iConnectionSettings = new ConnectionSettings( aConnection );
15         iModifyingCommunication = NULL;
16         iFetchingCommunication = new Communication( aConnection );
17
18         if ( iFetchingCommunication )
19         {
20                 connect( iFetchingCommunication,
21                                  SIGNAL( readProgress( int, int, int ) ),
22                                  this,
23                                  SLOT( readProgress( int, int, int ) )
24                                 );
25                 connect( iFetchingCommunication,
26                                  SIGNAL( requestFinished( int, QHttp::Error ) ),
27                                  this,
28                                  SLOT( requestFinished( int, QHttp::Error ) )
29                                 );
30         }
31 }
32
33 CommunicationManager::~CommunicationManager()
34 {
35         delete iConnectionSettings;
36         delete iModifyingCommunication;
37         delete iFetchingCommunication;
38         while ( !iMeetings.isEmpty() )
39                 delete iMeetings.takeFirst();
40 }
41
42 void CommunicationManager::fetchMeetings( const QDateTime &aFrom, const QDateTime &aUntil, const Room &aIn )
43 {
44         ReqMsgGetUserAvailability msg;
45         msg.setTimeZone();
46         msg.setTimeWindow( aFrom, aUntil );
47         msg.addUser( aIn.address() );
48                 
49         int id = iFetchingCommunication->request( msg.getContentTypeForHeader(), msg.getMessage() );
50         qDebug() << "CommunicationManager::fetchMeetings: id: " << id;
51         if( id )
52         {
53                 addRequest( GetUserAvailability, id );
54                 iRequestInfos.first()->room = new Room( aIn );
55         }
56 }
57
58 void CommunicationManager::getSecondaryIdForMeeting( Meeting &aMeeting )
59 {
60         ReqMsgConvertMeetingId msg( aMeeting.primaryId(), aMeeting.room().address() );
61         
62         QByteArray arr = msg.getMessage();
63
64         int id = iFetchingCommunication->request( msg.getContentTypeForHeader(), arr );
65         qDebug() << "CommunicationManager::getSecondaryIdForMeeting: id: " << id;
66         if( id )
67         {
68                 addRequest( ConvertId, id );
69                 iRequestInfos.first()->meeting = &aMeeting;
70         }
71 }
72
73 void CommunicationManager::fetchMeetingDetails( Meeting& aMeeting )
74 {
75         if( aMeeting.detailsAvailable() )
76         {
77                 emit meetingDetailsFetched( aMeeting );
78                 return;
79         }
80         if( aMeeting.secondaryId().isEmpty() )
81         {
82                 getSecondaryIdForMeeting( aMeeting );
83         }
84         else
85         {
86                 getMeetingDetails( aMeeting );
87         }
88 }
89
90 void CommunicationManager::getMeetingDetails( Meeting &aMeeting )
91 {
92         ReqMsgGetCalendarItem msg( aMeeting.secondaryId() );
93         
94         QByteArray arr = msg.getMessage();
95         
96         int id = iFetchingCommunication->request( msg.getContentTypeForHeader(), arr );
97         qDebug() << "CommunicationManager::getMeetingDetails: id: " << id;
98         if( id )
99         {
100                 addRequest( GetCalendarItem, id );
101                 iRequestInfos.first()->meeting = &aMeeting;
102         }
103 }
104
105 void CommunicationManager::requestFinished( int aRequestId, QHttp::Error aError )
106 {
107         RequestData* rd = takeRequest( aRequestId );
108         qDebug() << "CommunicationManager::requestFinished: id: " << aRequestId << " error: " << (int)aError;
109
110         if( aError != QHttp::NoError || rd == NULL )
111         {
112                 int err = (int)aError;
113                 if( rd == NULL )
114                         err = 10;
115                 delete rd;
116                 emit error( ERROR_BASE+(int)err, CommunicationManager::FetchingCommunication );
117                 return;
118         }
119
120         const QByteArray& response = iFetchingCommunication->response( aRequestId );
121
122         switch( rd->type )
123         {
124         case GetUserAvailability:
125                 {
126                 ResMsgGetUserAvailability msg( response );
127         
128                 while ( !iMeetings.isEmpty() )
129                         delete iMeetings.takeFirst();
130         
131                 int err = msg.getMeetingsFromResponse( iMeetings, *(rd->room) );
132                 if( err )
133                         emit error( ERROR_BASE+8, CommunicationManager::FetchingCommunication );
134                 else
135                         emit meetingsFetched( iMeetings );
136                 break;
137                 }
138         case ConvertId:
139                 {
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) );
147                 break;
148                 }
149         case GetCalendarItem:
150                 {
151                 ResMsgGetCalendarItem msg( response );
152                 int err = msg.getMeetingDetailsFromResponse( *(rd->meeting) );
153                 if( err )
154                         emit error( ERROR_BASE+9, CommunicationManager::FetchingCommunication );
155                 else
156                         emit meetingDetailsFetched( *(rd->meeting) );
157                 break;
158                 }
159         }
160         delete rd;
161 }
162
163 void CommunicationManager::readProgress( int /*aRequestId*/, int aDone, int aTotal )
164 {
165         emit readProgress( aDone, aTotal, CommunicationManager::FetchingCommunication );
166 }
167
168 void CommunicationManager::addRequest( RequestType aType, int aRequestId )
169 {
170         RequestData* rd = new RequestData( aType, aRequestId );
171         iRequestInfos.append( rd );
172 }
173
174 CommunicationManager::RequestData* CommunicationManager::takeRequest( int aRequestId )
175 {
176         if( aRequestId == 0 )
177                 return NULL;
178
179         for( int i = iRequestInfos.count() - 1; i >= 0 ; i-- )
180         {
181                 struct RequestData* rd = iRequestInfos[i];
182                 if( rd->requestId == aRequestId )
183                 {
184                         iRequestInfos.removeAt( i );
185                         return rd;
186                 }
187         }
188         return NULL;
189 }
190
191
192