User interface update
[qtmeetings] / src / IO / Communication / CommunicationManager.cpp
index 4212f68..32d6ed9 100644 (file)
@@ -6,14 +6,17 @@
 #include <QDateTime>
 #include <QDomDocument>
 #include <QDebug>
+#include "../../Domain/Configuration/Configuration.h";
 
 static const int ERROR_BASE=100;
 
-CommunicationManager::CommunicationManager( const ConnectionSettings &aConnection )
+
+CommunicationManager::CommunicationManager()
 {
-       iConnectionSettings = new ConnectionSettings( aConnection );
+
        iModifyingCommunication = NULL;
-       iFetchingCommunication = new Communication( aConnection );
+
+       iFetchingCommunication = new Communication();
 
        if ( iFetchingCommunication )
        {
@@ -23,27 +26,48 @@ CommunicationManager::CommunicationManager( const ConnectionSettings &aConnectio
                                 SLOT( readProgress( int, int, int ) )
                                );
                connect( iFetchingCommunication,
-                                SIGNAL( requestFinished( int, QHttp::Error ) ),
+                                SIGNAL( requestFinished( int, int ) ),
                                 this,
-                                SLOT( requestFinished( int, QHttp::Error ) )
+                                SLOT( requestFinished( int, int ) )
                                );
        }
+
 }
 
 CommunicationManager::~CommunicationManager()
 {
-       delete iConnectionSettings;
+       //delete iConnectionSettings;
        delete iModifyingCommunication;
        delete iFetchingCommunication;
        while ( !iMeetings.isEmpty() )
                delete iMeetings.takeFirst();
 }
 
-void CommunicationManager::fetchMeetings( const QDateTime &aFrom, const QDateTime &aUntil, const Room &aIn )
+void CommunicationManager::fetchMeetings( const int aWeek, const int aYear, const Room &aIn )
 {
+       //prevent making multiple simultaneous user availibility requests
+       //TODO: Would be nice to queue these requests instead of just aborting 
+       const RequestData* rd = findRequest( GetUserAvailability );
+       if( rd )
+               return;
+       
+       // Set from to beginning, 00:00 of the requested week
+       QDateTime from = QDateTime::currentDateTime();
+       from = from.addYears( aYear - from.date().year() );
+       from = from.addDays( -1*(from.date().dayOfWeek()-1) );
+       from = from.addDays( (aWeek - from.date().weekNumber())*7 );
+       QTime midnight = from.time();
+       midnight.setHMS( 0,0,0 );
+       from.setTime( midnight );
+       qDebug() << "CommunicationManager::fetchMeetings from: " << from.toString();
+       
+       QDateTime until = from.addDays(7);
+       qDebug() << "CommunicationManager::fetchMeetings until: " << until.toString();
+       
+       
        ReqMsgGetUserAvailability msg;
        msg.setTimeZone();
-       msg.setTimeWindow( aFrom, aUntil );
+       msg.setTimeWindow( from, until );
        msg.addUser( aIn.address() );
                
        int id = iFetchingCommunication->request( msg.getContentTypeForHeader(), msg.getMessage() );
@@ -87,6 +111,15 @@ void CommunicationManager::fetchMeetingDetails( Meeting& aMeeting )
        }
 }
 
+void CommunicationManager::cancelFetchMeetingDetails()
+{
+       const RequestData *rd = findRequest( GetCalendarItem );
+       if( rd != NULL ) {
+               int id = rd->requestId;
+               iCancelledRequests.append( id );
+       }
+}
+
 void CommunicationManager::getMeetingDetails( Meeting &aMeeting )
 {
        ReqMsgGetCalendarItem msg( aMeeting.secondaryId() );
@@ -102,42 +135,58 @@ void CommunicationManager::getMeetingDetails( Meeting &aMeeting )
        }
 }
 
-void CommunicationManager::requestFinished( int aRequestId, QHttp::Error aError )
+void CommunicationManager::requestFinished( int aRequestId, int aError )
 {
        RequestData* rd = takeRequest( aRequestId );
-       qDebug() << "CommunicationManager::requestFinished: id: " << aRequestId << " error: " << (int)aError;
+       if( iCancelledRequests.contains( rd->requestId ) )
+       {
+               iCancelledRequests.removeAll( rd->requestId );
+               delete rd;
+               return;
+       }
+       QByteArray* response = iFetchingCommunication->response( aRequestId );
+       qDebug() << "CommunicationManager::requestFinished: id: " << aRequestId << " error: " << aError;
 
-       if( aError != QHttp::NoError || rd == NULL )
+       if( aError != (int)(QHttp::NoError) || rd == NULL || response == NULL )
        {
-               int err = (int)aError;
-               if( rd == NULL )
+               int err = aError;
+               if( rd == NULL || response == NULL )
                        err = 10;
                delete rd;
                emit error( ERROR_BASE+(int)err, CommunicationManager::FetchingCommunication );
+               while ( !iMeetings.isEmpty() )
+                                       delete iMeetings.takeFirst();
+               emit meetingsFetched( iMeetings );
                return;
        }
 
-       const QByteArray& response = iFetchingCommunication->response( aRequestId );
-
        switch( rd->type )
        {
        case GetUserAvailability:
                {
-               ResMsgGetUserAvailability msg( response );
+               ResMsgGetUserAvailability msg( *response );
        
                while ( !iMeetings.isEmpty() )
                        delete iMeetings.takeFirst();
        
                int err = msg.getMeetingsFromResponse( iMeetings, *(rd->room) );
                if( err )
+                       {
                        emit error( ERROR_BASE+8, CommunicationManager::FetchingCommunication );
+                       while ( !iMeetings.isEmpty() )
+                                                               delete iMeetings.takeFirst();
+                                       emit meetingsFetched( iMeetings );
+                       }
                else
+                       {
+                       qDebug("*** COMMUNICATIONMANAGER ::: Meetings fetched!");
                        emit meetingsFetched( iMeetings );
+                       }
                break;
                }
        case ConvertId:
                {
-               ResponseMessage msg( response );
+               ResponseMessage msg( *response );
                QString id = msg.getNodeValue( QString( "Id" ),
                                                                           QDomNode::AttributeNode,
                                                                           QString( "AlternateId" ) );
@@ -148,15 +197,22 @@ void CommunicationManager::requestFinished( int aRequestId, QHttp::Error aError
                }
        case GetCalendarItem:
                {
-               ResMsgGetCalendarItem msg( response );
+               ResMsgGetCalendarItem msg( *response );
                int err = msg.getMeetingDetailsFromResponse( *(rd->meeting) );
                if( err )
+                       {
                        emit error( ERROR_BASE+9, CommunicationManager::FetchingCommunication );
+                       while ( !iMeetings.isEmpty() )
+                                                               delete iMeetings.takeFirst();
+                                       emit meetingsFetched( iMeetings );
+                       }
+
                else
                        emit meetingDetailsFetched( *(rd->meeting) );
                break;
                }
        }
+       delete response;
        delete rd;
 }
 
@@ -188,5 +244,16 @@ CommunicationManager::RequestData* CommunicationManager::takeRequest( int aReque
        return NULL;
 }
 
-
+const CommunicationManager::RequestData* CommunicationManager::findRequest( RequestType aRequestType ) const
+{
+       for( int i = iRequestInfos.count() - 1; i >= 0 ; i-- )
+       {
+               struct RequestData* rd = iRequestInfos[i];
+               if( rd->type == aRequestType )
+               {
+                       return rd;
+               }
+       }
+       return NULL;
+}