User interface update
[qtmeetings] / src / BusinessLogic / Engine.cpp
index 5245702..e46ddc9 100644 (file)
@@ -15,7 +15,7 @@
 #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.
@@ -28,9 +28,12 @@ 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()";
+       iCommunicationFailed = false;
+       iCurrentWeekFetched = false;
        
        initConfiguration();
        initDevice();
@@ -39,22 +42,24 @@ Engine::Engine() :
        
        //initialize idle time counter
        iIdleTimeCounter = new QTimer();
-       iIdleTimeCounter->setSingleShot( true );
-       // iIdleTimeCounter->setInterval( IDLE_TIME_MULTIPLIER * iConfiguration->displaySettings()->screensaver() );
-       iIdleTimeCounter->setInterval( 10000 );
+       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 ) ), this, SLOT( tick( QDateTime )/*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(Configuration::instance()->getRefreshinterval() * 1000);
+
        iAutoRefresh->start();
        connect( iAutoRefresh, SIGNAL( timeout() ), iAutoRefresh, SLOT( start() ) );
-       connect( iAutoRefresh, SIGNAL( timeout() ), this, SLOT( fetchMeetings() ) );
+       connect( iAutoRefresh, SIGNAL( timeout() ), this, SLOT( updateRoomInfo() ) );
+       // connect( iAutoRefresh, SIGNAL( timeout() ), this, SLOT( fetchMeetings() ) );
        
        if( iDevice->currentOperationMode() == DeviceManager::KioskMode )
        {
@@ -62,8 +67,8 @@ Engine::Engine() :
        }
 
        connectSignals();
-       
-       QTimer::singleShot( 0, this, SLOT( fetchMeetings() ) );
+       connect( Configuration::instance(), SIGNAL( configurationChanged() ), this, SLOT( configurationChanged() ) );
+       // QTimer::singleShot( 0, this, SLOT( fetchMeetings() ) );
 
        // TODO: continue implementation
 }
@@ -91,7 +96,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()
@@ -102,24 +107,22 @@ Room* Engine::defaultRoom()
 
 void Engine::checkStatusOfAllRooms()
 {
-//     qDebug() << "Engine::checkStatusOfAllRooms()";
+       // TODO: Check if date has changed
        // 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;
                }
@@ -127,21 +130,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;
                        }
@@ -150,7 +150,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 )
@@ -158,144 +158,102 @@ 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() ) ) )
-//             iWindowManager->roomStatusChanged( aRoom, status, until );
-}
-
-void Engine::fetchMeetings()
-{
-       Room *room = defaultRoom();
-       qDebug() << "Engine::fetchMeetings for " << room->name();
-       fetchMeetings( iClock->datetime(), iClock->datetime().addDays( 7 ), room );
+       //currently works only for default room
+       if ( aRoom->equals( *(iCurrentRoom) ) )
+       {
+               emit roomStatusChanged( status, until );
+       }
 }
 
 void Engine::fetchMeetingDetails( Meeting *aMeeting )
 {
        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 );
 }
 
-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++ )
-       {
-               if ( aMeeting->equals( *(aList.at( i )) ) )
-               {
-                       return true;
-               }
-       }
-       return false;
-}
-
 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++ )
+       // TODO: should check if this week's meetings were fetched
+       if( iCommunicationFailed || !iCurrentWeekFetched )
        {
-               // 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
-//                     iWeeklyView->insertMeeting( m );
-               }
+               iCurrentWeekFetched = true;
+               iCommunicationFailed = false;
+               iUIManager->connectionEstablished();
        }
 
-       // 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 ) ) )
-               {
-                       Meeting* m = iMeetings.takeAt( i );
-                       // signal the changes
-//                     iWeeklyView->deleteMeeting( m );
-                       // delete the meeting from the local list
-                       delete m;
-               }
+               // TODO: Check if these are current week's meetings and do not overwrite those
+               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() );
+       roomStatusInfoNeeded( iCurrentRoom /*defaultRoom()*/ );
 }
 
 void Engine::errorHandler( int aCode, const QString &aAddInfo )
+{      
+       if( aCode >= 100 && aCode < 150 )
+       {
+               iCommunicationFailed = true;
+
+               if ( iUIManager != 0 )
+                       {
+                               iUIManager->connectionLost();
+                       }
+       }
+       if ( iWindowManager != 0 )
+       {
+               iWindowManager->error( ErrorMapper::codeToString( aCode, aAddInfo ) );
+       }
+}
+
+void Engine::fetchMeetings( const int aWeek, const int aYear, const Room *aIn )
 {
-       qDebug() << "Engine::ErrorHandler, aCode: " << aCode;
-       // inform UI about the problem
-       if( aCode >= 100 && aCode <= 150 )
-               qDebug() << "CommunicationManager signaled an error:" << aCode;
-       // iWindowManager->closeProgressBar();
-       // iWindowManager->error( ErrorMapper::codeToString( aCode, aAddInfo ) );
+       qDebug()
+                       << "Engine::fetchMeetings( const int aWeek, const int aYear, const Room * )";
+       iCommunication->fetchMeetings(aWeek, aYear, *aIn);
 }
 
