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 = true;
42 //initialize idle time counter
43 iIdleTimeCounter = new QTimer();
44 iIdleTimeCounter->setSingleShot( true);
45 iIdleTimeCounter->setInterval(IDLE_TIME_MULTIPLIER * iConfiguration->displaySettings()->screensaver() );
46 iIdleTimeCounter->start();
48 // create application clock
50 connect( iClock, SIGNAL( tick( QDateTime ) ), this, SLOT( tick( QDateTime )/*checkStatusOfAllRooms()*/ ) );
51 // connect( iClock, SIGNAL( tick( QDateTime ) ), iWindowManager, SLOT( distributeDateTimeInfo( QDateTime ) ) );
53 // Create auto refresh timer
54 iAutoRefresh = new QTimer;
56 iAutoRefresh->setInterval(Configuration::instance()->getRefreshinterval() * 1000);
58 iAutoRefresh->start();
59 connect( iAutoRefresh, SIGNAL( timeout() ), iAutoRefresh, SLOT( start() ) );
60 connect( iAutoRefresh, SIGNAL( timeout() ), this, SLOT( updateRoomInfo() ) );
62 connect(iWindowManager, SIGNAL(closeClicked()), this, SLOT(closeApplication()));
64 // connect( iAutoRefresh, SIGNAL( timeout() ), this, SLOT( fetchMeetings() ) );
66 if( iDevice->currentOperationMode() == DeviceManager::KioskMode )
68 iWindowManager->setFullscreen();
72 connect( Configuration::instance(), SIGNAL( configurationChanged() ), this, SLOT( configurationChanged() ) );
73 // QTimer::singleShot( 0, this, SLOT( fetchMeetings() ) );
75 // TODO: continue implementation
80 qDebug() << "Engine::~Engine()";
81 while ( !iMeetings.isEmpty() )
82 delete iMeetings.takeFirst();
84 if ( iIdleTimeCounter != 0 )
86 iIdleTimeCounter->stop();
87 delete iIdleTimeCounter;
93 QT_DELETE( iUIManager );
94 QT_DELETE( iWindowManager );
97 void Engine::closeApplication()
99 qDebug() << "Engine::closeApplication()";
100 // closes application after 1 second
101 QTimer::singleShot( 1000, QApplication::instance(), SLOT( quit() ));
104 Room* Engine::defaultRoom()
106 qDebug() << "Engine::defaultRoom()";
107 return iConfiguration->defaultRoom();
110 void Engine::checkStatusOfAllRooms()
112 // TODO: Check if date has changed
113 // iterate trough on the rooms
114 for (int i = 0; i < iConfiguration->rooms().count(); i++)
116 // and check the status
117 roomStatusInfoNeeded( iConfiguration->rooms().at(i) );
121 int Engine::indexOfMeetingAt(Room *aRoom, QDateTime aAt)
123 // qDebug() << "Engine::indexOfMeetingAt( Room *, QDateTime )";
124 for ( int i = 0; i < iMeetings.count(); i++ )
126 // exchange server ensures that there is only one meeting in a room at a specified time
127 if (aRoom->equals(iMeetings.at( i )->room() ) && iMeetings.at( i )->startsAt() <= aAt && iMeetings.at( i )->endsAt() >= aAt)
135 int Engine::indexOfMeetingAfter(Room *aRoom, QDateTime aAfter)
137 // qDebug() << "Engine::indexOfMeetingAfter( Room *, QDateTime )";
138 // seeks for the next meeting on the SAME DAY
140 for (int i = 0; i < iMeetings.count(); i++)
142 // if the meeting is in the same room, on the same day but after the specified time
143 if (aRoom->equals(iMeetings.at( i )->room() ) && iMeetings.at( i )->startsAt().date() == aAfter.date() && iMeetings.at( i )->startsAt() > aAfter)
145 // if there was not any meeting find yet or the previously found is a later one then the (i)th
146 if (min == -1 || iMeetings.at( min )->startsAt() > iMeetings.at( i )->startsAt() )
155 void Engine::roomStatusInfoNeeded(Room *aRoom)
157 // qDebug() << "Engine::roomStatusInfoNeeded( Room * )";
163 int indexOfCurrentMeeting = indexOfMeetingAt(aRoom, iClock->datetime() );
164 int indexOfNextMeeting = indexOfMeetingAfter(aRoom, iClock->datetime() );
166 // if there is no meeting, then status is Free; otherwise Busy
167 Room::Status status = (indexOfCurrentMeeting == -1 ) ? Room::FreeStatus : Room::BusyStatus;
168 // if room is Busy, then check end time, otherwise...
169 QTime until = (status == Room::BusyStatus ) ? iMeetings.at( indexOfCurrentMeeting )->endsAt().time() :
170 // ...if there is meeting following on the same day then check end time, otherwise end is the of the working day
171 ( ( indexOfNextMeeting != -1 ) ? iMeetings.at( indexOfNextMeeting )->startsAt().time() : Engine::endOfTheDay );
173 //currently works only for default room
174 if ( aRoom->equals( *(iCurrentRoom) ) )
176 emit roomStatusChanged( status, until );
180 void Engine::fetchMeetingDetails( Meeting *aMeeting )
182 qDebug() << "Engine::fetchMeetingDetails( Meeting* )";
183 iCommunication->fetchMeetingDetails( *aMeeting );
186 void Engine::meetingsFetched( const QList<Meeting*> &aMeetings )
188 qDebug() << "Engine::meetingsFetched( const QList<Meeting*> & )";
189 QTime c = QTime::currentTime();
190 iLastCommunication.setHMS( c.hour(), c.minute(), c.second() );
192 qDebug() << "Error length: "<< iCommunicationError.length();
193 qDebug() << "Error: "<< iCommunicationError;
195 if( iCommunicationError.length() == 0 )
197 iCommunicationFailed = false;
198 iUIManager->connectionEstablished();
201 for ( int i = 0; i < iMeetings.count(); ++i )
203 // TODO: Check if these are current week's meetings and do not overwrite those
204 Meeting* m = iMeetings.takeAt( i );
208 for ( int i = 0; i < aMeetings.count(); ++i ) {
209 Meeting* m = new Meeting( *( aMeetings.at( i ) ) );
210 iMeetings.append( m );
213 // refresh room status info
214 roomStatusInfoNeeded( iCurrentRoom /*defaultRoom()*/ );
217 void Engine::errorHandler( int aCode, const QString &aAddInfo )
219 iCommunicationFailed = true;
221 if( aCode >= 100 && aCode < 150 )
223 if ( iUIManager != 0 )
225 iUIManager->connectionLost();
228 if ( iWindowManager != 0 )
230 iCommunicationError = ErrorMapper::codeToString( aCode, aAddInfo );
231 //iWindowManager->error( ErrorMapper::codeToString( aCode, aAddInfo ) );
235 bool Engine::connected()
237 return !iCommunicationFailed;
240 QTime Engine::lastUpdated()
242 return iLastCommunication;
245 QString Engine::errorMessage()
247 return iCommunicationError;
250 void Engine::fetchMeetings( const int aWeek, const int aYear, const Room *aIn )
253 << "Engine::fetchMeetings( const int aWeek, const int aYear, const Room * )";
254 iCommunicationError = "";
255 iCommunication->fetchMeetings(aWeek, aYear, *aIn);
258 void Engine::cancelFetchMeetingDetails()
260 iCommunication->cancelFetchMeetingDetails();
263 void Engine::shownWeekChanged( QDate aFrom )
265 qDebug() << "[Engine::shownWeekChanged] <Invoked>";
266 iCommunicationError = "";
267 iCommunication->fetchMeetings( aFrom.weekNumber(), aFrom.year(), *iCurrentRoom/*defaultRoom()*/ );
270 void Engine::changeDeviceMode()
272 connect( iDevice, SIGNAL( changeModeFailed() ), this, SLOT( changeModeFailed() ) );
273 iAutoRefresh->stop(); // Stop the meeting update
274 iDevice->changeMode();
277 void Engine::changeModeFailed()
279 qDebug() << "Engine::progressBarCancelled()";
280 iAutoRefresh->start(); //we start the metting updating
283 void Engine::initUserInterface()
285 qDebug() << "[Engine::initUserInterface] <Invoked>";
287 // Initialize the window manager and connect what ever signals can be connected
288 iWindowManager = new WindowManager;
289 // Create the UIManager which internally handles most of the UI actions
290 iUIManager = new UIManager( this, iWindowManager );
292 connect( iWindowManager, SIGNAL( eventDetected() ), this, SLOT( handleViewEvent() ) );
293 connect( iWindowManager, SIGNAL( previousViewRestored() ), iUIManager, SLOT( previousViewRestored() ) );
294 connect( iWindowManager, SIGNAL( dialogActivated() ), this, SLOT( dialogActivated() ) );
295 connect( iWindowManager, SIGNAL( dialogDeactivated() ), this, SLOT( dialogDeactivated() ) );
298 iWindowManager->setWindowState( Qt::WindowMaximized );
299 iWindowManager->show();
300 iUIManager->showMainView();
302 // This triggers the meeting fetching
303 iUIManager->currentRoomChanged( this->iCurrentRoom );
305 qDebug() << "[Engine::initUserInterface] <Finished>";
308 void Engine::handleViewEvent()
310 if ( iIdleTimeCounter != 0 && iIdleTimeCounter->isActive())
312 // Restart the idle time counter when view event is received
313 iIdleTimeCounter->stop();
314 iIdleTimeCounter->start();
318 void Engine::initConfiguration()
320 iConfiguration = Configuration::instance();
321 if ( iConfiguration == 0 )
323 QTimer::singleShot( 0, this, SLOT( closeApplication() ) );
325 iCurrentRoom = iConfiguration->defaultRoom();
328 void Engine::connectSignals()
330 // Connect engine objects signals to UIManager
331 connect( iClock, SIGNAL( tick( QDateTime ) ), iUIManager, SLOT( updateTime( QDateTime ) ) );
332 connect( iIdleTimeCounter, SIGNAL( timeout() ) , iUIManager, SLOT( roomStatusIndicatorRequested() ) );
334 iUIManager->connectDeviceManager( iDevice );
335 iUIManager->connectCommunicationManager( iCommunication );
338 void Engine::initCommunication()
340 // initialize communication
341 iCommunication = new CommunicationManager(/* *(iConfiguration->connectionSettings()) */);
342 connect( iCommunication, SIGNAL( error( int, CommunicationManager::CommunicationType ) ),
343 this, SLOT( errorHandler( int ) ) );
344 connect( iCommunication, SIGNAL( meetingsFetched( const QList<Meeting*>& ) ),
345 this, SLOT( meetingsFetched( const QList<Meeting*>& ) ) );
348 void Engine::initDevice()
350 // create device manager
351 iDevice = new DeviceManager( iConfiguration->startupSettings() );
352 connect( iDevice, SIGNAL( error( int, const QString& ) ), this, SLOT( errorHandler( int, const QString& ) ) );
353 iDevice->initDeviceManager();
356 void Engine::dialogActivated()
358 if ( iIdleTimeCounter != 0 )
360 iIdleTimeCounter->stop();
364 void Engine::dialogDeactivated()
366 if ( iIdleTimeCounter != 0 )
368 iIdleTimeCounter->start();
372 void Engine::previousViewRestored()
374 if ( iIdleTimeCounter != 0 )
376 iIdleTimeCounter->start();
380 void Engine::stopIdleTimeCounter()
382 if ( iIdleTimeCounter != 0 )
384 iIdleTimeCounter->stop();
388 void Engine::startIdleTimeCounter()
390 if ( iIdleTimeCounter != 0 )
392 iIdleTimeCounter->start();
396 void Engine::currentRoomChanged(Room *aRoom)
398 qDebug() << "[Engine::currentRoomChanged] <invoked>";
399 iCurrentRoom = aRoom;
400 roomStatusInfoNeeded( iCurrentRoom );
403 void Engine::tick( QDateTime aCurrentDateTime )
405 // Called once every second
406 checkStatusOfAllRooms();
407 if( aCurrentDateTime.date() != iCurrentDate)
409 // Check if week has changed and fetch meetings for this week
410 if( aCurrentDateTime.date().weekNumber() != iCurrentDate.weekNumber()
411 || aCurrentDateTime.date().year() != iCurrentDate.year() )
413 qDebug() << "[Engine::tick] detected week change, fetching meetings";
414 fetchMeetings( aCurrentDateTime.date().weekNumber(), aCurrentDateTime.date().year(), iCurrentRoom );
417 iCurrentDate = aCurrentDateTime.date();
421 void Engine::updateRoomInfo()
423 qDebug() << "ENGINE::: updateMeetings";
424 roomStatusInfoNeeded(iCurrentRoom);
427 void Engine::configurationChanged()
429 iAutoRefresh->setInterval(Configuration::instance()->getRefreshinterval() * 1000);