59db6d76e0d1822474c3965f2a1227d1cec27522
[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 // ===============================================
67 //              INITIALIZE THE UIMANAGER
68 void UIManager::createWeeklyView()
69 {
70         iWeeklyView = new WeeklyViewWidget( QDateTime::currentDateTime(), iEngine->iConfiguration );
71         
72         // Connect signals to UIManager
73         connect( iWeeklyView, SIGNAL( settingsButtonClicked() ), this, SLOT( settingsViewRequest() ) );
74         connect( iWeeklyView, SIGNAL( currentRoomChanged( Room * ) ), this, SLOT( currentRoomChanged( Room * ) ) );
75         connect( iWeeklyView, SIGNAL( meetingActivated( Meeting * ) ), this, SLOT( showMeetingProgressBar( Meeting * ) ) );
76         // Connect signals to engine
77         connect( iWeeklyView, SIGNAL( meetingActivated( Meeting * ) ), iEngine, SLOT( fetchMeetingDetails( Meeting * ) ) );
78         connect( iWeeklyView, SIGNAL( shownWeekChanged( QDate ) ), iEngine, SLOT( shownWeekChanged( QDate ) ) );
79         connect( iWeeklyView, SIGNAL( currentRoomChanged( Room * ) ), iEngine, SLOT( currentRoomChanged( Room * ) ) );
80 }
81
82 void UIManager::createSettingsView()
83 {
84         iSettingsView = new SettingsView;
85         
86         // Connect signals
87         connect( iSettingsView, SIGNAL( okClicked() ), this, SLOT( settingsOkClicked() ) );
88         connect( iSettingsView, SIGNAL( cancelClicked() ), this, SLOT( settingsCancelClicked() ) );
89 }
90
91 void UIManager::createRoomStatusIndicator()
92 {
93         iRoomStatusIndicator = new RoomStatusIndicatorWidget( iEngine->defaultRoom(), Room::FreeStatus, QTime::currentTime(), iEngine->iConfiguration->displaySettings()->timeFormat() );
94         connect( iEngine, SIGNAL( roomStatusChanged( Room::Status, QTime ) ), iRoomStatusIndicator, SLOT( statusChanged( Room::Status, QTime ) ) );
95         connect( iWeeklyView, SIGNAL( currentRoomChanged( Room * ) ), iRoomStatusIndicator, SLOT( currentRoomChanged( Room * ) ) );
96 }
97
98 void UIManager::createPasswordDialog()
99 {
100         iPasswordDialog = new PasswordDialog( iEngine->iConfiguration->adminPassword(), "", tr("Enter password") );
101         connect( iPasswordDialog, SIGNAL( passwordEntered( PasswordDialog::PasswordStatus ) ), this, SLOT( passwordEntered( PasswordDialog::PasswordStatus ) ) );
102 }
103
104 void UIManager::createProgressBar()
105 {
106         iProgressBar = new ProgressBar( tr("CHANGE THIS"), true );
107         // Connect to UIManager
108         connect( iProgressBar, SIGNAL( cancel() ), this, SLOT( progressBarCancelled() ) );
109         // Connect to Engine
110         connect( iProgressBar, SIGNAL( cancel() ), iEngine, SLOT( cancelFetchMeetingDetails() ) );
111 }
112
113 void UIManager::createMeetingInfoDialog()
114 {
115         iMeetingInfo = new MeetingInfoDialog();
116 }
117
118 void UIManager::connectDeviceManager( DeviceManager *aDeviceManager )
119 {
120         connect( aDeviceManager, SIGNAL( changeModeOrdered( DeviceManager::OperationMode ) ),
121                         this, SLOT( changeModeOrdered( DeviceManager::OperationMode ) ) );
122         
123         connect( aDeviceManager, SIGNAL( changingMode( const QString & ) ), this, SLOT( updateProgressBarText( const QString & ) ) );
124         connect( aDeviceManager, SIGNAL( changeModeFailed() ), this, SLOT( hideProgressBar() ) );
125 }
126
127 void UIManager::connectCommunicationManager( CommunicationManager *aCommunicationManager )
128 {
129         connect( aCommunicationManager, SIGNAL( meetingDetailsFetched( Meeting & ) ), this, SLOT( meetingDetailsFetched( Meeting & ) ) );
130         connect( aCommunicationManager, SIGNAL( meetingsFetched( const QList<Meeting *> & ) ), this, SLOT( meetingsFetched( const QList<Meeting *> & ) ) );
131 }
132
133 // ============================================
134 //              UIMANAGER SLOTS
135 void UIManager::settingsViewRequest()
136 {
137         // Show the settings view and stop the idle timer
138         if ( iSettingsView != 0 )
139         {
140                 iWindowManager->showView( static_cast<ViewBase *>( iSettingsView ) );
141                 iEngine->stopIdleTimeCounter();
142         }
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                 iEngine->startIdleTimeCounter();
152         }
153 }
154
155 void UIManager::meetingsFetched( const QList<Meeting*> &aMeetings )
156 {
157         qDebug() << "[UIManager::meetingsFetched] <Change the weekly views method to slot so we don't need this>";
158         if ( iWeeklyView != 0 )
159         {
160                 iWeeklyView->refreshMeetings( aMeetings );
161         }
162 }
163
164 void UIManager::showMeetingProgressBar( Meeting *aMeeting )
165 {
166         if ( iProgressBar != 0 )
167         {
168                 iProgressBar->update( tr( "Fetching meeting info..." ), tr( "Please wait" ) );
169                 iProgressBar->toggleCancellable( true );
170                 iWindowManager->showDialog( static_cast<QDialog *>( iProgressBar ), false, false );
171         }
172 }
173
174 void UIManager::meetingDetailsFetched(Meeting &aDetailedMeeting)
175 {
176         qDebug() << "[UIManager::meetingDetailsFetched] <Invoked>";
177         if ( iMeetingInfo != 0 )
178         {
179                 if ( iProgressBar != 0 && iProgressBar->isVisible() )
180                 {
181                         iProgressBar->close(); // Close it in case it's visible
182                 }
183                 MeetingInfoDialog *tmp = new MeetingInfoDialog( &aDetailedMeeting );
184                 iWindowManager->showDialog( static_cast<QDialog *>( tmp ) );
185 // TODO : We should use the member variable and implement correctly the setMeeting() method !!!
186 //              iMeetingInfo->setMeeting( &aDetailedMeeting );
187 //              iWindowManager->showDialog( static_cast<QDialog *>( iMeetingInfo ) );
188         }
189 }
190
191 void UIManager::roomStatusIndicatorRequested()
192 {
193         if ( iRoomStatusIndicator != 0 )
194         {
195                 iWindowManager->showView( static_cast<ViewBase *>( iRoomStatusIndicator ) );
196                 iEngine->stopIdleTimeCounter();
197         }
198 }
199
200 void UIManager::previousViewRestored()
201 {
202         iEngine->startIdleTimeCounter();
203 }
204
205 void UIManager::progressBarCancelled()
206 {
207         if ( iProgressBar != 0 )
208         {
209                 iProgressBar->close();
210         }
211 }
212
213 void UIManager::changeModeOrdered( DeviceManager::OperationMode aMode )
214 {
215         qDebug() << "[UIManager::changeModeOrdered] <Invoked>";
216
217         if ( iPasswordDialog != 0 )
218         {
219                 QString text = tr( "You are about to change operation mode to %1." )
220                                                 .arg( iEngine->iDevice->operationModeToString( aMode ) );
221                 iPasswordDialog->update( text );
222                 iWindowManager->showDialog( static_cast<QDialog *>( iPasswordDialog ) );
223         }
224 }
225
226 void UIManager::connectionLost()
227 {
228         qDebug() << "UIManager::connectionLost()";
229         iWeeklyView->connectionLost();
230         iSettingsView->connectionLost();
231         iRoomStatusIndicator->connectionLost();
232 }
233
234 void UIManager::connectionEstablished()
235 {
236         qDebug() << "UIManager::connectionEstablished()";
237         iWeeklyView->connectionEstablished();
238         iSettingsView->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 from = QDateTime( iWeeklyView->beginnigOfShownWeek() );
248                 QDateTime to = QDateTime( from.addDays( 8 ) );
249                 iEngine->fetchMeetings( from, to, aRoom );
250         }
251 }
252
253 void UIManager::updateTime(QDateTime aDateTime)
254 {
255         if ( iWeeklyView != 0 )
256         {
257                 iWeeklyView->setCurrentDateTime( aDateTime );
258         }
259         if ( iRoomStatusIndicator != 0 )
260         {
261                 iRoomStatusIndicator->setCurrentTime( aDateTime.time() );
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                 iEngine->startIdleTimeCounter();
318         }
319 }