Changed something cannot remember what
[qtmeetings] / src / BusinessLogic / Engine.cpp
index 2f943a7..fa9ccc1 100644 (file)
 #include "Engine.h"
-
-#include <QTimer>
-#include <QList>
 #include "Room.h"
 #include "Meeting.h"
 #include "ConnectionSettings.h"
 #include "Configuration.h"
+#include "DisplaySettings.h"
 #include "CommunicationManager.h"
-#include "DeviceManager.h"
+// #include "DeviceManager.h"
 #include "Clock.h"
 #include "ErrorMapper.h"
+#include "UIManager.h"
 
+#include <QApplication>
+#include <QTimer>
+#include <QList>
 #include <QtDebug>
 
-QTime Engine::endOfTheDay = QTime( 23, 59, 0, 0 ); // end of the day is 11:59pm
+QTime Engine::endOfTheDay = QTime( 23, 59, 0, 0); // end of the day is 11:59pm
+const int IDLE_TIME_MULTIPLIER = 60000; // Multiplies milliseconds to minutes
+
+// Macro to help deleting objects. This could be global.
+#define QT_DELETE(X) \
+       if ( X != 0 ) \
+       { \
+               delete X; \
+               X = 0; \
+       }
+
 
 Engine::Engine() :
-               iClock( 0 ), iConfiguration( Configuration::instance() ), iCommunication( 0 ), iCurrentRoom( 0 )
+               iClock( 0 ), iConfiguration( 0 ), iCommunication( 0 ),
+               iWindowManager( 0 ), iUIManager( 0 )
 {
-       // if reading of configuration fails, signal that initialization failed
-       if ( iConfiguration == 0 )
-       {
-               QTimer::singleShot( 0, this, SIGNAL( initializationFailed() ) );
-               return;
-       }
-       // initialize communication
-       iCommunication = new CommunicationManager( *(iConfiguration->connectionSettings()) );
-       connect( iCommunication,
-                        SIGNAL( error( int, CommunicationManager::CommunicationType  ) ),
-                        this,
-                        SLOT( errorHandler( int ) ) );
-       connect( iCommunication,
-                        SIGNAL( meetingsFetched( const QList<Meeting*>& ) ),
-                        this,
-                        SLOT( meetingsFetched( const QList<Meeting*>& ) )
-                       );
-       connect( iCommunication,
-                        SIGNAL( meetingDetailsFetched( Meeting& ) ),
-                        this,
-                        SLOT( meetingDetailsFetched( Meeting& ) )
-                       );
+       qDebug() << "Engine::Engine()";
+       
+       initConfiguration();
+       initDevice();
+       initCommunication();
+       initUserInterface();
+       
+       //initialize idle time counter
+       iIdleTimeCounter = new QTimer();
+       iIdleTimeCounter->setSingleShot( true);
+       iIdleTimeCounter->setInterval(IDLE_TIME_MULTIPLIER * iConfiguration->displaySettings()->screensaver() );
+       iIdleTimeCounter->start();
 
        // create application clock
        iClock = new Clock;
        connect( iClock, SIGNAL( tick( QDateTime ) ), this, SLOT( checkStatusOfAllRooms() ) );
+       // connect( iClock, SIGNAL( tick( QDateTime ) ), iWindowManager, SLOT( distributeDateTimeInfo( QDateTime ) ) );
 
+       // Create auto refresh timer
        iAutoRefresh = new QTimer;
-       iAutoRefresh->setInterval( iConfiguration->connectionSettings()->refreshInterval() * 1000 );
+       iAutoRefresh->setInterval(iConfiguration->connectionSettings()->refreshInterval() * 1000);
        iAutoRefresh->start();
        connect( iAutoRefresh, SIGNAL( timeout() ), iAutoRefresh, SLOT( start() ) );
-       connect( iAutoRefresh, SIGNAL( timeout() ), this, SLOT( fetchMeetings() ) );
-
-       iDevice = new DeviceManager( iConfiguration->startupSettings() );
-       connect( iDevice, SIGNAL( error( int, const QString& ) ), this, SLOT( errorHandler( int, const QString& ) ) );
-       iDevice->initDeviceManager();
+       // connect( iAutoRefresh, SIGNAL( timeout() ), this, SLOT( fetchMeetings() ) );
+       
+       if( iDevice->currentOperationMode() == DeviceManager::KioskMode )
+       {
+               iWindowManager->setFullscreen();
+       }
 
-       QTimer::singleShot( 0, this, SLOT( fetchMeetings() ) );
+       connectSignals();
+       
+       // QTimer::singleShot( 0, this, SLOT( fetchMeetings() ) );
 
        // TODO: continue implementation
 }
 
 Engine::~Engine()
 {
+       qDebug() << "Engine::~Engine()";
        while ( !iMeetings.isEmpty() )
                delete iMeetings.takeFirst();
-}
+       
+       if ( iIdleTimeCounter != 0 )
+       {
+               iIdleTimeCounter->stop();
+               delete iIdleTimeCounter;
+               iIdleTimeCounter = 0;
+       }
+       QT_DELETE( iClock );
+       QT_DELETE( iDevice );
 
