54990059ee36b3d13411dd55679d92743df1a4d4
[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 = 5000; // 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( 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, *defaultRoom() );
224 //      fetchMeetings( from, to, iWindowManager->weeklyView()->currentRoom() );
225 }
226
227 void Engine::changeDeviceMode( bool aChange )
228 {
229         if ( aChange )
230         {
231                 connect( iDevice, SIGNAL( changingModeFailed() ), this, SLOT( changeModeFailed() ) );
232                 iAutoRefresh->stop(); // Stop the meeting update
233         }
234         iDevice->changeMode( aChange );
235 }
236
237 void Engine::changeModeFailed()
238 {
239         qDebug() << "Engine::progressBarCancelled()";
240         iDevice->changeMode( false );
241         iAutoRefresh->start(); //we start the metting updating
242 }
243
244 void Engine::initUserInterface()
245 {
246         qDebug() << "[Engine::initUserInterface] <Invoked>";
247         
248         // Initialize the window manager and connect what ever signals can be connected
249         iWindowManager = new WindowManager;
250         // Create the UIManager which internally handles most of the UI actions
251         iUIManager = new UIManager( this, iWindowManager );
252         
253         connect( iWindowManager, SIGNAL( eventDetected() ), this, SLOT( handleViewEvent() ) );
254         connect( iWindowManager, SIGNAL( previousViewRestored() ), iUIManager, SLOT( previousViewRestored() ) );
255 //      connect( iWindowManager, SIGNAL( dialogActivated() ), this, SLOT( dialogActivated() ) );
256         connect( iWindowManager, SIGNAL( dialogDeactivated() ), this, SLOT( dialogDeactivated() ) );
257         
258         // Show the UI
259         iWindowManager->setWindowState( Qt::WindowMaximized );
260         iWindowManager->show();
261         iUIManager->showMainView();
262         
263         // This triggers the meeting fetching
264         iUIManager->currentRoomChanged( this->iCurrentRoom );
265         
266         qDebug() << "[Engine::initUserInterface] <Finished>";
267 }
268
269 void Engine::handleViewEvent()
270 {
271         if ( iIdleTimeCounter != 0 )
272         {
273                 // Restart the idle time counter when view event is received
274                 iIdleTimeCounter->stop();
275                 iIdleTimeCounter->start();
276         }
277 }
278
279 void Engine::initConfiguration()
280 {
281         iConfiguration = Configuration::instance();
282         if ( iConfiguration == 0 )
283         {
284                 QTimer::singleShot( 0, this, SLOT( closeApplication() ) );
285         }
286         iCurrentRoom = iConfiguration->defaultRoom();
287 }
288
289 void Engine::connectSignals()
290 {
291         // Connect engine objects signals to UIManager
292         connect( iClock, SIGNAL( tick( QDateTime ) ), iUIManager, SLOT( updateTime( QDateTime ) ) );
293         connect( iIdleTimeCounter, SIGNAL( timeout() ) , iUIManager, SLOT( roomStatusIndicatorRequested() ) );
294         
295         iUIManager->connectDeviceManager( iDevice );
296         iUIManager->connectCommunicationManager( iCommunication );
297 }
298
299 void Engine::initCommunication()
300 {
301         // initialize communication
302         iCommunication = new CommunicationManager( *(iConfiguration->connectionSettings()) );
303         connect( iCommunication, SIGNAL( error( int, CommunicationManager::CommunicationType  ) ),
304                         this, SLOT( errorHandler( int ) ) );
305         connect( iCommunication, SIGNAL( meetingsFetched( const QList<Meeting*>& ) ),
306                         this, SLOT( meetingsFetched( const QList<Meeting*>& ) ) );
307 }
308
309 void Engine::initDevice()
310 {
311         // create device manager
312         iDevice = new DeviceManager( iConfiguration->startupSettings() );
313         connect( iDevice, SIGNAL( error( int, const QString& ) ), this, SLOT( errorHandler( int, const QString& ) ) );  
314         iDevice->initDeviceManager();
315 }
316
317 void Engine::dialogActivated()
318 {
319         if ( iIdleTimeCounter != 0 )
320         {
321                 iIdleTimeCounter->stop();
322         }
323 }
324
325 void Engine::dialogDeactivated()
326 {
327         if ( iIdleTimeCounter != 0 )
328         {
329                 iIdleTimeCounter->start();
330         }
331 }
332
333 void Engine::previousViewRestored()
334 {
335         if ( iIdleTimeCounter != 0 )
336         {
337                 iIdleTimeCounter->start();
338         }
339 }
340
341 void Engine::stopIdleTimeCounter()
342 {
343         if ( iIdleTimeCounter != 0 )
344         {
345                 iIdleTimeCounter->stop();
346         }
347 }
348
349 void Engine::startIdleTimeCounter()
350 {
351         if ( iIdleTimeCounter != 0 )
352         {
353                 iIdleTimeCounter->start();
354         }
355 }
356
357 void Engine::currentRoomChanged(Room *aRoom)
358 {
359         qDebug() << "[Engine::currentRoomChanged] <invoked>";
360         iCurrentRoom = aRoom;
361 }