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