Fixed the room status indicator view partly. Image is still not shown
[qtmeetings] / src / BusinessLogic / UIManager.cpp
1 #include "UIManager.h"
2
3 #include <QDateTime>
4 #include <QTime>
5
6 #include "Engine.h"
7 #include "WindowManager.h"
8 #include "ViewBase.h"
9 #include "WeeklyViewWidget.h"
10 #include "SettingsView.h"
11 #include "RoomStatusIndicatorWidget.h"
12 #include "MeetingInfoDialog.h"
13 #include "ProgressBar.h"
14 #include "CommunicationManager.h"
15 #include "Configuration.h"
16 #include "DisplaySettings.h"
17
18 #include <QtDebug>
19
20 #define QT_DELETE(X) \
21         if ( X != 0 ) \
22         { \
23                 delete X; \
24                 X = 0; \
25         }
26
27 UIManager::UIManager( Engine *aEngine, WindowManager *aWindowManager ) :
28         iEngine( aEngine ),
29         iWindowManager( aWindowManager ),
30         iWeeklyView( 0 ),
31         iSettingsView( 0 ),
32         iRoomStatusIndicator( 0 ),
33         iPasswordDialog( 0 ),
34         iProgressBar( 0 ),
35         iMeetingInfo( 0 )
36 {
37         if ( iEngine == 0 ) return;
38         if ( iWindowManager == 0 ) return;
39         
40         createWeeklyView();
41         createSettingsView();
42         createRoomStatusIndicator();
43         createPasswordDialog();
44         createProgressBar();
45         createMeetingInfoDialog();
46 }
47
48 UIManager::~UIManager()
49 {
50         iEngine = 0;
51         iWindowManager = 0;
52         
53         QT_DELETE( iMeetingInfo );
54         QT_DELETE( iProgressBar );
55         QT_DELETE( iPasswordDialog );
56         QT_DELETE( iRoomStatusIndicator );
57         QT_DELETE( iSettingsView );
58         QT_DELETE( iWeeklyView );
59 }
60
61 void UIManager::showMainView()
62 {
63         iWindowManager->showView( iWeeklyView );
64 }
65
66 void UIManager::showProgressBar( QString aText )
67 {
68         if ( iProgressBar != 0 )
69         {
70                 iProgressBar->update( aText );
71                 iWindowManager->showDialog( iProgressBar );
72         }
73 }
74
75 // ===============================================
76 //              INITIALIZE THE UIMANAGER
77 void UIManager::createWeeklyView()
78 {
79         iWeeklyView = new WeeklyViewWidget( QDateTime::currentDateTime(), iEngine->iConfiguration );
80         
81         // Connect signals to UIManager
82         connect( iWeeklyView, SIGNAL( settingsButtonClicked() ), this, SLOT( settingsViewRequest() ) );
83         connect( iWeeklyView, SIGNAL( currentRoomChanged( Room * ) ), this, SLOT( currentRoomChanged( Room * ) ) );
84         connect( iWeeklyView, SIGNAL( meetingActivated( Meeting * ) ), this, SLOT( showMeetingProgressBar( Meeting * ) ) );
85         // Connect signals to engine
86         connect( iWeeklyView, SIGNAL( meetingActivated( Meeting * ) ), iEngine, SLOT( fetchMeetingDetails( Meeting * ) ) );
87         connect( iWeeklyView, SIGNAL( shownWeekChanged( QDate ) ), iEngine, SLOT( shownWeekChanged( QDate ) ) );
88         connect( iWeeklyView, SIGNAL( currentRoomChanged( Room * ) ), iEngine, SLOT( currentRoomChanged( Room * ) ) );
89 }
90
91 void UIManager::createSettingsView()
92 {
93         iSettingsView = new SettingsView;
94         
95         // Connect signals
96         connect( iSettingsView, SIGNAL( okClicked() ), this, SLOT( settingsOkClicked() ) );
97 }
98
99 void UIManager::createRoomStatusIndicator()
100 {
101         iRoomStatusIndicator = new RoomStatusIndicatorWidget( iEngine->defaultRoom(), Room::FreeStatus, QTime::currentTime().addSecs(100000000), iEngine->iConfiguration->displaySettings()->timeFormat() );
102         
103         // Connect engine signals
104         connect( iEngine, SIGNAL( roomStatusChanged( Room::Status, QTime ) ), iRoomStatusIndicator, SLOT( statusChanged( Room::Status, QTime ) ) );
105         connect( iWeeklyView, SIGNAL( currentRoomChanged( Room * ) ), iRoomStatusIndicator, SLOT( currentRoomChanged( Room * ) ) );
106 }
107
108 void UIManager::createPasswordDialog()
109 {
110         iPasswordDialog = new PasswordDialog( iEngine->iConfiguration->adminPassword(), tr("UIManager::createPasswordDialog"), tr("UIManager::createPasswordDialog") );
111         
112         connect( iPasswordDialog, SIGNAL( passwordEntered( PasswordDialog::PasswordStatus ) ), this, SLOT( passwordEntered( PasswordDialog::PasswordStatus ) ) );
113 }
114
115 void UIManager::createProgressBar()
116 {
117         iProgressBar = new ProgressBar( tr("CHANGE THIS"), true );
118         
119         // Connect to UIManager
120         connect( iProgressBar, SIGNAL( cancel() ), this, SLOT( progressBarCancelled() ) );
121         // Connect to Engine
122         connect( iProgressBar, SIGNAL( cancel() ), iEngine, SLOT( cancelFetchMeetingDetails() ) );
123 }
124
125 void UIManager::createMeetingInfoDialog()
126 {
127         iMeetingInfo = new MeetingInfoDialog();
128 }
129
130 void UIManager::connectDeviceManager( DeviceManager *aDeviceManager )
131 {
132         connect( aDeviceManager, SIGNAL( changeModeOrdered( DeviceManager::OperationMode ) ),
133                         this, SLOT( changeModeOrdered( DeviceManager::OperationMode ) ) );
134         
135         connect( aDeviceManager, SIGNAL( changingMode( const QString & ) ), this, SLOT( updateProgressBarText( const QString & ) ) );
136         connect( aDeviceManager, SIGNAL( changeModeFailed() ), this, SLOT( hideProgressBar() ) );
137 }
138
139 void UIManager::connectCommunicationManager( CommunicationManager *aCommunicationManager )
140 {
141         connect( aCommunicationManager, SIGNAL( meetingDetailsFetched( Meeting & ) ), this, SLOT( meetingDetailsFetched( Meeting & ) ) );
142         connect( aCommunicationManager, SIGNAL( meetingsFetched( const QList<Meeting *> & ) ), this, SLOT( meetingsFetched( const QList<Meeting *> & ) ) );
143 }
144
145 // ============================================
146 //              UIMANAGER SLOTS
147 void UIManager::settingsViewRequest()
148 {
149         // Show the settings view and stop the idle timer
150         if ( iSettingsView != 0 )
151         {
152                 iWindowManager->showView( static_cast<ViewBase *>( iSettingsView ) );
153                 iEngine->stopIdleTimeCounter();
154         }
155 }
156
157 void UIManager::settingsOkClicked()
158 {
159         // Show the weekly view and restart the idle timer
160         if ( iWeeklyView != 0 )
161         {
162                 iWindowManager->showView( static_cast<ViewBase *>( iWeeklyView ) );
163                 iEngine->startIdleTimeCounter();
164         }
165 }
166
167 void UIManager::meetingsFetched( const QList<Meeting*> &aMeetings )
168 {
169         qDebug() << "[UIManager::meetingsFetched] <Change the weekly views method to slot so we don't need this>";
170         if ( iWeeklyView != 0 )
171         {
172                 iWeeklyView->refreshMeetings( aMeetings );
173         }
174 }
175
176 void UIManager::showMeetingProgressBar( Meeting *aMeeting )
177 {
178         if ( iProgressBar != 0 )
179         {
180                 iProgressBar->update( tr("Fetching meeting info...") );
181                 iWindowManager->showDialog( static_cast<QDialog *>( iProgressBar ), false, false );
182                 iEngine->stopIdleTimeCounter();
183         }
184 }
185
186 void UIManager::meetingDetailsFetched(Meeting &aDetailedMeeting)
187 {
188         qDebug() << "[UIManager::meetingDetailsFetched] <Invoked>";
189         if ( iMeetingInfo != 0 )
190         {
191                 if ( iProgressBar != 0 && iProgressBar->isVisible() )
192                 {
193                         iProgressBar->close(); // Close it in case it's visible
194                 }
195                 iEngine->stopIdleTimeCounter(); // Timer restarted when the dialog is dismissed.
196                 MeetingInfoDialog *tmp = new MeetingInfoDialog( &aDetailedMeeting );
197                 iWindowManager->showDialog( static_cast<QDialog *>( tmp ) );
198                 
199 // TODO : We should use the member variable and implement correctly the setMeeting() method !!!
200 //              iMeetingInfo->setMeeting( &aDetailedMeeting );
201 //              iWindowManager->showDialog( static_cast<QDialog *>( iMeetingInfo ) );
202         }
203 }
204
205 void UIManager::roomStatusIndicatorRequested()
206 {
207         if ( iRoomStatusIndicator != 0 )
208         {
209                 iWindowManager->showView( static_cast<ViewBase *>( iRoomStatusIndicator ) );
210                 iEngine->stopIdleTimeCounter();
211         }
212 }
213
214 void UIManager::previousViewRestored()
215 {
216         iEngine->startIdleTimeCounter();
217 }
218
219 void UIManager::progressBarCancelled()
220 {
221         if ( iProgressBar != 0 )
222         {
223                 iProgressBar->close();
224                 iEngine->startIdleTimeCounter();
225         }
226 }
227
228 void UIManager::changeModeOrdered( DeviceManager::OperationMode aMode )
229 {
230         qDebug() << "[UIManager::changeModeOrdered] <Invoked>";
231         
232         QString message = tr( "You are about to change operation mode to %1." )
233                                 .arg( iEngine->iDevice->operationModeToString( aMode ) );
234
235         if ( iPasswordDialog != 0 )
236         {
237                 // TODO : Set the new text for password dialog
238                 iWindowManager->showDialog( static_cast<QDialog *>( iPasswordDialog ) );
239         }
240 }
241
242 void UIManager::currentRoomChanged(Room *aRoom)
243 {
244         qDebug() << "[UIManager::currentRoomChanged] <Invoked>";
245         if ( iWeeklyView != 0 )
246         {
247                 QDateTime from = QDateTime( iWeeklyView->beginnigOfShownWeek() );
248                 QDateTime to = QDateTime( from.addDays( 8 ) );
249                 iEngine->fetchMeetings( from, to, aRoom );
250         }
251 }
252
253 void UIManager::updateTime(QDateTime aDateTime)
254 {
255         if ( iWeeklyView != 0 )
256         {
257                 iWeeklyView->setCurrentDateTime( aDateTime );
258         }
259         if ( iRoomStatusIndicator != 0 )
260         {
261                 iRoomStatusIndicator->setCurrentTime( aDateTime.time() );
262         }
263 }
264
265 void UIManager::passwordEntered( PasswordDialog::PasswordStatus aStatus )
266 {
267         switch( aStatus )
268         {
269                 case PasswordDialog::Correct:
270                         // Show the progress bar..
271                         if ( iProgressBar != 0 )
272                         {
273                                 iWindowManager->showDialog( static_cast<QDialog *>( iProgressBar ), false );
274                         }
275                         // ... and initiate the mode changing
276                         iEngine->changeDeviceMode( true );
277                         break;
278                 case PasswordDialog::Incorrect:
279                         iWindowManager->error( tr("Incorrect Password") );
280                 case PasswordDialog::Canceled:
281                         iEngine->changeDeviceMode( false );
282                         break;
283         }
284         
285         // Close the dialog after we have handled the status
286         if ( iPasswordDialog != 0 )
287         {
288                 iPasswordDialog->close();
289         }
290 }
291
292 void UIManager::updateProgressBarText(const QString &aText)
293 {
294         if ( iProgressBar != 0 )
295         {
296                 iProgressBar->update( aText );
297         }
298 }
299
300 void UIManager::hideProgressBar()
301 {
302         qDebug() << "[UIManager::hideProgressBar] <Invoked>";
303         if ( iProgressBar != 0 && iProgressBar->isVisible() )
304         {
305                 iProgressBar->close();
306         }
307 }