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