Merged and got it partly working
[qtmeetings] / src / BusinessLogic / Engine.cpp
index 0b68a03..1a79851 100644 (file)
@@ -8,18 +8,14 @@
 // #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 "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.
@@ -32,7 +28,8 @@ const int IDLE_TIME_MULTIPLIER = 60000; // Multiplies milliseconds to minutes
 
 
 Engine::Engine() :
-               iClock( 0 ), iConfiguration( 0 ), iCommunication( 0 )
+               iClock( 0 ), iConfiguration( 0 ), iCommunication( 0 ),
+               iWindowManager( 0 ), iUIManager( 0 )
 {
        qDebug() << "Engine::Engine()";
        
@@ -47,19 +44,18 @@ Engine::Engine() :
        // iIdleTimeCounter->setInterval( IDLE_TIME_MULTIPLIER * iConfiguration->displaySettings()->screensaver() );
        iIdleTimeCounter->setInterval( 10000 );
        iIdleTimeCounter->start();
-       // 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 ) ) );
 
+       // 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() ) );
+       connect( iAutoRefresh, SIGNAL( timeout() ), this, SLOT( fetchMeetings() ) );
        
        if( iDevice->currentOperationMode() == DeviceManager::KioskMode )
        {
@@ -87,11 +83,8 @@ Engine::~Engine()
        }
        QT_DELETE( iClock );
        QT_DELETE( iDevice );
-       
-       QT_DELETE( iRoomStatusIndicator );
-       QT_DELETE( iSettingsView );
-       QT_DELETE( iWeeklyView );
-       QT_DELETE( iPasswordDialog );
+
+       QT_DELETE( iUIManager );
        QT_DELETE( iWindowManager );
 }
 
@@ -99,7 +92,7 @@ void Engine::closeApplication()
 {
        qDebug() << "Engine::closeApplication()";
        // closes application after 1 second
-       QTimer::singleShot( 1000, QApplication::instance(), SLOT( quit() ) );
+       QTimer::singleShot( 1000, QApplication::instance(), SLOT( quit() ));
 }
 
 Room* Engine::defaultRoom()
@@ -112,22 +105,20 @@ 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;
                }
@@ -135,21 +126,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;
                        }
@@ -158,7 +146,7 @@ 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 )
@@ -166,15 +154,17 @@ void Engine::roomStatusInfoNeeded( Room *aRoom )
                return;
        }
 
-       int indexOfCurrentMeeting = indexOfMeetingAt( aRoom, iClock->datetime() );
-       int indexOfNextMeeting = indexOfMeetingAfter( aRoom, iClock->datetime() );
+       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 );
 
        //currently works only for deafult room
 //     if( aRoom->equals( *(defaultRoom() ) ) )
@@ -185,21 +175,16 @@ void Engine::fetchMeetings()
 {
        Room *room = defaultRoom();
        qDebug() << "Engine::fetchMeetings for " << room->name();
-       fetchMeetings( iClock->datetime(), iClock->datetime().addDays( 7 ), room );
+       fetchMeetings(iClock->datetime(), iClock->datetime().addDays( 7), room);
 }
 
