Merged and got it partly 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] <Change the weekly views method to slot so we don't need this>";
169         if ( iWeeklyView != 0 )
170         {
171                 iWeeklyView->refreshMeetings( aMeetings );
172         }
173 }
174
175 void UIManager::showMeetingProgressBar( Meeting *aMeeting )
176 {
177         if ( iProgressBar != 0 )
178         {
179                 iProgressBar->update( tr("Fetching meeting info...") );
180                 iWindowManager->showDialog( static_cast<QDialog *>( iProgressBar ), false );
181         }
182 }
183
184 void UIManager::meetingDetailsFetched(Meeting &aDetailedMeeting)
185 {
186         qDebug() << "[UIManager::meetingDetailsFetched] <Invoked>";
187         if ( iMeetingInfo != 0 )
188         {
189                 if ( iProgressBar != 0 && iProgressBar->isVisible() )
190                 {
191                         iProgressBar->close(); // Close it in case it's visible
192                 }
193                 // iMeetingInfo->setMeeting( &aDetailedMeeting );
194                 MeetingInfoDialog *tmp = new MeetingInfoDialog( &aDetailedMeeting );
195                 iWindowManager->showDialog( static_cast<QDialog *>( tmp/*iMeetingInfo*/ ) );
196         }
197 }
198
199 void UIManager::roomStatusIndicatorRequested()
200 {
201         if ( iRoomStatusIndicator != 0 )
202         {
203                 iWindowManager->showView( static_cast<ViewBase *>( iRoomStatusIndicator ) );
204                 iEngine->stopIdleTimeCounter();
205         }
206 }
207
208 void UIManager::previousViewRestored()
209 {
210         iEngine->startIdleTimeCounter();
211 }
212
213 void UIManager::progressBarCancelled()
214 {
215         if ( iProgressBar != 0 )
216         {
217                 iProgressBar->close();
218         }
219 }
220
221 void UIManager::changeModeOrdered( DeviceManager::OperationMode aMode )
222 {
223         qDebug() << "[UIManager::changeModeOrdered] <Invoked>";
224         
225         QString message = tr( "You are about to change operation mode to %1." )
226                                 .arg( iEngine->iDevice->operationModeToString( aMode ) );
227
228         if ( iPasswordDialog != 0 )
229         {
230                 // TODO : Set the new text for password dialog
231                 iWindowManager->showDialog( static_cast<QDialog *>( iPasswordDialog ) );
232         }
233 }
234
235 void UIManager::currentRoomChanged(Room *aRoom)
236 {
237         if ( iWeeklyView != 0 )
238         {
239                 QDateTime from = QDateTime( iWeeklyView->beginnigOfShownWeek() );
240                 QDateTime to = QDateTime( from.addDays( 8 ) );
241                 iEngine->fetchMeetings( from, to, aRoom );
242         }
243 }
244
245 void UIManager::updateTime(QDateTime aDateTime)
246 {
247         if ( iWeeklyView != 0 )
248         {
249                 iWeeklyView->setCurrentDateTime( aDateTime );
250         }
251 }
252
253 void UIManager::passwordEntered( PasswordDialog::PasswordStatus aStatus )
254 {
255         switch( aStatus )
256         {
257                 case PasswordDialog::Correct:
258                         // Show the progress bar..
259                         if ( iProgressBar != 0 )
260                         {
261                                 iWindowManager->showDialog( static_cast<QDialog *>( iProgressBar ), false );
262                         }
263                         // ... and initiate the mode changing
264                         iEngine->changeDeviceMode( true );
265                         break;
266                 case PasswordDialog::Incorrect:
267                         iWindowManager->error( tr("Incorrect Password") );
268                 case PasswordDialog::Canceled:
269                         iEngine->changeDeviceMode( false );
270                         break;
271         }
272         
273         // Close the dialog after we have handled the status
274         if ( iPasswordDialog != 0 )
275         {
276                 iPasswordDialog->close();
277         }
278 }
279
280 void UIManager::updateProgressBarText(const QString &aText)
281 {
282         if ( iProgressBar != 0 )
283         {
284                 iProgressBar->update( aText );
285         }
286 }
287
288 void UIManager::hideProgressBar()
289 {
290         qDebug() << "[UIManager::hideProgressBar] <Invoked>";
291         if ( iProgressBar != 0 && iProgressBar->isVisible() )
292         {
293                 iProgressBar->close();
294         }
295 }