- Re-factored the code so that Engine is now the main class that owns WindowManager...
[qtmeetings] / src / BusinessLogic / Engine.cpp
index 2f943a7..e529799 100644 (file)
@@ -1,49 +1,64 @@
 #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 "Clock.h"
 #include "ErrorMapper.h"
+#include "WeeklyViewWidget.h"
 
+#include <QApplication>
+#include <QTimer>
+#include <QList>
 #include <QtDebug>
 
 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
 
 Engine::Engine() :
-               iClock( 0 ), iConfiguration( Configuration::instance() ), iCommunication( 0 ), iCurrentRoom( 0 )
+               iClock( 0 ), iConfiguration( Configuration::instance() ), iCommunication( 0 )
 {
        // if reading of configuration fails, signal that initialization failed
        if ( iConfiguration == 0 )
        {
-               QTimer::singleShot( 0, this, SIGNAL( initializationFailed() ) );
+               QTimer::singleShot( 0, this, SLOT( closeApplication() ) );
                return;
        }
+               
+       //initialize window manager
+       iWindowManager = new WindowManager( iConfiguration );
+       connect( iWindowManager, SIGNAL( roomStatusInfoNeeded( Room * ) ), this, SLOT( roomStatusInfoNeeded( Room * ) ) );
+       connect( iWindowManager, SIGNAL( observedEventDetected() ), this, SLOT( observedEventDetected() ) );
+       connect( iWindowManager, SIGNAL( meetingActivated( Meeting * ) ), this, SLOT( fetchMeetingDetails( Meeting * ) ) );
+       connect( iWindowManager, SIGNAL( currentRoomChanged( Room * ) ), this, SLOT( currentRoomChanged( Room * ) ) );
+       connect( iWindowManager, SIGNAL( shownWeekChanged( QDate ) ), this, SLOT( shownWeekChanged( QDate ) ) );
+       connect( iWindowManager, SIGNAL( passwordEntered( PasswordDialog::PasswordStatus ) ),
+                       this, SLOT( passwordEntered( PasswordDialog::PasswordStatus ) ) );
+       connect( iWindowManager, SIGNAL( progressBarCancelled() ), this, SLOT( progressBarCancelled() ) );
+       
        // 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& ) )
-                       );
+       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& ) ) );
+
+       //initialize idle time counter
+       iIdleTimeCounter = new QTimer();
+       iIdleTimeCounter->setSingleShot( true );
+       iIdleTimeCounter->setInterval( IDLE_TIME_MULTIPLIER * iConfiguration->displaySettings()->screensaver() );
+       iIdleTimeCounter->start();
+       connect( iIdleTimeCounter, SIGNAL( timeout() ), iWindowManager, SLOT( showRoomStatus() ) );
 
        // create application clock
        iClock = new Clock;
        connect( iClock, SIGNAL( tick( QDateTime ) ), this, SLOT( checkStatusOfAllRooms() ) );
+       connect( iClock, SIGNAL( tick( QDateTime ) ), iWindowManager, SLOT( distributeDateTimeInfo( QDateTime ) ) );
 
        iAutoRefresh = new QTimer;
        iAutoRefresh->setInterval( iConfiguration->connectionSettings()->refreshInterval() * 1000 );
@@ -51,9 +66,15 @@ Engine::Engine() :
        connect( iAutoRefresh, SIGNAL( timeout() ), iAutoRefresh, SLOT( start() ) );
        connect( iAutoRefresh, SIGNAL( timeout() ), this, SLOT( fetchMeetings() ) );
 
+       // create device manager
        iDevice = new DeviceManager( iConfiguration->startupSettings() );
-       connect( iDevice, SIGNAL( error( int, const QString& ) ), this, SLOT( errorHandler( int, const QString& ) ) );
+       connect( iDevice, SIGNAL( error( int, const QString& ) ), this, SLOT( errorHandler( int, const QString& ) ) );  
+       connect( iDevice, SIGNAL( changeModeOrdered( DeviceManager::OperationMode ) ), 
+                       this, SLOT( changeModeOrdered( DeviceManager::OperationMode ) ) );
        iDevice->initDeviceManager();
+       
+       if( iDevice->currentOperationMode() == DeviceManager::KioskMode )
+               iWindowManager->fullScreen();
 
        QTimer::singleShot( 0, this, SLOT( fetchMeetings() ) );
 
@@ -64,26 +85,39 @@ Engine::~Engine()
 {
        while ( !iMeetings.isEmpty() )
                delete iMeetings.takeFirst();
+       iIdleTimeCounter->stop();
+       delete iIdleTimeCounter;
+       iIdleTimeCounter = 0;
+       delete iWindowManager;
+       iWindowManager = 0;
+       delete iClock;
+       iClock = 0;
+       delete iDevice;
+       iDevice = 0;
 }
 
-Room* Engine::defaultRoom()
+void Engine::closeApplication()
 {
-       return iConfiguration->defaultRoom();
+       qDebug() << "Engine::closeApplication()";
+       // closes application after 1 second
+       QTimer::singleShot( 1000, QApplication::instance(), SLOT( quit() ) );
 }
 
-Clock* Engine::clock()
+void Engine::observedEventDetected()
 {
-       return iClock;
-}
-
-Configuration* Engine::configuration()
-{
-       return iConfiguration;
+       iWindowManager->showWeeklyView();
+       // prepare to restart idle counter
+       if ( iIdleTimeCounter->isActive() )
+       {
+               iIdleTimeCounter->stop();
+       }
+       // (re)start idle counter
+       iIdleTimeCounter->start();
 }
 
