4 #include "ConnectionSettings.h"
5 #include "Configuration.h"
6 #include "DisplaySettings.h"
7 #include "CommunicationManager.h"
8 #include "DeviceManager.h"
10 #include "ErrorMapper.h"
11 #include "WeeklyViewWidget.h"
13 #include <QApplication>
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
22 iClock( 0), iConfiguration(Configuration::instance() ), iCommunication( 0)
24 qDebug() << "Engine::Engine()";
25 iCommunicationFailed = false;
26 // if reading of configuration fails, signal that initialization failed
27 if (iConfiguration == 0)
29 QTimer::singleShot( 0, this, SLOT( closeApplication() ));
33 //initialize window manager
34 iWindowManager = new WindowManager( iConfiguration );
35 connect(iWindowManager, SIGNAL( roomStatusInfoNeeded( Room * ) ), this, SLOT( roomStatusInfoNeeded( Room * ) ));
36 connect(iWindowManager, SIGNAL( observedEventDetected() ), this, SLOT( observedEventDetected() ));
37 connect(iWindowManager, SIGNAL( meetingActivated( Meeting * ) ), this, SLOT( fetchMeetingDetails( Meeting * ) ));
38 connect(iWindowManager, SIGNAL( currentRoomChanged( Room * ) ), this, SLOT( currentRoomChanged( Room * ) ));
39 connect(iWindowManager, SIGNAL( shownWeekChanged( QDate ) ), this, SLOT( shownWeekChanged( QDate ) ));
40 connect(iWindowManager, SIGNAL( passwordEntered( PasswordDialog::PasswordStatus ) ), this, SLOT( passwordEntered( PasswordDialog::PasswordStatus ) ));
42 // initialize communication
43 iCommunication = new CommunicationManager( *(iConfiguration->connectionSettings()) );
44 connect(iCommunication, SIGNAL( error( int, CommunicationManager::CommunicationType ) ), this, SLOT( errorHandler( int ) ));
45 connect(iCommunication, SIGNAL( meetingsFetched( const QList<Meeting*>& ) ), this, SLOT( meetingsFetched( const QList<Meeting*>& ) ));
46 connect(iCommunication, SIGNAL( meetingDetailsFetched( Meeting& ) ), this, SLOT( meetingDetailsFetched( Meeting& ) ));
48 //initialize idle time counter
49 iIdleTimeCounter = new QTimer();
50 iIdleTimeCounter->setSingleShot( true);
51 iIdleTimeCounter->setInterval(IDLE_TIME_MULTIPLIER
52 * iConfiguration->displaySettings()->screensaver() );
53 iIdleTimeCounter->start();
54 connect(iIdleTimeCounter, SIGNAL( timeout() ), iWindowManager, SLOT( showRoomStatus() ));
56 // create application clock
58 connect(iClock, SIGNAL( tick( QDateTime ) ), this, SLOT( checkStatusOfAllRooms() ));
59 connect(iClock, SIGNAL( tick( QDateTime ) ), iWindowManager, SLOT( distributeDateTimeInfo( QDateTime ) ));
61 iAutoRefresh = new QTimer;
62 iAutoRefresh->setInterval(iConfiguration->connectionSettings()->refreshInterval() * 1000);
63 iAutoRefresh->start();
64 connect(iAutoRefresh, SIGNAL( timeout() ), iAutoRefresh, SLOT( start() ));
65 connect(iAutoRefresh, SIGNAL( timeout() ), this, SLOT( fetchMeetings() ));
67 // create device manager
68 iDevice = new DeviceManager( iConfiguration->startupSettings() );
69 connect(iDevice, SIGNAL( error( int, const QString& ) ), this, SLOT( errorHandler( int, const QString& ) ));
70 connect(iDevice, SIGNAL( changeModeOrdered( DeviceManager::OperationMode ) ), this, SLOT( changeModeOrdered( DeviceManager::OperationMode ) ));
71 iDevice->initDeviceManager();
73 if (iDevice->currentOperationMode() == DeviceManager::KioskMode)
74 iWindowManager->fullScreen();
76 QTimer::singleShot( 0, this, SLOT( fetchMeetings() ));
78 // TODO: continue implementation
83 qDebug() << "Engine::~Engine()";
84 while ( !iMeetings.isEmpty() )
85 delete iMeetings.takeFirst();
86 iIdleTimeCounter->stop();
87 delete iIdleTimeCounter;
89 delete iWindowManager;
97 void Engine::closeApplication()
99 qDebug() << "Engine::closeApplication()";
100 // closes application after 1 second
101 QTimer::singleShot( 1000, QApplication::instance(), SLOT( quit() ));
104 void Engine::observedEventDetected()
106 qDebug() << "Engine::observedEventDetected()";
107 if ( !iIdleTimeCounter->isActive() )
109 iWindowManager->weeklyView()->showCurrentWeek();
111 iWindowManager->showWeeklyView();
112 // prepare to restart idle counter
113 if (iIdleTimeCounter->isActive() )
115 iIdleTimeCounter->stop();
117 // (re)start idle counter
118 iIdleTimeCounter->start();
121 Room* Engine::defaultRoom()
123 qDebug() << "Engine::defaultRoom()";
124 return iConfiguration->defaultRoom();
127 void Engine::checkStatusOfAllRooms()
129 qDebug() << "Engine::checkStatusOfAllRooms()";
130 // iterate trough on the rooms
131 for (int i = 0; i < iConfiguration->rooms().count(); i++)
133 // and check the status
134 roomStatusInfoNeeded(iConfiguration->rooms().at(i) );
138 int Engine::indexOfMeetingAt(Room *aRoom, QDateTime aAt)
140 qDebug() << "Engine::indexOfMeetingAt( Room *, QDateTime )";
141 for (int i = 0; i < iMeetings.count(); i++)
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() ) && iMeetings.at( i )->startsAt() <= aAt && iMeetings.at( i )->endsAt() >= aAt)
152 int Engine::indexOfMeetingAfter(Room *aRoom, QDateTime aAfter)
154 qDebug() << "Engine::indexOfMeetingAfter( Room *, QDateTime )";
155 // seeks for the next meeting on the SAME DAY
157 for (int i = 0; i < iMeetings.count(); i++)
159 // if the meeting is in the same room, on the same day but after the specified time
160 if (aRoom->equals(iMeetings.at( i )->room() ) && iMeetings.at( i )->startsAt().date() == aAfter.date() && iMeetings.at( i )->startsAt() > aAfter)
162 // if there was not any meeting find yet or the previously found is a later one then the (i)th
163 if (min == -1 || iMeetings.at( min )->startsAt() > iMeetings.at( i )->startsAt() )
172 void Engine::roomStatusInfoNeeded(Room *aRoom)
174 qDebug() << "Engine::roomStatusInfoNeeded( Room * )";
180 int indexOfCurrentMeeting = indexOfMeetingAt(aRoom, iClock->datetime() );
181 int indexOfNextMeeting = indexOfMeetingAfter(aRoom, iClock->datetime() );
183 // if there is no meeting, then status is Free; otherwise Busy
186 (indexOfCurrentMeeting == -1 ) ? Room::FreeStatus : Room::BusyStatus;
187 // if room is Busy, then check end time, otherwise...
188 QTime until = (status == Room::BusyStatus ) ? iMeetings.at( indexOfCurrentMeeting )->endsAt().time() :
189 // ...if there is meeting following on the same day then check end time, otherwise end is the of the working day
190 ((indexOfNextMeeting != -1 ) ? iMeetings.at( indexOfNextMeeting )->startsAt().time() : Engine::endOfTheDay );
192 //currently works only for deafult room
193 if (aRoom->equals( *(defaultRoom() )) )
194 iWindowManager->roomStatusChanged(aRoom, status, until);
197 void Engine::fetchMeetings()
199 Room *room = defaultRoom();
200 qDebug() << "Engine::fetchMeetings for " << room->name();
201 fetchMeetings(iClock->datetime(), iClock->datetime().addDays( 7), room);
204 void Engine::fetchMeetingDetails(Meeting *aMeeting)
206 qDebug() << "Engine::fetchMeetingDetails( Meeting* )";
207 iWindowManager->showProgressBar(tr("Please Wait"), true);
208 iWindowManager->updateProgressBar(tr("Fetching Meeting Details...") );
209 connect(iWindowManager,
210 SIGNAL( progressBarCancelled() ), this,
211 SLOT( fetchMeetingDetailsCancelled() ));
212 iCommunication->fetchMeetingDetails( *aMeeting);
215 bool Engine::isMeetingInList(const QList<Meeting*> &aList, const Meeting *aMeeting)
218 << "Engine::isMeetingInList( const QList<Meeting*> &, const Meeting * )";
219 for (int i = 0; i < aList.count(); i++)
221 if (aMeeting->equals( *(aList.at(i))) )
229 void Engine::meetingsFetched(const QList<Meeting*> &aMeetings)
231 qDebug() << "Engine::meetingsFetched( const QList<Meeting*> & )";
232 if( iCommunicationFailed )
234 iCommunicationFailed = false;
235 //iWindowManager->connectionEstablished();
238 // check if there is any new meeting in the list came from the server -> added
239 for (int i = 0; i < aMeetings.count(); i++)
241 // if the (i)th meeting is not in the local meeting list
242 if ( !isMeetingInList(iMeetings, aMeetings.at(i) ) )
244 // add to the local database =)
245 Meeting* m = new Meeting( *(aMeetings.at( i )) );
247 // and signal the changes
248 iWindowManager->insertMeeting(m);
252 // check if there is any meeting NOT in the list came from the server -> deleted
253 for (int i = 0; i < iMeetings.count(); i++)
255 // if the (i)th meeting is in the local but NOT in the server's meeting list
256 if ( !isMeetingInList(aMeetings, iMeetings.at(i) ) )
258 Meeting* m = iMeetings.takeAt(i);
259 // signal the changes
260 iWindowManager->deleteMeeting(m);
261 // delete the meeting from the local list
266 // refresh room status info
267 roomStatusInfoNeeded(defaultRoom() );
270 void Engine::meetingDetailsFetched(Meeting &aDetailedMeeting)
272 qDebug() << "Engine::meetingDetailsFetched( Meeting & )";
273 iWindowManager->closeProgressBar();
274 if( iCommunicationFailed )
276 iCommunicationFailed = false;
277 iWindowManager->connectionEstablished();
279 iWindowManager->showMeetingInfo( &aDetailedMeeting);
282 void Engine::errorHandler(int aCode, const QString &aAddInfo)
284 qDebug() << "Engine::ErrorHandler, aCode: " << aCode;
285 // inform UI about the problem
286 if( aCode >= 100 && aCode <= 150 ) { //communication errors
288 //we don't want these to close operation changing
289 qDebug() << "CommunicationManager signaled an error:" << aCode;
290 if( !iCommunicationFailed )
292 // Only inform window manager when first error appears
293 iCommunicationFailed = true;
294 //iWindowManager->connectionLost();
297 iWindowManager->closeProgressBar();
299 iWindowManager->error(ErrorMapper::codeToString(aCode, aAddInfo) );
302 void Engine::currentRoomChanged(Room *aCurrentRoom)
304 qDebug() << "Engine::currentRoomChanged to " << aCurrentRoom->name();
305 QDateTime from(iWindowManager->weeklyView()->beginnigOfShownWeek() );
306 QDateTime to(from.addDays( 8) );
307 fetchMeetings(from, to, aCurrentRoom);
310 void Engine::fetchMeetings(const QDateTime &aFrom, const QDateTime &aUntil, const Room *aIn)
313 << "Engine::fetchMeetings( const QDateTime &, const QDateTime &, const Room * )";
314 iCommunication->fetchMeetings(aFrom, aUntil, *aIn);
317 void Engine::shownWeekChanged(QDate aFrom)
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() );
327 void Engine::changeModeOrdered(DeviceManager::OperationMode aMode)
329 qDebug() << "Engine::changeModeOrdered( DeviceManager::OperationMode )";
330 QString message = tr( "You are about to change operation mode to %1." )
331 .arg(iDevice->operationModeToString(aMode) );
333 iWindowManager->showPasswordDialog(iConfiguration->adminPassword(), message);
336 void Engine::passwordEntered(PasswordDialog::PasswordStatus aPasswordStatus)
338 qDebug() << "Engine::passwordEntered( PasswordDialog::PasswordStatus )";
339 iWindowManager->closePasswordDialog();
341 switch (aPasswordStatus)
343 case PasswordDialog::Correct:
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);
354 case PasswordDialog::Incorrect:
356 iWindowManager->error(tr("Incorrect password.") );
357 iDevice->changeMode( false);
360 default: //case PasswordDialog::Canceled
362 iDevice->changeMode( false);
367 void Engine::changeModeFailed()
369 qDebug() << "Engine::changeModeFailed()";
370 iWindowManager->closeProgressBar();
371 iAutoRefresh->start(); //we start the metting updating
374 void Engine::fetchMeetingDetailsCancelled()
376 iCommunication->cancelFetchMeetingDetails();
377 iWindowManager->closeProgressBar();