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 )
33 qDebug() << "Engine::Engine()";
40 //initialize idle time counter
41 iIdleTimeCounter = new QTimer();
42 iIdleTimeCounter->setSingleShot( true );
43 // iIdleTimeCounter->setInterval( IDLE_TIME_MULTIPLIER * iConfiguration->displaySettings()->screensaver() );
44 iIdleTimeCounter->setInterval( 10000 );
45 iIdleTimeCounter->start();
47 // create application clock
49 connect( iClock, SIGNAL( tick( QDateTime ) ), this, SLOT( checkStatusOfAllRooms() ) );
50 // connect( iClock, SIGNAL( tick( QDateTime ) ), iWindowManager, SLOT( distributeDateTimeInfo( QDateTime ) ) );
52 // Create auto refresh timer
53 iAutoRefresh = new QTimer;
54 iAutoRefresh->setInterval(iConfiguration->connectionSettings()->refreshInterval() * 1000);
55 iAutoRefresh->start();
56 connect( iAutoRefresh, SIGNAL( timeout() ), iAutoRefresh, SLOT( start() ) );
57 connect( iAutoRefresh, SIGNAL( timeout() ), this, SLOT( fetchMeetings() ) );
59 if( iDevice->currentOperationMode() == DeviceManager::KioskMode )
61 iWindowManager->setFullscreen();
66 QTimer::singleShot( 0, this, SLOT( fetchMeetings() ) );
68 // TODO: continue implementation
73 qDebug() << "Engine::~Engine()";
74 while ( !iMeetings.isEmpty() )
75 delete iMeetings.takeFirst();
77 if ( iIdleTimeCounter != 0 )
79 iIdleTimeCounter->stop();
80 delete iIdleTimeCounter;
86 QT_DELETE( iUIManager );
87 QT_DELETE( iWindowManager );
90 void Engine::closeApplication()
92 qDebug() << "Engine::closeApplication()";
93 // closes application after 1 second
94 QTimer::singleShot( 1000, QApplication::instance(), SLOT( quit() ));
97 Room* Engine::defaultRoom()
99 qDebug() << "Engine::defaultRoom()";
100 return iConfiguration->defaultRoom();
103 void Engine::checkStatusOfAllRooms()
105 // qDebug() << "Engine::checkStatusOfAllRooms()";
106 // iterate trough on the rooms
107 for (int i = 0; i < iConfiguration->rooms().count(); i++)
109 // and check the status
110 roomStatusInfoNeeded(iConfiguration->rooms().at(i) );
114 int Engine::indexOfMeetingAt(Room *aRoom, QDateTime aAt)
116 // qDebug() << "Engine::indexOfMeetingAt( Room *, QDateTime )";
117 for ( int i = 0; i < iMeetings.count(); i++ )
119 // exchange server ensures that there is only one meeting in a room at a specified time
120 if (aRoom->equals(iMeetings.at( i )->room() ) && iMeetings.at( i )->startsAt() <= aAt && iMeetings.at( i )->endsAt() >= aAt)
128 int Engine::indexOfMeetingAfter(Room *aRoom, QDateTime aAfter)
130 // qDebug() << "Engine::indexOfMeetingAfter( Room *, QDateTime )";
131 // seeks for the next meeting on the SAME DAY
133 for (int i = 0; i < iMeetings.count(); i++)
135 // if the meeting is in the same room, on the same day but after the specified time
136 if (aRoom->equals(iMeetings.at( i )->room() ) && iMeetings.at( i )->startsAt().date() == aAfter.date() && iMeetings.at( i )->startsAt() > aAfter)
138 // if there was not any meeting find yet or the previously found is a later one then the (i)th
139 if (min == -1 || iMeetings.at( min )->startsAt() > iMeetings.at( i )->startsAt() )
148 void Engine::roomStatusInfoNeeded(Room *aRoom)
150 // qDebug() << "Engine::roomStatusInfoNeeded( Room * )";
156 int indexOfCurrentMeeting = indexOfMeetingAt(aRoom, iClock->datetime() );
157 int indexOfNextMeeting = indexOfMeetingAfter(aRoom, iClock->datetime() );
159 // if there is no meeting, then status is Free; otherwise Busy
162 (indexOfCurrentMeeting == -1 ) ? Room::FreeStatus : Room::BusyStatus;
163 // if room is Busy, then check end time, otherwise...
164 QTime until = (status == Room::BusyStatus ) ? iMeetings.at( indexOfCurrentMeeting )->endsAt().time() :
165 // ...if there is meeting following on the same day then check end time, otherwise end is the of the working day
166 ((indexOfNextMeeting != -1 ) ? iMeetings.at( indexOfNextMeeting )->startsAt().time() : Engine::endOfTheDay );
168 //currently works only for deafult room
169 // if( aRoom->equals( *(defaultRoom() ) ) )
170 // iWindowManager->roomStatusChanged( aRoom, status, until );
173 void Engine::fetchMeetings()
175 Room *room = defaultRoom();
176 qDebug() << "Engine::fetchMeetings for " << room->name();
177 fetchMeetings(iClock->datetime(), iClock->datetime().addDays( 7), room);
180 bool Engine::isMeetingInList( const QList<Meeting*> &aList, const Meeting *aMeeting )
183 << "Engine::isMeetingInList( const QList<Meeting*> &, const Meeting * )";
184 for (int i = 0; i < aList.count(); i++)
186 if (aMeeting->equals( *(aList.at(i))) )
194 void Engine::meetingsFetched(const QList<Meeting*> &aMeetings)
196 qDebug() << "Engine::meetingsFetched( const QList<Meeting*> & )";
197 // check if there is any new meeting in the list came from the server -> added
198 for (int i = 0; i < aMeetings.count(); i++)
200 // if the (i)th meeting is not in the local meeting list
201 if ( !isMeetingInList(iMeetings, aMeetings.at(i) ) )
203 // add to the local database =)
204 Meeting* m = new Meeting( *(aMeetings.at( i )) );
206 // and signal the changes
207 // iWeeklyView->insertMeeting( m );
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 // iWeeklyView->deleteMeeting( m );
220 // delete the meeting from the local list
225 // refresh room status info
226 roomStatusInfoNeeded(defaultRoom() );
229 void Engine::errorHandler( int aCode, const QString &aAddInfo )
231 iWindowManager->error( ErrorMapper::codeToString( aCode, aAddInfo ) );
234 void Engine::fetchMeetings( const QDateTime &aFrom, const QDateTime &aUntil, const Room *aIn )
237 << "Engine::fetchMeetings( const QDateTime &, const QDateTime &, const Room * )";
238 iCommunication->fetchMeetings(aFrom, aUntil, *aIn);
241 void Engine::fetchMeetingDetails(Meeting *aMeeting)
243 qDebug() << "[Engine::fetchMeetingDetails] <TODO : METHOD NOT IMPLEMENTED>";
244 // Meeting tempMeeting = aMeeting;
245 // iCommunication->fetchMeetingDetails( tempMeeting );
248 void Engine::cancelFetchMeetingDetails()
250 iCommunication->cancelFetchMeetingDetails();
253 void Engine::shownWeekChanged( QDate aFrom )
255 qDebug() << "[Engine::shownWeekChanged] <Invoked>";
256 QDateTime from( aFrom );
257 QDateTime to( aFrom.addDays( 7 ), QTime( 23, 59 ) );
258 qDebug() << "[Engine::shownWeekChanged] <From " << aFrom.toString( "d.m. h:mm" ) << " to " << to.toString( "d.m. h:mm" ) << ">";
259 iCommunication->fetchMeetings( from, to, *defaultRoom() );
260 // fetchMeetings( from, to, iWindowManager->weeklyView()->currentRoom() );
263 void Engine::changeDeviceMode( bool aChange )
267 connect( iDevice, SIGNAL( changingModeFailed() ), this, SLOT( changeModeFailed() ) );
268 iAutoRefresh->stop(); // Stop the meeting update
270 iDevice->changeMode( aChange );
273 void Engine::changeModeFailed()
275 qDebug() << "Engine::progressBarCancelled()";
276 iDevice->changeMode( false );
277 iAutoRefresh->start(); //we start the metting updating
280 void Engine::initUserInterface()
282 qDebug() << "[Engine::initUserInterface] <Invoked>";
284 // Initialize the window manager and connect what ever signals can be connected
285 iWindowManager = new WindowManager;
286 // Create the UIManager which internally handles most of the UI actions
287 iUIManager = new UIManager( this, iWindowManager );
289 connect( iWindowManager, SIGNAL( eventDetected() ), this, SLOT( handleViewEvent() ) );
290 connect( iWindowManager, SIGNAL( previousViewRestored() ), iUIManager, SLOT( previousViewRestored() ) );
291 connect( iWindowManager, SIGNAL( dialogActivated() ), this, SLOT( dialogActivated() ) );
292 connect( iWindowManager, SIGNAL( dialogDeactivated() ), this, SLOT( dialogDeactivated() ) );
295 iWindowManager->setWindowState( Qt::WindowMaximized );
296 iWindowManager->show();
297 iUIManager->showMainView();
299 qDebug() << "[Engine::initUserInterface] <Finished>";
302 void Engine::handleViewEvent()
304 if ( iIdleTimeCounter != 0 )
306 // Restart the idle time counter when view event is received
307 iIdleTimeCounter->stop();
308 iIdleTimeCounter->start();
312 void Engine::initConfiguration()
314 iConfiguration = Configuration::instance();
315 if ( iConfiguration == 0 )
317 QTimer::singleShot( 0, this, SLOT( closeApplication() ) );
321 void Engine::connectSignals()
323 // Connect engine objects signals to UIManager
324 connect( iClock, SIGNAL( tick( QDateTime ) ), iUIManager, SLOT( updateTime( QDateTime ) ) );
325 connect( iIdleTimeCounter, SIGNAL( timeout() ) , iUIManager, SLOT( roomStatusIndicatorRequested() ) );
327 iUIManager->connectDeviceManager( iDevice );
328 iUIManager->connectCommunicationManager( iCommunication );
331 void Engine::initCommunication()
333 // initialize communication
334 iCommunication = new CommunicationManager( *(iConfiguration->connectionSettings()) );
335 connect( iCommunication, SIGNAL( error( int, CommunicationManager::CommunicationType ) ),
336 this, SLOT( errorHandler( int ) ) );
337 connect( iCommunication, SIGNAL( meetingsFetched( const QList<Meeting*>& ) ),
338 this, SLOT( meetingsFetched( const QList<Meeting*>& ) ) );
341 void Engine::initDevice()
343 // create device manager
344 iDevice = new DeviceManager( iConfiguration->startupSettings() );
345 connect( iDevice, SIGNAL( error( int, const QString& ) ), this, SLOT( errorHandler( int, const QString& ) ) );
346 iDevice->initDeviceManager();
349 void Engine::dialogActivated()
351 if ( iIdleTimeCounter != 0 )
353 iIdleTimeCounter->stop();
357 void Engine::dialogDeactivated()
359 if ( iIdleTimeCounter != 0 )
361 iIdleTimeCounter->start();
365 void Engine::previousViewRestored()
367 if ( iIdleTimeCounter != 0 )
369 iIdleTimeCounter->start();
373 void Engine::stopIdleTimeCounter()
375 if ( iIdleTimeCounter != 0 )
377 iIdleTimeCounter->stop();
381 void Engine::startIdleTimeCounter()
383 if ( iIdleTimeCounter != 0 )
385 iIdleTimeCounter->start();