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