195b0aaab2c4cbf8fc01cf47336aee4f764ef513
[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         iCommunicationFailed = false;
36         
37         initConfiguration();
38         initDevice();
39         initCommunication();
40         initUserInterface();
41         
42         //initialize idle time counter
43         iIdleTimeCounter = new QTimer();
44         iIdleTimeCounter->setSingleShot( true);
45         iIdleTimeCounter->setInterval(IDLE_TIME_MULTIPLIER * iConfiguration->displaySettings()->screensaver() );
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 Room* Engine::defaultRoom()
99 {
100         qDebug() << "Engine::defaultRoom()";
101         return iConfiguration->defaultRoom();
102 }
103
104 void Engine::checkStatusOfAllRooms()
105 {
106 //      qDebug() << "Engine::checkStatusOfAllRooms()";
107         // iterate trough on the rooms
108         for (int i = 0; i < iConfiguration->rooms().count(); i++)
109         {
110                 // and check the status
111                 roomStatusInfoNeeded(iConfiguration->rooms().at(i) );
112         }
113 }
114
115 int Engine::indexOfMeetingAt(Room *aRoom, QDateTime aAt)
116 {
117 //      qDebug() << "Engine::indexOfMeetingAt( Room *, QDateTime )";
118         for ( int i = 0; i < iMeetings.count(); i++ )
119         {
120                 // exchange server ensures that there is only one meeting in a room at a specified time
121                 if (aRoom->equals(iMeetings.at( i )->room() ) && iMeetings.at( i )->startsAt() <= aAt && iMeetings.at( i )->endsAt() >= aAt)
122                 {
123                         return i;
124                 }
125         }
126         return -1;
127 }
128
129 int Engine::indexOfMeetingAfter(Room *aRoom, QDateTime aAfter)
130 {
131 //      qDebug() << "Engine::indexOfMeetingAfter( Room *, QDateTime )";
132         // seeks for the next meeting on the SAME DAY
133         int min = -1;
134         for (int i = 0; i < iMeetings.count(); i++)
135         {
136                 // if the meeting is in the same room, on the same day but after the specified time
137                 if (aRoom->equals(iMeetings.at( i )->room() ) && iMeetings.at( i )->startsAt().date() == aAfter.date() && iMeetings.at( i )->startsAt() > aAfter)
138                 {
139                         // if there was not any meeting find yet or the previously found is a later one then the (i)th
140                         if (min == -1 || iMeetings.at( min )->startsAt() > iMeetings.at( i )->startsAt() )
141                         {
142                                 min = i;
143                         }
144                 }
145         }
146         return min;
147 }
148
149 void Engine::roomStatusInfoNeeded(Room *aRoom)
150 {
151 //      qDebug() << "Engine::roomStatusInfoNeeded( Room * )";
152         if ( aRoom == 0 )
153         {
154                 return;
155         }
156
157         int indexOfCurrentMeeting = indexOfMeetingAt(aRoom, iClock->datetime() );
158         int indexOfNextMeeting = indexOfMeetingAfter(aRoom, iClock->datetime() );
159
160         // if there is no meeting, then status is Free; otherwise Busy
161         Room::Status status = (indexOfCurrentMeeting == -1 ) ? Room::FreeStatus : Room::BusyStatus;
162         // if room is Busy, then check end time, otherwise...
163         QTime until = (status == Room::BusyStatus ) ? iMeetings.at( indexOfCurrentMeeting )->endsAt().time() :
164         // ...if there is meeting following on the same day then check end time, otherwise end is the of the working day
165         ( ( indexOfNextMeeting != -1 ) ? iMeetings.at( indexOfNextMeeting )->startsAt().time() : Engine::endOfTheDay );
166
167         //currently works only for deafult room
168 //      if( aRoom->equals( *(defaultRoom() ) ) )
169 //              iWindowManager->roomStatusChanged( aRoom, status, until );
170 }
171
172 /*
173 void Engine::fetchMeetings()
174 {
175         qDebug() << "Engine::fetchMeetings for " << iCurrentRoom;
176         QDateTime from( iWindowManager->weeklyView()->beginnigOfShownWeek() );
177         QDateTime to( from.addDays( 7 ) );
178         // fetchMeetings( from, to, iWindowManager->weeklyView()->currentRoom() );
179         // Signal is connected to the currentRoomChanged slot which keeps the iCurrentRoom up to date
180         fetchMeetings( from, to, iCurrentRoom );
181 }
182 */
183
184 void Engine::fetchMeetingDetails( Meeting *aMeeting )
185 {
186         qDebug() << "Engine::fetchMeetingDetails( Meeting* )";
187 /*      iWindowManager->showProgressBar(tr("Please Wait"), true);
188         iWindowManager->updateProgressBar(tr("Fetching Meeting Details...") );
189         connect(iWindowManager, 
190         SIGNAL( progressBarCancelled() ), this, 
191         SLOT( fetchMeetingDetailsCancelled() ));
192         iCommunication->fetchMeetingDetails( *aMeeting); */
193         iCommunication->fetchMeetingDetails( *aMeeting );
194 }
195
196 void Engine::meetingsFetched( const QList<Meeting*> &aMeetings )
197 {
198         qDebug() << "Engine::meetingsFetched( const QList<Meeting*> & )";
199         if( iCommunicationFailed )
200         {
201                 iCommunicationFailed = false;
202                 iWindowManager->connectionEstablished();
203         }
204
205         for ( int i = 0; i < iMeetings.count(); ++i ) 
206         {
207                 Meeting* m = iMeetings.takeAt( i );
208                 delete m;
209         }
210         iMeetings.clear();
211         for ( int i = 0; i < aMeetings.count(); ++i ) {
212                 Meeting* m = new Meeting( *( aMeetings.at( i ) ) );
213                 iMeetings.append( m );
214         }
215
216         // refresh room status info
217         roomStatusInfoNeeded( defaultRoom() );
218 }
219
220 void Engine::errorHandler( int aCode, const QString &aAddInfo )
221 {       
222         if( aCode >= 100 && aCode < 150 )
223         {
224                 iCommunicationFailed = true;
225                 //TODO: Call window manager
226                 //if ( iWindowManager != 0 ) iWindowManager->connectionLost();
227         }
228         if ( iWindowManager != 0 )
229         {
230                 iWindowManager->error( ErrorMapper::codeToString( aCode, aAddInfo ) );
231         }
232 }
233
234 void Engine::fetchMeetings( const QDateTime &aFrom, const QDateTime &aUntil, const Room *aIn )
235 {
236         qDebug()
237                         << "Engine::fetchMeetings( const QDateTime &, const QDateTime &, const Room * )";
238         iCommunication->fetchMeetings(aFrom, aUntil, *aIn);
239 }
240
241 void Engine::cancelFetchMeetingDetails()
242 {
243         iCommunication->cancelFetchMeetingDetails();
244 }
245
246 void Engine::shownWeekChanged( QDate aFrom )
247 {
248         qDebug() << "[Engine::shownWeekChanged] <Invoked>";
249         QDateTime from( aFrom );
250         QDateTime to( aFrom.addDays( 7 ), QTime( 23, 59 ) );
251         qDebug() << "[Engine::shownWeekChanged] <From " << aFrom.toString( "d.m. h:mm" ) << " to " << to.toString( "d.m. h:mm" ) << ">";
252         iCommunication->fetchMeetings( from, to, *defaultRoom() );
253 //      fetchMeetings( from, to, iWindowManager->weeklyView()->currentRoom() );
254 }
255
256 void Engine::changeDeviceMode( bool aChange )
257 {
258         if ( aChange )
259         }
260         {
261                 connect( iDevice, SIGNAL( changingModeFailed() ), this, SLOT( changeModeFailed() ) );
262                 iAutoRefresh->stop(); // Stop the meeting update
263         }
264         iDevice->changeMode( aChange );
265 }
266
267 void Engine::changeModeFailed()
268 {
269         qDebug() << "Engine::progressBarCancelled()";
270         iDevice->changeMode( false );
271         iAutoRefresh->start(); //we start the metting updating
272 }
273
274 void Engine::initUserInterface()
275 {
276         qDebug() << "[Engine::initUserInterface] <Invoked>";
277         
278         // Initialize the window manager and connect what ever signals can be connected
279         iWindowManager = new WindowManager;
280         // Create the UIManager which internally handles most of the UI actions
281         iUIManager = new UIManager( this, iWindowManager );
282         
283         connect( iWindowManager, SIGNAL( eventDetected() ), this, SLOT( handleViewEvent() ) );
284         connect( iWindowManager, SIGNAL( previousViewRestored() ), iUIManager, SLOT( previousViewRestored() ) );
285         connect( iWindowManager, SIGNAL( dialogActivated() ), this, SLOT( dialogActivated() ) );
286         connect( iWindowManager, SIGNAL( dialogDeactivated() ), this, SLOT( dialogDeactivated() ) );
287         
288         // Show the UI
289         iWindowManager->setWindowState( Qt::WindowMaximized );
290         iWindowManager->show();
291         iUIManager->showMainView();
292         
293         qDebug() << "[Engine::initUserInterface] <Finished>";
294 }
295
296 void Engine::handleViewEvent()
297 {
298         if ( iIdleTimeCounter != 0 )
299         {
300                 // Restart the idle time counter when view event is received
301                 iIdleTimeCounter->stop();
302                 iIdleTimeCounter->start();
303         }
304 }
305
306 void Engine::initConfiguration()
307 {
308         iConfiguration = Configuration::instance();
309         if ( iConfiguration == 0 )
310         {
311                 QTimer::singleShot( 0, this, SLOT( closeApplication() ) );
312         }
313         iCurrentRoom = iConfiguration->defaultRoom();
314 }
315
316 void Engine::connectSignals()
317 {
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() ) );
321         
322         iUIManager->connectDeviceManager( iDevice );
323         iUIManager->connectCommunicationManager( iCommunication );
324 }
325
326 void Engine::initCommunication()
327 {
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*>& ) ) );
334 }
335
336 void Engine::initDevice()
337 {
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();
342 }
343
344 void Engine::dialogActivated()
345 {
346         if ( iIdleTimeCounter != 0 )
347         {
348                 iIdleTimeCounter->stop();
349         }
350 }
351
352 void Engine::dialogDeactivated()
353 {
354         if ( iIdleTimeCounter != 0 )
355         {
356                 iIdleTimeCounter->start();
357         }
358 }
359
360 void Engine::previousViewRestored()
361 {
362         if ( iIdleTimeCounter != 0 )
363         {
364                 iIdleTimeCounter->start();
365         }
366 }
367
368 void Engine::stopIdleTimeCounter()
369 {
370         if ( iIdleTimeCounter != 0 )
371         {
372                 iIdleTimeCounter->stop();
373         }
374 }
375
376 void Engine::startIdleTimeCounter()
377 {
378         if ( iIdleTimeCounter != 0 )
379         {
380                 iIdleTimeCounter->start();
381         }
382 }
383
384 void Engine::currentRoomChanged(Room *aRoom)
385 {
386         iCurrentRoom = aRoom;
387 }