added refresh on statusbar click
[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         if ( iWeeklyView != 0 )
150         {
151                 iWindowManager->showView( static_cast<ViewBase *>( iWeeklyView ) );
152                 QT_DELETE(iSettingsView);
153                 iEngine->startIdleTimeCounter();
154                 currentRoomChanged(iWeeklyView->currentRoom());
155         }
156 }
157
158 void UIManager::meetingsFetched( const QList<Meeting*> &aMeetings )
159 {
160         qDebug() << "[UIManager::meetingsFetched] <Change the weekly views method to slot so we don't need this>";
161         if ( iWeeklyView != 0 )
162         {
163                 iWeeklyView->refreshMeetings( aMeetings );
164         }
165 }
166
167 void UIManager::showMeetingProgressBar( Meeting */*aMeeting*/ )
168 {
169         if ( iProgressBar != 0 )
170         {
171                 iProgressBar->update( tr( "Fetching meeting info..." ), tr( "Please wait" ) );
172                 iProgressBar->toggleCancellable( true );
173                 iWindowManager->showDialog( static_cast<QDialog *>( iProgressBar ), false, false );
174         }
175 }
176
177 void UIManager::meetingDetailsFetched(Meeting &aDetailedMeeting)
178 {
179         qDebug() << "[UIManager::meetingDetailsFetched] <Invoked>";
180         if ( iMeetingInfo != 0 )
181         {
182                 if ( iProgressBar != 0 && iProgressBar->isVisible() )
183                 {
184                         iProgressBar->close(); // Close it in case it's visible
185                 }
186                 MeetingInfoDialog *tmp = new MeetingInfoDialog( &aDetailedMeeting );
187                 iWindowManager->showDialog( static_cast<QDialog *>( tmp ) );
188 // TODO : We should use the member variable and implement correctly the setMeeting() method !!!
189 //              iMeetingInfo->setMeeting( &aDetailedMeeting );
190 //              iWindowManager->showDialog( static_cast<QDialog *>( iMeetingInfo ) );
191         }
192 }
193
194 void UIManager::roomStatusIndicatorRequested()
195 {
196         if ( iRoomStatusIndicator != 0 )
197         {
198                 iWindowManager->showView( static_cast<ViewBase *>( iRoomStatusIndicator ) );
199                 iEngine->stopIdleTimeCounter();
200         }
201 }
202
203 void UIManager::previousViewRestored()
204 {
205         iEngine->startIdleTimeCounter();
206 }
207
208 void UIManager::progressBarCancelled()
209 {
210         if ( iProgressBar != 0 )
211         {
212                 iProgressBar->close();
213         }
214 }
215
216 void UIManager::changeModeOrdered( DeviceManager::OperationMode aMode )
217 {
218         qDebug() << "[UIManager::changeModeOrdered] <Invoked>";
219
220         if ( iPasswordDialog != 0 )
221         {
222                 QString text = tr( "You are about to change operation mode to %1." )
223                                                 .arg( iEngine->iDevice->operationModeToString( aMode ) );
224                 iPasswordDialog->update( text );
225                 iWindowManager->showDialog( static_cast<QDialog *>( iPasswordDialog ) );
226         }
227 }
228
229 void UIManager::connectionLost()
230 {
231         qDebug() << "UIManager::connectionLost()";
232         iWeeklyView->connectionLost();
233         iRoomStatusIndicator->connectionLost();
234 }
235
236 void UIManager::connectionEstablished()
237 {
238         qDebug() << "UIManager::connectionEstablished()";
239         iWeeklyView->connectionEstablished();
240         iRoomStatusIndicator->connectionEstablished();
241 }
242
243 void UIManager::currentRoomChanged(Room *aRoom)
244 {
245         qDebug() << "[UIManager::currentRoomChanged] <Invoked>";
246         if ( iWeeklyView != 0 )
247         {
248                 QDateTime shown = QDateTime( iWeeklyView->beginnigOfShownWeek() );
249                 iEngine->fetchMeetings( shown.date().weekNumber(), shown.date().year(), aRoom );
250         }
251 }
252
253 void UIManager::updateTime(QDateTime aDateTime)
254 {
255         if ( iWeeklyView != 0 )
256         {
257                 iWeeklyView->setConnectionStatus( aDateTime, iEngine->connected(), iEngine->lastUpdated(), iEngine->errorMessage() );
258         }
259         if ( iRoomStatusIndicator != 0 )
260         {
261                 iRoomStatusIndicator->setConnectionStatus( aDateTime, iEngine->connected(), iEngine->lastUpdated(), iEngine->errorMessage() );
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                                 iProgressBar->update( tr( "" ), tr( "Changing operation mode" ) );
274                                 iProgressBar->toggleCancellable( false );
275                                 iWindowManager->showDialog( static_cast<QDialog *>( iProgressBar ), false, false );
276                         }
277                         // ... and initiate the mode changing
278                         iEngine->changeDeviceMode();
279                         break;
280                 case PasswordDialog::Incorrect:
281                         iWindowManager->error( tr("Incorrect Password") );
282                         break;
283                 case PasswordDialog::Canceled:
284                         break;
285         }
286         
287         // Close the dialog after we have handled the status
288         if ( iPasswordDialog != 0 )
289         {
290                 iPasswordDialog->close();
291         }
292 }
293
294 void UIManager::updateProgressBarText(const QString &aText)
295 {
296         if ( iProgressBar != 0 )
297         {
298                 iProgressBar->update( aText );
299         }
300 }
301
302 void UIManager::hideProgressBar()
303 {
304         qDebug() << "[UIManager::hideProgressBar] <Invoked>";
305         if ( iProgressBar != 0 && iProgressBar->isVisible() )
306         {
307                 iProgressBar->close();
308         }
309 }
310
311 void UIManager::settingsCancelClicked()
312 {
313         // Show the weekly view and restart the idle timer
314         if ( iWeeklyView != 0 )
315         {
316                 iWindowManager->showView( static_cast<ViewBase *>( iWeeklyView ) );
317                 QT_DELETE(iSettingsView);
318                 iEngine->startIdleTimeCounter();
319         }
320 }