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