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 "UIManager.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
21 // Macro to help deleting objects. This could be global.
22 #define QT_DELETE(X) \
31 iClock( 0 ), iConfiguration( 0 ), iCommunication( 0 )
33 qDebug() << "Engine::Engine()";
40 //initialize idle time counter
41 iIdleTimeCounter = new QTimer();
42 iIdleTimeCounter->setSingleShot( true );
43 // iIdleTimeCounter->setInterval( IDLE_TIME_MULTIPLIER * iConfiguration->displaySettings()->screensaver() );
44 iIdleTimeCounter->setInterval( 10000 );
45 iIdleTimeCounter->start();
47 // create application clock
49 connect( iClock, SIGNAL( tick( QDateTime ) ), this, SLOT( checkStatusOfAllRooms() ) );
50 // connect( iClock, SIGNAL( tick( QDateTime ) ), iWindowManager, SLOT( distributeDateTimeInfo( QDateTime ) ) );
52 // Create auto refresh timer
53 iAutoRefresh = new QTimer;
54 iAutoRefresh->setInterval( iConfiguration->connectionSettings()->refreshInterval() * 1000 );
55 iAutoRefresh->start();
56 connect( iAutoRefresh, SIGNAL( timeout() ), iAutoRefresh, SLOT( start() ) );
57 connect( iAutoRefresh, SIGNAL( timeout() ), this, SLOT( fetchMeetings() ) );
59 if( iDevice->currentOperationMode() == DeviceManager::KioskMode )
61 iWindowManager->setFullscreen();
66 QTimer::singleShot( 0, this, SLOT( fetchMeetings() ) );
68 // TODO: continue implementation
73 qDebug() << "Engine::~Engine()";
74 while ( !iMeetings.isEmpty() )
75 delete iMeetings.takeFirst();
77 if ( iIdleTimeCounter != 0 )
79 iIdleTimeCounter->stop();
80 delete iIdleTimeCounter;
86 QT_DELETE( iUIManager );
87 QT_DELETE( iWindowManager );
90 void Engine::closeApplication()
92 qDebug() << "Engine::closeApplication()";
93 // closes application after 1 second
94 QTimer::singleShot( 1000, QApplication::instance(), SLOT( quit() ) );
97 Room* Engine::defaultRoom()
99 qDebug() << "Engine::defaultRoom()";
100 return iConfiguration->defaultRoom();
103 void Engine::checkStatusOfAllRooms()
105 // qDebug() << "Engine::checkStatusOfAllRooms()";
106 // iterate trough on the rooms
107 for ( int i = 0; i < iConfiguration->rooms().count(); i++ )
109 // and check the status
110 roomStatusInfoNeeded( iConfiguration->rooms().at( i ) );
114 int Engine::indexOfMeetingAt( Room *aRoom, QDateTime aAt )
116 // qDebug() << "Engine::indexOfMeetingAt( Room *, QDateTime )";
117 for ( int i = 0; i < iMeetings.count(); i++ )
119 // exchange server ensures that there is only one meeting in a room at a specified time
120 if ( aRoom->equals( iMeetings.at( i )->room() )
121 && iMeetings.at( i )->startsAt() <= aAt
122 && iMeetings.at( i )->endsAt() >= aAt )
130 int Engine::indexOfMeetingAfter( Room *aRoom, QDateTime aAfter )
132 // qDebug() << "Engine::indexOfMeetingAfter( Room *, QDateTime )";
133 // seeks for the next meeting on the SAME DAY
135 for ( int i = 0; i < iMeetings.count(); i++ )
137 // if the meeting is in the same room, on the same day but after the specified time
138 if ( aRoom->equals( iMeetings.at( i )->room() )
139 && iMeetings.at( i )->startsAt().date() == aAfter.date()
140 && iMeetings.at( i )->startsAt() > aAfter )
142 // if there was not any meeting find yet or the previously found is a later one then the (i)th
144 || iMeetings.at( min )->startsAt() > iMeetings.at( i )->startsAt() )
153 void Engine::roomStatusInfoNeeded( Room *aRoom )
155 // qDebug() << "Engine::roomStatusInfoNeeded( Room * )";
161 int indexOfCurrentMeeting = indexOfMeetingAt( aRoom, iClock->datetime() );
162 int indexOfNextMeeting = indexOfMeetingAfter( aRoom, iClock->datetime() );
164 // if there is no meeting, then status is Free; otherwise Busy
165 Room::Status status = ( indexOfCurrentMeeting == -1 ) ? Room::FreeStatus : Room::BusyStatus;
166 // if room is Busy, then check end time, otherwise...
167 QTime until = ( status == Room::BusyStatus ) ? iMeetings.at( indexOfCurrentMeeting )->endsAt().time() :
168 // ...if there is meeting following on the same day then check end time, otherwise end is the of the working day
169 (( indexOfNextMeeting != -1 ) ? iMeetings.at( indexOfNextMeeting )->startsAt().time() : Engine::endOfTheDay );
171 //currently works only for deafult room
172 // if( aRoom->equals( *(defaultRoom() ) ) )
173 // iWindowManager->roomStatusChanged( aRoom, status, until );
176 void Engine::fetchMeetings()
178 Room *room = defaultRoom();
179 qDebug() << "Engine::fetchMeetings for " << room->name();
180 fetchMeetings( iClock->datetime(), iClock->datetime().addDays( 7 ), room );
183 void Engine::fetchMeetingDetails( Meeting *aMeeting )
185 qDebug() << "Engine::fetchMeetingDetails( Meeting* )";
186 // iWindowManager->showProgressBar( tr("Please Wait"), true );
187 // iWindowManager->updateProgressBar( tr("Fetching Meeting Details...") );
188 /* connect( iWindowManager,
189 SIGNAL( progressBarCancelled() ),
191 SLOT( fetchMeetingDetailsCancelled() )
193 iCommunication->fetchMeetingDetails( *aMeeting );
196 bool Engine::isMeetingInList( const QList<Meeting*> &aList, const Meeting *aMeeting )
198 qDebug() << "Engine::isMeetingInList( const QList<Meeting*> &, const Meeting * )";
199 for ( int i = 0; i < aList.count(); i++ )
201 if ( aMeeting->equals( *(aList.at( i )) ) )
209 void Engine::meetingsFetched( const QList<Meeting*> &aMeetings )
211 qDebug() << "Engine::meetingsFetched( const QList<Meeting*> & )";
212 // check if there is any new meeting in the list came from the server -> added
213 for ( int i = 0; i < aMeetings.count(); i++ )
215 // if the (i)th meeting is not in the local meeting list
216 if ( !isMeetingInList( iMeetings, aMeetings.at( i ) ) )
218 // add to the local database =)
219 Meeting* m = new Meeting( *(aMeetings.at( i )) );
220 iMeetings.append( m );
221 // and signal the changes
222 // iWeeklyView->insertMeeting( m );
226 // check if there is any meeting NOT in the list came from the server -> deleted
227 for ( int i = 0; i < iMeetings.count(); i++ )
229 // if the (i)th meeting is in the local but NOT in the server's meeting list
230 if ( !isMeetingInList( aMeetings, iMeetings.at( i ) ) )
232 Meeting* m = iMeetings.takeAt( i );
233 // signal the changes
234 // iWeeklyView->deleteMeeting( m );
235 // delete the meeting from the local list
240 // refresh room status info
241 roomStatusInfoNeeded( defaultRoom() );
244 void Engine::errorHandler( int aCode, const QString &aAddInfo )
246 qDebug() << "Engine::ErrorHandler, aCode: " << aCode;
247 // inform UI about the problem
248 if( aCode >= 100 && aCode <= 150 )
249 qDebug() << "CommunicationManager signaled an error:" << aCode;
250 // iWindowManager->closeProgressBar();
251 // iWindowManager->error( ErrorMapper::codeToString( aCode, aAddInfo ) );
254 void Engine::fetchMeetings( const QDateTime &aFrom, const QDateTime &aUntil, const Room *aIn )
256 qDebug() << "Engine::fetchMeetings( const QDateTime &, const QDateTime &, const Room * )";
257 iCommunication->fetchMeetings( aFrom, aUntil, *aIn );
260 void Engine::shownWeekChanged( QDate aFrom )
262 qDebug() << "Engine::shownWeekChanged( QDate )";
263 QDateTime from( aFrom );
264 QDateTime to( aFrom.addDays( 7 ), QTime( 23, 59 ) );
265 qDebug() << "Engine::shownWeekChanged " << aFrom.toString( "d.m. h:mm" ) << " to " << to.toString( "d.m. h:mm" );
266 // fetchMeetings( from, to, iWindowManager->weeklyView()->currentRoom() );
269 void Engine::passwordEntered( PasswordDialog::PasswordStatus aPasswordStatus )
271 qDebug() << "Engine::passwordEntered( PasswordDialog::PasswordStatus )";
272 // iWindowManager->closePasswordDialog();
274 switch ( aPasswordStatus )
276 case PasswordDialog::Correct :
278 connect( iDevice, SIGNAL( changingModeFailed() ), this, SLOT( progressBarCancelled() ) );
279 iDevice->changeMode( true );
282 case PasswordDialog::Incorrect :
284 // iWindowManager->error( tr( "Incorrect password." ) );
285 iDevice->changeMode( false );
288 default : //case PasswordDialog::Canceled
290 iDevice->changeMode( false );
295 void Engine::progressBarCancelled()
297 qDebug() << "Engine::progressBarCancelled()";
298 iDevice->changeMode( false );
301 void Engine::initUserInterface()
303 qDebug() << "[Engine::initUserInterface] <Invoked>";
305 // Initialize the window manager and connect what ever signals can be connected
306 iWindowManager = new WindowManager;
307 // Create the UIManager which internally handles most of the UI actions
308 iUIManager = new UIManager( this, iWindowManager );
310 connect( iWindowManager, SIGNAL( eventDetected() ), this, SLOT( handleViewEvent() ) );
311 connect( iWindowManager, SIGNAL( previousViewRestored() ), iUIManager, SLOT( previousViewRestored() ) );
312 connect( iWindowManager, SIGNAL( dialogActivated() ), this, SLOT( dialogActivated() ) );
313 connect( iWindowManager, SIGNAL( dialogDeactivated() ), this, SLOT( dialogDeactivated() ) );
316 iWindowManager->setWindowState( Qt::WindowMaximized );
317 iWindowManager->show();
318 iUIManager->showMainView();
320 qDebug() << "[Engine::initUserInterface] <Finished>";
323 void Engine::handleViewEvent()
325 if ( iIdleTimeCounter != 0 )
327 // Restart the idle time counter when view event is received
328 iIdleTimeCounter->stop();
329 iIdleTimeCounter->start();
333 void Engine::initConfiguration()
335 iConfiguration = Configuration::instance();
336 if ( iConfiguration == 0 )
338 QTimer::singleShot( 0, this, SLOT( closeApplication() ) );
342 void Engine::connectSignals()
344 // Connect engine objects signals to UIManager
345 connect( iClock, SIGNAL( tick( QDateTime ) ), iUIManager, SLOT( updateTime( QDateTime ) ) );
346 connect( iIdleTimeCounter, SIGNAL( timeout() ) , iUIManager, SLOT( roomStatusIndicatorRequested() ) );
348 iUIManager->connectDeviceManager( iDevice );
349 iUIManager->connectCommunicationManager( iCommunication );
352 void Engine::initCommunication()
354 // initialize communication
355 iCommunication = new CommunicationManager( *(iConfiguration->connectionSettings()) );
356 connect( iCommunication, SIGNAL( error( int, CommunicationManager::CommunicationType ) ),
357 this, SLOT( errorHandler( int ) ) );
358 connect( iCommunication, SIGNAL( meetingsFetched( const QList<Meeting*>& ) ),
359 this, SLOT( meetingsFetched( const QList<Meeting*>& ) ) );
362 void Engine::initDevice()
364 // create device manager
365 iDevice = new DeviceManager( iConfiguration->startupSettings() );
366 connect( iDevice, SIGNAL( error( int, const QString& ) ), this, SLOT( errorHandler( int, const QString& ) ) );
367 iDevice->initDeviceManager();
370 void Engine::dialogActivated()
372 if ( iIdleTimeCounter != 0 )
374 iIdleTimeCounter->stop();
378 void Engine::dialogDeactivated()
380 if ( iIdleTimeCounter != 0 )
382 iIdleTimeCounter->start();
386 void Engine::previousViewRestored()
388 if ( iIdleTimeCounter != 0 )
390 iIdleTimeCounter->start();
394 void Engine::fetchMeetingDetailsCancelled()
396 iCommunication->cancelFetchMeetingDetails();
397 // iWindowManager->closeProgressBar();
400 void Engine::stopIdleTimeCounter()
402 if ( iIdleTimeCounter != 0 )
404 iIdleTimeCounter->stop();
408 void Engine::startIdleTimeCounter()
410 if ( iIdleTimeCounter != 0 )
412 iIdleTimeCounter->start();