Modified the dialog signaling to prevent idle screen coming from visible while dialog...
[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 // ===============================================
67 //              INITIALIZE THE UIMANAGER
68 void UIManager::createWeeklyView()
69 {
70         iWeeklyView = new WeeklyViewWidget( QDateTime::currentDateTime(), iEngine->iConfiguration );
71         
72         // Connect signals to UIManager
73         connect( iWeeklyView, SIGNAL( settingsButtonClicked() ), this, SLOT( settingsViewRequest() ) );
74         connect( iWeeklyView, SIGNAL( currentRoomChanged( Room * ) ), this, SLOT( currentRoomChanged( Room * ) ) );
75         connect( iWeeklyView, SIGNAL( meetingActivated( Meeting * ) ), this, SLOT( showMeetingProgressBar( Meeting * ) ) );
76         // Connect signals to engine
77         connect( iWeeklyView, SIGNAL( meetingActivated( Meeting * ) ), iEngine, SLOT( fetchMeetingDetails( Meeting * ) ) );
78         connect( iWeeklyView, SIGNAL( shownWeekChanged( QDate ) ), iEngine, SLOT( shownWeekChanged( QDate ) ) );
79         connect( iWeeklyView, SIGNAL( currentRoomChanged( Room * ) ), iEngine, SLOT( currentRoomChanged( Room * ) ) );
80 }
81
82 void UIManager::createSettingsView()
83 {
84         iSettingsView = new SettingsView;
85         
86         // Connect signals
87         connect( iSettingsView, SIGNAL( okClicked() ), this, SLOT( settingsOkClicked() ) );
88 }
89
90 void UIManager::createRoomStatusIndicator()
91 {
92         iRoomStatusIndicator = new RoomStatusIndicatorWidget( iEngine->defaultRoom(), Room::FreeStatus, QTime::currentTime().addSecs(100000000), iEngine->iConfiguration->displaySettings()->timeFormat() );
93         
94         // Connect engine signals
95         connect( iEngine, SIGNAL( roomStatusChanged( Room::Status, QTime ) ), iRoomStatusIndicator, SLOT( statusChanged( Room::Status, QTime ) ) );
96         connect( iWeeklyView, SIGNAL( currentRoomChanged( Room * ) ), iRoomStatusIndicator, SLOT( currentRoomChanged( Room * ) ) );
97 }
98
99 void UIManager::createPasswordDialog()
100 {
101         iPasswordDialog = new PasswordDialog( iEngine->iConfiguration->adminPassword(), "", tr("Enter password") );
102         connect( iPasswordDialog, SIGNAL( passwordEntered( PasswordDialog::PasswordStatus ) ), this, SLOT( passwordEntered( PasswordDialog::PasswordStatus ) ) );
103 }
104
105 void UIManager::createProgressBar()
106 {
107         iProgressBar = new ProgressBar( tr("CHANGE THIS"), true );
108         
109         // Connect to UIManager
110         connect( iProgressBar, SIGNAL( cancel() ), this, SLOT( progressBarCancelled() ) );
111         // Connect to Engine
112         connect( iProgressBar, SIGNAL( cancel() ), iEngine, SLOT( cancelFetchMeetingDetails() ) );
113 }
114
115 void UIManager::createMeetingInfoDialog()
116 {
117         iMeetingInfo = new MeetingInfoDialog();
118 }
119
120 void UIManager::connectDeviceManager( DeviceManager *aDeviceManager )
121 {
122         connect( aDeviceManager, SIGNAL( changeModeOrdered( DeviceManager::OperationMode ) ),
123                         this, SLOT( changeModeOrdered( DeviceManager::OperationMode ) ) );
124         
125         connect( aDeviceManager, SIGNAL( changingMode( const QString & ) ), this, SLOT( updateProgressBarText( const QString & ) ) );
126         connect( aDeviceManager, SIGNAL( changeModeFailed() ), this, SLOT( hideProgressBar() ) );
127 }
128
129 void UIManager::connectCommunicationManager( CommunicationManager *aCommunicationManager )
130 {
131         connect( aCommunicationManager, SIGNAL( meetingDetailsFetched( Meeting & ) ), this, SLOT( meetingDetailsFetched( Meeting & ) ) );
132         connect( aCommunicationManager, SIGNAL( meetingsFetched( const QList<Meeting *> & ) ), this, SLOT( meetingsFetched( const QList<Meeting *> & ) ) );
133 }
134
135 // ============================================
136 //              UIMANAGER SLOTS
137 void UIManager::settingsViewRequest()
138 {
139         // Show the settings view and stop the idle timer
140         if ( iSettingsView != 0 )
141         {
142                 iWindowManager->showView( static_cast<ViewBase *>( iSettingsView ) );
143                 iEngine->stopIdleTimeCounter();
144         }
145 }
146
147 void UIManager::settingsOkClicked()
148 {
149         // Show the weekly view and restart the idle timer
150         if ( iWeeklyView != 0 )
151         {
152                 iWindowManager->showView( static_cast<ViewBase *>( iWeeklyView ) );
153                 iEngine->startIdleTimeCounter();
154         }
155 }
156
157 void UIManager::meetingsFetched( const QList<Meeting*> &aMeetings )
158 {
159         qDebug() << "[UIManager::meetingsFetched] <Change the weekly views method to slot so we don't need this>";
160         if ( iWeeklyView != 0 )
161         {
162                 iWeeklyView->refreshMeetings( aMeetings );
163         }
164 }
165
166 void UIManager::showMeetingProgressBar( Meeting *aMeeting )
167 {
168         if ( iProgressBar != 0 )
169         {
170                 iProgressBar->update( tr( "Fetching meeting info..." ), tr( "Please wait" ) );
171                 iProgressBar->toggleCancellable( true );
172                 iWindowManager->showDialog( static_cast<QDialog *>( iProgressBar ), false, false );
173         }
174 }
175
176 void UIManager::meetingDetailsFetched(Meeting &aDetailedMeeting)
177 {
178         qDebug() << "[UIManager::meetingDetailsFetched] <Invoked>";
179         if ( iMeetingInfo != 0 )
180         {
181                 if ( iProgressBar != 0 && iProgressBar->isVisible() )
182                 {
183                         iProgressBar->close(); // Close it in case it's visible
184                 }
185                 iEngine->stopIdleTimeCounter(); // Timer restarted when the dialog is dismissed.
186                 MeetingInfoDialog *tmp = new MeetingInfoDialog( &aDetailedMeeting );
187                 iWindowManager->showDialog( static_cast<QDialog *>( tmp ) );
188                 
189 // TODO : We should use the member variable and implement correctly the setMeeting() method !!!
190 //              iMeetingInfo->setMeeting( &aDetailedMeeting );
191 //              iWindowManager->showDialog( static_cast<QDialog *>( iMeetingInfo ) );
192         }
193 }
194
195 void UIManager::roomStatusIndicatorRequested()
196 {
197         if ( iRoomStatusIndicator != 0 )
198         {
199                 iWindowManager->showView( static_cast<ViewBase *>( iRoomStatusIndicator ) );
200                 iEngine->stopIdleTimeCounter();
201         }
202 }
203
204 void UIManager::previousViewRestored()
205 {
206         iEngine->startIdleTimeCounter();
207 }
208
209 void UIManager::progressBarCancelled()
210 {
211         if ( iProgressBar != 0 )
212         {
213                 iProgressBar->close();
214         }
215 }
216
217 void UIManager::changeModeOrdered( DeviceManager::OperationMode aMode )
218 {
219         qDebug() << "[UIManager::changeModeOrdered] <Invoked>";
220
221         if ( iPasswordDialog != 0 )
222         {
223                 QString text = tr( "You are about to change operation mode to %1." )
224                                                 .arg( iEngine->iDevice->operationModeToString( aMode ) );
225                 iPasswordDialog->update( text );
226                 iWindowManager->showDialog( static_cast<QDialog *>( iPasswordDialog ) );
227         }
228 }
229
230 void UIManager::currentRoomChanged(Room *aRoom)
231 {
232         qDebug() << "[UIManager::currentRoomChanged] <Invoked>";
233         if ( iWeeklyView != 0 )
234         {
235                 QDateTime from = QDateTime( iWeeklyView->beginnigOfShownWeek() );
236                 QDateTime to = QDateTime( from.addDays( 8 ) );
237                 iEngine->fetchMeetings( from, to, aRoom );
238         }
239 }
240
241 void UIManager::updateTime(QDateTime aDateTime)
242 {
243         if ( iWeeklyView != 0 )
244         {
245                 iWeeklyView->setCurrentDateTime( aDateTime );
246         }
247         if ( iRoomStatusIndicator != 0 )
248         {
249                 iRoomStatusIndicator->setCurrentTime( aDateTime.time() );
250         }
251 }
252
253 void UIManager::passwordEntered( PasswordDialog::PasswordStatus aStatus )
254 {
255         switch( aStatus )
256         {
257                 case PasswordDialog::Correct:
258                         // Show the progress bar..
259                         if ( iProgressBar != 0 )
260                         {
261                                 iProgressBar->update( tr( "" ), tr( "Changing operation mode" ) );
262                                 iProgressBar->toggleCancellable( false );
263                                 iWindowManager->showDialog( static_cast<QDialog *>( iProgressBar ), false, false );
264                         }
265                         // ... and initiate the mode changing
266                         iEngine->changeDeviceMode( true );
267                         break;
268                 case PasswordDialog::Incorrect:
269                         iWindowManager->error( tr("Incorrect Password") );
270                 case PasswordDialog::Canceled:
271                         iEngine->changeDeviceMode( false );
272                         break;
273         }
274         
275         // Close the dialog after we have handled the status
276         if ( iPasswordDialog != 0 )
277         {
278                 iPasswordDialog->close();
279         }
280 }
281
282 void UIManager::updateProgressBarText(const QString &aText)
283 {
284         if ( iProgressBar != 0 )
285         {
286                 iProgressBar->update( aText );
287         }
288 }
289
290 void UIManager::hideProgressBar()
291 {
292         qDebug() << "[UIManager::hideProgressBar] <Invoked>";
293         if ( iProgressBar != 0 && iProgressBar->isVisible() )
294         {
295                 iProgressBar->close();
296         }
297 }