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