-DeviceManager* Engine::deviceManager()
+Room* Engine::defaultRoom()
 {
-       return iDevice;
+       return iConfiguration->defaultRoom();
 }
 
 void Engine::checkStatusOfAllRooms()
@@ -143,8 +177,6 @@ void Engine::roomStatusInfoNeeded( Room *aRoom )
        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 );
-
        // if there is no meeting, then status is Free; otherwise Busy
        Room::Status status = ( indexOfCurrentMeeting == -1 ) ? Room::FreeStatus : Room::BusyStatus;
        // if room is Busy, then check end time, otherwise...
@@ -152,21 +184,16 @@ void Engine::roomStatusInfoNeeded( Room *aRoom )
                          // ...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();
+       Room *room = defaultRoom();
        qDebug() << "Engine::fetchMeetings for " << room->name();
-       fetchMeetings( iClock->datetime().addDays( -14 ), iClock->datetime().addDays( 14 ), room );
-}
-
-void Engine::fetchMeetings( const QDateTime &aFrom, const QDateTime &aUntil, Room *aIn )
-{
-       iCommunication->fetchMeetings( aFrom, aUntil, *aIn );
+       fetchMeetings( iClock->datetime(), iClock->datetime().addDays( 7 ), room );
 }
 
 void Engine::fetchMeetingDetails( Meeting *aMeeting )
@@ -198,7 +225,7 @@ void Engine::meetingsFetched( const QList<Meeting*> &aMeetings )
                        Meeting* m = new Meeting( *(aMeetings.at( i )) );
                        iMeetings.append( m );
                        // and signal the changes
-                       emit meetingAdded( m );
+                       iWindowManager->insertMeeting( m );
                }
        }
 
@@ -210,7 +237,7 @@ void Engine::meetingsFetched( const QList<Meeting*> &aMeetings )
                {
                        Meeting* m = iMeetings.takeAt( i );
                        // signal the changes
-                       emit meetingDeleted( m );
+                       iWindowManager->deleteMeeting( m );
                        // delete the meeting from the local list
                        delete m;
                }
@@ -222,7 +249,7 @@ void Engine::meetingsFetched( const QList<Meeting*> &aMeetings )
 
 void Engine::meetingDetailsFetched( Meeting &aDetailedMeeting )
 {
-       emit meetingDetailsFetched( &aDetailedMeeting );
+       iWindowManager->showMeetingInfo( &aDetailedMeeting );
 }
 
 void Engine::errorHandler( int aCode, const QString &aAddInfo )
@@ -231,11 +258,67 @@ void Engine::errorHandler( int aCode, const QString &aAddInfo )
        // inform UI about the problem
        if( aCode >= 100 && aCode <= 110 )
                qDebug() << "CommunicationManager signaled an error:" << aCode;
-       emit error( ErrorMapper::codeToString( aCode, aAddInfo ) );
+       iWindowManager->error( ErrorMapper::codeToString( aCode, aAddInfo ) );
 }
 
 void Engine::currentRoomChanged( Room *aCurrentRoom )
 {
-       iCurrentRoom = aCurrentRoom;
-       qDebug() << "Engine::currentRoomChanged to " << iCurrentRoom->name();
+       qDebug() << "Engine::currentRoomChanged to " << aCurrentRoom->name();
+       QDateTime from( iWindowManager->weeklyView()->beginnigOfShownWeek() );
+       QDateTime to( from.addDays( 8 ) );
+       fetchMeetings( from, to, aCurrentRoom );
+}
+
+void Engine::fetchMeetings( const QDateTime &aFrom, const QDateTime &aUntil, const Room *aIn )
+{
+       qDebug() << "Engine::fetchMeetings";
+       iCommunication->fetchMeetings( aFrom, aUntil, *aIn );
+}
+
+void Engine::shownWeekChanged( QDate aFrom )
+{
+       QDateTime from( aFrom );
+       QDateTime to( aFrom.addDays( 7 ), QTime( 23, 59 ) );
+       qDebug() << "Engine::shownWeekChanged " << aFrom.toString( "d.m. h:mm" ) << " to " << to.toString( "d.m. h:mm" );
+       fetchMeetings( from, to, iWindowManager->weeklyView()->currentRoom() );
+}
+
+void Engine::changeModeOrdered( DeviceManager::OperationMode aMode )
+{
+       QString message = tr( "You are about to change operation mode to %1." )
+                               .arg( iDevice->operationModeToString( aMode ) );
+       
+       iWindowManager->showPasswordDialog( iConfiguration->adminPassword(), message );
+}
+
+void Engine::passwordEntered( PasswordDialog::PasswordStatus aPasswordStatus )
+{
+       qDebug() << "Engine::passwordEntered( PasswordDialog::PasswordStatus )";
+       iWindowManager->closePasswordDialog();
+       
+       switch ( aPasswordStatus )
+       {
+               case PasswordDialog::Correct :
+               {
+                       iWindowManager->showProgressBar( "Changing current operation mode." );
+                       break;
+               }
+               case PasswordDialog::Incorrect :
+               {
+                       iWindowManager->error( tr( "Incorrect password." ) );
+                       iDevice->handleKeyPresses( true );
+                       break;
+               }
+               default : //case PasswordDialog::Canceled
+               {
+                       iDevice->handleKeyPresses( true );
+               }
+       }
+}
+
+void Engine::progressBarCancelled()
+{
+       //TODO: cancel the on-going event
+       iWindowManager->closeProgressBar();
+       iDevice->handleKeyPresses( true );
 }