- Re-factored the code so that Engine is now the main class that owns WindowManager...
[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 "WeeklyViewWidget.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 Engine::Engine() :
22                 iClock( 0 ), iConfiguration( Configuration::instance() ), iCommunication( 0 )
23 {
24         // if reading of configuration fails, signal that initialization failed
25         if ( iConfiguration == 0 )
26         {
27                 QTimer::singleShot( 0, this, SLOT( closeApplication() ) );
28                 return;
29         }
30                 
31         //initialize window manager
32         iWindowManager = new WindowManager( iConfiguration );
33         connect( iWindowManager, SIGNAL( roomStatusInfoNeeded( Room * ) ), this, SLOT( roomStatusInfoNeeded( Room * ) ) );
34         connect( iWindowManager, SIGNAL( observedEventDetected() ), this, SLOT( observedEventDetected() ) );
35         connect( iWindowManager, SIGNAL( meetingActivated( Meeting * ) ), this, SLOT( fetchMeetingDetails( Meeting * ) ) );
36         connect( iWindowManager, SIGNAL( currentRoomChanged( Room * ) ), this, SLOT( currentRoomChanged( Room * ) ) );
37         connect( iWindowManager, SIGNAL( shownWeekChanged( QDate ) ), this, SLOT( shownWeekChanged( QDate ) ) );
38         connect( iWindowManager, SIGNAL( passwordEntered( PasswordDialog::PasswordStatus ) ),
39                         this, SLOT( passwordEntered( PasswordDialog::PasswordStatus ) ) );
40         connect( iWindowManager, SIGNAL( progressBarCancelled() ), this, SLOT( progressBarCancelled() ) );
41         
42         // initialize communication
43         iCommunication = new CommunicationManager( *(iConfiguration->connectionSettings()) );
44         connect( iCommunication, SIGNAL( error( int, CommunicationManager::CommunicationType  ) ),
45                         this, SLOT( errorHandler( int ) ) );
46         connect( iCommunication, SIGNAL( meetingsFetched( const QList<Meeting*>& ) ),
47                         this, SLOT( meetingsFetched( const QList<Meeting*>& ) ) );
48         connect( iCommunication, SIGNAL( meetingDetailsFetched( Meeting& ) ),
49                         this, SLOT( meetingDetailsFetched( Meeting& ) ) );
50
51         //initialize idle time counter
52         iIdleTimeCounter = new QTimer();
53         iIdleTimeCounter->setSingleShot( true );
54         iIdleTimeCounter->setInterval( IDLE_TIME_MULTIPLIER * iConfiguration->displaySettings()->screensaver() );
55         iIdleTimeCounter->start();
56         connect( iIdleTimeCounter, SIGNAL( timeout() ), iWindowManager, SLOT( showRoomStatus() ) );
57
58         // create application clock
59         iClock = new Clock;
60         connect( iClock, SIGNAL( tick( QDateTime ) ), this, SLOT( checkStatusOfAllRooms() ) );
61         connect( iClock, SIGNAL( tick( QDateTime ) ), iWindowManager, SLOT( distributeDateTimeInfo( QDateTime ) ) );
62
63         iAutoRefresh = new QTimer;
64         iAutoRefresh->setInterval( iConfiguration->connectionSettings()->refreshInterval() * 1000 );
65         iAutoRefresh->start();
66         connect( iAutoRefresh, SIGNAL( timeout() ), iAutoRefresh, SLOT( start() ) );
67         connect( iAutoRefresh, SIGNAL( timeout() ), this, SLOT( fetchMeetings() ) );
68
69         // create device manager
70         iDevice = new DeviceManager( iConfiguration->startupSettings() );
71         connect( iDevice, SIGNAL( error( int, const QString& ) ), this, SLOT( errorHandler( int, const QString& ) ) );  
72         connect( iDevice, SIGNAL( changeModeOrdered( DeviceManager::OperationMode ) ), 
73                         this, SLOT( changeModeOrdered( DeviceManager::OperationMode ) ) );
74         iDevice->initDeviceManager();
75         
76         if( iDevice->currentOperationMode() == DeviceManager::KioskMode )
77                 iWindowManager->fullScreen();
78
79         QTimer::singleShot( 0, this, SLOT( fetchMeetings() ) );
80
81         // TODO: continue implementation
82 }
83
84 Engine::~Engine()
85 {
86         while ( !iMeetings.isEmpty() )
87                 delete iMeetings.takeFirst();
88         iIdleTimeCounter->stop();
89         delete iIdleTimeCounter;
90         iIdleTimeCounter = 0;
91         delete iWindowManager;
92         iWindowManager = 0;
93         delete iClock;
94         iClock = 0;
95         delete iDevice;
96         iDevice = 0;
97 }
98
99 void Engine::closeApplication()
100 {
101         qDebug() << "Engine::closeApplication()";
102         // closes application after 1 second
103         QTimer::singleShot( 1000, QApplication::instance(), SLOT( quit() ) );
104 }
105
106 void Engine::observedEventDetected()
107 {
108         iWindowManager->showWeeklyView();
109         // prepare to restart idle counter
110         if ( iIdleTimeCounter->isActive() )
111         {
112                 iIdleTimeCounter->stop();
113         }
114         // (re)start idle counter
115         iIdleTimeCounter->start();
116 }
117
118 Room* Engine::defaultRoom()
119 {
120         return iConfiguration->defaultRoom();
121 }
122
123 void Engine::checkStatusOfAllRooms()
124 {
125         // iterate trough on the rooms
126         for ( int i = 0; i < iConfiguration->rooms().count(); i++ )
127         {
128                 // and check the status
129                 roomStatusInfoNeeded( iConfiguration->rooms().at( i ) );
130         }
131 }
132
133 int Engine::indexOfMeetingAt( Room *aRoom, QDateTime aAt )
134 {
135         for ( int i = 0; i < iMeetings.count(); i++ )
136         {
137                 // exchange server ensures that there is only one meeting in a room at a specified time
138                 if ( aRoom->equals( iMeetings.at( i )->room() )
139                           && iMeetings.at( i )->startsAt() <= aAt
140                           && iMeetings.at( i )->endsAt() >= aAt )
141                 {
142                         return i;
143                 }
144         }
145         return -1;
146 }
147
148 int Engine::indexOfMeetingAfter( Room *aRoom, QDateTime aAfter )
149 {
150         // seeks for the next meeting on the SAME DAY
151         int min = -1;
152         for ( int i = 0; i < iMeetings.count(); i++ )
153         {
154                 // if the meeting is in the same room, on the same day but after the specified time
155                 if ( aRoom->equals( iMeetings.at( i )->room() )
156                           && iMeetings.at( i )->startsAt().date() == aAfter.date()
157                           && iMeetings.at( i )->startsAt() > aAfter )
158                 {
159                         // if there was not any meeting find yet or the previously found is a later one then the (i)th
160                         if ( min == -1
161                                   || iMeetings.at( min )->startsAt() > iMeetings.at( i )->startsAt() )
162                         {
163                                 min = i;
164                         }
165                 }
166         }
167         return min;
168 }
169
170 void Engine::roomStatusInfoNeeded( Room *aRoom )
171 {
172         if ( aRoom == 0 )
173         {
174                 return;
175         }
176
177         int indexOfCurrentMeeting = indexOfMeetingAt( aRoom, iClock->datetime() );
178         int indexOfNextMeeting = indexOfMeetingAfter( aRoom, iClock->datetime() );
179
180         // if there is no meeting, then status is Free; otherwise Busy
181         Room::Status status = ( indexOfCurrentMeeting == -1 ) ? Room::FreeStatus : Room::BusyStatus;
182         // if room is Busy, then check end time, otherwise...
183         QTime until = ( status == Room::BusyStatus ) ? iMeetings.at( indexOfCurrentMeeting )->endsAt().time() :
184                           // ...if there is meeting following on the same day then check end time, otherwise end is the of the working day
185                           (( indexOfNextMeeting != -1 ) ? iMeetings.at( indexOfNextMeeting )->startsAt().time() : Engine::endOfTheDay );
186
187         //currently works only for deafult room
188         if( aRoom->equals( *(defaultRoom() ) ) )
189                 iWindowManager->roomStatusChanged( aRoom, status, until );
190 }
191
192 void Engine::fetchMeetings()
193 {
194         Room *room = defaultRoom();
195         qDebug() << "Engine::fetchMeetings for " << room->name();
196         fetchMeetings( iClock->datetime(), iClock->datetime().addDays( 7 ), room );
197 }
198
199 void Engine::fetchMeetingDetails( Meeting *aMeeting )
200 {
201         iCommunication->fetchMeetingDetails( *aMeeting );
202 }
203
204 bool Engine::isMeetingInList( const QList<Meeting*> &aList, const Meeting *aMeeting )
205 {
206         for ( int i = 0; i < aList.count(); i++ )
207         {
208                 if ( aMeeting->equals( *(aList.at( i )) ) )
209                 {
210                         return true;
211                 }
212         }
213         return false;
214 }
215
216 void Engine::meetingsFetched( const QList<Meeting*> &aMeetings )
217 {
218         // check if there is any new meeting in the list came from the server -> added
219         for ( int i = 0; i < aMeetings.count(); i++ )
220         {
221                 // if the (i)th meeting is not in the local meeting list
222                 if ( !isMeetingInList( iMeetings, aMeetings.at( i ) ) )
223                 {
224                         // add to the local database =)
225                         Meeting* m = new Meeting( *(aMeetings.at( i )) );
226                         iMeetings.append( m );
227                         // and signal the changes
228                         iWindowManager->insertMeeting( m );
229                 }
230         }
231
232         // check if there is any meeting NOT in the list came from the server -> deleted
233         for ( int i = 0; i < iMeetings.count(); i++ )
234         {
235                 // if the (i)th meeting is in the local but NOT in the server's meeting list
236                 if ( !isMeetingInList( aMeetings, iMeetings.at( i ) ) )
237                 {
238                         Meeting* m = iMeetings.takeAt( i );
239                         // signal the changes
240                         iWindowManager->deleteMeeting( m );
241                         // delete the meeting from the local list
242                         delete m;
243                 }
244         }
245
246         // refresh room status info
247         roomStatusInfoNeeded( defaultRoom() );
248 }
249
250 void Engine::meetingDetailsFetched( Meeting &aDetailedMeeting )
251 {
252         iWindowManager->showMeetingInfo( &aDetailedMeeting );
253 }
254
255 void Engine::errorHandler( int aCode, const QString &aAddInfo )
256 {
257         qDebug() << "Engine::ErrorHandler, aCode: " << aCode;
258         // inform UI about the problem
259         if( aCode >= 100 && aCode <= 110 )
260                 qDebug() << "CommunicationManager signaled an error:" << aCode;
261         iWindowManager->error( ErrorMapper::codeToString( aCode, aAddInfo ) );
262 }
263
264 void Engine::currentRoomChanged( Room *aCurrentRoom )
265 {
266         qDebug() << "Engine::currentRoomChanged to " << aCurrentRoom->name();
267         QDateTime from( iWindowManager->weeklyView()->beginnigOfShownWeek() );
268         QDateTime to( from.addDays( 8 ) );
269         fetchMeetings( from, to, aCurrentRoom );
270 }
271
272 void Engine::fetchMeetings( const QDateTime &aFrom, const QDateTime &aUntil, const Room *aIn )
273 {
274         qDebug() << "Engine::fetchMeetings";
275         iCommunication->fetchMeetings( aFrom, aUntil, *aIn );
276 }
277
278 void Engine::shownWeekChanged( QDate aFrom )
279 {
280         QDateTime from( aFrom );
281         QDateTime to( aFrom.addDays( 7 ), QTime( 23, 59 ) );
282         qDebug() << "Engine::shownWeekChanged " << aFrom.toString( "d.m. h:mm" ) << " to " << to.toString( "d.m. h:mm" );
283         fetchMeetings( from, to, iWindowManager->weeklyView()->currentRoom() );
284 }
285
286 void Engine::changeModeOrdered( DeviceManager::OperationMode aMode )
287 {
288         QString message = tr( "You are about to change operation mode to %1." )
289                                 .arg( iDevice->operationModeToString( aMode ) );
290         
291         iWindowManager->showPasswordDialog( iConfiguration->adminPassword(), message );
292 }
293
294 void Engine::passwordEntered( PasswordDialog::PasswordStatus aPasswordStatus )
295 {
296         qDebug() << "Engine::passwordEntered( PasswordDialog::PasswordStatus )";
297         iWindowManager->closePasswordDialog();
298         
299         switch ( aPasswordStatus )
300         {
301                 case PasswordDialog::Correct :
302                 {
303                         iWindowManager->showProgressBar( "Changing current operation mode." );
304                         break;
305                 }
306                 case PasswordDialog::Incorrect :
307                 {
308                         iWindowManager->error( tr( "Incorrect password." ) );
309                         iDevice->handleKeyPresses( true );
310                         break;
311                 }
312                 default : //case PasswordDialog::Canceled
313                 {
314                         iDevice->handleKeyPresses( true );
315                 }
316         }
317 }
318
319 void Engine::progressBarCancelled()
320 {
321         //TODO: cancel the on-going event
322         iWindowManager->closeProgressBar();
323         iDevice->handleKeyPresses( true );
324 }