19f445fdc61d7d80f7104cb00c85e873e191e5e7
[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( tr("Changing current operation mode.") );
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") );
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 }
136
137 void UIManager::connectCommunicationManager( CommunicationManager *aCommunicationManager )
138 {
139         connect( aCommunicationManager, SIGNAL( meetingDetailsFetched( Meeting & ) ), this, SLOT( meetingDetailsFetched( Meeting & ) ) );
140 }
141
142 // ============================================
143 //              UIMANAGER SLOTS
144 void UIManager::settingsViewRequest()
145 {
146         // Show the settings view and stop the idle timer
147         if ( iSettingsView != 0 )
148         {
149                 iWindowManager->showView( static_cast<ViewBase *>( iSettingsView ) );
150                 iEngine->stopIdleTimeCounter();
151         }
152 }
153
154 void UIManager::settingsOkClicked()
155 {
156         // Show the weekly view and restart the idle timer
157         if ( iWeeklyView != 0 )
158         {
159                 iWindowManager->showView( static_cast<ViewBase *>( iWeeklyView ) );
160                 iEngine->startIdleTimeCounter();
161         }
162 }
163
164 void UIManager::meetingsFetched( const QList<Meeting*> &aMeetings )
165 {
166         
167 }
168
169 void UIManager::showMeetingProgressBar( Meeting *aMeeting )
170 {
171         if ( iProgressBar != 0 )
172         {
173                 iProgressBar->update( tr("Fetching meeting info...") );
174                 iWindowManager->showDialog( static_cast<QDialog *>( iProgressBar ) );
175         }
176 }
177
178 void UIManager::meetingDetailsFetched(Meeting &aDetailedMeeting)
179 {
180         if ( iMeetingInfo != 0 )
181         {
182                 if ( iProgressBar != 0 )
183                 {
184                         iProgressBar->close(); // Close it in case it's visible
185                 }
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         QString message = tr( "You are about to change operation mode to %1." )
218                                 .arg( iEngine->iDevice->operationModeToString( aMode ) );
219
220         if ( iPasswordDialog != 0 )
221         {
222                 // TODO : Set the new text for password dialog
223                 iWindowManager->showDialog( static_cast<QDialog *>( iPasswordDialog ) );
224         }
225 }
226
227 void UIManager::currentRoomChanged(Room *aRoom)
228 {
229         if ( iWeeklyView != 0 )
230         {
231                 QDateTime from = QDateTime( iWeeklyView->beginnigOfShownWeek() );
232                 QDateTime to = QDateTime( from.addDays( 8 ) );
233                 iEngine->fetchMeetings( from, to, aRoom );
234         }
235 }
236
237 void UIManager::updateTime(QDateTime aDateTime)
238 {
239         if ( iWeeklyView != 0 )
240         {
241                 iWeeklyView->setCurrentDateTime( aDateTime );
242         }
243 }
244
245 void UIManager::passwordEntered( PasswordDialog::PasswordStatus aStatus )
246 {
247         switch( aStatus )
248         {
249                 case PasswordDialog::Correct:
250                         // Show the progress bar..
251                         if ( iProgressBar != 0 )
252                         {
253                                 iWindowManager->showDialog( static_cast<QDialog *>( iProgressBar ) );
254                         }
255                         // ... and initiate the mode changing
256                         iEngine->changeDeviceMode( true );
257                         break;
258                 case PasswordDialog::Incorrect:
259                         iWindowManager->error( tr("Incorrect Password") );
260                 case PasswordDialog::Canceled:
261                         iEngine->changeDeviceMode( false );
262                         break;
263         }
264         
265         // Close the dialog after we have handled the status
266         if ( iPasswordDialog != 0 )
267         {
268                 iPasswordDialog->close();
269         }
270 }
271
272 void UIManager::updateProgressBarText(const QString &aText)
273 {
274         if ( iProgressBar != 0 )
275         {
276                 iProgressBar->update( aText );
277         }
278 }