-void Engine::fetchMeetingDetails( Meeting *aMeeting )
+bool Engine::isMeetingInList(const QList<Meeting*> &aList, const Meeting *aMeeting)
 {
-       qDebug() << "Engine::fetchMeetingDetails( Meeting* )";
-       iCommunication->fetchMeetingDetails( *aMeeting );
-}
-
-bool Engine::isMeetingInList( const QList<Meeting*> &aList, const Meeting *aMeeting )
-{
-       qDebug() << "Engine::isMeetingInList( const QList<Meeting*> &, const Meeting * )";
-       for ( int i = 0; i < aList.count(); i++ )
+       qDebug()
+                       << "Engine::isMeetingInList( const QList<Meeting*> &, const Meeting * )";
+       for (int i = 0; i < aList.count(); i++)
        {
-               if ( aMeeting->equals( *(aList.at( i )) ) )
+               if (aMeeting->equals( *(aList.at(i))) )
                {
                        return true;
                }
@@ -207,181 +192,118 @@ bool Engine::isMeetingInList( const QList<Meeting*> &aList, const Meeting *aMeet
        return false;
 }
 
-void Engine::meetingsFetched( const QList<Meeting*> &aMeetings )
+void Engine::meetingsFetched(const QList<Meeting*> &aMeetings)
 {
        qDebug() << "Engine::meetingsFetched( const QList<Meeting*> & )";
+       
        // check if there is any new meeting in the list came from the server -> added
-       for ( int i = 0; i < aMeetings.count(); i++ )
+       for (int i = 0; i < aMeetings.count(); i++)
        {
                // if the (i)th meeting is not in the local meeting list
-               if ( !isMeetingInList( iMeetings, aMeetings.at( i ) ) )
+               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
-                       iWeeklyView->insertMeeting( m );
+                       iMeetings.append(m);
                }
        }
 
        // check if there is any meeting NOT in the list came from the server -> deleted
-       for ( int i = 0; i < iMeetings.count(); i++ )
+       for (int i = 0; i < iMeetings.count(); i++)
        {
                // if the (i)th meeting is in the local but NOT in the server's meeting list
-               if ( !isMeetingInList( aMeetings, iMeetings.at( i ) ) )
+               if ( !isMeetingInList(aMeetings, iMeetings.at(i) ) )
                {
-                       Meeting* m = iMeetings.takeAt( i );
-                       // signal the changes
-                       iWeeklyView->deleteMeeting( m );
+                       Meeting* m = iMeetings.takeAt(i);
                        // delete the meeting from the local list
                        delete m;
                }
        }
 
        // refresh room status info
-       roomStatusInfoNeeded( defaultRoom() );
+       roomStatusInfoNeeded(defaultRoom() );
 }
 
-void Engine::meetingDetailsFetched( Meeting &aDetailedMeeting )
-{
-       qDebug() << "Engine::meetingDetailsFetched( Meeting & )";
-       
-       if ( iMeetingInfoDialog != 0 )
+void Engine::errorHandler( int aCode, const QString &aAddInfo )
+{      
+       if ( iWindowManager != 0 )
        {
-               iMeetingInfoDialog->setMeeting( &aDetailedMeeting );
-               iWindowManager->showDialog( iMeetingInfoDialog );
+               iWindowManager->error( ErrorMapper::codeToString( aCode, aAddInfo ) );
        }
-
 }
 
-void Engine::errorHandler( int aCode, const QString &aAddInfo )
+void Engine::fetchMeetings( const QDateTime &aFrom, const QDateTime &aUntil, const Room *aIn )
 {
-       qDebug() << "Engine::ErrorHandler, aCode: " << aCode;
-       // inform UI about the problem
-       if( aCode >= 100 && aCode <= 110 )
-               qDebug() << "CommunicationManager signaled an error:" << aCode;
-//     iWindowManager->error( ErrorMapper::codeToString( aCode, aAddInfo ) );
+       qDebug()
+                       << "Engine::fetchMeetings( const QDateTime &, const QDateTime &, const Room * )";
+       iCommunication->fetchMeetings(aFrom, aUntil, *aIn);
 }
 
-void Engine::currentRoomChanged( Room *aCurrentRoom )
+void Engine::fetchMeetingDetails(Meeting *aMeeting)
 {
-       qDebug() << "Engine::currentRoomChanged to " << aCurrentRoom->name();
-//     QDateTime from( iWindowManager->weeklyView()->beginnigOfShownWeek() );
-//     QDateTime to( from.addDays( 8 ) );
-//     fetchMeetings( from, to, aCurrentRoom );
+       qDebug() << "[Engine::fetchMeetingDetails] <TODO : METHOD NOT IMPLEMENTED>";
+       Meeting tempMeeting(aMeeting->primaryId(), aMeeting->room(), aMeeting->startsAt(), aMeeting->endsAt() );
+       iCommunication->fetchMeetingDetails( tempMeeting );
+//     Meeting tempMeeting = aMeeting;
+//     iCommunication->fetchMeetingDetails( tempMeeting );
 }
 
-void Engine::fetchMeetings( const QDateTime &aFrom, const QDateTime &aUntil, const Room *aIn )
+void Engine::cancelFetchMeetingDetails()
 {
-       qDebug() << "Engine::fetchMeetings( const QDateTime &, const QDateTime &, const Room * )";
-       iCommunication->fetchMeetings( aFrom, aUntil, *aIn );
+       iCommunication->cancelFetchMeetingDetails();
 }
 
 void Engine::shownWeekChanged( QDate aFrom )
 {
-       qDebug() << "Engine::shownWeekChanged( QDate )";
+       qDebug() << "[Engine::shownWeekChanged] <Invoked>";
        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" );
+       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::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 ) );
-
-       // iPasswordDialog->update( message );
-       iWindowManager->showDialog( static_cast<QDialog *>( iPasswordDialog ) );
-}
-
-void Engine::passwordEntered( PasswordDialog::PasswordStatus aPasswordStatus )
+void Engine::changeDeviceMode( bool aChange )
 {
-       qDebug() << "Engine::passwordEntered( PasswordDialog::PasswordStatus )";
-//     iWindowManager->closePasswordDialog();
-       
-       switch ( aPasswordStatus )
+       if ( aChange )
        {
-               case PasswordDialog::Correct :
-               {
-//                     iWindowManager->showProgressBar( "Changing current operation mode." );
-//                     connect( iWindowManager, SIGNAL( progressBarCancelled() ), this, SLOT( progressBarCancelled() ) );
-//                     connect( iDevice, SIGNAL( changingMode( const QString & ) ),
-//                                     iWindowManager, SLOT( updateProgressBar( const QString & ) ) );
-                       iDevice->changeMode( true );
-                       break;
-               }
-               case PasswordDialog::Incorrect :
-               {
-//                     iWindowManager->error( tr( "Incorrect password." ) );
-                       iDevice->changeMode( false );
-                       break;
-               }
-               default : //case PasswordDialog::Canceled
-               {
-                       iDevice->changeMode( false );
-               }
+               connect( iDevice, SIGNAL( changingModeFailed() ), this, SLOT( changeModeFailed() ) );
+               iAutoRefresh->stop(); // Stop the meeting update
        }
+       iDevice->changeMode( aChange );
 }
 
