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