-Room* Engine::defaultRoom()
-{
-       return iConfiguration->defaultRoom();
+       QT_DELETE( iUIManager );
+       QT_DELETE( iWindowManager );
 }
 
-Clock* Engine::clock()
+void Engine::closeApplication()
 {
-       return iClock;
+       qDebug() << "Engine::closeApplication()";
+       // closes application after 1 second
+       QTimer::singleShot( 1000, QApplication::instance(), SLOT( quit() ));
 }
 
-Configuration* Engine::configuration()
-{
-       return iConfiguration;
-}
-
-DeviceManager* Engine::deviceManager()
+Room* Engine::defaultRoom()
 {
-       return iDevice;
+       qDebug() << "Engine::defaultRoom()";
+       return iConfiguration->defaultRoom();
 }
 
 void Engine::checkStatusOfAllRooms()
 {
+//     qDebug() << "Engine::checkStatusOfAllRooms()";
        // iterate trough on the rooms
-       for ( int i = 0; i < iConfiguration->rooms().count(); i++ )
+       for (int i = 0; i < iConfiguration->rooms().count(); i++)
        {
                // and check the status
-               roomStatusInfoNeeded( iConfiguration->rooms().at( i ) );
+               roomStatusInfoNeeded(iConfiguration->rooms().at(i) );
        }
 }
 
