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() )
121 && iMeetings.at( i )->startsAt() <= aAt
122 && iMeetings.at( i )->endsAt() >= aAt )
130 int Engine::indexOfMeetingAfter( Room *aRoom, QDateTime aAfter )
132 // qDebug() << "Engine::indexOfMeetingAfter( Room *, QDateTime )";
133 // seeks for the next meeting on the SAME DAY
135 for ( int i = 0; i < iMeetings.count(); i++ )
137 // if the meeting is in the same room, on the same day but after the specified time
138 if ( aRoom->equals( iMeetings.at( i )->room() )
139 && iMeetings.at( i )->startsAt().date() == aAfter.date()
140 && iMeetings.at( i )->startsAt() > aAfter )
142 // if there was not any meeting find yet or the previously found is a later one then the (i)th
144 || iMeetings.at( min )->startsAt() > iMeetings.at( i )->startsAt() )
153 void Engine::roomStatusInfoNeeded( Room *aRoom )
155 // qDebug() << "Engine::roomStatusInfoNeeded( Room * )";
161 int indexOfCurrentMeeting = indexOfMeetingAt( aRoom, iClock->datetime() );
162 int indexOfNextMeeting = indexOfMeetingAfter( aRoom, iClock->datetime() );
164 // if there is no meeting, then status is Free; otherwise Busy
165 Room::Status status = ( indexOfCurrentMeeting == -1 ) ? Room::FreeStatus : Room::BusyStatus;
166 // if room is Busy, then check end time, otherwise...
167 QTime until = ( status == Room::BusyStatus ) ? iMeetings.at( indexOfCurrentMeeting )->endsAt().time() :
168 // ...if there is meeting following on the same day then check end time, otherwise end is the of the working day
169 (( indexOfNextMeeting != -1 ) ? iMeetings.at( indexOfNextMeeting )->startsAt().time() : Engine::endOfTheDay );
171 //currently works only for deafult room
172 // if( aRoom->equals( *(defaultRoom() ) ) )
173 // iWindowManager->roomStatusChanged( aRoom, status, until );
176 void Engine::fetchMeetings()
178 Room *room = defaultRoom();
179 qDebug() << "Engine::fetchMeetings for " << room->name();
180 fetchMeetings( iClock->datetime(), iClock->datetime().addDays( 7 ), room );
183 bool Engine::isMeetingInList( const QList<Meeting*> &aList, const Meeting *aMeeting )
185 qDebug() << "Engine::isMeetingInList( const QList<Meeting*> &, const Meeting * )";
186 for ( int i = 0; i < aList.count(); i++ )
188 if ( aMeeting->equals( *(aList.at( i )) ) )
196 void Engine::meetingsFetched( const QList<Meeting*> &aMeetings )
198 qDebug() << "Engine::meetingsFetched( const QList<Meeting*> & )";
199 // check if there is any new meeting in the list came from the server -> added
200 for ( int i = 0; i < aMeetings.count(); i++ )
202 // if the (i)th meeting is not in the local meeting list
203 if ( !isMeetingInList( iMeetings, aMeetings.at( i ) ) )
205 // add to the local database =)
206 Meeting* m = new Meeting( *(aMeetings.at( i )) );
207 iMeetings.append( m );
208 // and signal the changes
209 // iWeeklyView->insertMeeting( m );
213 // check if there is any meeting NOT in the list came from the server -> deleted
214 for ( int i = 0; i < iMeetings.count(); i++ )
216 // if the (i)th meeting is in the local but NOT in the server's meeting list
217 if ( !isMeetingInList( aMeetings, iMeetings.at( i ) ) )
219 Meeting* m = iMeetings.takeAt( i );
220 // signal the changes
221 // iWeeklyView->deleteMeeting( m );
222 // delete the meeting from the local list
227 // refresh room status info
228 roomStatusInfoNeeded( defaultRoom() );
231 void Engine::errorHandler( int aCode, const QString &aAddInfo )
233 qDebug() << "Engine::ErrorHandler, aCode: " << aCode;
234 // inform UI about the problem
235 if( aCode >= 100 && aCode <= 150 ) { //communication errors
236 //we don't want these to close operation changing
237 qDebug() << "CommunicationManager signaled an error:" << aCode;
240 iWindowManager->error( ErrorMapper::codeToString( aCode, aAddInfo ) );
243 void Engine::fetchMeetings( const QDateTime &aFrom, const QDateTime &aUntil, const Room *aIn )
245 qDebug() << "Engine::fetchMeetings( const QDateTime &, const QDateTime &, const Room * )";
246 iCommunication->fetchMeetings( aFrom, aUntil, *aIn );
249 void Engine::fetchMeetingDetails(Meeting *aMeeting)
251 qDebug() << "[Engine::fetchMeetingDetails] <TODO : METHOD NOT IMPLEMENTED>";
252 // Meeting tempMeeting = aMeeting;
253 // iCommunication->fetchMeetingDetails( tempMeeting );
256 void Engine::cancelFetchMeetingDetails()
258 iCommunication->cancelFetchMeetingDetails();
261 void Engine::shownWeekChanged( QDate aFrom )
263 qDebug() << "[Engine::shownWeekChanged] <Invoked>";
264 QDateTime from( aFrom );
265 QDateTime to( aFrom.addDays( 7 ), QTime( 23, 59 ) );
266 qDebug() << "[Engine::shownWeekChanged] <From " << aFrom.toString( "d.m. h:mm" ) << " to " << to.toString( "d.m. h:mm" ) << ">";
267 iCommunication->fetchMeetings( from, to, *defaultRoom() );
268 // fetchMeetings( from, to, iWindowManager->weeklyView()->currentRoom() );
271 void Engine::changeDeviceMode( bool aChange )
275 connect( iDevice, SIGNAL( changingModeFailed() ), this, SLOT( changeModeFailed() ) );
276 iAutoRefresh->stop(); // Stop the meeting update
278 iDevice->changeMode( aChange );
281 void Engine::changeModeFailed()
283 qDebug() << "Engine::progressBarCancelled()";
284 iDevice->changeMode( false );
285 iAutoRefresh->start(); //we start the metting updating
288 void Engine::initUserInterface()
290 qDebug() << "[Engine::initUserInterface] <Invoked>";
292 // Initialize the window manager and connect what ever signals can be connected
293 iWindowManager = new WindowManager;
294 // Create the UIManager which internally handles most of the UI actions
295 iUIManager = new UIManager( this, iWindowManager );
297 connect( iWindowManager, SIGNAL( eventDetected() ), this, SLOT( handleViewEvent() ) );
298 connect( iWindowManager, SIGNAL( previousViewRestored() ), iUIManager, SLOT( previousViewRestored() ) );
299 connect( iWindowManager, SIGNAL( dialogActivated() ), this, SLOT( dialogActivated() ) );
300 connect( iWindowManager, SIGNAL( dialogDeactivated() ), this, SLOT( dialogDeactivated() ) );
303 iWindowManager->setWindowState( Qt::WindowMaximized );
304 iWindowManager->show();
305 iUIManager->showMainView();
307 qDebug() << "[Engine::initUserInterface] <Finished>";
310 void Engine::handleViewEvent()
312 if ( iIdleTimeCounter != 0 )
314 // Restart the idle time counter when view event is received
315 iIdleTimeCounter->stop();
316 iIdleTimeCounter->start();
320 void Engine::initConfiguration()
322 iConfiguration = Configuration::instance();
323 if ( iConfiguration == 0 )
325 QTimer::singleShot( 0, this, SLOT( closeApplication() ) );
329 void Engine::connectSignals()
331 // Connect engine objects signals to UIManager
332 connect( iClock, SIGNAL( tick( QDateTime ) ), iUIManager, SLOT( updateTime( QDateTime ) ) );
333 connect( iIdleTimeCounter, SIGNAL( timeout() ) , iUIManager, SLOT( roomStatusIndicatorRequested() ) );
335 iUIManager->connectDeviceManager( iDevice );
336 iUIManager->connectCommunicationManager( iCommunication );
339 void Engine::initCommunication()
341 // initialize communication
342 iCommunication = new CommunicationManager( *(iConfiguration->connectionSettings()) );
343 connect( iCommunication, SIGNAL( error( int, CommunicationManager::CommunicationType ) ),
344 this, SLOT( errorHandler( int ) ) );
345 connect( iCommunication, SIGNAL( meetingsFetched( const QList<Meeting*>& ) ),
346 this, SLOT( meetingsFetched( const QList<Meeting*>& ) ) );
349 void Engine::initDevice()
351 // create device manager
352 iDevice = new DeviceManager( iConfiguration->startupSettings() );
353 connect( iDevice, SIGNAL( error( int, const QString& ) ), this, SLOT( errorHandler( int, const QString& ) ) );
354 iDevice->initDeviceManager();
357 void Engine::dialogActivated()
359 if ( iIdleTimeCounter != 0 )
361 iIdleTimeCounter->stop();
365 void Engine::dialogDeactivated()
367 if ( iIdleTimeCounter != 0 )
369 iIdleTimeCounter->start();
373 void Engine::previousViewRestored()
375 if ( iIdleTimeCounter != 0 )
377 iIdleTimeCounter->start();
381 void Engine::stopIdleTimeCounter()
383 if ( iIdleTimeCounter != 0 )
385 iIdleTimeCounter->stop();
389 void Engine::startIdleTimeCounter()
391 if ( iIdleTimeCounter != 0 )
393 iIdleTimeCounter->start();