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