- Fixed week view refresh after settings are changed
[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         connect(iWindowManager, SIGNAL(showSettingsClicked()), this, SLOT(settingsViewRequest()));
41
42         createWeeklyView();
43         createRoomStatusIndicator();
44         createPasswordDialog();
45         createProgressBar();
46         createMeetingInfoDialog();
47 }
48
49 UIManager::~UIManager()
50 {
51         iEngine = 0;
52         iWindowManager = 0;
53         
54         QT_DELETE( iMeetingInfo );
55         QT_DELETE( iProgressBar );
56         QT_DELETE( iPasswordDialog );
57         QT_DELETE( iRoomStatusIndicator );
58         QT_DELETE( iSettingsView );
59         QT_DELETE( iWeeklyView );
60 }
61
62 void UIManager::showMainView()
63 {
64         iWindowManager->showView( iWeeklyView );
65 }
66
67 // ===============================================
68 //              INITIALIZE THE UIMANAGER
69 void UIManager::createWeeklyView()
70 {
71         iWeeklyView = new WeeklyViewWidget( QDateTime::currentDateTime(), iEngine->iConfiguration );
72         
73         // Connect signals to UIManager
74         connect( iWeeklyView, SIGNAL( settingsButtonClicked() ), this, SLOT( settingsViewRequest() ) );
75         connect( iWeeklyView, SIGNAL( currentRoomChanged( Room * ) ), this, SLOT( currentRoomChanged( Room * ) ) );
76         connect( iWeeklyView, SIGNAL( meetingActivated( Meeting * ) ), this, SLOT( showMeetingProgressBar( Meeting * ) ) );
77         // Connect signals to engine
78         connect( iWeeklyView, SIGNAL( refreshButtonClicked() ), iEngine, SLOT( updateRoomInfo() ) );
79         connect( iWeeklyView, SIGNAL( meetingActivated( Meeting * ) ), iEngine, SLOT( fetchMeetingDetails( Meeting * ) ) );
80         connect( iWeeklyView, SIGNAL( shownWeekChanged( QDate ) ), iEngine, SLOT( shownWeekChanged( QDate ) ) );
81         connect( iWeeklyView, SIGNAL( currentRoomChanged( Room * ) ), iEngine, SLOT( currentRoomChanged( Room * ) ) );
82 }
83
84 void UIManager::createSettingsView()
85 {
86         iSettingsView = new SettingsView;
87         
88         // Connect signals
89         connect( iSettingsView, SIGNAL( okClicked() ), this, SLOT( settingsOkClicked() ) );
90         connect( iSettingsView, SIGNAL( cancelClicked() ), this, SLOT( settingsCancelClicked() ) );
91 }
92
93 void UIManager::createRoomStatusIndicator()
94 {
95         iRoomStatusIndicator = new RoomStatusIndicatorWidget( iEngine->defaultRoom(), Room::FreeStatus, QTime::currentTime(), iEngine->iConfiguration->displaySettings()->timeFormat() );
96         connect( iEngine, SIGNAL( roomStatusChanged( Room::Status, QTime ) ), iRoomStatusIndicator, SLOT( statusChanged( Room::Status, QTime ) ) );
97         connect( iWeeklyView, SIGNAL( currentRoomChanged( Room * ) ), iRoomStatusIndicator, SLOT( currentRoomChanged( Room * ) ) );
98 }
99
100 void UIManager::createPasswordDialog()
101 {
102         iPasswordDialog = new PasswordDialog( iEngine->iConfiguration->adminPassword(), "", tr("Enter password") );
103         connect( iPasswordDialog, SIGNAL( passwordEntered( PasswordDialog::PasswordStatus ) ), this, SLOT( passwordEntered( PasswordDialog::PasswordStatus ) ) );
104 }
105
106 void UIManager::createProgressBar()
107 {
108         iProgressBar = new ProgressBar( tr("CHANGE THIS"), true );
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         createSettingsView();
140
141         // Show the settings view and stop the idle timer
142         iWindowManager->showView( static_cast<ViewBase *>( iSettingsView ) );
143         iEngine->stopIdleTimeCounter();
144 }
145
146 void UIManager::settingsOkClicked()
147 {
148         // Show the weekly view and restart the idle timer
149         QT_DELETE(iWeeklyView);
150         createWeeklyView();
151
152         if ( iWeeklyView != 0 )
153         {
154                 iWindowManager->showView( static_cast<ViewBase *>( iWeeklyView ) );
155                 QT_DELETE(iSettingsView);
156                 iEngine->startIdleTimeCounter();
157                 currentRoomChanged(iWeeklyView->currentRoom());
158         }
159 }
160
161 void UIManager::meetingsFetched( const QList<Meeting*> &aMeetings )
162 {
163         qDebug() << "[UIManager::meetingsFetched] <Change the weekly views method to slot so we don't need this>";
164         if ( iWeeklyView != 0 )
165         {
166                 iWeeklyView->refreshMeetings( aMeetings );
167         }
168 }
169
170 void UIManager::showMeetingProgressBar( Meeting */*aMeeting*/ )
171 {
172         if ( iProgressBar != 0 )
173         {
174                 iProgressBar->update( tr( "Fetching meeting info..." ), tr( "Please wait" ) );
175                 iProgressBar->toggleCancellable( true );
176                 iWindowManager->showDialog( static_cast<QDialog *>( iProgressBar ), false, false );
177         }
178 }
179
180 void UIManager::meetingDetailsFetched(Meeting &aDetailedMeeting)
181 {
182         qDebug() << "[UIManager::meetingDetailsFetched] <Invoked>";
183         if ( iMeetingInfo != 0 )
184         {
185                 if ( iProgressBar != 0 && iProgressBar->isVisible() )
186                 {
187                         iProgressBar->close(); // Close it in case it's visible
188                 }
189                 MeetingInfoDialog *tmp = new MeetingInfoDialog( &aDetailedMeeting );
190                 iWindowManager->showDialog( static_cast<QDialog *>( tmp ) );
191 // TODO : We should use the member variable and implement correctly the setMeeting() method !!!
192 //              iMeetingInfo->setMeeting( &aDetailedMeeting );
193 //              iWindowManager->showDialog( static_cast<QDialog *>( iMeetingInfo ) );
194         }
195 }
196
197 void UIManager::roomStatusIndicatorRequested()
198 {
199         if ( iRoomStatusIndicator != 0 )
200         {
201                 iWindowManager->showView( static_cast<ViewBase *>( iRoomStatusIndicator ) );
202                 iEngine->stopIdleTimeCounter();
203         }
204 }
205
206 void UIManager::previousViewRestored()
207 {
208         iEngine->startIdleTimeCounter();
209 }
210
211 void UIManager::progressBarCancelled()
212 {
213         if ( iProgressBar != 0 )
214         {
215                 iProgressBar->close();
216         }
217 }
218
219 void UIManager::changeModeOrdered( DeviceManager::OperationMode aMode )
220 {
221         qDebug() << "[UIManager::changeModeOrdered] <Invoked>";
222
223         if ( iPasswordDialog != 0 )
224         {
225                 QString text = tr( "You are about to change operation mode to %1." )
226                                                 .arg( iEngine->iDevice->operationModeToString( aMode ) );
227                 iPasswordDialog->update( text );
228                 iWindowManager->showDialog( static_cast<QDialog *>( iPasswordDialog ) );
229         }
230 }
231
232 void UIManager::connectionLost()
233 {
234         qDebug() << "UIManager::connectionLost()";
235         iWeeklyView->connectionLost();
236         iRoomStatusIndicator->connectionLost();
237 }
238
239 void UIManager::connectionEstablished()
240 {
241         qDebug() << "UIManager::connectionEstablished()";
242         iWeeklyView->connectionEstablished();
243         iRoomStatusIndicator->connectionEstablished();
244 }
245
246 void UIManager::currentRoomChanged(Room *aRoom)
247 {
248         qDebug() << "[UIManager::currentRoomChanged] <Invoked>";
249         if ( iWeeklyView != 0 )
250         {
251                 QDateTime shown = QDateTime( iWeeklyView->beginnigOfShownWeek() );
252                 iEngine->fetchMeetings( shown.date().weekNumber(), shown.date().year(), aRoom );
253         }
254 }
255
256 void UIManager::updateTime(QDateTime aDateTime)
257 {
258         if ( iWeeklyView != 0 )
259         {
260                 iWeeklyView->setConnectionStatus( aDateTime, iEngine->connected(), iEngine->lastUpdated(), iEngine->errorMessage() );
261         }
262         if ( iRoomStatusIndicator != 0 )
263         {
264                 iRoomStatusIndicator->setConnectionStatus( aDateTime, iEngine->connected(), iEngine->lastUpdated(), iEngine->errorMessage() );
265         }
266 }
267
268 void UIManager::passwordEntered( PasswordDialog::PasswordStatus aStatus )
269 {
270         switch( aStatus )
271         {
272                 case PasswordDialog::Correct:
273                         // Show the progress bar..
274                         if ( iProgressBar != 0 )
275                         {
276                                 iProgressBar->update( tr( "" ), tr( "Changing operation mode" ) );
277                                 iProgressBar->toggleCancellable( false );
278                                 iWindowManager->showDialog( static_cast<QDialog *>( iProgressBar ), false, false );
279                         }
280                         // ... and initiate the mode changing
281                         iEngine->changeDeviceMode();
282                         break;
283                 case PasswordDialog::Incorrect:
284                         iWindowManager->error( tr("Incorrect Password") );
285                         break;
286                 case PasswordDialog::Canceled:
287                         break;
288         }
289         
290         // Close the dialog after we have handled the status
291         if ( iPasswordDialog != 0 )
292         {
293                 iPasswordDialog->close();
294         }
295 }
296
297 void UIManager::updateProgressBarText(const QString &aText)
298 {
299         if ( iProgressBar != 0 )
300         {
301                 iProgressBar->update( aText );
302         }
303 }
304
305 void UIManager::hideProgressBar()
306 {
307         qDebug() << "[UIManager::hideProgressBar] <Invoked>";
308         if ( iProgressBar != 0 && iProgressBar->isVisible() )
309         {
310                 iProgressBar->close();
311         }
312 }
313
314 void UIManager::settingsCancelClicked()
315 {
316         // Show the weekly view and restart the idle timer
317         if ( iWeeklyView != 0 )
318         {
319                 iWindowManager->showView( static_cast<ViewBase *>( iWeeklyView ) );
320                 QT_DELETE(iSettingsView);
321                 iEngine->startIdleTimeCounter();
322         }
323 }