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 // if reading of configuration fails, signal that initialization failed
26 if (iConfiguration == 0)
28 QTimer::singleShot( 0, this, SLOT( closeApplication() ));
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 ) ));
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& ) ));
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() ));
55 // create application clock
57 connect(iClock, SIGNAL( tick( QDateTime ) ), this, SLOT( checkStatusOfAllRooms() ));
58 connect(iClock, SIGNAL( tick( QDateTime ) ), iWindowManager, SLOT( distributeDateTimeInfo( QDateTime ) ));
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() ));
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();
72 if (iDevice->currentOperationMode() == DeviceManager::KioskMode)
73 iWindowManager->fullScreen();
75 QTimer::singleShot( 0, this, SLOT( fetchMeetings() ));
77 // TODO: continue implementation
82 qDebug() << "Engine::~Engine()";
83 while ( !iMeetings.isEmpty() )
84 delete iMeetings.takeFirst();
85 iIdleTimeCounter->stop();
86 delete iIdleTimeCounter;
88 delete iWindowManager;
96 void Engine::closeApplication()
98 qDebug() << "Engine::closeApplication()";
99 // closes application after 1 second
100 QTimer::singleShot( 1000, QApplication::instance(), SLOT( quit() ));
103 void Engine::observedEventDetected()
105 qDebug() << "Engine::observedEventDetected()";
106 if ( !iIdleTimeCounter->isActive() )
108 iWindowManager->weeklyView()->showCurrentWeek();
110 iWindowManager->showWeeklyView();
111 // prepare to restart idle counter
112 if (iIdleTimeCounter->isActive() )
114 iIdleTimeCounter->stop();
116 // (re)start idle counter
117 iIdleTimeCounter->start();
120 Room* Engine::defaultRoom()
122 qDebug() << "Engine::defaultRoom()";
123 return iConfiguration->defaultRoom();
126 void Engine::checkStatusOfAllRooms()
128 qDebug() << "Engine::checkStatusOfAllRooms()";
129 // iterate trough on the rooms
130 for (int i = 0; i < iConfiguration->rooms().count(); i++)
132 // and check the status
133 roomStatusInfoNeeded(iConfiguration->rooms().at(i) );
137 int Engine::indexOfMeetingAt(Room *aRoom, QDateTime aAt)
139 qDebug() << "Engine::indexOfMeetingAt( Room *, QDateTime )";
140 for (int i = 0; i < iMeetings.count(); i++)
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)
151 int Engine::indexOfMeetingAfter(Room *aRoom, QDateTime aAfter)
153 qDebug() << "Engine::indexOfMeetingAfter( Room *, QDateTime )";
154 // seeks for the next meeting on the SAME DAY
156 for (int i = 0; i < iMeetings.count(); i++)
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)
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() )
171 void Engine::roomStatusInfoNeeded(Room *aRoom)
173 qDebug() << "Engine::roomStatusInfoNeeded( Room * )";
179 int indexOfCurrentMeeting = indexOfMeetingAt(aRoom, iClock->datetime() );
180 int indexOfNextMeeting = indexOfMeetingAfter(aRoom, iClock->datetime() );
182 // if there is no meeting, then status is Free; otherwise Busy
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 );
191 //currently works only for deafult room
192 if (aRoom->equals( *(defaultRoom() )) )
193 iWindowManager->roomStatusChanged(aRoom, status, until);
196 void Engine::fetchMeetings()
198 Room *room = defaultRoom();
199 qDebug() << "Engine::fetchMeetings for " << room->name();
200 fetchMeetings(iClock->datetime(), iClock->datetime().addDays( 7), room);
203 void Engine::fetchMeetingDetails(Meeting *aMeeting)
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);
214 bool Engine::isMeetingInList(const QList<Meeting*> &aList, const Meeting *aMeeting)
217 << "Engine::isMeetingInList( const QList<Meeting*> &, const Meeting * )";
218 for (int i = 0; i < aList.count(); i++)
220 if (aMeeting->equals( *(aList.at(i))) )
228 void Engine::meetingsFetched(const QList<Meeting*> &aMeetings)
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++)
234 // if the (i)th meeting is not in the local meeting list
235 if ( !isMeetingInList(iMeetings, aMeetings.at(i) ) )
237 // add to the local database =)
238 Meeting* m = new Meeting( *(aMeetings.at( i )) );
240 // and signal the changes
241 iWindowManager->insertMeeting(m);
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++)
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) ) )
251 Meeting* m = iMeetings.takeAt(i);
252 // signal the changes
253 iWindowManager->deleteMeeting(m);
254 // delete the meeting from the local list
259 // refresh room status info
260 roomStatusInfoNeeded(defaultRoom() );
263 void Engine::meetingDetailsFetched(Meeting &aDetailedMeeting)
265 qDebug() << "Engine::meetingDetailsFetched( Meeting & )";
266 iWindowManager->closeProgressBar();
267 iWindowManager->showMeetingInfo( &aDetailedMeeting);
270 void Engine::errorHandler(int aCode, const QString &aAddInfo)
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) );
280 void Engine::currentRoomChanged(Room *aCurrentRoom)
282 qDebug() << "Engine::currentRoomChanged to " << aCurrentRoom->name();
283 QDateTime from(iWindowManager->weeklyView()->beginnigOfShownWeek() );
284 QDateTime to(from.addDays( 8) );
285 fetchMeetings(from, to, aCurrentRoom);
288 void Engine::fetchMeetings(const QDateTime &aFrom, const QDateTime &aUntil, const Room *aIn)
291 << "Engine::fetchMeetings( const QDateTime &, const QDateTime &, const Room * )";
292 iCommunication->fetchMeetings(aFrom, aUntil, *aIn);
295 void Engine::shownWeekChanged(QDate aFrom)
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() );
305 void Engine::changeModeOrdered(DeviceManager::OperationMode aMode)
307 qDebug() << "Engine::changeModeOrdered( DeviceManager::OperationMode )";
308 QString message = tr( "You are about to change operation mode to %1." )
309 .arg(iDevice->operationModeToString(aMode) );
311 iWindowManager->showPasswordDialog(iConfiguration->adminPassword(), message);
314 void Engine::passwordEntered(PasswordDialog::PasswordStatus aPasswordStatus)
316 qDebug() << "Engine::passwordEntered( PasswordDialog::PasswordStatus )";
317 iWindowManager->closePasswordDialog();
319 switch (aPasswordStatus)
321 case PasswordDialog::Correct:
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);
330 case PasswordDialog::Incorrect:
332 iWindowManager->error(tr("Incorrect password.") );
333 iDevice->changeMode( false);
336 default: //case PasswordDialog::Canceled
338 iDevice->changeMode( false);
343 void Engine::progressBarCancelled()
345 qDebug() << "Engine::progressBarCancelled()";
346 //TODO: cancel the on-going event
347 iWindowManager->closeProgressBar();
348 iDevice->changeMode( false);
351 void Engine::fetchMeetingDetailsCancelled()
353 iCommunication->cancelFetchMeetingDetails();
354 iWindowManager->closeProgressBar();