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 ) ),
40 this, SLOT( passwordEntered( PasswordDialog::PasswordStatus ) ) );
43 // initialize communication
44 iCommunication = new CommunicationManager( *(iConfiguration->connectionSettings()) );
45 connect( iCommunication, SIGNAL( error( int, CommunicationManager::CommunicationType ) ),
46 this, SLOT( errorHandler( int ) ) );
47 connect( iCommunication, SIGNAL( meetingsFetched( const QList<Meeting*>& ) ),
48 this, SLOT( meetingsFetched( const QList<Meeting*>& ) ) );
49 connect( iCommunication, SIGNAL( meetingDetailsFetched( Meeting& ) ),
50 this, SLOT( meetingDetailsFetched( Meeting& ) ) );
52 //initialize idle time counter
53 iIdleTimeCounter = new QTimer();
54 iIdleTimeCounter->setSingleShot( true );
55 iIdleTimeCounter->setInterval( IDLE_TIME_MULTIPLIER * iConfiguration->displaySettings()->screensaver() );
56 iIdleTimeCounter->start();
57 connect( iIdleTimeCounter, SIGNAL( timeout() ), iWindowManager, SLOT( showRoomStatus() ) );
59 // create application clock
61 connect( iClock, SIGNAL( tick( QDateTime ) ), this, SLOT( checkStatusOfAllRooms() ) );
62 connect( iClock, SIGNAL( tick( QDateTime ) ), iWindowManager, SLOT( distributeDateTimeInfo( QDateTime ) ) );
64 iAutoRefresh = new QTimer;
65 iAutoRefresh->setInterval( iConfiguration->connectionSettings()->refreshInterval() * 1000 );
66 iAutoRefresh->start();
67 connect( iAutoRefresh, SIGNAL( timeout() ), iAutoRefresh, SLOT( start() ) );
68 connect( iAutoRefresh, SIGNAL( timeout() ), this, SLOT( fetchMeetings() ) );
70 // create device manager
71 iDevice = new DeviceManager( iConfiguration->startupSettings() );
72 connect( iDevice, SIGNAL( error( int, const QString& ) ), this, SLOT( errorHandler( int, const QString& ) ) );
73 connect( iDevice, SIGNAL( changeModeOrdered( DeviceManager::OperationMode ) ),
74 this, SLOT( changeModeOrdered( DeviceManager::OperationMode ) ) );
75 iDevice->initDeviceManager();
77 if( iDevice->currentOperationMode() == DeviceManager::KioskMode )
78 iWindowManager->fullScreen();
80 QTimer::singleShot( 0, this, SLOT( fetchMeetings() ) );
82 // TODO: continue implementation
87 qDebug() << "Engine::~Engine()";
88 while ( !iMeetings.isEmpty() )
89 delete iMeetings.takeFirst();
90 iIdleTimeCounter->stop();
91 delete iIdleTimeCounter;
93 delete iWindowManager;
101 void Engine::closeApplication()
103 qDebug() << "Engine::closeApplication()";
104 // closes application after 1 second
105 QTimer::singleShot( 1000, QApplication::instance(), SLOT( quit() ) );
108 void Engine::observedEventDetected()
110 qDebug() << "Engine::observedEventDetected()";
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() )
145 && iMeetings.at( i )->startsAt() <= aAt
146 && iMeetings.at( i )->endsAt() >= aAt )
154 int Engine::indexOfMeetingAfter( Room *aRoom, QDateTime aAfter )
156 qDebug() << "Engine::indexOfMeetingAfter( Room *, QDateTime )";
157 // seeks for the next meeting on the SAME DAY
159 for ( int i = 0; i < iMeetings.count(); i++ )
161 // if the meeting is in the same room, on the same day but after the specified time
162 if ( aRoom->equals( iMeetings.at( i )->room() )
163 && iMeetings.at( i )->startsAt().date() == aAfter.date()
164 && iMeetings.at( i )->startsAt() > aAfter )
166 // if there was not any meeting find yet or the previously found is a later one then the (i)th
168 || iMeetings.at( min )->startsAt() > iMeetings.at( i )->startsAt() )
177 void Engine::roomStatusInfoNeeded( Room *aRoom )
179 qDebug() << "Engine::roomStatusInfoNeeded( Room * )";
185 int indexOfCurrentMeeting = indexOfMeetingAt( aRoom, iClock->datetime() );
186 int indexOfNextMeeting = indexOfMeetingAfter( aRoom, iClock->datetime() );
188 // if there is no meeting, then status is Free; otherwise Busy
189 Room::Status status = ( 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 iCommunication->fetchMeetingDetails( *aMeeting );
213 bool Engine::isMeetingInList( const QList<Meeting*> &aList, const Meeting *aMeeting )
215 qDebug() << "Engine::isMeetingInList( const QList<Meeting*> &, const Meeting * )";
216 for ( int i = 0; i < aList.count(); i++ )
218 if ( aMeeting->equals( *(aList.at( i )) ) )
226 void Engine::meetingsFetched( const QList<Meeting*> &aMeetings )
228 qDebug() << "Engine::meetingsFetched( const QList<Meeting*> & )";
229 // check if there is any new meeting in the list came from the server -> added
230 for ( int i = 0; i < aMeetings.count(); i++ )
232 // if the (i)th meeting is not in the local meeting list
233 if ( !isMeetingInList( iMeetings, aMeetings.at( i ) ) )
235 // add to the local database =)
236 Meeting* m = new Meeting( *(aMeetings.at( i )) );
237 iMeetings.append( m );
238 // and signal the changes
239 iWindowManager->insertMeeting( m );
243 // check if there is any meeting NOT in the list came from the server -> deleted
244 for ( int i = 0; i < iMeetings.count(); i++ )
246 // if the (i)th meeting is in the local but NOT in the server's meeting list
247 if ( !isMeetingInList( aMeetings, iMeetings.at( i ) ) )
249 Meeting* m = iMeetings.takeAt( i );
250 // signal the changes
251 iWindowManager->deleteMeeting( m );
252 // delete the meeting from the local list
257 // refresh room status info
258 roomStatusInfoNeeded( defaultRoom() );
261 void Engine::meetingDetailsFetched( Meeting &aDetailedMeeting )
263 qDebug() << "Engine::meetingDetailsFetched( Meeting & )";
264 iWindowManager->showMeetingInfo( &aDetailedMeeting );
267 void Engine::errorHandler( int aCode, const QString &aAddInfo )
269 qDebug() << "Engine::ErrorHandler, aCode: " << aCode;
270 // inform UI about the problem
271 if( aCode >= 100 && aCode <= 110 )
272 qDebug() << "CommunicationManager signaled an error:" << aCode;
273 iWindowManager->error( ErrorMapper::codeToString( aCode, aAddInfo ) );
276 void Engine::currentRoomChanged( Room *aCurrentRoom )
278 qDebug() << "Engine::currentRoomChanged to " << aCurrentRoom->name();
279 QDateTime from( iWindowManager->weeklyView()->beginnigOfShownWeek() );
280 QDateTime to( from.addDays( 8 ) );
281 fetchMeetings( from, to, aCurrentRoom );
284 void Engine::fetchMeetings( const QDateTime &aFrom, const QDateTime &aUntil, const Room *aIn )
286 qDebug() << "Engine::fetchMeetings( const QDateTime &, const QDateTime &, const Room * )";
287 iCommunication->fetchMeetings( aFrom, aUntil, *aIn );
290 void Engine::shownWeekChanged( QDate aFrom )
292 qDebug() << "Engine::shownWeekChanged( QDate )";
293 QDateTime from( aFrom );
294 QDateTime to( aFrom.addDays( 7 ), QTime( 23, 59 ) );
295 qDebug() << "Engine::shownWeekChanged " << aFrom.toString( "d.m. h:mm" ) << " to " << to.toString( "d.m. h:mm" );
296 fetchMeetings( from, to, iWindowManager->weeklyView()->currentRoom() );
299 void Engine::changeModeOrdered( DeviceManager::OperationMode aMode )
301 qDebug() << "Engine::changeModeOrdered( DeviceManager::OperationMode )";
302 QString message = tr( "You are about to change operation mode to %1." )
303 .arg( iDevice->operationModeToString( aMode ) );
305 iWindowManager->showPasswordDialog( iConfiguration->adminPassword(), message );
308 void Engine::passwordEntered( PasswordDialog::PasswordStatus aPasswordStatus )
310 qDebug() << "Engine::passwordEntered( PasswordDialog::PasswordStatus )";
311 iWindowManager->closePasswordDialog();
313 switch ( aPasswordStatus )
315 case PasswordDialog::Correct :
317 iWindowManager->showProgressBar( "Changing current operation mode." );
318 connect( iWindowManager, SIGNAL( progressBarCancelled() ), this, SLOT( progressBarCancelled() ) );
319 connect( iDevice, SIGNAL( changingMode( const QString & ) ),
320 iWindowManager, SLOT( updateProgressBar( const QString & ) ) );
321 iDevice->changeMode( true );
324 case PasswordDialog::Incorrect :
326 iWindowManager->error( tr( "Incorrect password." ) );
327 iDevice->changeMode( false );
330 default : //case PasswordDialog::Canceled
332 iDevice->changeMode( false );
337 void Engine::progressBarCancelled()
339 qDebug() << "Engine::progressBarCancelled()";
340 //TODO: cancel the on-going event
341 iWindowManager->closeProgressBar();
342 iDevice->changeMode( false );