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 ) ) );
39 // initialize communication
40 iCommunication = new CommunicationManager( *(iConfiguration->connectionSettings()) );
41 connect( iCommunication, SIGNAL( error( int, CommunicationManager::CommunicationType ) ),
42 this, SLOT( errorHandler( int ) ) );
43 connect( iCommunication, SIGNAL( meetingsFetched( const QList<Meeting*>& ) ),
44 this, SLOT( meetingsFetched( const QList<Meeting*>& ) ) );
45 connect( iCommunication, SIGNAL( meetingDetailsFetched( Meeting& ) ),
46 this, SLOT( meetingDetailsFetched( Meeting& ) ) );
48 //initialize idle time counter
49 iIdleTimeCounter = new QTimer();
50 iIdleTimeCounter->setSingleShot( true );
51 iIdleTimeCounter->setInterval( IDLE_TIME_MULTIPLIER * iConfiguration->displaySettings()->screensaver() );
52 iIdleTimeCounter->start();
53 connect( iIdleTimeCounter, SIGNAL( timeout() ), iWindowManager, SLOT( showRoomStatus() ) );
55 // create application clock
57 connect( iClock, SIGNAL( tick( QDateTime ) ), this, SLOT( checkStatusOfAllRooms() ) );
58 connect( iClock, SIGNAL( tick( QDateTime ) ), iWindowManager, SLOT( distributeDateTimeInfo( QDateTime ) ) );
60 iAutoRefresh = new QTimer;
61 iAutoRefresh->setInterval( iConfiguration->connectionSettings()->refreshInterval() * 1000 );
62 iAutoRefresh->start();
63 connect( iAutoRefresh, SIGNAL( timeout() ), iAutoRefresh, SLOT( start() ) );
64 connect( iAutoRefresh, SIGNAL( timeout() ), this, SLOT( fetchMeetings() ) );
66 // create device manager
67 iDevice = new DeviceManager( iConfiguration->startupSettings() );
68 connect( iDevice, SIGNAL( error( int, const QString& ) ), this, SLOT( errorHandler( int, const QString& ) ) );
69 connect( iDevice, SIGNAL( changeModeOrdered( DeviceManager::OperationMode ) ),
70 this, SLOT( changeModeOrdered( DeviceManager::OperationMode ) ) );
71 iDevice->initDeviceManager();
73 if( iDevice->currentOperationMode() == DeviceManager::KioskMode )
74 iWindowManager->fullScreen();
76 QTimer::singleShot( 0, this, SLOT( fetchMeetings() ) );
78 // TODO: continue implementation
83 while ( !iMeetings.isEmpty() )
84 delete iMeetings.takeFirst();
85 iIdleTimeCounter->stop();
86 delete iIdleTimeCounter;
88 delete iWindowManager;
96 void Engine::closeApplication()
98 qDebug() << "Engine::closeApplication()";
99 // closes application after 1 second
100 QTimer::singleShot( 1000, QApplication::instance(), SLOT( quit() ) );
103 void Engine::observedEventDetected()
105 iWindowManager->showWeeklyView();
106 // prepare to restart idle counter
107 if ( iIdleTimeCounter->isActive() )
109 iIdleTimeCounter->stop();
111 // (re)start idle counter
112 iIdleTimeCounter->start();
115 Room* Engine::defaultRoom()
117 return iConfiguration->defaultRoom();
120 void Engine::checkStatusOfAllRooms()
122 // iterate trough on the rooms
123 for ( int i = 0; i < iConfiguration->rooms().count(); i++ )
125 // and check the status
126 roomStatusInfoNeeded( iConfiguration->rooms().at( i ) );
130 int Engine::indexOfMeetingAt( Room *aRoom, QDateTime aAt )
132 for ( int i = 0; i < iMeetings.count(); i++ )
134 // exchange server ensures that there is only one meeting in a room at a specified time
135 if ( aRoom->equals( iMeetings.at( i )->room() )
136 && iMeetings.at( i )->startsAt() <= aAt
137 && iMeetings.at( i )->endsAt() >= aAt )
145 int Engine::indexOfMeetingAfter( Room *aRoom, QDateTime aAfter )
147 // seeks for the next meeting on the SAME DAY
149 for ( int i = 0; i < iMeetings.count(); i++ )
151 // if the meeting is in the same room, on the same day but after the specified time
152 if ( aRoom->equals( iMeetings.at( i )->room() )
153 && iMeetings.at( i )->startsAt().date() == aAfter.date()
154 && iMeetings.at( i )->startsAt() > aAfter )
156 // if there was not any meeting find yet or the previously found is a later one then the (i)th
158 || iMeetings.at( min )->startsAt() > iMeetings.at( i )->startsAt() )
167 void Engine::roomStatusInfoNeeded( Room *aRoom )
174 int indexOfCurrentMeeting = indexOfMeetingAt( aRoom, iClock->datetime() );
175 int indexOfNextMeeting = indexOfMeetingAfter( aRoom, iClock->datetime() );
177 // if there is no meeting, then status is Free; otherwise Busy
178 Room::Status status = ( indexOfCurrentMeeting == -1 ) ? Room::FreeStatus : Room::BusyStatus;
179 // if room is Busy, then check end time, otherwise...
180 QTime until = ( status == Room::BusyStatus ) ? iMeetings.at( indexOfCurrentMeeting )->endsAt().time() :
181 // ...if there is meeting following on the same day then check end time, otherwise end is the of the working day
182 (( indexOfNextMeeting != -1 ) ? iMeetings.at( indexOfNextMeeting )->startsAt().time() : Engine::endOfTheDay );
184 //currently works only for deafult room
185 if( aRoom->equals( *(defaultRoom() ) ) )
186 iWindowManager->roomStatusChanged( aRoom, status, until );
189 void Engine::fetchMeetings()
191 Room *room = defaultRoom();
192 qDebug() << "Engine::fetchMeetings for " << room->name();
193 fetchMeetings( iClock->datetime(), iClock->datetime().addDays( 7 ), room );
196 void Engine::fetchMeetingDetails( Meeting *aMeeting )
198 iCommunication->fetchMeetingDetails( *aMeeting );
201 bool Engine::isMeetingInList( const QList<Meeting*> &aList, const Meeting *aMeeting )
203 for ( int i = 0; i < aList.count(); i++ )
205 if ( aMeeting->equals( *(aList.at( i )) ) )
213 void Engine::meetingsFetched( const QList<Meeting*> &aMeetings )
215 // check if there is any new meeting in the list came from the server -> added
216 for ( int i = 0; i < aMeetings.count(); i++ )
218 // if the (i)th meeting is not in the local meeting list
219 if ( !isMeetingInList( iMeetings, aMeetings.at( i ) ) )
221 // add to the local database =)
222 Meeting* m = new Meeting( *(aMeetings.at( i )) );
223 iMeetings.append( m );
224 // and signal the changes
225 iWindowManager->insertMeeting( m );
229 // check if there is any meeting NOT in the list came from the server -> deleted
230 for ( int i = 0; i < iMeetings.count(); i++ )
232 // if the (i)th meeting is in the local but NOT in the server's meeting list
233 if ( !isMeetingInList( aMeetings, iMeetings.at( i ) ) )
235 Meeting* m = iMeetings.takeAt( i );
236 // signal the changes
237 iWindowManager->deleteMeeting( m );
238 // delete the meeting from the local list
243 // refresh room status info
244 roomStatusInfoNeeded( defaultRoom() );
247 void Engine::meetingDetailsFetched( Meeting &aDetailedMeeting )
249 iWindowManager->showMeetingInfo( &aDetailedMeeting );
252 void Engine::errorHandler( int aCode, const QString &aAddInfo )
254 qDebug() << "Engine::ErrorHandler, aCode: " << aCode;
255 // inform UI about the problem
256 if( aCode >= 100 && aCode <= 110 )
257 qDebug() << "CommunicationManager signaled an error:" << aCode;
258 iWindowManager->error( ErrorMapper::codeToString( aCode, aAddInfo ) );
261 void Engine::currentRoomChanged( Room *aCurrentRoom )
263 qDebug() << "Engine::currentRoomChanged to " << aCurrentRoom->name();
264 QDateTime from( iWindowManager->weeklyView()->beginnigOfShownWeek() );
265 QDateTime to( from.addDays( 8 ) );
266 fetchMeetings( from, to, aCurrentRoom );
269 void Engine::fetchMeetings( const QDateTime &aFrom, const QDateTime &aUntil, const Room *aIn )
271 qDebug() << "Engine::fetchMeetings";
272 iCommunication->fetchMeetings( aFrom, aUntil, *aIn );
275 void Engine::shownWeekChanged( QDate aFrom )
277 QDateTime from( aFrom );
278 QDateTime to( aFrom.addDays( 7 ), QTime( 23, 59 ) );
279 qDebug() << "Engine::shownWeekChanged " << aFrom.toString( "d.m. h:mm" ) << " to " << to.toString( "d.m. h:mm" );
280 fetchMeetings( from, to, iWindowManager->weeklyView()->currentRoom() );
283 void Engine::changeModeOrdered( DeviceManager::OperationMode aMode )
285 QString message = tr( "You are about to change operation mode to %1." )
286 .arg( iDevice->operationModeToString( aMode ) );
288 iWindowManager->showPasswordDialog( iConfiguration->adminPassword(), message );