-int Engine::indexOfMeetingAt( Room *aRoom, QDateTime aAt )
+int Engine::indexOfMeetingAt(Room *aRoom, QDateTime aAt)
 {
+//     qDebug() << "Engine::indexOfMeetingAt( Room *, QDateTime )";
        for ( int i = 0; i < iMeetings.count(); i++ )
        {
                // exchange server ensures that there is only one meeting in a room at a specified time
-               if ( aRoom->equals( iMeetings.at( i )->room() )
-                         && iMeetings.at( i )->startsAt() <= aAt
-                         && iMeetings.at( i )->endsAt() >= aAt )
+               if (aRoom->equals(iMeetings.at( i )->room() ) && iMeetings.at( i )->startsAt() <= aAt && iMeetings.at( i )->endsAt() >= aAt)
                {
                        return i;
                }
@@ -111,20 +125,18 @@ int Engine::indexOfMeetingAt( Room *aRoom, QDateTime aAt )
        return -1;
 }
 
-int Engine::indexOfMeetingAfter( Room *aRoom, QDateTime aAfter )
+int Engine::indexOfMeetingAfter(Room *aRoom, QDateTime aAfter)
 {
+//     qDebug() << "Engine::indexOfMeetingAfter( Room *, QDateTime )";
        // seeks for the next meeting on the SAME DAY
        int min = -1;
-       for ( int i = 0; i < iMeetings.count(); i++ )
+       for (int i = 0; i < iMeetings.count(); i++)
        {
                // if the meeting is in the same room, on the same day but after the specified time
-               if ( aRoom->equals( iMeetings.at( i )->room() )
-                         && iMeetings.at( i )->startsAt().date() == aAfter.date()
-                         && iMeetings.at( i )->startsAt() > aAfter )
+               if (aRoom->equals(iMeetings.at( i )->room() ) && iMeetings.at( i )->startsAt().date() == aAfter.date() && iMeetings.at( i )->startsAt() > aAfter)
                {
                        // if there was not any meeting find yet or the previously found is a later one then the (i)th
-                       if ( min == -1
-                                 || iMeetings.at( min )->startsAt() > iMeetings.at( i )->startsAt() )
+                       if (min == -1 || iMeetings.at( min )->startsAt() > iMeetings.at( i )->startsAt() )
                        {
                                min = i;
                        }
@@ -133,109 +145,233 @@ int Engine::indexOfMeetingAfter( Room *aRoom, QDateTime aAfter )
        return min;
 }
 
-void Engine::roomStatusInfoNeeded( Room *aRoom )
+void Engine::roomStatusInfoNeeded(Room *aRoom)
 {
+//     qDebug() << "Engine::roomStatusInfoNeeded( Room * )";
        if ( aRoom == 0 )
        {
                return;
        }
 
-       int indexOfCurrentMeeting = indexOfMeetingAt( aRoom, iClock->datetime() );
-       int indexOfNextMeeting = indexOfMeetingAfter( aRoom, iClock->datetime() );
-
-//     qDebug() << QString( "Engine::roomStatusInfoNeeded\troom:%1current:%2 next:%3" ).arg( aRoom->toString() ).arg( indexOfCurrentMeeting ).arg( indexOfNextMeeting );
+       int indexOfCurrentMeeting = indexOfMeetingAt(aRoom, iClock->datetime() );
+       int indexOfNextMeeting = indexOfMeetingAfter(aRoom, iClock->datetime() );
 
        // if there is no meeting, then status is Free; otherwise Busy
-       Room::Status status = ( indexOfCurrentMeeting == -1 ) ? Room::FreeStatus : Room::BusyStatus;
+       Room::Status status = (indexOfCurrentMeeting == -1 ) ? Room::FreeStatus : Room::BusyStatus;
        // if room is Busy, then check end time, otherwise...
-       QTime until = ( status == Room::BusyStatus ) ? iMeetings.at( indexOfCurrentMeeting )->endsAt().time() :
-                         // ...if there is meeting following on the same day then check end time, otherwise end is the of the working day
-                         (( indexOfNextMeeting != -1 ) ? iMeetings.at( indexOfNextMeeting )->startsAt().time() : Engine::endOfTheDay );
+       QTime until = (status == Room::BusyStatus ) ? iMeetings.at( indexOfCurrentMeeting )->endsAt().time() :
+       // ...if there is meeting following on the same day then check end time, otherwise end is the of the working day
+       ( ( indexOfNextMeeting != -1 ) ? iMeetings.at( indexOfNextMeeting )->startsAt().time() : Engine::endOfTheDay );
 
-       emit roomStatusChanged( aRoom, status, until );
+       //currently works only for deafult room
+//     if( aRoom->equals( *(defaultRoom() ) ) )
+//             iWindowManager->roomStatusChanged( aRoom, status, until );
 }
 
+/*
 void Engine::fetchMeetings()
 {
-       // TODO : define interval correctly. at the moment it's +/- 14 days
-       Room *room = iCurrentRoom;
-       if ( room == 0 ) room = defaultRoom();
-       qDebug() << "Engine::fetchMeetings for " << room->name();
-       fetchMeetings( iClock->datetime().addDays( -14 ), iClock->datetime().addDays( 14 ), room );
+       qDebug() << "Engine::fetchMeetings for " << iCurrentRoom;
+       QDateTime from( iWindowManager->weeklyView()->beginnigOfShownWeek() );
+       QDateTime to( from.addDays( 7 ) );
+       // fetchMeetings( from, to, iWindowManager->weeklyView()->currentRoom() );
+       // Signal is connected to the currentRoomChanged slot which keeps the iCurrentRoom up to date
+       fetchMeetings( from, to, iCurrentRoom );
 }
+*/
 
-void Engine::fetchMeetings( const QDateTime &aFrom, const QDateTime &aUntil, Room *aIn )
+void Engine::fetchMeetingDetails( Meeting *aMeeting )
 {
-       iCommunication->fetchMeetings( aFrom, aUntil, *aIn );
+       qDebug() << "Engine::fetchMeetingDetails( Meeting* )";
+/*     iWindowManager->showProgressBar(tr("Please Wait"), true);
+       iWindowManager->updateProgressBar(tr("Fetching Meeting Details...") );
+       connect(iWindowManager, 
+       SIGNAL( progressBarCancelled() ), this, 
+       SLOT( fetchMeetingDetailsCancelled() ));
+       iCommunication->fetchMeetingDetails( *aMeeting); */
+       iCommunication->fetchMeetingDetails( *aMeeting );
 }
 
-void Engine::fetchMeetingDetails( Meeting *aMeeting )
+void Engine::meetingsFetched( const QList<Meeting*> &aMeetings )
 {
-       iCommunication->fetchMeetingDetails( *aMeeting );
+       qDebug() << "Engine::meetingsFetched( const QList<Meeting*> & )";
+       
+       for ( int i = 0; i < iMeetings.count(); ++i ) {
+               Meeting* m = iMeetings.takeAt( i );
+               delete m;
+       }
+       iMeetings.clear();
+       for ( int i = 0; i < aMeetings.count(); ++i ) {
+               Meeting* m = new Meeting( *( aMeetings.at( i ) ) );
+               iMeetings.append( m );
+       }
+
+       // refresh room status info
+       roomStatusInfoNeeded( defaultRoom() );
+}
+
+void Engine::errorHandler( int aCode, const QString &aAddInfo )
+{      
+       if ( iWindowManager != 0 )
+       {
+               iWindowManager->error( ErrorMapper::codeToString( aCode, aAddInfo ) );
+       }
+}
+
+void Engine::fetchMeetings( const QDateTime &aFrom, const QDateTime &aUntil, const Room *aIn )
+{
+       qDebug()
+                       << "Engine::fetchMeetings( const QDateTime &, const QDateTime &, const Room * )";
+       iCommunication->fetchMeetings(aFrom, aUntil, *aIn);
+}
+
+void Engine::cancelFetchMeetingDetails()
+{
+       iCommunication->cancelFetchMeetingDetails();
 }
 
-bool Engine::isMeetingInList( const QList<Meeting*> &aList, const Meeting *aMeeting )
+void Engine::shownWeekChanged( QDate aFrom )
 {
-       for ( int i = 0; i < aList.count(); i++ )
+       qDebug() << "[Engine::shownWeekChanged] <Invoked>";
+       QDateTime from( aFrom );
+       QDateTime to( aFrom.addDays( 7 ), QTime( 23, 59 ) );
+       qDebug() << "[Engine::shownWeekChanged] <From " << aFrom.toString( "d.m. h:mm" ) << " to " << to.toString( "d.m. h:mm" ) << ">";
+       iCommunication->fetchMeetings( from, to, *defaultRoom() );
+//     fetchMeetings( from, to, iWindowManager->weeklyView()->currentRoom() );
+}
+
+void Engine::changeDeviceMode( bool aChange )
+{
+       if ( aChange )
        {
-               if ( aMeeting->equals( *(aList.at( i )) ) )
-               {
-                       return true;
-               }
+               connect( iDevice, SIGNAL( changingModeFailed() ), this, SLOT( changeModeFailed() ) );
+               iAutoRefresh->stop(); // Stop the meeting update
        }
-       return false;
+       iDevice->changeMode( aChange );
 }
 
-void Engine::meetingsFetched( const QList<Meeting*> &aMeetings )
+void Engine::changeModeFailed()
+{
+       qDebug() << "Engine::progressBarCancelled()";
+       iDevice->changeMode( false );
+       iAutoRefresh->start(); //we start the metting updating
+}
+
+void Engine::initUserInterface()
 {
-       // check if there is any new meeting in the list came from the server -> added
-       for ( int i = 0; i < aMeetings.count(); i++ )
+       qDebug() << "[Engine::initUserInterface] <Invoked>";
+       
+       // Initialize the window manager and connect what ever signals can be connected
+       iWindowManager = new WindowManager;
+       // Create the UIManager which internally handles most of the UI actions
+       iUIManager = new UIManager( this, iWindowManager );
+       
+       connect( iWindowManager, SIGNAL( eventDetected() ), this, SLOT( handleViewEvent() ) );
+       connect( iWindowManager, SIGNAL( previousViewRestored() ), iUIManager, SLOT( previousViewRestored() ) );
+//     connect( iWindowManager, SIGNAL( dialogActivated() ), this, SLOT( dialogActivated() ) );
+       connect( iWindowManager, SIGNAL( dialogDeactivated() ), this, SLOT( dialogDeactivated() ) );
+       
+       // Show the UI
+       iWindowManager->setWindowState( Qt::WindowMaximized );
+       iWindowManager->show();
+       iUIManager->showMainView();
+       
+       // This triggers the meeting fetching
+       iUIManager->currentRoomChanged( this->iCurrentRoom );
+       
+       qDebug() << "[Engine::initUserInterface] <Finished>";
+}
+
+void Engine::handleViewEvent()
+{
+       if ( iIdleTimeCounter != 0 )
        {
-               // if the (i)th meeting is not in the local meeting list
-               if ( !isMeetingInList( iMeetings, aMeetings.at( i ) ) )
-               {
-                       // add to the local database =)
-                       Meeting* m = new Meeting( *(aMeetings.at( i )) );
-                       iMeetings.append( m );
-                       // and signal the changes
-                       emit meetingAdded( m );
-               }
+               // Restart the idle time counter when view event is received
+               iIdleTimeCounter->stop();
+               iIdleTimeCounter->start();
        }
+}
 
-       // check if there is any meeting NOT in the list came from the server -> deleted
-       for ( int i = 0; i < iMeetings.count(); i++ )
+void Engine::initConfiguration()
+{
+       iConfiguration = Configuration::instance();
+       if ( iConfiguration == 0 )
        {
-               // if the (i)th meeting is in the local but NOT in the server's meeting list
-               if ( !isMeetingInList( aMeetings, iMeetings.at( i ) ) )
-               {
-                       Meeting* m = iMeetings.takeAt( i );
-                       // signal the changes
-                       emit meetingDeleted( m );
-                       // delete the meeting from the local list
-                       delete m;
-               }
+               QTimer::singleShot( 0, this, SLOT( closeApplication() ) );
        }
+       iCurrentRoom = iConfiguration->defaultRoom();
+}
 
-       // refresh room status info
-       roomStatusInfoNeeded( defaultRoom() );
+void Engine::connectSignals()
+{
+       // Connect engine objects signals to UIManager
+       connect( iClock, SIGNAL( tick( QDateTime ) ), iUIManager, SLOT( updateTime( QDateTime ) ) );
+       connect( iIdleTimeCounter, SIGNAL( timeout() ) , iUIManager, SLOT( roomStatusIndicatorRequested() ) );
+       
+       iUIManager->connectDeviceManager( iDevice );
+       iUIManager->connectCommunicationManager( iCommunication );
 }
 
-void Engine::meetingDetailsFetched( Meeting &aDetailedMeeting )
+void Engine::initCommunication()
 {
-       emit meetingDetailsFetched( &aDetailedMeeting );
+       // initialize communication
+       iCommunication = new CommunicationManager( *(iConfiguration->connectionSettings()) );
+       connect( iCommunication, SIGNAL( error( int, CommunicationManager::CommunicationType  ) ),
+                       this, SLOT( errorHandler( int ) ) );
+       connect( iCommunication, SIGNAL( meetingsFetched( const QList<Meeting*>& ) ),
+                       this, SLOT( meetingsFetched( const QList<Meeting*>& ) ) );
 }
 
-void Engine::errorHandler( int aCode, const QString &aAddInfo )
+void Engine::initDevice()
 {
-       qDebug() << "Engine::ErrorHandler, aCode: " << aCode;
-       // inform UI about the problem
-       if( aCode >= 100 && aCode <= 110 )
-               qDebug() << "CommunicationManager signaled an error:" << aCode;
-       emit error( ErrorMapper::codeToString( aCode, aAddInfo ) );
+       // create device manager
+       iDevice = new DeviceManager( iConfiguration->startupSettings() );
+       connect( iDevice, SIGNAL( error( int, const QString& ) ), this, SLOT( errorHandler( int, const QString& ) ) );  
+       iDevice->initDeviceManager();
+}
+
+void Engine::dialogActivated()
+{
+       if ( iIdleTimeCounter != 0 )
+       {
+               iIdleTimeCounter->stop();
+       }
+}
+
+void Engine::dialogDeactivated()
+{
+       if ( iIdleTimeCounter != 0 )
+       {
+               iIdleTimeCounter->start();
+       }
+}
+
+void Engine::previousViewRestored()
+{
+       if ( iIdleTimeCounter != 0 )
+       {
+               iIdleTimeCounter->start();
+       }
 }
 
-void Engine::currentRoomChanged( Room *aCurrentRoom )
+void Engine::stopIdleTimeCounter()
 {
-       iCurrentRoom = aCurrentRoom;
-       qDebug() << "Engine::currentRoomChanged to " << iCurrentRoom->name();
+       if ( iIdleTimeCounter != 0 )
+       {
+               iIdleTimeCounter->stop();
+       }
 }
+
+void Engine::startIdleTimeCounter()
+{
+       if ( iIdleTimeCounter != 0 )
+       {
+               iIdleTimeCounter->start();
+       }
+}
+
+void Engine::currentRoomChanged(Room *aRoom)
+{
+       qDebug() << "[Engine::currentRoomChanged] <invoked>";
+       iCurrentRoom = aRoom;
+}
\ No newline at end of file