Merged the engine
[qtmeetings] / src / BusinessLogic / Engine.cpp
1 #include "Engine.h"
2 #include "Room.h"
3 #include "Meeting.h"
4 #include "ConnectionSettings.h"
5 #include "Configuration.h"
6 #include "DisplaySettings.h"
7 #include "CommunicationManager.h"
8 // #include "DeviceManager.h"
9 #include "Clock.h"
10 #include "ErrorMapper.h"
11 #include "UIManager.h"
12
13 #include <QApplication>
14 #include <QTimer>
15 #include <QList>
16 #include <QtDebug>
17
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
20
21 // Macro to help deleting objects. This could be global.
22 #define QT_DELETE(X) \
23         if ( X != 0 ) \
24         { \
25                 delete X; \
26                 X = 0; \
27         }
28
29
30 Engine::Engine() :
31                 iClock( 0 ), iConfiguration( 0 ), iCommunication( 0 ),
32                 iWindowManager( 0 ), iUIManager( 0 )
33 {
34         qDebug() << "Engine::Engine()";
35         
36         initConfiguration();
37         initDevice();
38         initCommunication();
39         initUserInterface();
40         
41         //initialize idle time counter
42         iIdleTimeCounter = new QTimer();
43         iIdleTimeCounter->setSingleShot( true );
44         // iIdleTimeCounter->setInterval( IDLE_TIME_MULTIPLIER * iConfiguration->displaySettings()->screensaver() );
45         iIdleTimeCounter->setInterval( 10000 );
46         iIdleTimeCounter->start();
47
48         // create application clock
49         iClock = new Clock;
50         connect( iClock, SIGNAL( tick( QDateTime ) ), this, SLOT( checkStatusOfAllRooms() ) );
51         // connect( iClock, SIGNAL( tick( QDateTime ) ), iWindowManager, SLOT( distributeDateTimeInfo( QDateTime ) ) );
52
53         // Create auto refresh timer
54         iAutoRefresh = new QTimer;
55         iAutoRefresh->setInterval(iConfiguration->connectionSettings()->refreshInterval() * 1000);
56         iAutoRefresh->start();
57         connect( iAutoRefresh, SIGNAL( timeout() ), iAutoRefresh, SLOT( start() ) );
58         connect( iAutoRefresh, SIGNAL( timeout() ), this, SLOT( fetchMeetings() ) );
59         
60         if( iDevice->currentOperationMode() == DeviceManager::KioskMode )
61         {
62                 iWindowManager->setFullscreen();
63         }
64
65         connectSignals();
66         
67         QTimer::singleShot( 0, this, SLOT( fetchMeetings() ) );
68
69         // TODO: continue implementation
70 }
71
72 Engine::~Engine()
73 {
74         qDebug() << "Engine::~Engine()";
75         while ( !iMeetings.isEmpty() )
76                 delete iMeetings.takeFirst();
77         
78         if ( iIdleTimeCounter != 0 )
79         {
80                 iIdleTimeCounter->stop();
81                 delete iIdleTimeCounter;
82                 iIdleTimeCounter = 0;
83         }
84         QT_DELETE( iClock );
85         QT_DELETE( iDevice );
86
87         QT_DELETE( iUIManager );
88         QT_DELETE( iWindowManager );
89 }
90
91 void Engine::closeApplication()
92 {
93         qDebug() << "Engine::closeApplication()";
94         // closes application after 1 second
95         QTimer::singleShot( 1000, QApplication::instance(), SLOT( quit() ));
96 }
97
98 <<<<<<< HEAD:src/BusinessLogic/Engine.cpp
99 =======
100 void Engine::observedEventDetected()
101 {
102         qDebug() << "Engine::observedEventDetected()";
103         if ( !iIdleTimeCounter->isActive() )
104         {
105                 iWindowManager->weeklyView()->showCurrentWeek();
106         }
107         iWindowManager->showWeeklyView();
108         // prepare to restart idle counter
109         if (iIdleTimeCounter->isActive() )
110         {
111                 iIdleTimeCounter->stop();
112         }
113         // (re)start idle counter
114         iIdleTimeCounter->start();
115 }
116
117 >>>>>>> 60b7802da53961d1998e276858bbc5e404b98a05:src/BusinessLogic/Engine.cpp
118 Room* Engine::defaultRoom()
119 {
120         qDebug() << "Engine::defaultRoom()";
121         return iConfiguration->defaultRoom();
122 }
123
124 void Engine::checkStatusOfAllRooms()
125 {
126 //      qDebug() << "Engine::checkStatusOfAllRooms()";
127         // iterate trough on the rooms
128         for (int i = 0; i < iConfiguration->rooms().count(); i++)
129         {
130                 // and check the status
131                 roomStatusInfoNeeded(iConfiguration->rooms().at(i) );
132         }
133 }
134
135 int Engine::indexOfMeetingAt(Room *aRoom, QDateTime aAt)
136 {
137 //      qDebug() << "Engine::indexOfMeetingAt( Room *, QDateTime )";
138         for ( int i = 0; i < iMeetings.count(); i++ )
139         {
140                 // exchange server ensures that there is only one meeting in a room at a specified time
141                 if (aRoom->equals(iMeetings.at( i )->room() ) && iMeetings.at( i )->startsAt() <= aAt && iMeetings.at( i )->endsAt() >= aAt)
142                 {
143                         return i;
144                 }
145         }
146         return -1;
147 }
148
149 int Engine::indexOfMeetingAfter(Room *aRoom, QDateTime aAfter)
150 {
151 //      qDebug() << "Engine::indexOfMeetingAfter( Room *, QDateTime )";
152         // seeks for the next meeting on the SAME DAY
153         int min = -1;
154         for (int i = 0; i < iMeetings.count(); i++)
155         {
156                 // if the meeting is in the same room, on the same day but after the specified time
157                 if (aRoom->equals(iMeetings.at( i )->room() ) && iMeetings.at( i )->startsAt().date() == aAfter.date() && iMeetings.at( i )->startsAt() > aAfter)
158                 {
159                         // if there was not any meeting find yet or the previously found is a later one then the (i)th
160                         if (min == -1 || iMeetings.at( min )->startsAt() > iMeetings.at( i )->startsAt() )
161                         {
162                                 min = i;
163                         }
164                 }
165         }
166         return min;
167 }
168
169 void Engine::roomStatusInfoNeeded(Room *aRoom)
170 {
171 //      qDebug() << "Engine::roomStatusInfoNeeded( Room * )";
172         if ( aRoom == 0 )
173         {
174                 return;
175         }
176
177         int indexOfCurrentMeeting = indexOfMeetingAt(aRoom, iClock->datetime() );
178         int indexOfNextMeeting = indexOfMeetingAfter(aRoom, iClock->datetime() );
179
180         // if there is no meeting, then status is Free; otherwise Busy
181         Room::Status
182                         status =
183                                         (indexOfCurrentMeeting == -1 ) ? Room::FreeStatus : Room::BusyStatus;
184         // if room is Busy, then check end time, otherwise...
185         QTime until = (status == Room::BusyStatus ) ? iMeetings.at( indexOfCurrentMeeting )->endsAt().time() :
186         // ...if there is meeting following on the same day then check end time, otherwise end is the of the working day
187         ((indexOfNextMeeting != -1 ) ? iMeetings.at( indexOfNextMeeting )->startsAt().time() : Engine::endOfTheDay );
188
189         //currently works only for deafult room
190 //      if( aRoom->equals( *(defaultRoom() ) ) )
191 //              iWindowManager->roomStatusChanged( aRoom, status, until );
192 }
193
194 void Engine::fetchMeetings()
195 {
196         Room *room = defaultRoom();
197         qDebug() << "Engine::fetchMeetings for " << room->name();
198         fetchMeetings(iClock->datetime(), iClock->datetime().addDays( 7), room);
199 }
200
201 void Engine::fetchMeetingDetails(Meeting *aMeeting)
202 {
203         qDebug() << "Engine::fetchMeetingDetails( Meeting* )";
204 /*      iWindowManager->showProgressBar(tr("Please Wait"), true);
205         iWindowManager->updateProgressBar(tr("Fetching Meeting Details...") );
206         connect(iWindowManager, 
207         SIGNAL( progressBarCancelled() ), this, 
208         SLOT( fetchMeetingDetailsCancelled() ));
209         iCommunication->fetchMeetingDetails( *aMeeting); */
210         
211         iCommunication->fetchMeetingDetails( *aMeeting );
212 }
213
214 void Engine::meetingsFetched( const QList<Meeting*> &aMeetings )
215 {
216         qDebug() << "Engine::meetingsFetched( const QList<Meeting*> & )";
217         
218         for ( int i = 0; i < iMeetings.count(); ++i ) {
219                 Meeting* m = iMeetings.takeAt( i );
220                 delete m;
221         }
222         iMeetings.clear();
223         for ( int i = 0; i < aMeetings.count(); ++i ) {
224                 Meeting* m = new Meeting( *(aMeetings.at( i )) );
225                 iMeetings.append( m );
226         }
227
228         // refresh room status info
229         roomStatusInfoNeeded(defaultRoom() );
230 }
231
232 void Engine::errorHandler( int aCode, const QString &aAddInfo )
233 {       
234         if ( iWindowManager != 0 )
235         {
236                 iWindowManager->error( ErrorMapper::codeToString( aCode, aAddInfo ) );
237         }
238 }
239
240 void Engine::fetchMeetings( const QDateTime &aFrom, const QDateTime &aUntil, const Room *aIn )
241 {
242         qDebug()
243                         << "Engine::fetchMeetings( const QDateTime &, const QDateTime &, const Room * )";
244         iCommunication->fetchMeetings(aFrom, aUntil, *aIn);
245 }
246
247 void Engine::cancelFetchMeetingDetails()
248 {
249         iCommunication->cancelFetchMeetingDetails();
250 }
251
252 void Engine::shownWeekChanged( QDate aFrom )
253 {
254         qDebug() << "[Engine::shownWeekChanged] <Invoked>";
255         QDateTime from( aFrom );
256         QDateTime to( aFrom.addDays( 7 ), QTime( 23, 59 ) );
257         qDebug() << "[Engine::shownWeekChanged] <From " << aFrom.toString( "d.m. h:mm" ) << " to " << to.toString( "d.m. h:mm" ) << ">";
258         iCommunication->fetchMeetings( from, to, *defaultRoom() );
259 //      fetchMeetings( from, to, iWindowManager->weeklyView()->currentRoom() );
260 }
261
262 void Engine::changeDeviceMode( bool aChange )
263 {
264         if ( aChange )
265         {
266                 connect( iDevice, SIGNAL( changingModeFailed() ), this, SLOT( changeModeFailed() ) );
267                 iAutoRefresh->stop(); // Stop the meeting update
268         }
269         iDevice->changeMode( aChange );
270 }
271
272 void Engine::changeModeFailed()
273 {
274         qDebug() << "Engine::progressBarCancelled()";
275         iDevice->changeMode( false );
276         iAutoRefresh->start(); //we start the metting updating
277 }
278
279 void Engine::initUserInterface()
280 {
281         qDebug() << "[Engine::initUserInterface] <Invoked>";
282         
283         // Initialize the window manager and connect what ever signals can be connected
284         iWindowManager = new WindowManager;
285         // Create the UIManager which internally handles most of the UI actions
286         iUIManager = new UIManager( this, iWindowManager );
287         
288         connect( iWindowManager, SIGNAL( eventDetected() ), this, SLOT( handleViewEvent() ) );
289         connect( iWindowManager, SIGNAL( previousViewRestored() ), iUIManager, SLOT( previousViewRestored() ) );
290         connect( iWindowManager, SIGNAL( dialogActivated() ), this, SLOT( dialogActivated() ) );
291         connect( iWindowManager, SIGNAL( dialogDeactivated() ), this, SLOT( dialogDeactivated() ) );
292         
293         // Show the UI
294         iWindowManager->setWindowState( Qt::WindowMaximized );
295         iWindowManager->show();
296         iUIManager->showMainView();
297         
298         qDebug() << "[Engine::initUserInterface] <Finished>";
299 }
300
301 void Engine::handleViewEvent()
302 {
303         if ( iIdleTimeCounter != 0 )
304         {
305                 // Restart the idle time counter when view event is received
306                 iIdleTimeCounter->stop();
307                 iIdleTimeCounter->start();
308         }
309 }
310
311 void Engine::initConfiguration()
312 {
313         iConfiguration = Configuration::instance();
314         if ( iConfiguration == 0 )
315         {
316                 QTimer::singleShot( 0, this, SLOT( closeApplication() ) );
317         }
318 }
319
320 void Engine::connectSignals()
321 {
322         // Connect engine objects signals to UIManager
323         connect( iClock, SIGNAL( tick( QDateTime ) ), iUIManager, SLOT( updateTime( QDateTime ) ) );
324         connect( iIdleTimeCounter, SIGNAL( timeout() ) , iUIManager, SLOT( roomStatusIndicatorRequested() ) );
325         
326         iUIManager->connectDeviceManager( iDevice );
327         iUIManager->connectCommunicationManager( iCommunication );
328 }
329
330 void Engine::initCommunication()
331 {
332         // initialize communication
333         iCommunication = new CommunicationManager( *(iConfiguration->connectionSettings()) );
334         connect( iCommunication, SIGNAL( error( int, CommunicationManager::CommunicationType  ) ),
335                         this, SLOT( errorHandler( int ) ) );
336         connect( iCommunication, SIGNAL( meetingsFetched( const QList<Meeting*>& ) ),
337                         this, SLOT( meetingsFetched( const QList<Meeting*>& ) ) );
338 }
339
340 void Engine::initDevice()
341 {
342         // create device manager
343         iDevice = new DeviceManager( iConfiguration->startupSettings() );
344         connect( iDevice, SIGNAL( error( int, const QString& ) ), this, SLOT( errorHandler( int, const QString& ) ) );  
345         iDevice->initDeviceManager();
346 }
347
348 void Engine::dialogActivated()
349 {
350         if ( iIdleTimeCounter != 0 )
351         {
352                 iIdleTimeCounter->stop();
353         }
354 }
355
356 void Engine::dialogDeactivated()
357 {
358         if ( iIdleTimeCounter != 0 )
359         {
360                 iIdleTimeCounter->start();
361         }
362 }
363
364 void Engine::previousViewRestored()
365 {
366         if ( iIdleTimeCounter != 0 )
367         {
368                 iIdleTimeCounter->start();
369         }
370 }
371
372 void Engine::stopIdleTimeCounter()
373 {
374         if ( iIdleTimeCounter != 0 )
375         {
376                 iIdleTimeCounter->stop();
377         }
378 }
379
380 void Engine::startIdleTimeCounter()
381 {
382         if ( iIdleTimeCounter != 0 )
383         {
384                 iIdleTimeCounter->start();
385         }
386 }
387