4 #include "ConnectionSettings.h"
5 #include "Configuration.h"
6 #include "DisplaySettings.h"
7 #include "CommunicationManager.h"
8 // #include "DeviceManager.h"
10 #include "ErrorMapper.h"
11 #include "UIManager.h"
13 #include <QApplication>
18 QTime Engine::endOfTheDay = QTime( 23, 59, 0, 0); // end of the day is 11:59pm
19 const int IDLE_TIME_MULTIPLIER = 60000; // Multiplies milliseconds to minutes
21 // Macro to help deleting objects. This could be global.
22 #define QT_DELETE(X) \
31 iClock( 0 ), iConfiguration( 0 ), iCommunication( 0 ),
32 iWindowManager( 0 ), iUIManager( 0 )
34 qDebug() << "Engine::Engine()";
35 iCommunicationFailed = false;
36 iCurrentWeekFetched = false;
43 //initialize idle time counter
44 iIdleTimeCounter = new QTimer();
45 iIdleTimeCounter->setSingleShot( true);
46 iIdleTimeCounter->setInterval(IDLE_TIME_MULTIPLIER * iConfiguration->displaySettings()->screensaver() );
47 iIdleTimeCounter->start();
49 // create application clock
51 connect( iClock, SIGNAL( tick( QDateTime ) ), this, SLOT( tick( QDateTime )/*checkStatusOfAllRooms()*/ ) );
52 // connect( iClock, SIGNAL( tick( QDateTime ) ), iWindowManager, SLOT( distributeDateTimeInfo( QDateTime ) ) );
54 // Create auto refresh timer
55 iAutoRefresh = new QTimer;
56 iAutoRefresh->setInterval(iConfiguration->connectionSettings()->refreshInterval() * 1000);
57 iAutoRefresh->start();
58 connect( iAutoRefresh, SIGNAL( timeout() ), iAutoRefresh, SLOT( start() ) );
59 // connect( iAutoRefresh, SIGNAL( timeout() ), this, SLOT( fetchMeetings() ) );
61 if( iDevice->currentOperationMode() == DeviceManager::KioskMode )
63 iWindowManager->setFullscreen();
68 // QTimer::singleShot( 0, this, SLOT( fetchMeetings() ) );
70 // TODO: continue implementation
75 qDebug() << "Engine::~Engine()";
76 while ( !iMeetings.isEmpty() )
77 delete iMeetings.takeFirst();
79 if ( iIdleTimeCounter != 0 )
81 iIdleTimeCounter->stop();
82 delete iIdleTimeCounter;
88 QT_DELETE( iUIManager );
89 QT_DELETE( iWindowManager );
92 void Engine::closeApplication()
94 qDebug() << "Engine::closeApplication()";
95 // closes application after 1 second
96 QTimer::singleShot( 1000, QApplication::instance(), SLOT( quit() ));
99 Room* Engine::defaultRoom()
101 qDebug() << "Engine::defaultRoom()";
102 return iConfiguration->defaultRoom();
105 void Engine::checkStatusOfAllRooms()
107 // TODO: Check if date has changed
108 // qDebug() << "Engine::checkStatusOfAllRooms()";
109 // iterate trough on the rooms
110 for (int i = 0; i < iConfiguration->rooms().count(); i++)
112 // and check the status
113 roomStatusInfoNeeded( iConfiguration->rooms().at(i) );
117 int Engine::indexOfMeetingAt(Room *aRoom, QDateTime aAt)
119 // qDebug() << "Engine::indexOfMeetingAt( Room *, QDateTime )";
120 for ( int i = 0; i < iMeetings.count(); i++ )
122 // exchange server ensures that there is only one meeting in a room at a specified time
123 if (aRoom->equals(iMeetings.at( i )->room() ) && iMeetings.at( i )->startsAt() <= aAt && iMeetings.at( i )->endsAt() >= aAt)
131 int Engine::indexOfMeetingAfter(Room *aRoom, QDateTime aAfter)
133 // qDebug() << "Engine::indexOfMeetingAfter( Room *, QDateTime )";
134 // seeks for the next meeting on the SAME DAY
136 for (int i = 0; i < iMeetings.count(); i++)
138 // if the meeting is in the same room, on the same day but after the specified time
139 if (aRoom->equals(iMeetings.at( i )->room() ) && iMeetings.at( i )->startsAt().date() == aAfter.date() && iMeetings.at( i )->startsAt() > aAfter)
141 // if there was not any meeting find yet or the previously found is a later one then the (i)th
142 if (min == -1 || iMeetings.at( min )->startsAt() > iMeetings.at( i )->startsAt() )
151 void Engine::roomStatusInfoNeeded(Room *aRoom)
153 // qDebug() << "Engine::roomStatusInfoNeeded( Room * )";
159 int indexOfCurrentMeeting = indexOfMeetingAt(aRoom, iClock->datetime() );
160 int indexOfNextMeeting = indexOfMeetingAfter(aRoom, iClock->datetime() );
162 // if there is no meeting, then status is Free; otherwise Busy
163 Room::Status status = (indexOfCurrentMeeting == -1 ) ? Room::FreeStatus : Room::BusyStatus;
164 // if room is Busy, then check end time, otherwise...
165 QTime until = (status == Room::BusyStatus ) ? iMeetings.at( indexOfCurrentMeeting )->endsAt().time() :
166 // ...if there is meeting following on the same day then check end time, otherwise end is the of the working day
167 ( ( indexOfNextMeeting != -1 ) ? iMeetings.at( indexOfNextMeeting )->startsAt().time() : Engine::endOfTheDay );
169 //currently works only for default room
170 if ( aRoom->equals( *(iCurrentRoom) ) )
172 emit roomStatusChanged( status, until );
176 void Engine::fetchMeetingDetails( Meeting *aMeeting )
178 qDebug() << "Engine::fetchMeetingDetails( Meeting* )";
179 iCommunication->fetchMeetingDetails( *aMeeting );
182 void Engine::meetingsFetched( const QList<Meeting*> &aMeetings )
184 qDebug() << "Engine::meetingsFetched( const QList<Meeting*> & )";
185 // TODO: should check if this week's meetings were fetched
186 if( iCommunicationFailed || !iCurrentWeekFetched )
188 iCurrentWeekFetched = true;
189 iCommunicationFailed = false;
190 iUIManager->connectionEstablished();
193 for ( int i = 0; i < iMeetings.count(); ++i )
195 // TODO: Check if these are current week's meetings and do not overwrite those
196 Meeting* m = iMeetings.takeAt( i );
200 for ( int i = 0; i < aMeetings.count(); ++i ) {
201 Meeting* m = new Meeting( *( aMeetings.at( i ) ) );
202 iMeetings.append( m );
205 // refresh room status info
206 roomStatusInfoNeeded( iCurrentRoom /*defaultRoom()*/ );
209 void Engine::errorHandler( int aCode, const QString &aAddInfo )
211 if( aCode >= 100 && aCode < 150 )
213 iCommunicationFailed = true;
214 if ( iUIManager != 0 ) iUIManager->connectionLost();
216 if ( iWindowManager != 0 )
218 iWindowManager->error( ErrorMapper::codeToString( aCode, aAddInfo ) );
222 void Engine::fetchMeetings( const int aWeek, const int aYear, const Room *aIn )
225 << "Engine::fetchMeetings( const int aWeek, const int aYear, const Room * )";
226 iCommunication->fetchMeetings(aWeek, aYear, *aIn);
229 void Engine::cancelFetchMeetingDetails()
231 iCommunication->cancelFetchMeetingDetails();
234 void Engine::shownWeekChanged( QDate aFrom )
236 qDebug() << "[Engine::shownWeekChanged] <Invoked>";
237 iCommunication->fetchMeetings( aFrom.weekNumber(), aFrom.year(), *iCurrentRoom/*defaultRoom()*/ );
240 void Engine::changeDeviceMode()
242 connect( iDevice, SIGNAL( changeModeFailed() ), this, SLOT( changeModeFailed() ) );
243 iAutoRefresh->stop(); // Stop the meeting update
244 iDevice->changeMode();
247 void Engine::changeModeFailed()
249 qDebug() << "Engine::progressBarCancelled()";
250 iAutoRefresh->start(); //we start the metting updating
253 void Engine::initUserInterface()
255 qDebug() << "[Engine::initUserInterface] <Invoked>";
257 // Initialize the window manager and connect what ever signals can be connected
258 iWindowManager = new WindowManager;
259 // Create the UIManager which internally handles most of the UI actions
260 iUIManager = new UIManager( this, iWindowManager );
262 connect( iWindowManager, SIGNAL( eventDetected() ), this, SLOT( handleViewEvent() ) );
263 connect( iWindowManager, SIGNAL( previousViewRestored() ), iUIManager, SLOT( previousViewRestored() ) );
264 connect( iWindowManager, SIGNAL( dialogActivated() ), this, SLOT( dialogActivated() ) );
265 connect( iWindowManager, SIGNAL( dialogDeactivated() ), this, SLOT( dialogDeactivated() ) );
268 iWindowManager->setWindowState( Qt::WindowMaximized );
269 iWindowManager->show();
270 iUIManager->showMainView();
272 // This triggers the meeting fetching
273 iUIManager->currentRoomChanged( this->iCurrentRoom );
275 qDebug() << "[Engine::initUserInterface] <Finished>";
278 void Engine::handleViewEvent()
280 if ( iIdleTimeCounter != 0 && iIdleTimeCounter->isActive())
282 // Restart the idle time counter when view event is received
283 iIdleTimeCounter->stop();
284 iIdleTimeCounter->start();
288 void Engine::initConfiguration()
290 iConfiguration = Configuration::instance();
291 if ( iConfiguration == 0 )
293 QTimer::singleShot( 0, this, SLOT( closeApplication() ) );
295 iCurrentRoom = iConfiguration->defaultRoom();
298 void Engine::connectSignals()
300 // Connect engine objects signals to UIManager
301 connect( iClock, SIGNAL( tick( QDateTime ) ), iUIManager, SLOT( updateTime( QDateTime ) ) );
302 connect( iIdleTimeCounter, SIGNAL( timeout() ) , iUIManager, SLOT( roomStatusIndicatorRequested() ) );
304 iUIManager->connectDeviceManager( iDevice );
305 iUIManager->connectCommunicationManager( iCommunication );
308 void Engine::initCommunication()
310 // initialize communication
311 iCommunication = new CommunicationManager( *(iConfiguration->connectionSettings()) );
312 connect( iCommunication, SIGNAL( error( int, CommunicationManager::CommunicationType ) ),
313 this, SLOT( errorHandler( int ) ) );
314 connect( iCommunication, SIGNAL( meetingsFetched( const QList<Meeting*>& ) ),
315 this, SLOT( meetingsFetched( const QList<Meeting*>& ) ) );
318 void Engine::initDevice()
320 // create device manager
321 iDevice = new DeviceManager( iConfiguration->startupSettings() );
322 connect( iDevice, SIGNAL( error( int, const QString& ) ), this, SLOT( errorHandler( int, const QString& ) ) );
323 iDevice->initDeviceManager();
326 void Engine::dialogActivated()
328 if ( iIdleTimeCounter != 0 )
330 iIdleTimeCounter->stop();
334 void Engine::dialogDeactivated()
336 if ( iIdleTimeCounter != 0 )
338 iIdleTimeCounter->start();
342 void Engine::previousViewRestored()
344 if ( iIdleTimeCounter != 0 )
346 iIdleTimeCounter->start();
350 void Engine::stopIdleTimeCounter()
352 if ( iIdleTimeCounter != 0 )
354 iIdleTimeCounter->stop();
358 void Engine::startIdleTimeCounter()
360 if ( iIdleTimeCounter != 0 )
362 iIdleTimeCounter->start();
366 void Engine::currentRoomChanged(Room *aRoom)
368 qDebug() << "[Engine::currentRoomChanged] <invoked>";
369 iCurrentRoom = aRoom;
370 roomStatusInfoNeeded( iCurrentRoom );
373 void Engine::tick( QDateTime aCurrentDateTime )
375 // Called once every second
376 checkStatusOfAllRooms();
377 if( aCurrentDateTime.date() != iCurrentDate)
379 // Check if week has changed and fetch meetings for this week
380 if( aCurrentDateTime.date().weekNumber() != iCurrentDate.weekNumber()
381 || aCurrentDateTime.date().year() != iCurrentDate.year() )
383 qDebug() << "[Engine::tick] detected week change, fetching meetings";
384 fetchMeetings( aCurrentDateTime.date().weekNumber(), aCurrentDateTime.date().year(), iCurrentRoom );
387 iCurrentDate = aCurrentDateTime.date();