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 ) ),
41 this, SLOT( passwordEntered( PasswordDialog::PasswordStatus ) ) );
44 // initialize communication
45 iCommunication = new CommunicationManager( *(iConfiguration->connectionSettings()) );
46 connect( iCommunication, SIGNAL( error( int, CommunicationManager::CommunicationType ) ),
47 this, SLOT( errorHandler( int ) ) );
48 connect( iCommunication, SIGNAL( meetingsFetched( const QList<Meeting*>& ) ),
49 this, SLOT( meetingsFetched( const QList<Meeting*>& ) ) );
50 connect( iCommunication, SIGNAL( meetingDetailsFetched( Meeting& ) ),
51 this, SLOT( meetingDetailsFetched( Meeting& ) ) );
53 //initialize idle time counter
54 iIdleTimeCounter = new QTimer();
55 iIdleTimeCounter->setSingleShot( true );
56 iIdleTimeCounter->setInterval( IDLE_TIME_MULTIPLIER * iConfiguration->displaySettings()->screensaver() );
57 iIdleTimeCounter->start();
58 connect( iIdleTimeCounter, SIGNAL( timeout() ), iWindowManager, SLOT( showRoomStatus() ) );
60 // create application clock
62 connect( iClock, SIGNAL( tick( QDateTime ) ), this, SLOT( checkStatusOfAllRooms() ) );
63 connect( iClock, SIGNAL( tick( QDateTime ) ), iWindowManager, SLOT( distributeDateTimeInfo( QDateTime ) ) );
65 iAutoRefresh = new QTimer;
66 iAutoRefresh->setInterval( iConfiguration->connectionSettings()->refreshInterval() * 1000 );
67 iAutoRefresh->start();
68 connect( iAutoRefresh, SIGNAL( timeout() ), iAutoRefresh, SLOT( start() ) );
69 connect( iAutoRefresh, SIGNAL( timeout() ), this, SLOT( fetchMeetings() ) );
71 // create device manager
72 iDevice = new DeviceManager( iConfiguration->startupSettings() );
73 connect( iDevice, SIGNAL( error( int, const QString& ) ), this, SLOT( errorHandler( int, const QString& ) ) );
74 connect( iDevice, SIGNAL( changeModeOrdered( DeviceManager::OperationMode ) ),
75 this, SLOT( changeModeOrdered( DeviceManager::OperationMode ) ) );
76 iDevice->initDeviceManager();
78 if( iDevice->currentOperationMode() == DeviceManager::KioskMode )
79 iWindowManager->fullScreen();
81 QTimer::singleShot( 0, this, SLOT( fetchMeetings() ) );
83 // TODO: continue implementation
88 qDebug() << "Engine::~Engine()";
89 while ( !iMeetings.isEmpty() )
90 delete iMeetings.takeFirst();
91 iIdleTimeCounter->stop();
92 delete iIdleTimeCounter;
94 delete iWindowManager;
102 void Engine::closeApplication()
104 qDebug() << "Engine::closeApplication()";
105 // closes application after 1 second
106 QTimer::singleShot( 1000, QApplication::instance(), SLOT( quit() ) );
109 void Engine::observedEventDetected()
111 qDebug() << "Engine::observedEventDetected()";
112 iWindowManager->showWeeklyView();
113 // prepare to restart idle counter
114 if ( iIdleTimeCounter->isActive() )
116 iIdleTimeCounter->stop();
118 // (re)start idle counter
119 iIdleTimeCounter->start();
122 Room* Engine::defaultRoom()
124 qDebug() << "Engine::defaultRoom()";
125 return iConfiguration->defaultRoom();
128 void Engine::checkStatusOfAllRooms()
130 qDebug() << "Engine::checkStatusOfAllRooms()";
131 // iterate trough on the rooms
132 for ( int i = 0; i < iConfiguration->rooms().count(); i++ )
134 // and check the status
135 roomStatusInfoNeeded( iConfiguration->rooms().at( i ) );
139 int Engine::indexOfMeetingAt( Room *aRoom, QDateTime aAt )
141 qDebug() << "Engine::indexOfMeetingAt( Room *, QDateTime )";
142 for ( int i = 0; i < iMeetings.count(); i++ )
144 // exchange server ensures that there is only one meeting in a room at a specified time
145 if ( aRoom->equals( iMeetings.at( i )->room() )
146 && iMeetings.at( i )->startsAt() <= aAt
147 && 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() )
164 && iMeetings.at( i )->startsAt().date() == aAfter.date()
165 && iMeetings.at( i )->startsAt() > aAfter )
167 // if there was not any meeting find yet or the previously found is a later one then the (i)th
169 || iMeetings.at( min )->startsAt() > iMeetings.at( i )->startsAt() )
178 void Engine::roomStatusInfoNeeded( Room *aRoom )
180 qDebug() << "Engine::roomStatusInfoNeeded( Room * )";
186 int indexOfCurrentMeeting = indexOfMeetingAt( aRoom, iClock->datetime() );
187 int indexOfNextMeeting = indexOfMeetingAfter( aRoom, iClock->datetime() );
189 // if there is no meeting, then status is Free; otherwise Busy
190 Room::Status status = ( indexOfCurrentMeeting == -1 ) ? Room::FreeStatus : Room::BusyStatus;
191 // if room is Busy, then check end time, otherwise...
192 QTime until = ( status == Room::BusyStatus ) ? iMeetings.at( indexOfCurrentMeeting )->endsAt().time() :
193 // ...if there is meeting following on the same day then check end time, otherwise end is the of the working day
194 (( indexOfNextMeeting != -1 ) ? iMeetings.at( indexOfNextMeeting )->startsAt().time() : Engine::endOfTheDay );
196 //currently works only for deafult room
197 if( aRoom->equals( *(defaultRoom() ) ) )
198 iWindowManager->roomStatusChanged( aRoom, status, until );
201 void Engine::fetchMeetings()
203 Room *room = defaultRoom();
204 qDebug() << "Engine::fetchMeetings for " << room->name();
205 fetchMeetings( iClock->datetime(), iClock->datetime().addDays( 7 ), room );
208 void Engine::fetchMeetingDetails( Meeting *aMeeting )
210 qDebug() << "Engine::fetchMeetingDetails( Meeting* )";
211 iWindowManager->showProgressBar( tr("Please Wait"), true );
212 iWindowManager->updateProgressBar( tr("Fetching Meeting Details...") );
213 connect( iWindowManager,
214 SIGNAL( progressBarCancelled() ),
216 SLOT( fetchMeetingDetailsCancelled() )
218 iCommunication->fetchMeetingDetails( *aMeeting );
221 bool Engine::isMeetingInList( const QList<Meeting*> &aList, const Meeting *aMeeting )
223 qDebug() << "Engine::isMeetingInList( const QList<Meeting*> &, const Meeting * )";
224 for ( int i = 0; i < aList.count(); i++ )
226 if ( aMeeting->equals( *(aList.at( i )) ) )
234 void Engine::meetingsFetched( const QList<Meeting*> &aMeetings )
236 qDebug() << "Engine::meetingsFetched( const QList<Meeting*> & )";
237 if( iCommunicationFailed )
239 iCommunicationFailed = false;
240 //iWindowManager->connectionEstablished();
243 // check if there is any new meeting in the list came from the server -> added
244 for ( int i = 0; i < aMeetings.count(); i++ )
246 // if the (i)th meeting is not in the local meeting list
247 if ( !isMeetingInList( iMeetings, aMeetings.at( i ) ) )
249 // add to the local database =)
250 Meeting* m = new Meeting( *(aMeetings.at( i )) );
251 iMeetings.append( m );
252 // and signal the changes
253 iWindowManager->insertMeeting( m );
257 // check if there is any meeting NOT in the list came from the server -> deleted
258 for ( int i = 0; i < iMeetings.count(); i++ )
260 // if the (i)th meeting is in the local but NOT in the server's meeting list
261 if ( !isMeetingInList( aMeetings, iMeetings.at( i ) ) )
263 Meeting* m = iMeetings.takeAt( i );
264 // signal the changes
265 iWindowManager->deleteMeeting( m );
266 // delete the meeting from the local list
271 // refresh room status info
272 roomStatusInfoNeeded( defaultRoom() );
275 void Engine::meetingDetailsFetched( Meeting &aDetailedMeeting )
277 qDebug() << "Engine::meetingDetailsFetched( Meeting & )";
278 iWindowManager->closeProgressBar();
279 if( iCommunicationFailed )
281 iCommunicationFailed = false;
282 iWindowManager->connectionEstablished();
284 iWindowManager->showMeetingInfo( &aDetailedMeeting );
287 void Engine::errorHandler( int aCode, const QString &aAddInfo )
289 qDebug() << "Engine::ErrorHandler, aCode: " << aCode;
290 // inform UI about the problem
291 if( aCode >= 100 && aCode <= 150 ) { //communication errors
293 //we don't want these to close operation changing
294 qDebug() << "CommunicationManager signaled an error:" << aCode;
295 if( !iCommunicationFailed )
297 // Only inform window manager when first error appears
298 iCommunicationFailed = true;
299 //iWindowManager->connectionLost();
302 iWindowManager->closeProgressBar();
304 iWindowManager->error( ErrorMapper::codeToString( aCode, aAddInfo ) );
307 void Engine::currentRoomChanged( Room *aCurrentRoom )
309 qDebug() << "Engine::currentRoomChanged to " << aCurrentRoom->name();
310 QDateTime from( iWindowManager->weeklyView()->beginnigOfShownWeek() );
311 QDateTime to( from.addDays( 8 ) );
312 fetchMeetings( from, to, aCurrentRoom );
315 void Engine::fetchMeetings( const QDateTime &aFrom, const QDateTime &aUntil, const Room *aIn )
317 qDebug() << "Engine::fetchMeetings( const QDateTime &, const QDateTime &, const Room * )";
318 iCommunication->fetchMeetings( aFrom, aUntil, *aIn );
321 void Engine::shownWeekChanged( QDate aFrom )
323 qDebug() << "Engine::shownWeekChanged( QDate )";
324 QDateTime from( aFrom );
325 QDateTime to( aFrom.addDays( 7 ), QTime( 23, 59 ) );
326 qDebug() << "Engine::shownWeekChanged " << aFrom.toString( "d.m. h:mm" ) << " to " << to.toString( "d.m. h:mm" );
327 fetchMeetings( from, to, iWindowManager->weeklyView()->currentRoom() );
330 void Engine::changeModeOrdered( DeviceManager::OperationMode aMode )
332 qDebug() << "Engine::changeModeOrdered( DeviceManager::OperationMode )";
333 QString message = tr( "You are about to change operation mode to %1." )
334 .arg( iDevice->operationModeToString( aMode ) );
336 iWindowManager->showPasswordDialog( iConfiguration->adminPassword(), message );
339 void Engine::passwordEntered( PasswordDialog::PasswordStatus aPasswordStatus )
341 qDebug() << "Engine::passwordEntered( PasswordDialog::PasswordStatus )";
342 iWindowManager->closePasswordDialog();
344 switch ( aPasswordStatus )
346 case PasswordDialog::Correct :
348 iAutoRefresh->stop(); //we stop the metting updating
349 iWindowManager->showProgressBar( "Changing current operation mode." );
350 connect( iDevice, SIGNAL( changingMode( const QString & ) ),
351 iWindowManager, SLOT( updateProgressBar( const QString & ) ) );
352 connect( iDevice, SIGNAL( changeModeFailed() ), this, SLOT( changeModeFailed() ) );
353 iDevice->changeMode( true );
356 case PasswordDialog::Incorrect :
358 iWindowManager->error( tr( "Incorrect password." ) );
359 iDevice->changeMode( false );
362 default : //case PasswordDialog::Canceled
364 iDevice->changeMode( false );
369 void Engine::changeModeFailed()
371 qDebug() << "Engine::changeModeFailed()";
372 iWindowManager->closeProgressBar();
373 iAutoRefresh->start(); //we start the metting updating
376 void Engine::fetchMeetingDetailsCancelled()
378 iCommunication->cancelFetchMeetingDetails();
379 iWindowManager->closeProgressBar();