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( *(defaultRoom() ) ) )
170 // iWindowManager->roomStatusChanged( aRoom, status, until );
174 void Engine::fetchMeetings()
176 qDebug() << "Engine::fetchMeetings for " << iCurrentRoom;
177 QDateTime from( iWindowManager->weeklyView()->beginnigOfShownWeek() );
178 QDateTime to( from.addDays( 7 ) );
179 // fetchMeetings( from, to, iWindowManager->weeklyView()->currentRoom() );
180 // Signal is connected to the currentRoomChanged slot which keeps the iCurrentRoom up to date
181 fetchMeetings( from, to, iCurrentRoom );
185 void Engine::fetchMeetingDetails( Meeting *aMeeting )
187 qDebug() << "Engine::fetchMeetingDetails( Meeting* )";
188 /* iWindowManager->showProgressBar(tr("Please Wait"), true);
189 iWindowManager->updateProgressBar(tr("Fetching Meeting Details...") );
190 connect(iWindowManager,
191 SIGNAL( progressBarCancelled() ), this,
192 SLOT( fetchMeetingDetailsCancelled() ));
193 iCommunication->fetchMeetingDetails( *aMeeting); */
194 iCommunication->fetchMeetingDetails( *aMeeting );
197 void Engine::meetingsFetched( const QList<Meeting*> &aMeetings )
199 qDebug() << "Engine::meetingsFetched( const QList<Meeting*> & )";
200 // TODO: should check if this week's meetings were fetched
201 if( iCommunicationFailed || !iCurrentWeekFetched )
203 iCurrentWeekFetched = true;
204 iCommunicationFailed = false;
205 iUIManager->connectionEstablished();
208 for ( int i = 0; i < iMeetings.count(); ++i )
210 Meeting* m = iMeetings.takeAt( i );
214 for ( int i = 0; i < aMeetings.count(); ++i ) {
215 Meeting* m = new Meeting( *( aMeetings.at( i ) ) );
216 iMeetings.append( m );
219 // refresh room status info
220 roomStatusInfoNeeded( defaultRoom() );
223 void Engine::errorHandler( int aCode, const QString &aAddInfo )
225 if( aCode >= 100 && aCode < 150 )
227 iCommunicationFailed = true;
228 if ( iUIManager != 0 ) iUIManager->connectionLost();
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::cancelFetchMeetingDetails()
245 iCommunication->cancelFetchMeetingDetails();
248 void Engine::shownWeekChanged( QDate aFrom )
250 qDebug() << "[Engine::shownWeekChanged] <Invoked>";
251 QDateTime from( aFrom );
252 QDateTime to( aFrom.addDays( 7 ), QTime( 23, 59 ) );
253 qDebug() << "[Engine::shownWeekChanged] <From " << aFrom.toString( "d.m. h:mm" ) << " to " << to.toString( "d.m. h:mm" ) << ">";
254 iCommunication->fetchMeetings( from, to, *defaultRoom() );
255 // fetchMeetings( from, to, iWindowManager->weeklyView()->currentRoom() );
258 void Engine::changeDeviceMode()
260 connect( iDevice, SIGNAL( changeModeFailed() ), this, SLOT( changeModeFailed() ) );
261 iAutoRefresh->stop(); // Stop the meeting update
262 iDevice->changeMode();
265 void Engine::changeModeFailed()
267 qDebug() << "Engine::progressBarCancelled()";
268 iAutoRefresh->start(); //we start the metting updating
271 void Engine::initUserInterface()
273 qDebug() << "[Engine::initUserInterface] <Invoked>";
275 // Initialize the window manager and connect what ever signals can be connected
276 iWindowManager = new WindowManager;
277 // Create the UIManager which internally handles most of the UI actions
278 iUIManager = new UIManager( this, iWindowManager );
280 connect( iWindowManager, SIGNAL( eventDetected() ), this, SLOT( handleViewEvent() ) );
281 connect( iWindowManager, SIGNAL( previousViewRestored() ), iUIManager, SLOT( previousViewRestored() ) );
282 connect( iWindowManager, SIGNAL( dialogActivated() ), this, SLOT( dialogActivated() ) );
283 connect( iWindowManager, SIGNAL( dialogDeactivated() ), this, SLOT( dialogDeactivated() ) );
286 iWindowManager->setWindowState( Qt::WindowMaximized );
287 iWindowManager->show();
288 iUIManager->showMainView();
290 // This triggers the meeting fetching
291 iUIManager->currentRoomChanged( this->iCurrentRoom );
293 qDebug() << "[Engine::initUserInterface] <Finished>";
296 void Engine::handleViewEvent()
298 if ( iIdleTimeCounter != 0 && iIdleTimeCounter->isActive())
300 // Restart the idle time counter when view event is received
301 iIdleTimeCounter->stop();
302 iIdleTimeCounter->start();
306 void Engine::initConfiguration()
308 iConfiguration = Configuration::instance();
309 if ( iConfiguration == 0 )
311 QTimer::singleShot( 0, this, SLOT( closeApplication() ) );
313 iCurrentRoom = iConfiguration->defaultRoom();
316 void Engine::connectSignals()
318 // Connect engine objects signals to UIManager
319 connect( iClock, SIGNAL( tick( QDateTime ) ), iUIManager, SLOT( updateTime( QDateTime ) ) );
320 connect( iIdleTimeCounter, SIGNAL( timeout() ) , iUIManager, SLOT( roomStatusIndicatorRequested() ) );
322 iUIManager->connectDeviceManager( iDevice );
323 iUIManager->connectCommunicationManager( iCommunication );
326 void Engine::initCommunication()
328 // initialize communication
329 iCommunication = new CommunicationManager( *(iConfiguration->connectionSettings()) );
330 connect( iCommunication, SIGNAL( error( int, CommunicationManager::CommunicationType ) ),
331 this, SLOT( errorHandler( int ) ) );
332 connect( iCommunication, SIGNAL( meetingsFetched( const QList<Meeting*>& ) ),
333 this, SLOT( meetingsFetched( const QList<Meeting*>& ) ) );
336 void Engine::initDevice()
338 // create device manager
339 iDevice = new DeviceManager( iConfiguration->startupSettings() );
340 connect( iDevice, SIGNAL( error( int, const QString& ) ), this, SLOT( errorHandler( int, const QString& ) ) );
341 iDevice->initDeviceManager();
344 void Engine::dialogActivated()
346 if ( iIdleTimeCounter != 0 )
348 iIdleTimeCounter->stop();
352 void Engine::dialogDeactivated()
354 if ( iIdleTimeCounter != 0 )
356 iIdleTimeCounter->start();
360 void Engine::previousViewRestored()
362 if ( iIdleTimeCounter != 0 )
364 iIdleTimeCounter->start();
368 void Engine::stopIdleTimeCounter()
370 if ( iIdleTimeCounter != 0 )
372 iIdleTimeCounter->stop();
376 void Engine::startIdleTimeCounter()
378 if ( iIdleTimeCounter != 0 )
380 iIdleTimeCounter->start();
384 void Engine::currentRoomChanged(Room *aRoom)
386 qDebug() << "[Engine::currentRoomChanged] <invoked>";
387 iCurrentRoom = aRoom;