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 iWindowManager->showProgressBar( tr("Please Wait"), true );
211 iWindowManager->updateProgressBar( tr("Fetching Meeting Details...") );
212 connect( iWindowManager,
213 SIGNAL( progressBarCancelled() ),
215 SLOT( fetchMeetingDetailsCancelled() )
217 iCommunication->fetchMeetingDetails( *aMeeting );
220 bool Engine::isMeetingInList( const QList<Meeting*> &aList, const Meeting *aMeeting )
222 qDebug() << "Engine::isMeetingInList( const QList<Meeting*> &, const Meeting * )";
223 for ( int i = 0; i < aList.count(); i++ )
225 if ( aMeeting->equals( *(aList.at( i )) ) )
233 void Engine::meetingsFetched( const QList<Meeting*> &aMeetings )
235 qDebug() << "Engine::meetingsFetched( const QList<Meeting*> & )";
236 // check if there is any new meeting in the list came from the server -> added
237 for ( int i = 0; i < aMeetings.count(); i++ )
239 // if the (i)th meeting is not in the local meeting list
240 if ( !isMeetingInList( iMeetings, aMeetings.at( i ) ) )
242 // add to the local database =)
243 Meeting* m = new Meeting( *(aMeetings.at( i )) );
244 iMeetings.append( m );
245 // and signal the changes
246 iWindowManager->insertMeeting( m );
250 // check if there is any meeting NOT in the list came from the server -> deleted
251 for ( int i = 0; i < iMeetings.count(); i++ )
253 // if the (i)th meeting is in the local but NOT in the server's meeting list
254 if ( !isMeetingInList( aMeetings, iMeetings.at( i ) ) )
256 Meeting* m = iMeetings.takeAt( i );
257 // signal the changes
258 iWindowManager->deleteMeeting( m );
259 // delete the meeting from the local list
264 // refresh room status info
265 roomStatusInfoNeeded( defaultRoom() );
268 void Engine::meetingDetailsFetched( Meeting &aDetailedMeeting )
270 qDebug() << "Engine::meetingDetailsFetched( Meeting & )";
271 iWindowManager->closeProgressBar();
272 iWindowManager->showMeetingInfo( &aDetailedMeeting );
275 void Engine::errorHandler( int aCode, const QString &aAddInfo )
277 qDebug() << "Engine::ErrorHandler, aCode: " << aCode;
278 // inform UI about the problem
279 if( aCode >= 100 && aCode <= 150 )
280 qDebug() << "CommunicationManager signaled an error:" << aCode;
281 iWindowManager->closeProgressBar();
282 iWindowManager->error( ErrorMapper::codeToString( aCode, aAddInfo ) );
285 void Engine::currentRoomChanged( Room *aCurrentRoom )
287 qDebug() << "Engine::currentRoomChanged to " << aCurrentRoom->name();
288 QDateTime from( iWindowManager->weeklyView()->beginnigOfShownWeek() );
289 QDateTime to( from.addDays( 8 ) );
290 fetchMeetings( from, to, aCurrentRoom );
293 void Engine::fetchMeetings( const QDateTime &aFrom, const QDateTime &aUntil, const Room *aIn )
295 qDebug() << "Engine::fetchMeetings( const QDateTime &, const QDateTime &, const Room * )";
296 iCommunication->fetchMeetings( aFrom, aUntil, *aIn );
299 void Engine::shownWeekChanged( QDate aFrom )
301 qDebug() << "Engine::shownWeekChanged( QDate )";
302 QDateTime from( aFrom );
303 QDateTime to( aFrom.addDays( 7 ), QTime( 23, 59 ) );
304 qDebug() << "Engine::shownWeekChanged " << aFrom.toString( "d.m. h:mm" ) << " to " << to.toString( "d.m. h:mm" );
305 fetchMeetings( from, to, iWindowManager->weeklyView()->currentRoom() );
308 void Engine::changeModeOrdered( DeviceManager::OperationMode aMode )
310 qDebug() << "Engine::changeModeOrdered( DeviceManager::OperationMode )";
311 QString message = tr( "You are about to change operation mode to %1." )
312 .arg( iDevice->operationModeToString( aMode ) );
314 iWindowManager->showPasswordDialog( iConfiguration->adminPassword(), message );
317 void Engine::passwordEntered( PasswordDialog::PasswordStatus aPasswordStatus )
319 qDebug() << "Engine::passwordEntered( PasswordDialog::PasswordStatus )";
320 iWindowManager->closePasswordDialog();
322 switch ( aPasswordStatus )
324 case PasswordDialog::Correct :
326 iWindowManager->showProgressBar( "Changing current operation mode." );
327 connect( iWindowManager, SIGNAL( progressBarCancelled() ), this, SLOT( progressBarCancelled() ) );
328 connect( iDevice, SIGNAL( changingMode( const QString & ) ),
329 iWindowManager, SLOT( updateProgressBar( const QString & ) ) );
330 iDevice->changeMode( true );
333 case PasswordDialog::Incorrect :
335 iWindowManager->error( tr( "Incorrect password." ) );
336 iDevice->changeMode( false );
339 default : //case PasswordDialog::Canceled
341 iDevice->changeMode( false );
346 void Engine::progressBarCancelled()
348 qDebug() << "Engine::progressBarCancelled()";
349 //TODO: cancel the on-going event
350 iWindowManager->closeProgressBar();
351 iDevice->changeMode( false );
354 void Engine::fetchMeetingDetailsCancelled()
356 iCommunication->cancelFetchMeetingDetails();
357 iWindowManager->closeProgressBar();