User interface update
[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( meetingActivated( Meeting * ) ), iEngine, SLOT( fetchMeetingDetails( Meeting * ) ) );
79         connect( iWeeklyView, SIGNAL( shownWeekChanged( QDate ) ), iEngine, SLOT( shownWeekChanged( QDate ) ) );
80         connect( iWeeklyView, SIGNAL( currentRoomChanged( Room * ) ), iEngine, SLOT( currentRoomChanged( Room * ) ) );
81 }
82
83 void UIManager::createSettingsView()
84 {
85         iSettingsView = new SettingsView;
86         
87         // Connect signals
88         connect( iSettingsView, SIGNAL( okClicked() ), this, SLOT( settingsOkClicked() ) );
89         connect( iSettingsView, SIGNAL( cancelClicked() ), this, SLOT( settingsCancelClicked() ) );
90 }
91
92 void UIManager::createRoomStatusIndicator()
93 {
94         iRoomStatusIndicator = new RoomStatusIndicatorWidget( iEngine->defaultRoom(), Room::FreeStatus, QTime::currentTime(), iEngine->iConfiguration->displaySettings()->timeFormat() );
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         // Connect to UIManager
109         connect( iProgressBar, SIGNAL( cancel() ), this, SLOT( progressBarCancelled() ) );
110         // Connect to Engine
111         connect( iProgressBar, SIGNAL( cancel() ), iEngine, SLOT( cancelFetchMeetingDetails() ) );
112 }
113
114 void UIManager::createMeetingInfoDialog()
115 {
116         iMeetingInfo = new MeetingInfoDialog();
117 }
118
119 void UIManager::connectDeviceManager( DeviceManager *aDeviceManager )
120 {
121         connect( aDeviceManager, SIGNAL( changeModeOrdered( DeviceManager::OperationMode ) ),
122                         this, SLOT( changeModeOrdered( DeviceManager::OperationMode ) ) );
123         
124         connect( aDeviceManager, SIGNAL( changingMode( const QString & ) ), this, SLOT( updateProgressBarText( const QString & ) ) );
125         connect( aDeviceManager, SIGNAL( changeModeFailed() ), this, SLOT( hideProgressBar() ) );
126 }
127
128 void UIManager::connectCommunicationManager( CommunicationManager *aCommunicationManager )
129 {
130         connect( aCommunicationManager, SIGNAL( meetingDetailsFetched( Meeting & ) ), this, SLOT( meetingDetailsFetched( Meeting & ) ) );
131         connect( aCommunicationManager, SIGNAL( meetingsFetched( const QList<Meeting *> & ) ), this, SLOT( meetingsFetched( const QList<Meeting *> & ) ) );
132 }
133
134 // ============================================
135 //              UIMANAGER SLOTS
136 void UIManager::settingsViewRequest()
137 {
138         createSettingsView();
139
140         // Show the settings view and stop the idle timer
141         iWindowManager->showView( static_cast<ViewBase *>( iSettingsView ) );
142         iEngine->stopIdleTimeCounter();
143 }
144
145 void UIManager::settingsOkClicked()
146 {
147         // Show the weekly view and restart the idle timer
148         if ( iWeeklyView != 0 )
149         {
150                 iWindowManager->showView( static_cast<ViewBase *>( iWeeklyView ) );
151                 QT_DELETE(iSettingsView);
152                 iEngine->startIdleTimeCounter();
153                 currentRoomChanged(iWeeklyView->currentRoom());
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                 MeetingInfoDialog *tmp = new MeetingInfoDialog( &aDetailedMeeting );
186                 iWindowManager->showDialog( static_cast<QDialog *>( tmp ) );
187 // TODO : We should use the member variable and implement correctly the setMeeting() method !!!
188 //              iMeetingInfo->setMeeting( &aDetailedMeeting );
189 //              iWindowManager->showDialog( static_cast<QDialog *>( iMeetingInfo ) );
190         }
191 }
192
193 void UIManager::roomStatusIndicatorRequested()
194 {
195         if ( iRoomStatusIndicator != 0 )
196         {
197                 iWindowManager->showView( static_cast<ViewBase *>( iRoomStatusIndicator ) );
198                 iEngine->stopIdleTimeCounter();
199         }
200 }
201
202 void UIManager::previousViewRestored()
203 {
204         iEngine->startIdleTimeCounter();
205 }
206
207 void UIManager::progressBarCancelled()
208 {
209         if ( iProgressBar != 0 )
210         {
211                 iProgressBar->close();
212         }
213 }
214
215 void UIManager::changeModeOrdered( DeviceManager::OperationMode aMode )
216 {
217         qDebug() << "[UIManager::changeModeOrdered] <Invoked>";
218
219         if ( iPasswordDialog != 0 )
220         {
221                 QString text = tr( "You are about to change operation mode to %1." )
222                                                 .arg( iEngine->iDevice->operationModeToString( aMode ) );
223                 iPasswordDialog->update( text );
224                 iWindowManager->showDialog( static_cast<QDialog *>( iPasswordDialog ) );
225         }
226 }
227
228 void UIManager::connectionLost()
229 {
230         qDebug() << "UIManager::connectionLost()";
231         iWeeklyView->connectionLost();
232         iRoomStatusIndicator->connectionLost();
233 }
234
235 void UIManager::connectionEstablished()
236 {
237         qDebug() << "UIManager::connectionEstablished()";
238         iWeeklyView->connectionEstablished();
239         iRoomStatusIndicator->connectionEstablished();
240 }
241
242 void UIManager::currentRoomChanged(Room *aRoom)
243 {
244         qDebug() << "[UIManager::currentRoomChanged] <Invoked>";
245         if ( iWeeklyView != 0 )
246         {
247                 QDateTime shown = QDateTime( iWeeklyView->beginnigOfShownWeek() );
248                 iEngine->fetchMeetings( shown.date().weekNumber(), shown.date().year(), aRoom );
249         }
250 }
251
252 void UIManager::updateTime(QDateTime aDateTime)
253 {
254         if ( iWeeklyView != 0 )
255         {
256                 iWeeklyView->setCurrentDateTime( aDateTime );
257         }
258         if ( iRoomStatusIndicator != 0 )
259         {
260                 iRoomStatusIndicator->setCurrentTime( aDateTime.time() );
261         }
262 }
263
264 void UIManager::passwordEntered( PasswordDialog::PasswordStatus aStatus )
265 {
266         switch( aStatus )
267         {
268                 case PasswordDialog::Correct:
269                         // Show the progress bar..
270                         if ( iProgressBar != 0 )
271                         {
272                                 iProgressBar->update( tr( "" ), tr( "Changing operation mode" ) );
273                                 iProgressBar->toggleCancellable( false );
274                                 iWindowManager->showDialog( static_cast<QDialog *>( iProgressBar ), false, false );
275                         }
276                         // ... and initiate the mode changing
277                         iEngine->changeDeviceMode();
278                         break;
279                 case PasswordDialog::Incorrect:
280                         iWindowManager->error( tr("Incorrect Password") );
281                         break;
282                 case PasswordDialog::Canceled:
283                         break;
284         }
285         
286         // Close the dialog after we have handled the status
287         if ( iPasswordDialog != 0 )
288         {
289                 iPasswordDialog->close();
290         }
291 }
292
293 void UIManager::updateProgressBarText(const QString &aText)
294 {
295         if ( iProgressBar != 0 )
296         {
297                 iProgressBar->update( aText );
298         }
299 }
300
301 void UIManager::hideProgressBar()
302 {
303         qDebug() << "[UIManager::hideProgressBar] <Invoked>";
304         if ( iProgressBar != 0 && iProgressBar->isVisible() )
305         {
306                 iProgressBar->close();
307         }
308 }
309
310 void UIManager::settingsCancelClicked()
311 {
312         // Show the weekly view and restart the idle timer
313         if ( iWeeklyView != 0 )
314         {
315                 iWindowManager->showView( static_cast<ViewBase *>( iWeeklyView ) );
316                 QT_DELETE(iSettingsView);
317                 iEngine->startIdleTimeCounter();
318         }
319 }