Merged
[qtmeetings] / src / BusinessLogic / Engine.cpp
index a61006c..0c76679 100644 (file)
@@ -5,10 +5,14 @@
 #include "Configuration.h"
 #include "DisplaySettings.h"
 #include "CommunicationManager.h"
-#include "DeviceManager.h"
+// #include "DeviceManager.h"
 #include "Clock.h"
 #include "ErrorMapper.h"
 #include "WeeklyViewWidget.h"
+#include "SettingsView.h"
+#include "RoomStatusIndicatorWidget.h"
+#include "PasswordDialog.h"
+#include "MeetingInfoDialog.h"
 
 #include <QApplication>
 #include <QTimer>
 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 )
+               iClock( 0 ), iConfiguration( 0 ), iCommunication( 0 )
 {
        qDebug() << "Engine::Engine()";
-       // if reading of configuration fails, signal that initialization failed
-       if ( iConfiguration == 0 )
-       {
-               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 ) ) );
-
        
-       // 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& ) ) );
-
+       initConfiguration();
+       initDevice();
+       initCommunication();
+       initUserInterface();
+       
        //initialize idle time counter
        iIdleTimeCounter = new QTimer();
        iIdleTimeCounter->setSingleShot( true );
-       iIdleTimeCounter->setInterval( IDLE_TIME_MULTIPLIER * iConfiguration->displaySettings()->screensaver() );
+       // iIdleTimeCounter->setInterval( IDLE_TIME_MULTIPLIER * iConfiguration->displaySettings()->screensaver() );
+       iIdleTimeCounter->setInterval( 10000 );
        iIdleTimeCounter->start();
-       connect( iIdleTimeCounter, SIGNAL( timeout() ), iWindowManager, SLOT( showRoomStatus() ) );
+       // connect( iIdleTimeCounter, SIGNAL( timeout() ), iWindowManager, SLOT( showRoomStatus() ) );
+       connect( iIdleTimeCounter, SIGNAL( timeout() ), this, SLOT( idleTimerTimeout() ) );
 
        // create application clock
        iClock = new Clock;
        connect( iClock, SIGNAL( tick( QDateTime ) ), this, SLOT( checkStatusOfAllRooms() ) );
-       connect( iClock, SIGNAL( tick( QDateTime ) ), iWindowManager, SLOT( distributeDateTimeInfo( QDateTime ) ) );
+       // connect( iClock, SIGNAL( tick( QDateTime ) ), iWindowManager, SLOT( distributeDateTimeInfo( QDateTime ) ) );
 
        iAutoRefresh = new QTimer;
        iAutoRefresh->setInterval( iConfiguration->connectionSettings()->refreshInterval() * 1000 );
        iAutoRefresh->start();
        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( changeModeOrdered( DeviceManager::OperationMode ) ), 
-                       this, SLOT( changeModeOrdered( DeviceManager::OperationMode ) ) );
-       iDevice->initDeviceManager();
+//     connect( iAutoRefresh, SIGNAL( timeout() ), this, SLOT( fetchMeetings() ) );
        
        if( iDevice->currentOperationMode() == DeviceManager::KioskMode )
-               iWindowManager->fullScreen();
+       {
+               iWindowManager->setFullscreen();
+       }
 
+       connectSignals();
+       
        QTimer::singleShot( 0, this, SLOT( fetchMeetings() ) );
 
        // TODO: continue implementation
@@ -87,15 +78,21 @@ Engine::~Engine()
        qDebug() << "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;
+       
+       if ( iIdleTimeCounter != 0 )
+       {
+               iIdleTimeCounter->stop();
+               delete iIdleTimeCounter;
+               iIdleTimeCounter = 0;
+       }
+       QT_DELETE( iClock );
+       QT_DELETE( iDevice );
+       
+       QT_DELETE( iRoomStatusIndicator );
+       QT_DELETE( iSettingsView );
+       QT_DELETE( iWeeklyView );
+       QT_DELETE( iPasswordDialog );
+       QT_DELETE( iWindowManager );
 }
 
 void Engine::closeApplication()
@@ -105,19 +102,6 @@ void Engine::closeApplication()
        QTimer::singleShot( 1000, QApplication::instance(), SLOT( quit() ) );
 }
 
