Merged
[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 {
33         qDebug() << "Engine::Engine()";
34         
35         initConfiguration();
36         initDevice();
37         initCommunication();
38         initUserInterface();
39         
40         //initialize idle time counter
41         iIdleTimeCounter = new QTimer();
42         iIdleTimeCounter->setSingleShot( true );
43         // iIdleTimeCounter->setInterval( IDLE_TIME_MULTIPLIER * iConfiguration->displaySettings()->screensaver() );
44         iIdleTimeCounter->setInterval( 10000 );
45         iIdleTimeCounter->start();
46
47         // create application clock
48         iClock = new Clock;
49         connect( iClock, SIGNAL( tick( QDateTime ) ), this, SLOT( checkStatusOfAllRooms() ) );
50         // connect( iClock, SIGNAL( tick( QDateTime ) ), iWindowManager, SLOT( distributeDateTimeInfo( QDateTime ) ) );
51
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() ) );
58         
59         if( iDevice->currentOperationMode() == DeviceManager::KioskMode )
60         {
61                 iWindowManager->setFullscreen();
62         }
63
64         connectSignals();
65         
66         QTimer::singleShot( 0, this, SLOT( fetchMeetings() ) );
67
68         // TODO: continue implementation
69 }
70
71 Engine::~Engine()
72 {
73         qDebug() << "Engine::~Engine()";
74         while ( !iMeetings.isEmpty() )
75                 delete iMeetings.takeFirst();
76         
77         if ( iIdleTimeCounter != 0 )
78         {
79                 iIdleTimeCounter->stop();
80                 delete iIdleTimeCounter;
81                 iIdleTimeCounter = 0;
82         }
83         QT_DELETE( iClock );
84         QT_DELETE( iDevice );
85
86         QT_DELETE( iUIManager );
87         QT_DELETE( iWindowManager );
88 }
89
90 void Engine::closeApplication()
91 {
92         qDebug() << "Engine::closeApplication()";
93         // closes application after 1 second
94         QTimer::singleShot( 1000, QApplication::instance(), SLOT( quit() ));
95 }
96
97 Room* Engine::defaultRoom()
98 {
99         qDebug() << "Engine::defaultRoom()";
100         return iConfiguration->defaultRoom();
101 }
102
103 void Engine::checkStatusOfAllRooms()
104 {
105 //      qDebug() << "Engine::checkStatusOfAllRooms()";
106         // iterate trough on the rooms
107         for (int i = 0; i < iConfiguration->rooms().count(); i++)
108         {
109                 // and check the status
110                 roomStatusInfoNeeded(iConfiguration->rooms().at(i) );
111         }
112 }
113
114 int Engine::indexOfMeetingAt(Room *aRoom, QDateTime aAt)
115 {
116 //      qDebug() << "Engine::indexOfMeetingAt( Room *, QDateTime )";
117         for ( int i = 0; i < iMeetings.count(); i++ )
118         {
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)
121                 {
122                         return i;
123                 }
124         }
125         return -1;
126 }
127
128 int Engine::indexOfMeetingAfter(Room *aRoom, QDateTime aAfter)
129 {
130 //      qDebug() << "Engine::indexOfMeetingAfter( Room *, QDateTime )";
131         // seeks for the next meeting on the SAME DAY
132         int min = -1;
133         for (int i = 0; i < iMeetings.count(); i++)
134         {
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)
137                 {
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() )
140                         {
141                                 min = i;
142                         }
143                 }
144         }
145         return min;
146 }
147
148 void Engine::roomStatusInfoNeeded(Room *aRoom)
149 {
150 //      qDebug() << "Engine::roomStatusInfoNeeded( Room * )";
151         if ( aRoom == 0 )
152         {
153                 return;
154         }
155
156         int indexOfCurrentMeeting = indexOfMeetingAt(aRoom, iClock->datetime() );
157         int indexOfNextMeeting = indexOfMeetingAfter(aRoom, iClock->datetime() );
158
159         // if there is no meeting, then status is Free; otherwise Busy
160         Room::Status
161                         status =
162                                         (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 );
167
168         //currently works only for deafult room
169 //      if( aRoom->equals( *(defaultRoom() ) ) )
170 //              iWindowManager->roomStatusChanged( aRoom, status, until );
171 }
172
173 void Engine::fetchMeetings()
174 {
175         Room *room = defaultRoom();
176         qDebug() << "Engine::fetchMeetings for " << room->name();
177         fetchMeetings(iClock->datetime(), iClock->datetime().addDays( 7), room);
178 }
179
180 bool Engine::isMeetingInList( const QList<Meeting*> &aList, const Meeting *aMeeting )
181 {
182         qDebug()
183                         << "Engine::isMeetingInList( const QList<Meeting*> &, const Meeting * )";
184         for (int i = 0; i < aList.count(); i++)
185         {
186                 if (aMeeting->equals( *(aList.at(i))) )
187                 {
188                         return true;
189                 }
190         }
191         return false;
192 }
193
194 void Engine::meetingsFetched(const QList<Meeting*> &aMeetings)
195 {
196         qDebug() << "Engine::meetingsFetched( const QList<Meeting*> & )";
197         // check if there is any new meeting in the list came from the server -> added
198         for (int i = 0; i < aMeetings.count(); i++)
199         {
200                 // if the (i)th meeting is not in the local meeting list
201                 if ( !isMeetingInList(iMeetings, aMeetings.at(i) ) )
202                 {
203                         // add to the local database =)
204                         Meeting* m = new Meeting( *(aMeetings.at( i )) );
205                         iMeetings.append(m);
206                         // and signal the changes
207 //                      iWeeklyView->insertMeeting( 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                         // signal the changes
219 //                      iWeeklyView->deleteMeeting( m );
220                         // delete the meeting from the local list
221                         delete m;
222                 }
223         }
224
225         // refresh room status info
226         roomStatusInfoNeeded(defaultRoom() );
227 }
228
229 void Engine::errorHandler( int aCode, const QString &aAddInfo )
230 {       
231         iWindowManager->error( ErrorMapper::codeToString( aCode, aAddInfo ) );
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::fetchMeetingDetails(Meeting *aMeeting)
242 {
243         qDebug() << "[Engine::fetchMeetingDetails] <TODO : METHOD NOT IMPLEMENTED>";
244 //      Meeting tempMeeting = aMeeting;
245 //      iCommunication->fetchMeetingDetails( tempMeeting );
246 }
247
248 void Engine::cancelFetchMeetingDetails()
249 {
250         iCommunication->cancelFetchMeetingDetails();
251 }
252
253 void Engine::shownWeekChanged( QDate aFrom )
254 {
255         qDebug() << "[Engine::shownWeekChanged] <Invoked>";
256         QDateTime from( aFrom );
257         QDateTime to( aFrom.addDays( 7 ), QTime( 23, 59 ) );
258         qDebug() << "[Engine::shownWeekChanged] <From " << aFrom.toString( "d.m. h:mm" ) << " to " << to.toString( "d.m. h:mm" ) << ">";
259         iCommunication->fetchMeetings( from, to, *defaultRoom() );
260 //      fetchMeetings( from, to, iWindowManager->weeklyView()->currentRoom() );
261 }
262
263 void Engine::changeDeviceMode( bool aChange )
264 {
265         if ( aChange )
266         {
267                 connect( iDevice, SIGNAL( changingModeFailed() ), this, SLOT( changeModeFailed() ) );
268                 iAutoRefresh->stop(); // Stop the meeting update
269         }
270         iDevice->changeMode( aChange );
271 }
272
273 void Engine::changeModeFailed()
274 {
275         qDebug() << "Engine::progressBarCancelled()";
276         iDevice->changeMode( false );
277         iAutoRefresh->start(); //we start the metting updating
278 }
279
280 void Engine::initUserInterface()
281 {
282         qDebug() << "[Engine::initUserInterface] <Invoked>";
283         
284         // Initialize the window manager and connect what ever signals can be connected
285         iWindowManager = new WindowManager;
286         // Create the UIManager which internally handles most of the UI actions
287         iUIManager = new UIManager( this, iWindowManager );
288         
289         connect( iWindowManager, SIGNAL( eventDetected() ), this, SLOT( handleViewEvent() ) );
290         connect( iWindowManager, SIGNAL( previousViewRestored() ), iUIManager, SLOT( previousViewRestored() ) );
291         connect( iWindowManager, SIGNAL( dialogActivated() ), this, SLOT( dialogActivated() ) );
292         connect( iWindowManager, SIGNAL( dialogDeactivated() ), this, SLOT( dialogDeactivated() ) );
293         
294         // Show the UI
295         iWindowManager->setWindowState( Qt::WindowMaximized );
296         iWindowManager->show();
297         iUIManager->showMainView();
298         
299         qDebug() << "[Engine::initUserInterface] <Finished>";
300 }
301
302 void Engine::handleViewEvent()
303 {
304         if ( iIdleTimeCounter != 0 )
305         {
306                 // Restart the idle time counter when view event is received
307                 iIdleTimeCounter->stop();
308                 iIdleTimeCounter->start();
309         }
310 }
311
312 void Engine::initConfiguration()
313 {
314         iConfiguration = Configuration::instance();
315         if ( iConfiguration == 0 )
316         {
317                 QTimer::singleShot( 0, this, SLOT( closeApplication() ) );
318         }
319 }
320
321 void Engine::connectSignals()
322 {
323         // Connect engine objects signals to UIManager
324         connect( iClock, SIGNAL( tick( QDateTime ) ), iUIManager, SLOT( updateTime( QDateTime ) ) );
325         connect( iIdleTimeCounter, SIGNAL( timeout() ) , iUIManager, SLOT( roomStatusIndicatorRequested() ) );
326         
327         iUIManager->connectDeviceManager( iDevice );
328         iUIManager->connectCommunicationManager( iCommunication );
329 }
330
331 void Engine::initCommunication()
332 {
333         // initialize communication
334         iCommunication = new CommunicationManager( *(iConfiguration->connectionSettings()) );
335         connect( iCommunication, SIGNAL( error( int, CommunicationManager::CommunicationType  ) ),
336                         this, SLOT( errorHandler( int ) ) );
337         connect( iCommunication, SIGNAL( meetingsFetched( const QList<Meeting*>& ) ),
338                         this, SLOT( meetingsFetched( const QList<Meeting*>& ) ) );
339 }
340
341 void Engine::initDevice()
342 {
343         // create device manager
344         iDevice = new DeviceManager( iConfiguration->startupSettings() );
345         connect( iDevice, SIGNAL( error( int, const QString& ) ), this, SLOT( errorHandler( int, const QString& ) ) );  
346         iDevice->initDeviceManager();
347 }
348
349 void Engine::dialogActivated()
350 {
351         if ( iIdleTimeCounter != 0 )
352         {
353                 iIdleTimeCounter->stop();
354         }
355 }
356
357 void Engine::dialogDeactivated()
358 {
359         if ( iIdleTimeCounter != 0 )
360         {
361                 iIdleTimeCounter->start();
362         }
363 }
364
365 void Engine::previousViewRestored()
366 {
367         if ( iIdleTimeCounter != 0 )
368         {
369                 iIdleTimeCounter->start();
370         }
371 }
372
373 void Engine::stopIdleTimeCounter()
374 {
375         if ( iIdleTimeCounter != 0 )
376         {
377                 iIdleTimeCounter->stop();
378         }
379 }
380
381 void Engine::startIdleTimeCounter()
382 {
383         if ( iIdleTimeCounter != 0 )
384         {
385                 iIdleTimeCounter->start();
386         }
387 }