Merged and got it partly working
[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 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
162                         status =
163                                         (indexOfCurrentMeeting == -1 ) ? Room::FreeStatus : Room::BusyStatus;
164         // if room is Busy, then check end time, otherwise...
165         QTime until = (status == Room::BusyStatus ) ? iMeetings.at( indexOfCurrentMeeting )->endsAt().time() :
166         // ...if there is meeting following on the same day then check end time, otherwise end is the of the working day
167         ((indexOfNextMeeting != -1 ) ? iMeetings.at( indexOfNextMeeting )->startsAt().time() : Engine::endOfTheDay );
168
169         //currently works only for deafult room
170 //      if( aRoom->equals( *(defaultRoom() ) ) )
171 //              iWindowManager->roomStatusChanged( aRoom, status, until );
172 }
173
174 void Engine::fetchMeetings()
175 {
176         Room *room = defaultRoom();
177         qDebug() << "Engine::fetchMeetings for " << room->name();
178         fetchMeetings(iClock->datetime(), iClock->datetime().addDays( 7), room);
179 }
180
181 bool Engine::isMeetingInList(const QList<Meeting*> &aList, const Meeting *aMeeting)
182 {
183         qDebug()
184                         << "Engine::isMeetingInList( const QList<Meeting*> &, const Meeting * )";
185         for (int i = 0; i < aList.count(); i++)
186         {
187                 if (aMeeting->equals( *(aList.at(i))) )
188                 {
189                         return true;
190                 }
191         }
192         return false;
193 }
194
195 void Engine::meetingsFetched(const QList<Meeting*> &aMeetings)
196 {
197         qDebug() << "Engine::meetingsFetched( const QList<Meeting*> & )";
198         
199         // check if there is any new meeting in the list came from the server -> added
200         for (int i = 0; i < aMeetings.count(); i++)
201         {
202                 // if the (i)th meeting is not in the local meeting list
203                 if ( !isMeetingInList(iMeetings, aMeetings.at(i) ) )
204                 {
205                         // add to the local database =)
206                         Meeting* m = new Meeting( *(aMeetings.at( i )) );
207                         iMeetings.append(m);
208                 }
209         }
210
211         // check if there is any meeting NOT in the list came from the server -> deleted
212         for (int i = 0; i < iMeetings.count(); i++)
213         {
214                 // if the (i)th meeting is in the local but NOT in the server's meeting list
215                 if ( !isMeetingInList(aMeetings, iMeetings.at(i) ) )
216                 {
217                         Meeting* m = iMeetings.takeAt(i);
218                         // delete the meeting from the local list
219                         delete m;
220                 }
221         }
222
223         // refresh room status info
224         roomStatusInfoNeeded(defaultRoom() );
225 }
226
227 void Engine::errorHandler( int aCode, const QString &aAddInfo )
228 {       
229         if ( iWindowManager != 0 )
230         {
231                 iWindowManager->error( ErrorMapper::codeToString( aCode, aAddInfo ) );
232         }
233 }
234
235 void Engine::fetchMeetings( const QDateTime &aFrom, const QDateTime &aUntil, const Room *aIn )
236 {
237         qDebug()
238                         << "Engine::fetchMeetings( const QDateTime &, const QDateTime &, const Room * )";
239         iCommunication->fetchMeetings(aFrom, aUntil, *aIn);
240 }
241
242 void Engine::fetchMeetingDetails(Meeting *aMeeting)
243 {
244         qDebug() << "[Engine::fetchMeetingDetails] <TODO : METHOD NOT IMPLEMENTED>";
245         Meeting tempMeeting(aMeeting->primaryId(), aMeeting->room(), aMeeting->startsAt(), aMeeting->endsAt() );
246         iCommunication->fetchMeetingDetails( tempMeeting );
247 //      Meeting tempMeeting = aMeeting;
248 //      iCommunication->fetchMeetingDetails( tempMeeting );
249 }
250
251 void Engine::cancelFetchMeetingDetails()
252 {
253         iCommunication->cancelFetchMeetingDetails();
254 }
255
256 void Engine::shownWeekChanged( QDate aFrom )
257 {
258         qDebug() << "[Engine::shownWeekChanged] <Invoked>";
259         QDateTime from( aFrom );
260         QDateTime to( aFrom.addDays( 7 ), QTime( 23, 59 ) );
261         qDebug() << "[Engine::shownWeekChanged] <From " << aFrom.toString( "d.m. h:mm" ) << " to " << to.toString( "d.m. h:mm" ) << ">";
262         iCommunication->fetchMeetings( from, to, *defaultRoom() );
263 //      fetchMeetings( from, to, iWindowManager->weeklyView()->currentRoom() );
264 }
265
266 void Engine::changeDeviceMode( bool aChange )
267 {
268         if ( aChange )
269         {
270                 connect( iDevice, SIGNAL( changingModeFailed() ), this, SLOT( changeModeFailed() ) );
271                 iAutoRefresh->stop(); // Stop the meeting update
272         }
273         iDevice->changeMode( aChange );
274 }
275
276 void Engine::changeModeFailed()
277 {
278         qDebug() << "Engine::progressBarCancelled()";
279         iDevice->changeMode( false );
280         iAutoRefresh->start(); //we start the metting updating
281 }
282
283 void Engine::initUserInterface()
284 {
285         qDebug() << "[Engine::initUserInterface] <Invoked>";
286         
287         // Initialize the window manager and connect what ever signals can be connected
288         iWindowManager = new WindowManager;
289         // Create the UIManager which internally handles most of the UI actions
290         iUIManager = new UIManager( this, iWindowManager );
291         
292         connect( iWindowManager, SIGNAL( eventDetected() ), this, SLOT( handleViewEvent() ) );
293         connect( iWindowManager, SIGNAL( previousViewRestored() ), iUIManager, SLOT( previousViewRestored() ) );
294         connect( iWindowManager, SIGNAL( dialogActivated() ), this, SLOT( dialogActivated() ) );
295         connect( iWindowManager, SIGNAL( dialogDeactivated() ), this, SLOT( dialogDeactivated() ) );
296         
297         // Show the UI
298         iWindowManager->setWindowState( Qt::WindowMaximized );
299         iWindowManager->show();
300         iUIManager->showMainView();
301         
302         qDebug() << "[Engine::initUserInterface] <Finished>";
303 }
304
305 void Engine::handleViewEvent()
306 {
307         if ( iIdleTimeCounter != 0 )
308         {
309                 // Restart the idle time counter when view event is received
310                 iIdleTimeCounter->stop();
311                 iIdleTimeCounter->start();
312         }
313 }
314
315 void Engine::initConfiguration()
316 {
317         iConfiguration = Configuration::instance();
318         if ( iConfiguration == 0 )
319         {
320                 QTimer::singleShot( 0, this, SLOT( closeApplication() ) );
321         }
322 }
323
324 void Engine::connectSignals()
325 {
326         // Connect engine objects signals to UIManager
327         connect( iClock, SIGNAL( tick( QDateTime ) ), iUIManager, SLOT( updateTime( QDateTime ) ) );
328         connect( iIdleTimeCounter, SIGNAL( timeout() ) , iUIManager, SLOT( roomStatusIndicatorRequested() ) );
329         
330         iUIManager->connectDeviceManager( iDevice );
331         iUIManager->connectCommunicationManager( iCommunication );
332 }
333
334 void Engine::initCommunication()
335 {
336         // initialize communication
337         iCommunication = new CommunicationManager( *(iConfiguration->connectionSettings()) );
338         connect( iCommunication, SIGNAL( error( int, CommunicationManager::CommunicationType  ) ),
339                         this, SLOT( errorHandler( int ) ) );
340         connect( iCommunication, SIGNAL( meetingsFetched( const QList<Meeting*>& ) ),
341                         this, SLOT( meetingsFetched( const QList<Meeting*>& ) ) );
342 }
343
344 void Engine::initDevice()
345 {
346         // create device manager
347         iDevice = new DeviceManager( iConfiguration->startupSettings() );
348         connect( iDevice, SIGNAL( error( int, const QString& ) ), this, SLOT( errorHandler( int, const QString& ) ) );  
349         iDevice->initDeviceManager();
350 }
351
352 void Engine::dialogActivated()
353 {
354         if ( iIdleTimeCounter != 0 )
355         {
356                 iIdleTimeCounter->stop();
357         }
358 }
359
360 void Engine::dialogDeactivated()
361 {
362         if ( iIdleTimeCounter != 0 )
363         {
364                 iIdleTimeCounter->start();
365         }
366 }
367
368 void Engine::previousViewRestored()
369 {
370         if ( iIdleTimeCounter != 0 )
371         {
372                 iIdleTimeCounter->start();
373         }
374 }
375
376 void Engine::stopIdleTimeCounter()
377 {
378         if ( iIdleTimeCounter != 0 )
379         {
380                 iIdleTimeCounter->stop();
381         }
382 }
383
384 void Engine::startIdleTimeCounter()
385 {
386         if ( iIdleTimeCounter != 0 )
387         {
388                 iIdleTimeCounter->start();
389         }
390 }