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->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
160 Room::Status status = (indexOfCurrentMeeting == -1 ) ? Room::FreeStatus : Room::BusyStatus;
161 // if room is Busy, then check end time, otherwise...
162 QTime until = (status == Room::BusyStatus ) ? iMeetings.at( indexOfCurrentMeeting )->endsAt().time() :
163 // ...if there is meeting following on the same day then check end time, otherwise end is the of the working day
164 ( ( indexOfNextMeeting != -1 ) ? iMeetings.at( indexOfNextMeeting )->startsAt().time() : Engine::endOfTheDay );
166 //currently works only for deafult room
167 if ( aRoom->equals( *(iCurrentRoom) ) )
169 emit roomStatusChanged( status, until );
173 void Engine::fetchMeetingDetails( Meeting *aMeeting )
175 qDebug() << "Engine::fetchMeetingDetails( Meeting* )";
176 iCommunication->fetchMeetingDetails( *aMeeting );
179 void Engine::meetingsFetched( const QList<Meeting*> &aMeetings )
181 qDebug() << "Engine::meetingsFetched( const QList<Meeting*> & )";
183 for ( int i = 0; i < iMeetings.count(); ++i ) {
184 Meeting* m = iMeetings.takeAt( i );
188 for ( int i = 0; i < aMeetings.count(); ++i ) {
189 Meeting* m = new Meeting( *( aMeetings.at( i ) ) );
190 iMeetings.append( m );
193 // refresh room status info
194 roomStatusInfoNeeded( iCurrentRoom /*defaultRoom()*/ );
197 void Engine::errorHandler( int aCode, const QString &aAddInfo )
199 if ( iWindowManager != 0 )
201 iWindowManager->error( ErrorMapper::codeToString( aCode, aAddInfo ) );
205 void Engine::fetchMeetings( const QDateTime &aFrom, const QDateTime &aUntil, const Room *aIn )
208 << "Engine::fetchMeetings( const QDateTime &, const QDateTime &, const Room * )";
209 iCommunication->fetchMeetings(aFrom, aUntil, *aIn);
212 void Engine::cancelFetchMeetingDetails()
214 iCommunication->cancelFetchMeetingDetails();
217 void Engine::shownWeekChanged( QDate aFrom )
219 qDebug() << "[Engine::shownWeekChanged] <Invoked>";
220 QDateTime from( aFrom );
221 QDateTime to( aFrom.addDays( 7 ), QTime( 23, 59 ) );
222 qDebug() << "[Engine::shownWeekChanged] <From " << aFrom.toString( "d.m. h:mm" ) << " to " << to.toString( "d.m. h:mm" ) << ">";
223 iCommunication->fetchMeetings( from, to, *iCurrentRoom/*defaultRoom()*/ );
226 void Engine::changeDeviceMode()
228 connect( iDevice, SIGNAL( changeModeFailed() ), this, SLOT( changeModeFailed() ) );
229 iAutoRefresh->stop(); // Stop the meeting update
230 iDevice->changeMode();
233 void Engine::changeModeFailed()
235 qDebug() << "Engine::progressBarCancelled()";
236 iAutoRefresh->start(); //we start the metting updating
239 void Engine::initUserInterface()
241 qDebug() << "[Engine::initUserInterface] <Invoked>";
243 // Initialize the window manager and connect what ever signals can be connected
244 iWindowManager = new WindowManager;
245 // Create the UIManager which internally handles most of the UI actions
246 iUIManager = new UIManager( this, iWindowManager );
248 connect( iWindowManager, SIGNAL( eventDetected() ), this, SLOT( handleViewEvent() ) );
249 connect( iWindowManager, SIGNAL( previousViewRestored() ), iUIManager, SLOT( previousViewRestored() ) );
250 connect( iWindowManager, SIGNAL( dialogActivated() ), this, SLOT( dialogActivated() ) );
251 connect( iWindowManager, SIGNAL( dialogDeactivated() ), this, SLOT( dialogDeactivated() ) );
254 iWindowManager->setWindowState( Qt::WindowMaximized );
255 iWindowManager->show();
256 iUIManager->showMainView();
258 // This triggers the meeting fetching
259 iUIManager->currentRoomChanged( this->iCurrentRoom );
261 qDebug() << "[Engine::initUserInterface] <Finished>";
264 void Engine::handleViewEvent()
266 if ( iIdleTimeCounter != 0 && iIdleTimeCounter->isActive())
268 // Restart the idle time counter when view event is received
269 iIdleTimeCounter->stop();
270 iIdleTimeCounter->start();
274 void Engine::initConfiguration()
276 iConfiguration = Configuration::instance();
277 if ( iConfiguration == 0 )
279 QTimer::singleShot( 0, this, SLOT( closeApplication() ) );
281 iCurrentRoom = iConfiguration->defaultRoom();
284 void Engine::connectSignals()
286 // Connect engine objects signals to UIManager
287 connect( iClock, SIGNAL( tick( QDateTime ) ), iUIManager, SLOT( updateTime( QDateTime ) ) );
288 connect( iIdleTimeCounter, SIGNAL( timeout() ) , iUIManager, SLOT( roomStatusIndicatorRequested() ) );
290 iUIManager->connectDeviceManager( iDevice );
291 iUIManager->connectCommunicationManager( iCommunication );
294 void Engine::initCommunication()
296 // initialize communication
297 iCommunication = new CommunicationManager( *(iConfiguration->connectionSettings()) );
298 connect( iCommunication, SIGNAL( error( int, CommunicationManager::CommunicationType ) ),
299 this, SLOT( errorHandler( int ) ) );
300 connect( iCommunication, SIGNAL( meetingsFetched( const QList<Meeting*>& ) ),
301 this, SLOT( meetingsFetched( const QList<Meeting*>& ) ) );
304 void Engine::initDevice()
306 // create device manager
307 iDevice = new DeviceManager( iConfiguration->startupSettings() );
308 connect( iDevice, SIGNAL( error( int, const QString& ) ), this, SLOT( errorHandler( int, const QString& ) ) );
309 iDevice->initDeviceManager();
312 void Engine::dialogActivated()
314 if ( iIdleTimeCounter != 0 )
316 iIdleTimeCounter->stop();
320 void Engine::dialogDeactivated()
322 if ( iIdleTimeCounter != 0 )
324 iIdleTimeCounter->start();
328 void Engine::previousViewRestored()
330 if ( iIdleTimeCounter != 0 )
332 iIdleTimeCounter->start();
336 void Engine::stopIdleTimeCounter()
338 if ( iIdleTimeCounter != 0 )
340 iIdleTimeCounter->stop();
344 void Engine::startIdleTimeCounter()
346 if ( iIdleTimeCounter != 0 )
348 iIdleTimeCounter->start();
352 void Engine::currentRoomChanged(Room *aRoom)
354 qDebug() << "[Engine::currentRoomChanged] <invoked>";
355 iCurrentRoom = aRoom;
356 roomStatusInfoNeeded( iCurrentRoom );