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