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( 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 // qDebug() << "Engine::checkStatusOfAllRooms()";
108 // iterate trough on the rooms
109 for (int i = 0; i < iConfiguration->rooms().count(); i++)
111 // and check the status
112 roomStatusInfoNeeded( iConfiguration->rooms().at(i) );
116 int Engine::indexOfMeetingAt(Room *aRoom, QDateTime aAt)
118 // qDebug() << "Engine::indexOfMeetingAt( Room *, QDateTime )";
119 for ( int i = 0; i < iMeetings.count(); i++ )
121 // exchange server ensures that there is only one meeting in a room at a specified time
122 if (aRoom->equals(iMeetings.at( i )->room() ) && iMeetings.at( i )->startsAt() <= aAt && 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() ) && iMeetings.at( i )->startsAt().date() == aAfter.date() && iMeetings.at( i )->startsAt() > aAfter)
140 // if there was not any meeting find yet or the previously found is a later one then the (i)th
141 if (min == -1 || iMeetings.at( min )->startsAt() > iMeetings.at( i )->startsAt() )
150 void Engine::roomStatusInfoNeeded(Room *aRoom)
152 // qDebug() << "Engine::roomStatusInfoNeeded( Room * )";
158 int indexOfCurrentMeeting = indexOfMeetingAt(aRoom, iClock->datetime() );
159 int indexOfNextMeeting = indexOfMeetingAfter(aRoom, iClock->datetime() );
161 // if there is no meeting, then status is Free; otherwise Busy
162 Room::Status status = (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( *(iCurrentRoom) ) )
171 emit roomStatusChanged( status, until );
175 void Engine::fetchMeetingDetails( Meeting *aMeeting )
177 qDebug() << "Engine::fetchMeetingDetails( Meeting* )";
178 iCommunication->fetchMeetingDetails( *aMeeting );
181 void Engine::meetingsFetched( const QList<Meeting*> &aMeetings )
183 qDebug() << "Engine::meetingsFetched( const QList<Meeting*> & )";
184 // TODO: should check if this week's meetings were fetched
185 if( iCommunicationFailed || !iCurrentWeekFetched )
187 iCurrentWeekFetched = true;
188 iCommunicationFailed = false;
189 iUIManager->connectionEstablished();
192 for ( int i = 0; i < iMeetings.count(); ++i )
194 Meeting* m = iMeetings.takeAt( i );
198 for ( int i = 0; i < aMeetings.count(); ++i ) {
199 Meeting* m = new Meeting( *( aMeetings.at( i ) ) );
200 iMeetings.append( m );
203 // refresh room status info
204 roomStatusInfoNeeded( iCurrentRoom /*defaultRoom()*/ );
207 void Engine::errorHandler( int aCode, const QString &aAddInfo )
209 if( aCode >= 100 && aCode < 150 )
211 iCommunicationFailed = true;
212 if ( iUIManager != 0 ) iUIManager->connectionLost();
214 if ( iWindowManager != 0 )
216 iWindowManager->error( ErrorMapper::codeToString( aCode, aAddInfo ) );
220 void Engine::fetchMeetings( const QDateTime &aFrom, const QDateTime &aUntil, const Room *aIn )
223 << "Engine::fetchMeetings( const QDateTime &, const QDateTime &, const Room * )";
224 iCommunication->fetchMeetings(aFrom, aUntil, *aIn);
227 void Engine::cancelFetchMeetingDetails()
229 iCommunication->cancelFetchMeetingDetails();
232 void Engine::shownWeekChanged( QDate aFrom )
234 qDebug() << "[Engine::shownWeekChanged] <Invoked>";
235 QDateTime from( aFrom );
236 QDateTime to( aFrom.addDays( 7 ), QTime( 23, 59 ) );
237 qDebug() << "[Engine::shownWeekChanged] <From " << aFrom.toString( "d.m. h:mm" ) << " to " << to.toString( "d.m. h:mm" ) << ">";
238 iCommunication->fetchMeetings( from, to, *iCurrentRoom/*defaultRoom()*/ );
241 void Engine::changeDeviceMode()
243 connect( iDevice, SIGNAL( changeModeFailed() ), this, SLOT( changeModeFailed() ) );
244 iAutoRefresh->stop(); // Stop the meeting update
245 iDevice->changeMode();
248 void Engine::changeModeFailed()
250 qDebug() << "Engine::progressBarCancelled()";
251 iAutoRefresh->start(); //we start the metting updating
254 void Engine::initUserInterface()
256 qDebug() << "[Engine::initUserInterface] <Invoked>";
258 // Initialize the window manager and connect what ever signals can be connected
259 iWindowManager = new WindowManager;
260 // Create the UIManager which internally handles most of the UI actions
261 iUIManager = new UIManager( this, iWindowManager );
263 connect( iWindowManager, SIGNAL( eventDetected() ), this, SLOT( handleViewEvent() ) );
264 connect( iWindowManager, SIGNAL( previousViewRestored() ), iUIManager, SLOT( previousViewRestored() ) );
265 connect( iWindowManager, SIGNAL( dialogActivated() ), this, SLOT( dialogActivated() ) );
266 connect( iWindowManager, SIGNAL( dialogDeactivated() ), this, SLOT( dialogDeactivated() ) );
269 iWindowManager->setWindowState( Qt::WindowMaximized );
270 iWindowManager->show();
271 iUIManager->showMainView();
273 // This triggers the meeting fetching
274 iUIManager->currentRoomChanged( this->iCurrentRoom );
276 qDebug() << "[Engine::initUserInterface] <Finished>";
279 void Engine::handleViewEvent()
281 if ( iIdleTimeCounter != 0 && iIdleTimeCounter->isActive())
283 // Restart the idle time counter when view event is received
284 iIdleTimeCounter->stop();
285 iIdleTimeCounter->start();
289 void Engine::initConfiguration()
291 iConfiguration = Configuration::instance();
292 if ( iConfiguration == 0 )
294 QTimer::singleShot( 0, this, SLOT( closeApplication() ) );
296 iCurrentRoom = iConfiguration->defaultRoom();
299 void Engine::connectSignals()
301 // Connect engine objects signals to UIManager
302 connect( iClock, SIGNAL( tick( QDateTime ) ), iUIManager, SLOT( updateTime( QDateTime ) ) );
303 connect( iIdleTimeCounter, SIGNAL( timeout() ) , iUIManager, SLOT( roomStatusIndicatorRequested() ) );
305 iUIManager->connectDeviceManager( iDevice );
306 iUIManager->connectCommunicationManager( iCommunication );
309 void Engine::initCommunication()
311 // initialize communication
312 iCommunication = new CommunicationManager( *(iConfiguration->connectionSettings()) );
313 connect( iCommunication, SIGNAL( error( int, CommunicationManager::CommunicationType ) ),
314 this, SLOT( errorHandler( int ) ) );
315 connect( iCommunication, SIGNAL( meetingsFetched( const QList<Meeting*>& ) ),
316 this, SLOT( meetingsFetched( const QList<Meeting*>& ) ) );
319 void Engine::initDevice()
321 // create device manager
322 iDevice = new DeviceManager( iConfiguration->startupSettings() );
323 connect( iDevice, SIGNAL( error( int, const QString& ) ), this, SLOT( errorHandler( int, const QString& ) ) );
324 iDevice->initDeviceManager();
327 void Engine::dialogActivated()
329 if ( iIdleTimeCounter != 0 )
331 iIdleTimeCounter->stop();
335 void Engine::dialogDeactivated()
337 if ( iIdleTimeCounter != 0 )
339 iIdleTimeCounter->start();
343 void Engine::previousViewRestored()
345 if ( iIdleTimeCounter != 0 )
347 iIdleTimeCounter->start();
351 void Engine::stopIdleTimeCounter()
353 if ( iIdleTimeCounter != 0 )
355 iIdleTimeCounter->stop();
359 void Engine::startIdleTimeCounter()
361 if ( iIdleTimeCounter != 0 )
363 iIdleTimeCounter->start();
367 void Engine::currentRoomChanged(Room *aRoom)
369 qDebug() << "[Engine::currentRoomChanged] <invoked>";
370 iCurrentRoom = aRoom;
371 roomStatusInfoNeeded( iCurrentRoom );