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()";
41 //initialize idle time counter
42 iIdleTimeCounter = new QTimer();
43 iIdleTimeCounter->setSingleShot( true );
44 // iIdleTimeCounter->setInterval( IDLE_TIME_MULTIPLIER * iConfiguration->displaySettings()->screensaver() );
45 iIdleTimeCounter->setInterval( 10000 );
46 iIdleTimeCounter->start();
48 // create application clock
50 connect( iClock, SIGNAL( tick( QDateTime ) ), this, SLOT( checkStatusOfAllRooms() ) );
51 // connect( iClock, SIGNAL( tick( QDateTime ) ), iWindowManager, SLOT( distributeDateTimeInfo( QDateTime ) ) );
53 // Create auto refresh timer
54 iAutoRefresh = new QTimer;
55 iAutoRefresh->setInterval(iConfiguration->connectionSettings()->refreshInterval() * 1000);
56 iAutoRefresh->start();
57 connect( iAutoRefresh, SIGNAL( timeout() ), iAutoRefresh, SLOT( start() ) );
58 connect( iAutoRefresh, SIGNAL( timeout() ), this, SLOT( fetchMeetings() ) );
60 if( iDevice->currentOperationMode() == DeviceManager::KioskMode )
62 iWindowManager->setFullscreen();
67 QTimer::singleShot( 0, this, SLOT( fetchMeetings() ) );
69 // TODO: continue implementation
74 qDebug() << "Engine::~Engine()";
75 while ( !iMeetings.isEmpty() )
76 delete iMeetings.takeFirst();
78 if ( iIdleTimeCounter != 0 )
80 iIdleTimeCounter->stop();
81 delete iIdleTimeCounter;
87 QT_DELETE( iUIManager );
88 QT_DELETE( iWindowManager );
91 void Engine::closeApplication()
93 qDebug() << "Engine::closeApplication()";
94 // closes application after 1 second
95 QTimer::singleShot( 1000, QApplication::instance(), SLOT( quit() ));
98 Room* Engine::defaultRoom()
100 qDebug() << "Engine::defaultRoom()";
101 return iConfiguration->defaultRoom();
104 void Engine::checkStatusOfAllRooms()
106 // qDebug() << "Engine::checkStatusOfAllRooms()";
107 // iterate trough on the rooms
108 for (int i = 0; i < iConfiguration->rooms().count(); i++)
110 // and check the status
111 roomStatusInfoNeeded(iConfiguration->rooms().at(i) );
115 int Engine::indexOfMeetingAt(Room *aRoom, QDateTime aAt)
117 // qDebug() << "Engine::indexOfMeetingAt( Room *, QDateTime )";
118 for ( int i = 0; i < iMeetings.count(); i++ )
120 // exchange server ensures that there is only one meeting in a room at a specified time
121 if (aRoom->equals(iMeetings.at( i )->room() ) && iMeetings.at( i )->startsAt() <= aAt && iMeetings.at( i )->endsAt() >= aAt)
129 int Engine::indexOfMeetingAfter(Room *aRoom, QDateTime aAfter)
131 // qDebug() << "Engine::indexOfMeetingAfter( Room *, QDateTime )";
132 // seeks for the next meeting on the SAME DAY
134 for (int i = 0; i < iMeetings.count(); i++)
136 // if the meeting is in the same room, on the same day but after the specified time
137 if (aRoom->equals(iMeetings.at( i )->room() ) && iMeetings.at( i )->startsAt().date() == aAfter.date() && iMeetings.at( i )->startsAt() > aAfter)
139 // if there was not any meeting find yet or the previously found is a later one then the (i)th
140 if (min == -1 || iMeetings.at( min )->startsAt() > iMeetings.at( i )->startsAt() )
149 void Engine::roomStatusInfoNeeded(Room *aRoom)
151 // qDebug() << "Engine::roomStatusInfoNeeded( Room * )";
157 int indexOfCurrentMeeting = indexOfMeetingAt(aRoom, iClock->datetime() );
158 int indexOfNextMeeting = indexOfMeetingAfter(aRoom, iClock->datetime() );
160 // if there is no meeting, then status is Free; otherwise Busy
163 (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 deafult room
170 // if( aRoom->equals( *(defaultRoom() ) ) )
171 // iWindowManager->roomStatusChanged( aRoom, status, until );
174 void Engine::fetchMeetings()
176 Room *room = defaultRoom();
177 qDebug() << "Engine::fetchMeetings for " << room->name();
178 fetchMeetings(iClock->datetime(), iClock->datetime().addDays( 7), room);
181 bool Engine::isMeetingInList( const QList<Meeting*> &aList, const Meeting *aMeeting )
184 << "Engine::isMeetingInList( const QList<Meeting*> &, const Meeting * )";
185 for (int i = 0; i < aList.count(); i++)
187 if (aMeeting->equals( *(aList.at(i))) )
195 void Engine::meetingsFetched(const QList<Meeting*> &aMeetings)
197 qDebug() << "Engine::meetingsFetched( const QList<Meeting*> & )";
198 // check if there is any new meeting in the list came from the server -> added
199 for (int i = 0; i < aMeetings.count(); i++)
201 // if the (i)th meeting is not in the local meeting list
202 if ( !isMeetingInList(iMeetings, aMeetings.at(i) ) )
204 // add to the local database =)
205 Meeting* m = new Meeting( *(aMeetings.at( i )) );
207 // and signal the changes
211 // check if there is any meeting NOT in the list came from the server -> deleted
212 for (int i = 0; i < iMeetings.count(); i++)
214 // if the (i)th meeting is in the local but NOT in the server's meeting list
215 if ( !isMeetingInList(aMeetings, iMeetings.at(i) ) )
217 Meeting* m = iMeetings.takeAt(i);
218 // signal the changes
219 // delete the meeting from the local list
224 // refresh room status info
225 roomStatusInfoNeeded(defaultRoom() );
228 void Engine::errorHandler( int aCode, const QString &aAddInfo )
230 if ( iWindowManager != 0 )
232 iWindowManager->error( ErrorMapper::codeToString( aCode, aAddInfo ) );
236 void Engine::fetchMeetings( const QDateTime &aFrom, const QDateTime &aUntil, const Room *aIn )
239 << "Engine::fetchMeetings( const QDateTime &, const QDateTime &, const Room * )";
240 iCommunication->fetchMeetings(aFrom, aUntil, *aIn);
243 void Engine::fetchMeetingDetails(Meeting *aMeeting)
245 qDebug() << "[Engine::fetchMeetingDetails] <TODO : METHOD NOT IMPLEMENTED>";
246 Meeting tempMeeting(aMeeting->primaryId(), aMeeting->room(), aMeeting->startsAt(), aMeeting->endsAt() );
247 iCommunication->fetchMeetingDetails( tempMeeting );
248 // Meeting tempMeeting = aMeeting;
249 // iCommunication->fetchMeetingDetails( tempMeeting );
252 void Engine::cancelFetchMeetingDetails()
254 iCommunication->cancelFetchMeetingDetails();
257 void Engine::shownWeekChanged( QDate aFrom )
259 qDebug() << "[Engine::shownWeekChanged] <Invoked>";
260 QDateTime from( aFrom );
261 QDateTime to( aFrom.addDays( 7 ), QTime( 23, 59 ) );
262 qDebug() << "[Engine::shownWeekChanged] <From " << aFrom.toString( "d.m. h:mm" ) << " to " << to.toString( "d.m. h:mm" ) << ">";
263 iCommunication->fetchMeetings( from, to, *defaultRoom() );
264 // fetchMeetings( from, to, iWindowManager->weeklyView()->currentRoom() );
267 void Engine::changeDeviceMode( bool aChange )
271 connect( iDevice, SIGNAL( changingModeFailed() ), this, SLOT( changeModeFailed() ) );
272 iAutoRefresh->stop(); // Stop the meeting update
274 iDevice->changeMode( aChange );
277 void Engine::changeModeFailed()
279 qDebug() << "Engine::progressBarCancelled()";
280 iDevice->changeMode( false );
281 iAutoRefresh->start(); //we start the metting updating
284 void Engine::initUserInterface()
286 qDebug() << "[Engine::initUserInterface] <Invoked>";
288 // Initialize the window manager and connect what ever signals can be connected
289 iWindowManager = new WindowManager;
290 // Create the UIManager which internally handles most of the UI actions
291 iUIManager = new UIManager( this, iWindowManager );
293 connect( iWindowManager, SIGNAL( eventDetected() ), this, SLOT( handleViewEvent() ) );
294 connect( iWindowManager, SIGNAL( previousViewRestored() ), iUIManager, SLOT( previousViewRestored() ) );
295 connect( iWindowManager, SIGNAL( dialogActivated() ), this, SLOT( dialogActivated() ) );
296 connect( iWindowManager, SIGNAL( dialogDeactivated() ), this, SLOT( dialogDeactivated() ) );
299 iWindowManager->setWindowState( Qt::WindowMaximized );
300 iWindowManager->show();
301 iUIManager->showMainView();
303 qDebug() << "[Engine::initUserInterface] <Finished>";
306 void Engine::handleViewEvent()
308 if ( iIdleTimeCounter != 0 )
310 // Restart the idle time counter when view event is received
311 iIdleTimeCounter->stop();
312 iIdleTimeCounter->start();
316 void Engine::initConfiguration()
318 iConfiguration = Configuration::instance();
319 if ( iConfiguration == 0 )
321 QTimer::singleShot( 0, this, SLOT( closeApplication() ) );
325 void Engine::connectSignals()
327 // Connect engine objects signals to UIManager
328 connect( iClock, SIGNAL( tick( QDateTime ) ), iUIManager, SLOT( updateTime( QDateTime ) ) );
329 connect( iIdleTimeCounter, SIGNAL( timeout() ) , iUIManager, SLOT( roomStatusIndicatorRequested() ) );
331 iUIManager->connectDeviceManager( iDevice );
332 iUIManager->connectCommunicationManager( iCommunication );
335 void Engine::initCommunication()
337 // initialize communication
338 iCommunication = new CommunicationManager( *(iConfiguration->connectionSettings()) );
339 connect( iCommunication, SIGNAL( error( int, CommunicationManager::CommunicationType ) ),
340 this, SLOT( errorHandler( int ) ) );
341 connect( iCommunication, SIGNAL( meetingsFetched( const QList<Meeting*>& ) ),
342 this, SLOT( meetingsFetched( const QList<Meeting*>& ) ) );
345 void Engine::initDevice()
347 // create device manager
348 iDevice = new DeviceManager( iConfiguration->startupSettings() );
349 connect( iDevice, SIGNAL( error( int, const QString& ) ), this, SLOT( errorHandler( int, const QString& ) ) );
350 iDevice->initDeviceManager();
353 void Engine::dialogActivated()
355 if ( iIdleTimeCounter != 0 )
357 iIdleTimeCounter->stop();
361 void Engine::dialogDeactivated()
363 if ( iIdleTimeCounter != 0 )
365 iIdleTimeCounter->start();
369 void Engine::previousViewRestored()
371 if ( iIdleTimeCounter != 0 )
373 iIdleTimeCounter->start();
377 void Engine::stopIdleTimeCounter()
379 if ( iIdleTimeCounter != 0 )
381 iIdleTimeCounter->stop();
385 void Engine::startIdleTimeCounter()
387 if ( iIdleTimeCounter != 0 )
389 iIdleTimeCounter->start();