When moving from Room Status View to Weekly View, the current week of default room...
[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 ) ), this, SLOT( passwordEntered( PasswordDialog::PasswordStatus ) ));
40
41         // initialize communication
42         iCommunication = new CommunicationManager( *(iConfiguration->connectionSettings()) );
43         connect(iCommunication, SIGNAL( error( int, CommunicationManager::CommunicationType ) ), this, SLOT( errorHandler( int ) ));
44         connect(iCommunication, SIGNAL( meetingsFetched( const QList<Meeting*>& ) ), this, SLOT( meetingsFetched( const QList<Meeting*>& ) ));
45         connect(iCommunication, SIGNAL( meetingDetailsFetched( Meeting& ) ), this, SLOT( meetingDetailsFetched( Meeting& ) ));
46
47         //initialize idle time counter
48         iIdleTimeCounter = new QTimer();
49         iIdleTimeCounter->setSingleShot( true);
50         iIdleTimeCounter->setInterval(IDLE_TIME_MULTIPLIER
51                         * 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 ) ), this, SLOT( changeModeOrdered( DeviceManager::OperationMode ) ));
70         iDevice->initDeviceManager();
71
72         if (iDevice->currentOperationMode() == DeviceManager::KioskMode)
73                 iWindowManager->fullScreen();
74
75         QTimer::singleShot( 0, this, SLOT( fetchMeetings() ));
76
77         // TODO: continue implementation
78 }
79
80 Engine::~Engine()
81 {
82         qDebug() << "Engine::~Engine()";
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         qDebug() << "Engine::observedEventDetected()";
106         if ( !iIdleTimeCounter->isActive() )
107         {
108                 iWindowManager->weeklyView()->showCurrentWeek();
109         }
110         iWindowManager->showWeeklyView();
111         // prepare to restart idle counter
112         if (iIdleTimeCounter->isActive() )
113         {
114                 iIdleTimeCounter->stop();
115         }
116         // (re)start idle counter
117         iIdleTimeCounter->start();
118 }
119
120 Room* Engine::defaultRoom()
121 {
122         qDebug() << "Engine::defaultRoom()";
123         return iConfiguration->defaultRoom();
124 }
125
126 void Engine::checkStatusOfAllRooms()
127 {
128         qDebug() << "Engine::checkStatusOfAllRooms()";
129         // iterate trough on the rooms
130         for (int i = 0; i < iConfiguration->rooms().count(); i++)
131         {
132                 // and check the status
133                 roomStatusInfoNeeded(iConfiguration->rooms().at(i) );
134         }
135 }
136
137 int Engine::indexOfMeetingAt(Room *aRoom, QDateTime aAt)
138 {
139         qDebug() << "Engine::indexOfMeetingAt( Room *, QDateTime )";
140         for (int i = 0; i < iMeetings.count(); i++)
141         {
142                 // exchange server ensures that there is only one meeting in a room at a specified time
143                 if (aRoom->equals(iMeetings.at( i )->room() ) && iMeetings.at( i )->startsAt() <= aAt && iMeetings.at( i )->endsAt() >= aAt)
144                 {
145                         return i;
146                 }
147         }
148         return -1;
149 }
150
151 int Engine::indexOfMeetingAfter(Room *aRoom, QDateTime aAfter)
152 {
153         qDebug() << "Engine::indexOfMeetingAfter( Room *, QDateTime )";
154         // seeks for the next meeting on the SAME DAY
155         int min = -1;
156         for (int i = 0; i < iMeetings.count(); i++)
157         {
158                 // if the meeting is in the same room, on the same day but after the specified time
159                 if (aRoom->equals(iMeetings.at( i )->room() ) && iMeetings.at( i )->startsAt().date() == aAfter.date() && iMeetings.at( i )->startsAt() > aAfter)
160                 {
161                         // if there was not any meeting find yet or the previously found is a later one then the (i)th
162                         if (min == -1 || iMeetings.at( min )->startsAt() > iMeetings.at( i )->startsAt() )
163                         {
164                                 min = i;
165                         }
166                 }
167         }
168         return min;
169 }
170
171 void Engine::roomStatusInfoNeeded(Room *aRoom)
172 {
173         qDebug() << "Engine::roomStatusInfoNeeded( Room * )";
174         if (aRoom == 0)
175         {
176                 return;
177         }
178
179         int indexOfCurrentMeeting = indexOfMeetingAt(aRoom, iClock->datetime() );
180         int indexOfNextMeeting = indexOfMeetingAfter(aRoom, iClock->datetime() );
181
182         // if there is no meeting, then status is Free; otherwise Busy
183         Room::Status
184                         status =
185                                         (indexOfCurrentMeeting == -1 ) ? Room::FreeStatus : Room::BusyStatus;
186         // if room is Busy, then check end time, otherwise...
187         QTime until = (status == Room::BusyStatus ) ? iMeetings.at( indexOfCurrentMeeting )->endsAt().time() :
188         // ...if there is meeting following on the same day then check end time, otherwise end is the of the working day
189         ((indexOfNextMeeting != -1 ) ? iMeetings.at( indexOfNextMeeting )->startsAt().time() : Engine::endOfTheDay );
190
191         //currently works only for deafult room
192         if (aRoom->equals( *(defaultRoom() )) )
193                 iWindowManager->roomStatusChanged(aRoom, status, until);
194 }
195
196 void Engine::fetchMeetings()
197 {
198         Room *room = defaultRoom();
199         qDebug() << "Engine::fetchMeetings for " << room->name();
200         fetchMeetings(iClock->datetime(), iClock->datetime().addDays( 7), room);
201 }
202
203 void Engine::fetchMeetingDetails(Meeting *aMeeting)
204 {
205         qDebug() << "Engine::fetchMeetingDetails( Meeting* )";
206         iWindowManager->showProgressBar(tr("Please Wait"), true);
207         iWindowManager->updateProgressBar(tr("Fetching Meeting Details...") );
208         connect(iWindowManager, 
209         SIGNAL( progressBarCancelled() ), this, 
210         SLOT( fetchMeetingDetailsCancelled() ));
211         iCommunication->fetchMeetingDetails( *aMeeting);
212 }
213
214 bool Engine::isMeetingInList(const QList<Meeting*> &aList, const Meeting *aMeeting)
215 {
216         qDebug()
217                         << "Engine::isMeetingInList( const QList<Meeting*> &, const Meeting * )";
218         for (int i = 0; i < aList.count(); i++)
219         {
220                 if (aMeeting->equals( *(aList.at(i))) )
221                 {
222                         return true;
223                 }
224         }
225         return false;
226 }
227
228 void Engine::meetingsFetched(const QList<Meeting*> &aMeetings)
229 {
230         qDebug() << "Engine::meetingsFetched( const QList<Meeting*> & )";
231         // check if there is any new meeting in the list came from the server -> added
232         for (int i = 0; i < aMeetings.count(); i++)
233         {
234                 // if the (i)th meeting is not in the local meeting list
235                 if ( !isMeetingInList(iMeetings, aMeetings.at(i) ) )
236                 {
237                         // add to the local database =)
238                         Meeting* m = new Meeting( *(aMeetings.at( i )) );
239                         iMeetings.append(m);
240                         // and signal the changes
241                         iWindowManager->insertMeeting(m);
242                 }
243         }
244
245         // check if there is any meeting NOT in the list came from the server -> deleted
246         for (int i = 0; i < iMeetings.count(); i++)
247         {
248                 // if the (i)th meeting is in the local but NOT in the server's meeting list
249                 if ( !isMeetingInList(aMeetings, iMeetings.at(i) ) )
250                 {
251                         Meeting* m = iMeetings.takeAt(i);
252                         // signal the changes
253                         iWindowManager->deleteMeeting(m);
254                         // delete the meeting from the local list
255                         delete m;
256                 }
257         }
258
259         // refresh room status info
260         roomStatusInfoNeeded(defaultRoom() );
261 }
262
263 void Engine::meetingDetailsFetched(Meeting &aDetailedMeeting)
264 {
265         qDebug() << "Engine::meetingDetailsFetched( Meeting & )";
266         iWindowManager->closeProgressBar();
267         iWindowManager->showMeetingInfo( &aDetailedMeeting);
268 }
269
270 void Engine::errorHandler(int aCode, const QString &aAddInfo)
271 {
272         qDebug() << "Engine::ErrorHandler, aCode: " << aCode;
273         // inform UI about the problem
274         if (aCode >= 100 && aCode <= 150)
275                 qDebug() << "CommunicationManager signaled an error:" << aCode;
276         iWindowManager->closeProgressBar();
277         iWindowManager->error(ErrorMapper::codeToString(aCode, aAddInfo) );
278 }
279
280 void Engine::currentRoomChanged(Room *aCurrentRoom)
281 {
282         qDebug() << "Engine::currentRoomChanged to " << aCurrentRoom->name();
283         QDateTime from(iWindowManager->weeklyView()->beginnigOfShownWeek() );
284         QDateTime to(from.addDays( 8) );
285         fetchMeetings(from, to, aCurrentRoom);
286 }
287
288 void Engine::fetchMeetings(const QDateTime &aFrom, const QDateTime &aUntil, const Room *aIn)
289 {
290         qDebug()
291                         << "Engine::fetchMeetings( const QDateTime &, const QDateTime &, const Room * )";
292         iCommunication->fetchMeetings(aFrom, aUntil, *aIn);
293 }
294
295 void Engine::shownWeekChanged(QDate aFrom)
296 {
297         qDebug() << "Engine::shownWeekChanged( QDate )";
298         QDateTime from(aFrom);
299         QDateTime to(aFrom.addDays( 7), QTime( 23, 59) );
300         qDebug() << "Engine::shownWeekChanged " << aFrom.toString("d.m. h:mm")
301                         << " to " << to.toString("d.m. h:mm");
302         fetchMeetings(from, to, iWindowManager->weeklyView()->currentRoom() );
303 }
304
305 void Engine::changeModeOrdered(DeviceManager::OperationMode aMode)
306 {
307         qDebug() << "Engine::changeModeOrdered( DeviceManager::OperationMode )";
308         QString message = tr( "You are about to change operation mode to %1." )
309         .arg(iDevice->operationModeToString(aMode) );
310
311         iWindowManager->showPasswordDialog(iConfiguration->adminPassword(), message);
312 }
313
314 void Engine::passwordEntered(PasswordDialog::PasswordStatus aPasswordStatus)
315 {
316         qDebug() << "Engine::passwordEntered( PasswordDialog::PasswordStatus )";
317         iWindowManager->closePasswordDialog();
318
319         switch (aPasswordStatus)
320         {
321                 case PasswordDialog::Correct:
322                 {
323                         iWindowManager->showProgressBar("Changing current operation mode.", true);
324                         connect(iWindowManager, SIGNAL( progressBarCancelled() ), this, SLOT( progressBarCancelled() ));
325                         connect(iDevice, SIGNAL( changingMode( const QString & ) ), iWindowManager, SLOT( updateProgressBar( const QString & ) ));
326                         connect(iDevice, SIGNAL( changingModeFailed() ), this, SLOT( progressBarCancelled() ));
327                         iDevice->changeMode( true);
328                         break;
329                 }
330                 case PasswordDialog::Incorrect:
331                 {
332                         iWindowManager->error(tr("Incorrect password.") );
333                         iDevice->changeMode( false);
334                         break;
335                 }
336                 default: //case PasswordDialog::Canceled
337                 {
338                         iDevice->changeMode( false);
339                 }
340         }
341 }
342
343 void Engine::progressBarCancelled()
344 {
345         qDebug() << "Engine::progressBarCancelled()";
346         //TODO: cancel the on-going event
347         iWindowManager->closeProgressBar();
348         iDevice->changeMode( false);
349 }
350
351 void Engine::fetchMeetingDetailsCancelled()
352 {
353         iCommunication->cancelFetchMeetingDetails();
354         iWindowManager->closeProgressBar();
355 }