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