-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 )";
-       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() );
+       qDebug() << "[Engine::shownWeekChanged] <Invoked>";
+       iCommunication->fetchMeetings( aFrom.weekNumber(), aFrom.year(), *iCurrentRoom/*defaultRoom()*/ );
 }
 
-void Engine::passwordEntered( PasswordDialog::PasswordStatus aPasswordStatus )
+void Engine::changeDeviceMode()
 {
-       qDebug() << "Engine::passwordEntered( PasswordDialog::PasswordStatus )";
-//     iWindowManager->closePasswordDialog();
-       
-       switch ( aPasswordStatus )
-       {
-               case PasswordDialog::Correct :
-               {
-                       connect( iDevice, SIGNAL( changingModeFailed() ), this, SLOT( progressBarCancelled() ) );
-                       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( changeModeFailed() ), this, SLOT( changeModeFailed() ) );
+       iAutoRefresh->stop(); // Stop the meeting update
+       iDevice->changeMode();
 }
 
-void Engine::progressBarCancelled()
+void Engine::changeModeFailed()
 {
        qDebug() << "Engine::progressBarCancelled()";
-       iDevice->changeMode( false );
+       iAutoRefresh->start(); //we start the metting updating
 }
 
 void Engine::initUserInterface()
@@ -317,12 +275,15 @@ void Engine::initUserInterface()
        iWindowManager->show();
        iUIManager->showMainView();
        
+       // This triggers the meeting fetching
+       iUIManager->currentRoomChanged( this->iCurrentRoom );
+       
        qDebug() << "[Engine::initUserInterface] <Finished>";
 }
 
 void Engine::handleViewEvent()
 {
-       if ( iIdleTimeCounter != 0 )
+       if ( iIdleTimeCounter != 0 && iIdleTimeCounter->isActive())
        {
                // Restart the idle time counter when view event is received
                iIdleTimeCounter->stop();
@@ -337,6 +298,7 @@ void Engine::initConfiguration()
        {
                QTimer::singleShot( 0, this, SLOT( closeApplication() ) );
        }
+       iCurrentRoom = iConfiguration->defaultRoom();
 }
 
 void Engine::connectSignals()
@@ -352,7 +314,7 @@ void Engine::connectSignals()
 void Engine::initCommunication()
 {
        // initialize communication
-       iCommunication = new CommunicationManager( *(iConfiguration->connectionSettings()) );
+       iCommunication = new CommunicationManager(/* *(iConfiguration->connectionSettings()) */);
        connect( iCommunication, SIGNAL( error( int, CommunicationManager::CommunicationType  ) ),
                        this, SLOT( errorHandler( int ) ) );
        connect( iCommunication, SIGNAL( meetingsFetched( const QList<Meeting*>& ) ),
@@ -391,12 +353,6 @@ void Engine::previousViewRestored()
        }
 }
 
-void Engine::fetchMeetingDetailsCancelled()
-{
-       iCommunication->cancelFetchMeetingDetails();
-//     iWindowManager->closeProgressBar();
-}
-
 void Engine::stopIdleTimeCounter()
 {
        if ( iIdleTimeCounter != 0 )
@@ -412,3 +368,39 @@ void Engine::startIdleTimeCounter()
                iIdleTimeCounter->start();
        }
 }
+
+void Engine::currentRoomChanged(Room *aRoom)
+{
+       qDebug() << "[Engine::currentRoomChanged] <invoked>";
+       iCurrentRoom = aRoom;
+       roomStatusInfoNeeded( iCurrentRoom );
+}
+
+void Engine::tick( QDateTime aCurrentDateTime )
+{
+       // Called once every second
+       checkStatusOfAllRooms();
+       if( aCurrentDateTime.date() !=  iCurrentDate)
+       {
+               // Check if week has changed and fetch meetings for this week
+               if( aCurrentDateTime.date().weekNumber() != iCurrentDate.weekNumber()
+                       || aCurrentDateTime.date().year() != iCurrentDate.year() )
+               {
+                       qDebug() << "[Engine::tick] detected week change, fetching meetings";
+                       fetchMeetings( aCurrentDateTime.date().weekNumber(), aCurrentDateTime.date().year(), iCurrentRoom );
+               }
+       }
+       iCurrentDate = aCurrentDateTime.date();
+}
+
+
+void Engine::updateRoomInfo()
+{
+       qDebug() << "ENGINE::: updateMeetings";
+       roomStatusInfoNeeded(iCurrentRoom);
+}
+
+void Engine::configurationChanged()
+{
+       iAutoRefresh->setInterval(Configuration::instance()->getRefreshinterval() * 1000);
+}