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 // if reading of configuration fails, signal that initialization failed
25 if ( iConfiguration == 0 )
27 QTimer::singleShot( 0, this, SLOT( closeApplication() ) );
31 //initialize window manager
32 iWindowManager = new WindowManager( iConfiguration );
33 connect( iWindowManager, SIGNAL( roomStatusInfoNeeded( Room * ) ), this, SLOT( roomStatusInfoNeeded( Room * ) ) );
34 connect( iWindowManager, SIGNAL( observedEventDetected() ), this, SLOT( observedEventDetected() ) );
35 connect( iWindowManager, SIGNAL( meetingActivated( Meeting * ) ), this, SLOT( fetchMeetingDetails( Meeting * ) ) );
36 connect( iWindowManager, SIGNAL( currentRoomChanged( Room * ) ), this, SLOT( currentRoomChanged( Room * ) ) );
37 connect( iWindowManager, SIGNAL( shownWeekChanged( QDate ) ), this, SLOT( shownWeekChanged( QDate ) ) );
38 connect( iWindowManager, SIGNAL( passwordEntered( PasswordDialog::PasswordStatus ) ),
39 this, SLOT( passwordEntered( PasswordDialog::PasswordStatus ) ) );
40 connect( iWindowManager, SIGNAL( progressBarCancelled() ), this, SLOT( progressBarCancelled() ) );
42 // initialize communication
43 iCommunication = new CommunicationManager( *(iConfiguration->connectionSettings()) );
44 connect( iCommunication, SIGNAL( error( int, CommunicationManager::CommunicationType ) ),
45 this, SLOT( errorHandler( int ) ) );
46 connect( iCommunication, SIGNAL( meetingsFetched( const QList<Meeting*>& ) ),
47 this, SLOT( meetingsFetched( const QList<Meeting*>& ) ) );
48 connect( iCommunication, SIGNAL( meetingDetailsFetched( Meeting& ) ),
49 this, SLOT( meetingDetailsFetched( Meeting& ) ) );
51 //initialize idle time counter
52 iIdleTimeCounter = new QTimer();
53 iIdleTimeCounter->setSingleShot( true );
54 iIdleTimeCounter->setInterval( IDLE_TIME_MULTIPLIER * iConfiguration->displaySettings()->screensaver() );
55 iIdleTimeCounter->start();
56 connect( iIdleTimeCounter, SIGNAL( timeout() ), iWindowManager, SLOT( showRoomStatus() ) );
58 // create application clock
60 connect( iClock, SIGNAL( tick( QDateTime ) ), this, SLOT( checkStatusOfAllRooms() ) );
61 connect( iClock, SIGNAL( tick( QDateTime ) ), iWindowManager, SLOT( distributeDateTimeInfo( QDateTime ) ) );
63 iAutoRefresh = new QTimer;
64 iAutoRefresh->setInterval( iConfiguration->connectionSettings()->refreshInterval() * 1000 );
65 iAutoRefresh->start();
66 connect( iAutoRefresh, SIGNAL( timeout() ), iAutoRefresh, SLOT( start() ) );
67 connect( iAutoRefresh, SIGNAL( timeout() ), this, SLOT( fetchMeetings() ) );
69 // create device manager
70 iDevice = new DeviceManager( iConfiguration->startupSettings() );
71 connect( iDevice, SIGNAL( error( int, const QString& ) ), this, SLOT( errorHandler( int, const QString& ) ) );
72 connect( iDevice, SIGNAL( changeModeOrdered( DeviceManager::OperationMode ) ),
73 this, SLOT( changeModeOrdered( DeviceManager::OperationMode ) ) );
74 iDevice->initDeviceManager();
76 if( iDevice->currentOperationMode() == DeviceManager::KioskMode )
77 iWindowManager->fullScreen();
79 QTimer::singleShot( 0, this, SLOT( fetchMeetings() ) );
81 // TODO: continue implementation
86 while ( !iMeetings.isEmpty() )
87 delete iMeetings.takeFirst();
88 iIdleTimeCounter->stop();
89 delete iIdleTimeCounter;
91 delete iWindowManager;
99 void Engine::closeApplication()
101 qDebug() << "Engine::closeApplication()";
102 // closes application after 1 second
103 QTimer::singleShot( 1000, QApplication::instance(), SLOT( quit() ) );
106 void Engine::observedEventDetected()
108 iWindowManager->showWeeklyView();
109 // prepare to restart idle counter
110 if ( iIdleTimeCounter->isActive() )
112 iIdleTimeCounter->stop();
114 // (re)start idle counter
115 iIdleTimeCounter->start();
118 Room* Engine::defaultRoom()
120 return iConfiguration->defaultRoom();
123 void Engine::checkStatusOfAllRooms()
125 // iterate trough on the rooms
126 for ( int i = 0; i < iConfiguration->rooms().count(); i++ )
128 // and check the status
129 roomStatusInfoNeeded( iConfiguration->rooms().at( i ) );
133 int Engine::indexOfMeetingAt( Room *aRoom, QDateTime aAt )
135 for ( int i = 0; i < iMeetings.count(); i++ )
137 // exchange server ensures that there is only one meeting in a room at a specified time
138 if ( aRoom->equals( iMeetings.at( i )->room() )
139 && iMeetings.at( i )->startsAt() <= aAt
140 && iMeetings.at( i )->endsAt() >= aAt )
148 int Engine::indexOfMeetingAfter( Room *aRoom, QDateTime aAfter )
150 // seeks for the next meeting on the SAME DAY
152 for ( int i = 0; i < iMeetings.count(); i++ )
154 // if the meeting is in the same room, on the same day but after the specified time
155 if ( aRoom->equals( iMeetings.at( i )->room() )
156 && iMeetings.at( i )->startsAt().date() == aAfter.date()
157 && iMeetings.at( i )->startsAt() > aAfter )
159 // if there was not any meeting find yet or the previously found is a later one then the (i)th
161 || iMeetings.at( min )->startsAt() > iMeetings.at( i )->startsAt() )
170 void Engine::roomStatusInfoNeeded( Room *aRoom )
177 int indexOfCurrentMeeting = indexOfMeetingAt( aRoom, iClock->datetime() );
178 int indexOfNextMeeting = indexOfMeetingAfter( aRoom, iClock->datetime() );
180 // if there is no meeting, then status is Free; otherwise Busy
181 Room::Status status = ( indexOfCurrentMeeting == -1 ) ? Room::FreeStatus : Room::BusyStatus;
182 // if room is Busy, then check end time, otherwise...
183 QTime until = ( status == Room::BusyStatus ) ? iMeetings.at( indexOfCurrentMeeting )->endsAt().time() :
184 // ...if there is meeting following on the same day then check end time, otherwise end is the of the working day
185 (( indexOfNextMeeting != -1 ) ? iMeetings.at( indexOfNextMeeting )->startsAt().time() : Engine::endOfTheDay );
187 //currently works only for deafult room
188 if( aRoom->equals( *(defaultRoom() ) ) )
189 iWindowManager->roomStatusChanged( aRoom, status, until );
192 void Engine::fetchMeetings()
194 Room *room = defaultRoom();
195 qDebug() << "Engine::fetchMeetings for " << room->name();
196 fetchMeetings( iClock->datetime(), iClock->datetime().addDays( 7 ), room );
199 void Engine::fetchMeetingDetails( Meeting *aMeeting )
201 iCommunication->fetchMeetingDetails( *aMeeting );
204 bool Engine::isMeetingInList( const QList<Meeting*> &aList, const Meeting *aMeeting )
206 for ( int i = 0; i < aList.count(); i++ )
208 if ( aMeeting->equals( *(aList.at( i )) ) )
216 void Engine::meetingsFetched( const QList<Meeting*> &aMeetings )
218 // check if there is any new meeting in the list came from the server -> added
219 for ( int i = 0; i < aMeetings.count(); i++ )
221 // if the (i)th meeting is not in the local meeting list
222 if ( !isMeetingInList( iMeetings, aMeetings.at( i ) ) )
224 // add to the local database =)
225 Meeting* m = new Meeting( *(aMeetings.at( i )) );
226 iMeetings.append( m );
227 // and signal the changes
228 iWindowManager->insertMeeting( m );
232 // check if there is any meeting NOT in the list came from the server -> deleted
233 for ( int i = 0; i < iMeetings.count(); i++ )
235 // if the (i)th meeting is in the local but NOT in the server's meeting list
236 if ( !isMeetingInList( aMeetings, iMeetings.at( i ) ) )
238 Meeting* m = iMeetings.takeAt( i );
239 // signal the changes
240 iWindowManager->deleteMeeting( m );
241 // delete the meeting from the local list
246 // refresh room status info
247 roomStatusInfoNeeded( defaultRoom() );
250 void Engine::meetingDetailsFetched( Meeting &aDetailedMeeting )
252 iWindowManager->showMeetingInfo( &aDetailedMeeting );
255 void Engine::errorHandler( int aCode, const QString &aAddInfo )
257 qDebug() << "Engine::ErrorHandler, aCode: " << aCode;
258 // inform UI about the problem
259 if( aCode >= 100 && aCode <= 110 )
260 qDebug() << "CommunicationManager signaled an error:" << aCode;
261 iWindowManager->error( ErrorMapper::codeToString( aCode, aAddInfo ) );
264 void Engine::currentRoomChanged( Room *aCurrentRoom )
266 qDebug() << "Engine::currentRoomChanged to " << aCurrentRoom->name();
267 QDateTime from( iWindowManager->weeklyView()->beginnigOfShownWeek() );
268 QDateTime to( from.addDays( 8 ) );
269 fetchMeetings( from, to, aCurrentRoom );
272 void Engine::fetchMeetings( const QDateTime &aFrom, const QDateTime &aUntil, const Room *aIn )
274 qDebug() << "Engine::fetchMeetings";
275 iCommunication->fetchMeetings( aFrom, aUntil, *aIn );
278 void Engine::shownWeekChanged( QDate aFrom )
280 QDateTime from( aFrom );
281 QDateTime to( aFrom.addDays( 7 ), QTime( 23, 59 ) );
282 qDebug() << "Engine::shownWeekChanged " << aFrom.toString( "d.m. h:mm" ) << " to " << to.toString( "d.m. h:mm" );
283 fetchMeetings( from, to, iWindowManager->weeklyView()->currentRoom() );
286 void Engine::changeModeOrdered( DeviceManager::OperationMode aMode )
288 QString message = tr( "You are about to change operation mode to %1." )
289 .arg( iDevice->operationModeToString( aMode ) );
291 iWindowManager->showPasswordDialog( iConfiguration->adminPassword(), message );
294 void Engine::passwordEntered( PasswordDialog::PasswordStatus aPasswordStatus )
296 qDebug() << "Engine::passwordEntered( PasswordDialog::PasswordStatus )";
297 iWindowManager->closePasswordDialog();
299 switch ( aPasswordStatus )
301 case PasswordDialog::Correct :
303 iWindowManager->showProgressBar( "Changing current operation mode." );
306 case PasswordDialog::Incorrect :
308 iWindowManager->error( tr( "Incorrect password." ) );
309 iDevice->handleKeyPresses( true );
312 default : //case PasswordDialog::Canceled
314 iDevice->handleKeyPresses( true );
319 void Engine::progressBarCancelled()
321 //TODO: cancel the on-going event
322 iWindowManager->closeProgressBar();
323 iDevice->handleKeyPresses( true );