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