Merge branch 'master' of https://git.maemo.org/projects/qtmeetings
[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 <<<<<<< HEAD:src/BusinessLogic/Engine.cpp
107         iWindowManager->refreshMeetings( iMeetings );
108 =======
109         if ( !iIdleTimeCounter->isActive() )
110         {
111                 iWindowManager->weeklyView()->showCurrentWeek();
112         }
113         iWindowManager->showWeeklyView();
114 >>>>>>> 56f763c68a2ff56d344ef150b7378ca417d08b36:src/BusinessLogic/Engine.cpp
115         // prepare to restart idle counter
116         if (iIdleTimeCounter->isActive() )
117         {
118                 iIdleTimeCounter->stop();
119         }
120         // (re)start idle counter
121         iIdleTimeCounter->start();
122 }
123
124 Room* Engine::defaultRoom()
125 {
126         qDebug() << "Engine::defaultRoom()";
127         return iConfiguration->defaultRoom();
128 }
129
130 void Engine::checkStatusOfAllRooms()
131 {
132         qDebug() << "Engine::checkStatusOfAllRooms()";
133         // iterate trough on the rooms
134         for (int i = 0; i < iConfiguration->rooms().count(); i++)
135         {
136                 // and check the status
137                 roomStatusInfoNeeded(iConfiguration->rooms().at(i) );
138         }
139 }
140
141 int Engine::indexOfMeetingAt(Room *aRoom, QDateTime aAt)
142 {
143         qDebug() << "Engine::indexOfMeetingAt( Room *, QDateTime )";
144         for (int i = 0; i < iMeetings.count(); i++)
145         {
146                 // exchange server ensures that there is only one meeting in a room at a specified time
147                 if (aRoom->equals(iMeetings.at( i )->room() ) && iMeetings.at( i )->startsAt() <= aAt && iMeetings.at( i )->endsAt() >= aAt)
148                 {
149                         return i;
150                 }
151         }
152         return -1;
153 }
154
155 int Engine::indexOfMeetingAfter(Room *aRoom, QDateTime aAfter)
156 {
157         qDebug() << "Engine::indexOfMeetingAfter( Room *, QDateTime )";
158         // seeks for the next meeting on the SAME DAY
159         int min = -1;
160         for (int i = 0; i < iMeetings.count(); i++)
161         {
162                 // if the meeting is in the same room, on the same day but after the specified time
163                 if (aRoom->equals(iMeetings.at( i )->room() ) && iMeetings.at( i )->startsAt().date() == aAfter.date() && iMeetings.at( i )->startsAt() > aAfter)
164                 {
165                         // if there was not any meeting find yet or the previously found is a later one then the (i)th
166                         if (min == -1 || iMeetings.at( min )->startsAt() > iMeetings.at( i )->startsAt() )
167                         {
168                                 min = i;
169                         }
170                 }
171         }
172         return min;
173 }
174
175 void Engine::roomStatusInfoNeeded(Room *aRoom)
176 {
177         qDebug() << "Engine::roomStatusInfoNeeded( Room * )";
178         if (aRoom == 0)
179         {
180                 return;
181         }
182
183         int indexOfCurrentMeeting = indexOfMeetingAt(aRoom, iClock->datetime() );
184         int indexOfNextMeeting = indexOfMeetingAfter(aRoom, iClock->datetime() );
185
186         // if there is no meeting, then status is Free; otherwise Busy
187         Room::Status
188                         status =
189                                         (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() ), this, 
214         SLOT( fetchMeetingDetailsCancelled() ));
215         iCommunication->fetchMeetingDetails( *aMeeting);
216 }
217
218 <<<<<<< HEAD:src/BusinessLogic/Engine.cpp
219 void Engine::meetingsFetched( const QList<Meeting*> &aMeetings )
220 {
221         qDebug() << "Engine::meetingsFetched( const QList<Meeting*> & )";
222         
223         for ( int i = 0; i < iMeetings.count(); ++i ) {
224                 Meeting* m = iMeetings.takeAt( i );
225                 delete m;
226         }
227         iMeetings.clear();
228         for ( int i = 0; i < aMeetings.count(); ++i ) {
229                 Meeting* m = new Meeting( *(aMeetings.at( i )) );
230                 iMeetings.append( m );
231 =======
232 bool Engine::isMeetingInList(const QList<Meeting*> &aList, const Meeting *aMeeting)
233 {
234         qDebug()
235                         << "Engine::isMeetingInList( const QList<Meeting*> &, const Meeting * )";
236         for (int i = 0; i < aList.count(); i++)
237         {
238                 if (aMeeting->equals( *(aList.at(i))) )
239                 {
240                         return true;
241                 }
242         }
243         return false;
244 }
245
246 void Engine::meetingsFetched(const QList<Meeting*> &aMeetings)
247 {
248         qDebug() << "Engine::meetingsFetched( const QList<Meeting*> & )";
249         // check if there is any new meeting in the list came from the server -> added
250         for (int i = 0; i < aMeetings.count(); i++)
251         {
252                 // if the (i)th meeting is not in the local meeting list
253                 if ( !isMeetingInList(iMeetings, aMeetings.at(i) ) )
254                 {
255                         // add to the local database =)
256                         Meeting* m = new Meeting( *(aMeetings.at( i )) );
257                         iMeetings.append(m);
258                         // and signal the changes
259                         iWindowManager->insertMeeting(m);
260                 }
261         }
262
263         // check if there is any meeting NOT in the list came from the server -> deleted
264         for (int i = 0; i < iMeetings.count(); i++)
265         {
266                 // if the (i)th meeting is in the local but NOT in the server's meeting list
267                 if ( !isMeetingInList(aMeetings, iMeetings.at(i) ) )
268                 {
269                         Meeting* m = iMeetings.takeAt(i);
270                         // signal the changes
271                         iWindowManager->deleteMeeting(m);
272                         // delete the meeting from the local list
273                         delete m;
274                 }
275 >>>>>>> 56f763c68a2ff56d344ef150b7378ca417d08b36:src/BusinessLogic/Engine.cpp
276         }
277
278         iWindowManager->refreshMeetings( iMeetings );
279         // refresh room status info
280         roomStatusInfoNeeded(defaultRoom() );
281 }
282
283 void Engine::meetingDetailsFetched(Meeting &aDetailedMeeting)
284 {
285         qDebug() << "Engine::meetingDetailsFetched( Meeting & )";
286         iWindowManager->closeProgressBar();
287         iWindowManager->showMeetingInfo( &aDetailedMeeting);
288 }
289
290 void Engine::errorHandler(int aCode, const QString &aAddInfo)
291 {
292         qDebug() << "Engine::ErrorHandler, aCode: " << aCode;
293         // inform UI about the problem
294         if( aCode >= 100 && aCode <= 150 ) { //communication errors
295                 //we don't want these to close operation changing
296                 qDebug() << "CommunicationManager signaled an error:" << aCode;
297                 iWindowManager->closeProgressBar();
298         }
299         iWindowManager->error(ErrorMapper::codeToString(aCode, aAddInfo) );
300 }
301
302 void Engine::currentRoomChanged(Room *aCurrentRoom)
303 {
304         qDebug() << "Engine::currentRoomChanged to " << aCurrentRoom->name();
305         QDateTime from(iWindowManager->weeklyView()->beginnigOfShownWeek() );
306         QDateTime to(from.addDays( 8) );
307         fetchMeetings(from, to, aCurrentRoom);
308 }
309
310 void Engine::fetchMeetings(const QDateTime &aFrom, const QDateTime &aUntil, const Room *aIn)
311 {
312         qDebug()
313                         << "Engine::fetchMeetings( const QDateTime &, const QDateTime &, const Room * )";
314         iCommunication->fetchMeetings(aFrom, aUntil, *aIn);
315 }
316
317 void Engine::shownWeekChanged(QDate aFrom)
318 {
319         qDebug() << "Engine::shownWeekChanged( QDate )";
320         QDateTime from(aFrom);
321         QDateTime to(aFrom.addDays( 7), QTime( 23, 59) );
322         qDebug() << "Engine::shownWeekChanged " << aFrom.toString("d.m. h:mm")
323                         << " to " << to.toString("d.m. h:mm");
324         fetchMeetings(from, to, iWindowManager->weeklyView()->currentRoom() );
325 }
326
327 void Engine::changeModeOrdered(DeviceManager::OperationMode aMode)
328 {
329         qDebug() << "Engine::changeModeOrdered( DeviceManager::OperationMode )";
330         QString message = tr( "You are about to change operation mode to %1." )
331         .arg(iDevice->operationModeToString(aMode) );
332
333         iWindowManager->showPasswordDialog(iConfiguration->adminPassword(), message);
334 }
335
336 void Engine::passwordEntered(PasswordDialog::PasswordStatus aPasswordStatus)
337 {
338         qDebug() << "Engine::passwordEntered( PasswordDialog::PasswordStatus )";
339         iWindowManager->closePasswordDialog();
340
341         switch (aPasswordStatus)
342         {
343                 case PasswordDialog::Correct:
344                 {
345                         iAutoRefresh->stop(); //we stop the metting updating
346                         iWindowManager->showProgressBar( "Changing current operation mode." );
347                         connect(iDevice, SIGNAL( changingMode( const QString & ) ), iWindowManager, SLOT( updateProgressBar( const QString & ) ));
348                         connect( iDevice, SIGNAL( changingMode( const QString & ) ),
349                                         iWindowManager, SLOT( updateProgressBar( const QString & ) ) );
350                         connect( iDevice, SIGNAL( changeModeFailed() ), this, SLOT( changeModeFailed() ) );
351                         iDevice->changeMode( true);
352                         break;
353                 }
354                 case PasswordDialog::Incorrect:
355                 {
356                         iWindowManager->error(tr("Incorrect password.") );
357                         iDevice->changeMode( false);
358                         break;
359                 }
360                 default: //case PasswordDialog::Canceled
361                 {
362                         iDevice->changeMode( false);
363                 }
364         }
365 }
366
367 void Engine::changeModeFailed()
368 {
369         qDebug() << "Engine::changeModeFailed()";
370         iWindowManager->closeProgressBar();
371         iAutoRefresh->start(); //we start the metting updating
372 }
373
374 void Engine::fetchMeetingDetailsCancelled()
375 {
376         iCommunication->cancelFetchMeetingDetails();
377         iWindowManager->closeProgressBar();
378 }