-void Engine::observedEventDetected()
-{
-       qDebug() << "Engine::observedEventDetected()";
-       iWindowManager->showWeeklyView();
-       // prepare to restart idle counter
-       if ( iIdleTimeCounter->isActive() )
-       {
-               iIdleTimeCounter->stop();
-       }
-       // (re)start idle counter
-       iIdleTimeCounter->start();
-}
-
 Room* Engine::defaultRoom()
 {
        qDebug() << "Engine::defaultRoom()";
@@ -126,7 +110,7 @@ Room* Engine::defaultRoom()
 
 void Engine::checkStatusOfAllRooms()
 {
-       qDebug() << "Engine::checkStatusOfAllRooms()";
+//     qDebug() << "Engine::checkStatusOfAllRooms()";
        // iterate trough on the rooms
        for ( int i = 0; i < iConfiguration->rooms().count(); i++ )
        {
@@ -137,7 +121,7 @@ void Engine::checkStatusOfAllRooms()
 
 int Engine::indexOfMeetingAt( Room *aRoom, QDateTime aAt )
 {
-       qDebug() << "Engine::indexOfMeetingAt( Room *, QDateTime )";
+//     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
@@ -153,7 +137,7 @@ int Engine::indexOfMeetingAt( Room *aRoom, QDateTime aAt )
 
 int Engine::indexOfMeetingAfter( Room *aRoom, QDateTime aAfter )
 {
-       qDebug() << "Engine::indexOfMeetingAfter( Room *, QDateTime )";
+//     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++ )
@@ -176,7 +160,7 @@ int Engine::indexOfMeetingAfter( Room *aRoom, QDateTime aAfter )
 
 void Engine::roomStatusInfoNeeded( Room *aRoom )
 {
-       qDebug() << "Engine::roomStatusInfoNeeded( Room * )";
+//     qDebug() << "Engine::roomStatusInfoNeeded( Room * )";
        if ( aRoom == 0 )
        {
                return;
@@ -193,8 +177,8 @@ void Engine::roomStatusInfoNeeded( Room *aRoom )
                          (( indexOfNextMeeting != -1 ) ? iMeetings.at( indexOfNextMeeting )->startsAt().time() : Engine::endOfTheDay );
 
        //currently works only for deafult room
-       if( aRoom->equals( *(defaultRoom() ) ) )
-               iWindowManager->roomStatusChanged( aRoom, status, until );
+//     if( aRoom->equals( *(defaultRoom() ) ) )
+//             iWindowManager->roomStatusChanged( aRoom, status, until );
 }
 
 void Engine::fetchMeetings()
@@ -243,7 +227,7 @@ void Engine::meetingsFetched( const QList<Meeting*> &aMeetings )
                        Meeting* m = new Meeting( *(aMeetings.at( i )) );
                        iMeetings.append( m );
                        // and signal the changes
-                       iWindowManager->insertMeeting( m );
+                       iWeeklyView->insertMeeting( m );
                }
        }
 
@@ -255,7 +239,7 @@ void Engine::meetingsFetched( const QList<Meeting*> &aMeetings )
                {
                        Meeting* m = iMeetings.takeAt( i );
                        // signal the changes
-                       iWindowManager->deleteMeeting( m );
+                       iWeeklyView->deleteMeeting( m );
                        // delete the meeting from the local list
                        delete m;
                }
@@ -268,8 +252,13 @@ void Engine::meetingsFetched( const QList<Meeting*> &aMeetings )
 void Engine::meetingDetailsFetched( Meeting &aDetailedMeeting )
 {
        qDebug() << "Engine::meetingDetailsFetched( Meeting & )";
-       iWindowManager->closeProgressBar();
-       iWindowManager->showMeetingInfo( &aDetailedMeeting );
+       
+       if ( iMeetingInfoDialog != 0 )
+       {
+               iMeetingInfoDialog->setMeeting( &aDetailedMeeting );
+               iWindowManager->showDialog( iMeetingInfoDialog );
+       }
+
 }
 
 void Engine::errorHandler( int aCode, const QString &aAddInfo )
@@ -278,16 +267,16 @@ void Engine::errorHandler( int aCode, const QString &aAddInfo )
        // inform UI about the problem
        if( aCode >= 100 && aCode <= 150 )
                qDebug() << "CommunicationManager signaled an error:" << aCode;
-       iWindowManager->closeProgressBar();
-       iWindowManager->error( ErrorMapper::codeToString( aCode, aAddInfo ) );
+       // iWindowManager->closeProgressBar();
+       // iWindowManager->error( ErrorMapper::codeToString( aCode, aAddInfo ) );
 }
 
 void Engine::currentRoomChanged( Room *aCurrentRoom )
 {
        qDebug() << "Engine::currentRoomChanged to " << aCurrentRoom->name();
-       QDateTime from( iWindowManager->weeklyView()->beginnigOfShownWeek() );
-       QDateTime to( from.addDays( 8 ) );
-       fetchMeetings( from, to, aCurrentRoom );
+//     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 )
@@ -302,7 +291,7 @@ 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() );
+//     fetchMeetings( from, to, iWindowManager->weeklyView()->currentRoom() );
 }
 
 void Engine::changeModeOrdered( DeviceManager::OperationMode aMode )
@@ -310,30 +299,34 @@ void Engine::changeModeOrdered( DeviceManager::OperationMode aMode )
        qDebug() << "Engine::changeModeOrdered( DeviceManager::OperationMode )";
        QString message = tr( "You are about to change operation mode to %1." )
                                .arg( iDevice->operationModeToString( aMode ) );
-       
-       iWindowManager->showPasswordDialog( iConfiguration->adminPassword(), message );
+
+       // iPasswordDialog->update( message );
+       iWindowManager->showDialog( static_cast<QDialog *>( iPasswordDialog ) );
 }
 
 void Engine::passwordEntered( PasswordDialog::PasswordStatus aPasswordStatus )
 {
        qDebug() << "Engine::passwordEntered( PasswordDialog::PasswordStatus )";
-       iWindowManager->closePasswordDialog();
+//     iWindowManager->closePasswordDialog();
        
        switch ( aPasswordStatus )
        {
                case PasswordDialog::Correct :
                {
-                       iWindowManager->showProgressBar( "Changing current operation mode.", true );
-                       connect( iWindowManager, SIGNAL( progressBarCancelled() ), this, SLOT( progressBarCancelled() ) );
-                       connect( iDevice, SIGNAL( changingMode( const QString & ) ),
-                                       iWindowManager, SLOT( updateProgressBar( const QString & ) ) );
+//                     iWindowManager->showProgressBar( "Changing current operation mode." );
+//                     connect( iWindowManager, SIGNAL( progressBarCancelled() ), this, SLOT( progressBarCancelled() ) );
+//                     connect( iDevice, SIGNAL( changingMode( const QString & ) ),
+//                                     iWindowManager, SLOT( updateProgressBar( const QString & ) ) );
+                       // TODO : Connect the signal directory to progress bar dialog which should be instance in engine
+//                     connect( iDevice, SIGNAL( changingMode( const QString & ) ),
+//                                     iWindowManager, SLOT( updateProgressBar( const QString & ) ) );
                        connect( iDevice, SIGNAL( changingModeFailed() ), this, SLOT( progressBarCancelled() ) );
                        iDevice->changeMode( true );
                        break;
                }
                case PasswordDialog::Incorrect :
                {
-                       iWindowManager->error( tr( "Incorrect password." ) );
+//                     iWindowManager->error( tr( "Incorrect password." ) );
                        iDevice->changeMode( false );
                        break;
                }
@@ -348,10 +341,146 @@ void Engine::progressBarCancelled()
 {
        qDebug() << "Engine::progressBarCancelled()";
        //TODO: cancel the on-going event
-       iWindowManager->closeProgressBar();
+//     iWindowManager->closeProgressBar();
        iDevice->changeMode( false );
 }
 
+void Engine::initUserInterface()
+{
+       qDebug() << "[Engine::initUserInterface] <Invoked>";
+       // Initialize the window manager and connect what ever signals can be connected
+       iWindowManager = new WindowManager;
+       connect( iWindowManager, SIGNAL( eventDetected() ), this, SLOT( handleViewEvent() ) );
+       connect( iWindowManager, SIGNAL( previousViewRestored() ), this, SLOT( previousViewRestored() ) );
+       connect( iWindowManager, SIGNAL( dialogActivated() ), this, SLOT( dialogActivated() ) );
+       connect( iWindowManager, SIGNAL( dialogDeactivated() ), this, SLOT( dialogDeactivated() ) );
+       
+       // Initialize the weekly view and connect what ever signals can be connected at this stage
+       iWeeklyView = new WeeklyViewWidget(QDateTime::currentDateTime(), iConfiguration);
+       connect( iWeeklyView, SIGNAL( settingsButtonClicked() ), this, SLOT( settingsViewRequested() ) );
+       connect( iWeeklyView, SIGNAL( currentRoomChange( Room * ) ) , this, SLOT( currentRoomChange( Room * ) ) );
+       connect( iWeeklyView, SIGNAL( meetingActivated( Meeting * ) ), this, SLOT( fetchMeetingDetails( Meeting * ) ) ) ;
+       connect( iWeeklyView, SIGNAL( shownWeekChanged( QDate ) ) , this, SLOT( shownWeekChanged( QDate ) ) );
+       
+       // Initialize the settings view
+       iSettingsView = new SettingsView;
+       connect( iSettingsView, SIGNAL( okClicked() ) , this, SLOT( settingsOkClicked() ) );
+       
+       // Initialize the room status indicator
+       iRoomStatusIndicator = new RoomStatusIndicatorWidget( defaultRoom(), Room::FreeStatus, QTime::currentTime(), iConfiguration->displaySettings()->dateFormat() );
+       
+       // Create password dialog
+       iPasswordDialog = new PasswordDialog( iConfiguration->adminPassword(), tr("No Text Set"), tr("Title") );
+       connect( iPasswordDialog, SIGNAL( passwordEntered( PasswordDialog::PasswordStatus ) ), this, SLOT( passwordEntered( PasswordDialog::PasswordStatus ) ) );
+       
+       // Show the UI
+       iWindowManager->setWindowState( Qt::WindowMaximized );
+       iWindowManager->show();
+       iWindowManager->showView( iWeeklyView );
+       
+       qDebug() << "[Engine::initUserInterface] <Finished>";
+}
+
+void Engine::settingsViewRequested()
+{
+       if ( iSettingsView != 0 )
+       {
+               iWindowManager->showView( static_cast<ViewBase *>( iSettingsView ) );
+               // Room status indicator will not be shown when settings view is active
+               iIdleTimeCounter->stop();
+       }
+}
+
+void Engine::handleViewEvent()
+{
+       qDebug() << "[Engine::handleViewEvent] <Invoked>";
+       if ( iIdleTimeCounter != 0 )
+       {
+               // Restart the idle time counter when view event is received
+               iIdleTimeCounter->stop();
+               iIdleTimeCounter->start();
+       }
+}
+
+void Engine::initConfiguration()
+{
+       iConfiguration = Configuration::instance();
+       if ( iConfiguration == 0 )
+       {
+               QTimer::singleShot( 0, this, SLOT( closeApplication() ) );
+       }
+}
+
+void Engine::idleTimerTimeout()
+{
+       if ( iRoomStatusIndicator != 0 )
+       {
+               iWindowManager->showView( static_cast<ViewBase *>( iRoomStatusIndicator ) );
+       }
+}
+
+void Engine::settingsOkClicked()
+{
+       if ( iWeeklyView != 0 )
+       {
+               iWindowManager->showView( iWeeklyView );
+               // Start the idle time counter when we return to the main view
+               iIdleTimeCounter->start();
+       }
+}
+
+void Engine::connectSignals()
+{
+       // Handle weekly view signal connections
+       connect( iClock, SIGNAL( tick( QDateTime ) ), iWeeklyView, SLOT( setCurrentDateTime( QDateTime ) ) );
+}
+
+void Engine::initCommunication()
+{
+       // 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& ) ) );
+}
+
+void Engine::initDevice()
+{
+       // create device manager
+       iDevice = new DeviceManager( iConfiguration->startupSettings() );
+       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();
+}
+
+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::fetchMeetingDetailsCancelled()
 {
        iCommunication->cancelFetchMeetingDetails();