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 <<<<<<< HEAD:src/BusinessLogic/Engine.cpp
107 iWindowManager->refreshMeetings( iMeetings );
109 if ( !iIdleTimeCounter->isActive() )
111 iWindowManager->weeklyView()->showCurrentWeek();
113 iWindowManager->showWeeklyView();
114 >>>>>>> 56f763c68a2ff56d344ef150b7378ca417d08b36:src/BusinessLogic/Engine.cpp
115 // prepare to restart idle counter
116 if (iIdleTimeCounter->isActive() )
118 iIdleTimeCounter->stop();
120 // (re)start idle counter
121 iIdleTimeCounter->start();
124 Room* Engine::defaultRoom()
126 qDebug() << "Engine::defaultRoom()";
127 return iConfiguration->defaultRoom();
130 void Engine::checkStatusOfAllRooms()
132 qDebug() << "Engine::checkStatusOfAllRooms()";
133 // iterate trough on the rooms
134 for (int i = 0; i < iConfiguration->rooms().count(); i++)
136 // and check the status
137 roomStatusInfoNeeded(iConfiguration->rooms().at(i) );
141 int Engine::indexOfMeetingAt(Room *aRoom, QDateTime aAt)
143 qDebug() << "Engine::indexOfMeetingAt( Room *, QDateTime )";
144 for (int i = 0; i < iMeetings.count(); i++)
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)
155 int Engine::indexOfMeetingAfter(Room *aRoom, QDateTime aAfter)
157 qDebug() << "Engine::indexOfMeetingAfter( Room *, QDateTime )";
158 // seeks for the next meeting on the SAME DAY
160 for (int i = 0; i < iMeetings.count(); i++)
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)
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() )
175 void Engine::roomStatusInfoNeeded(Room *aRoom)
177 qDebug() << "Engine::roomStatusInfoNeeded( Room * )";
183 int indexOfCurrentMeeting = indexOfMeetingAt(aRoom, iClock->datetime() );
184 int indexOfNextMeeting = indexOfMeetingAfter(aRoom, iClock->datetime() );
186 // if there is no meeting, then status is Free; otherwise Busy
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 );
195 //currently works only for deafult room
196 if (aRoom->equals( *(defaultRoom() )) )
197 iWindowManager->roomStatusChanged(aRoom, status, until);
200 void Engine::fetchMeetings()
202 Room *room = defaultRoom();
203 qDebug() << "Engine::fetchMeetings for " << room->name();
204 fetchMeetings(iClock->datetime(), iClock->datetime().addDays( 7), room);
207 void Engine::fetchMeetingDetails(Meeting *aMeeting)
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);
218 <<<<<<< HEAD:src/BusinessLogic/Engine.cpp
219 void Engine::meetingsFetched( const QList<Meeting*> &aMeetings )
221 qDebug() << "Engine::meetingsFetched( const QList<Meeting*> & )";
223 for ( int i = 0; i < iMeetings.count(); ++i ) {
224 Meeting* m = iMeetings.takeAt( i );
228 for ( int i = 0; i < aMeetings.count(); ++i ) {
229 Meeting* m = new Meeting( *(aMeetings.at( i )) );
230 iMeetings.append( m );
232 bool Engine::isMeetingInList(const QList<Meeting*> &aList, const Meeting *aMeeting)
235 << "Engine::isMeetingInList( const QList<Meeting*> &, const Meeting * )";
236 for (int i = 0; i < aList.count(); i++)
238 if (aMeeting->equals( *(aList.at(i))) )
246 void Engine::meetingsFetched(const QList<Meeting*> &aMeetings)
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++)
252 // if the (i)th meeting is not in the local meeting list
253 if ( !isMeetingInList(iMeetings, aMeetings.at(i) ) )
255 // add to the local database =)
256 Meeting* m = new Meeting( *(aMeetings.at( i )) );
258 // and signal the changes
259 iWindowManager->insertMeeting(m);
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++)
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) ) )
269 Meeting* m = iMeetings.takeAt(i);
270 // signal the changes
271 iWindowManager->deleteMeeting(m);
272 // delete the meeting from the local list
275 >>>>>>> 56f763c68a2ff56d344ef150b7378ca417d08b36:src/BusinessLogic/Engine.cpp
278 iWindowManager->refreshMeetings( iMeetings );
279 // refresh room status info
280 roomStatusInfoNeeded(defaultRoom() );
283 void Engine::meetingDetailsFetched(Meeting &aDetailedMeeting)
285 qDebug() << "Engine::meetingDetailsFetched( Meeting & )";
286 iWindowManager->closeProgressBar();
287 iWindowManager->showMeetingInfo( &aDetailedMeeting);
290 void Engine::errorHandler(int aCode, const QString &aAddInfo)
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();
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();