-void Engine::progressBarCancelled()
+void Engine::changeModeFailed()
 {
        qDebug() << "Engine::progressBarCancelled()";
-       //TODO: cancel the on-going event
-//     iWindowManager->closeProgressBar();
        iDevice->changeMode( false );
+       iAutoRefresh->start(); //we start the metting updating
 }
 
 void Engine::initUserInterface()
 {
        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() ), this, SLOT( previousViewRestored() ) );
+       connect( iWindowManager, SIGNAL( previousViewRestored() ), iUIManager, 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 );
+       iUIManager->showMainView();
        
        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
@@ -399,28 +321,14 @@ void Engine::initConfiguration()
        }
 }
 
-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 ) ) );
+       // 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::initCommunication()
@@ -431,8 +339,6 @@ void Engine::initCommunication()
                        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()
@@ -440,8 +346,6 @@ 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();
 }
 
@@ -467,4 +371,20 @@ void Engine::previousViewRestored()
        {
                iIdleTimeCounter->start();
        }
-}
\ No newline at end of file
+}
+
+void Engine::stopIdleTimeCounter()
+{
+       if ( iIdleTimeCounter != 0 )
+       {
+               iIdleTimeCounter->stop();
+       }
+}
+
+void Engine::startIdleTimeCounter()
+{
+       if ( iIdleTimeCounter != 0 )
+       {
+               iIdleTimeCounter->start();
+       }
+}