Confilct solved
[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         
105         // Connect to UIManager
106         connect( iProgressBar, SIGNAL( cancel() ), this, SLOT( progressBarCancelled() ) );
107         // Connect to Engine
108         connect( iProgressBar, SIGNAL( cancel() ), iEngine, SLOT( cancelFetchMeetingDetails() ) );
109 }
110
111 void UIManager::createMeetingInfoDialog()
112 {
113         iMeetingInfo = new MeetingInfoDialog();
114 }
115
116 void UIManager::connectDeviceManager( DeviceManager *aDeviceManager )
117 {
118         connect( aDeviceManager, SIGNAL( changeModeOrdered( DeviceManager::OperationMode ) ),
119                         this, SLOT( changeModeOrdered( DeviceManager::OperationMode ) ) );
120         
121         connect( aDeviceManager, SIGNAL( changingMode( const QString & ) ), this, SLOT( updateProgressBarText( const QString & ) ) );
122         connect( aDeviceManager, SIGNAL( changeModeFailed() ), this, SLOT( hideProgressBar() ) );
123 }
124
125 void UIManager::connectCommunicationManager( CommunicationManager *aCommunicationManager )
126 {
127         connect( aCommunicationManager, SIGNAL( meetingDetailsFetched( Meeting & ) ), this, SLOT( meetingDetailsFetched( Meeting & ) ) );
128         connect( aCommunicationManager, SIGNAL( meetingsFetched( const QList<Meeting *> & ) ), this, SLOT( meetingsFetched( const QList<Meeting *> & ) ) );
129 }
130
131 // ============================================
132 //              UIMANAGER SLOTS
133 void UIManager::settingsViewRequest()
134 {
135         // Show the settings view and stop the idle timer
136         if ( iSettingsView != 0 )
137         {
138                 iWindowManager->showView( static_cast<ViewBase *>( iSettingsView ) );
139                 iEngine->stopIdleTimeCounter();
140         }
141 }
142
143 void UIManager::settingsOkClicked()
144 {
145         // Show the weekly view and restart the idle timer
146         if ( iWeeklyView != 0 )
147         {
148                 iWindowManager->showView( static_cast<ViewBase *>( iWeeklyView ) );
149                 iEngine->startIdleTimeCounter();
150         }
151 }
152
153 void UIManager::meetingsFetched( const QList<Meeting*> &aMeetings )
154 {
155         qDebug() << "[UIManager::meetingsFetched] <Change the weekly views method to slot so we don't need this>";
156         if ( iWeeklyView != 0 )
157         {
158                 iWeeklyView->refreshMeetings( aMeetings );
159         }
160 }
161
162 void UIManager::showMeetingProgressBar( Meeting *aMeeting )
163 {
164         if ( iProgressBar != 0 )
165         {
166                 iProgressBar->update( tr( "Fetching meeting info..." ), tr( "Please wait" ) );
167                 iProgressBar->toggleCancellable( true );
168                 iWindowManager->showDialog( static_cast<QDialog *>( iProgressBar ), false, false );
169                 iEngine->stopIdleTimeCounter();
170         }
171 }
172
173 void UIManager::meetingDetailsFetched(Meeting &aDetailedMeeting)
174 {
175         qDebug() << "[UIManager::meetingDetailsFetched] <Invoked>";
176         if ( iMeetingInfo != 0 )
177         {
178                 if ( iProgressBar != 0 && iProgressBar->isVisible() )
179                 {
180                         iProgressBar->close(); // Close it in case it's visible
181                 }
182                 MeetingInfoDialog *tmp = new MeetingInfoDialog( &aDetailedMeeting );
183                 iWindowManager->showDialog( static_cast<QDialog *>( tmp ) );
184 // TODO : We should use the member variable and implement correctly the setMeeting() method !!!
185 //              iMeetingInfo->setMeeting( &aDetailedMeeting );
186 //              iWindowManager->showDialog( static_cast<QDialog *>( iMeetingInfo ) );
187         }
188 }
189
190 void UIManager::roomStatusIndicatorRequested()
191 {
192         if ( iRoomStatusIndicator != 0 )
193         {
194                 iWindowManager->showView( static_cast<ViewBase *>( iRoomStatusIndicator ) );
195                 iEngine->stopIdleTimeCounter();
196         }
197 }
198
199 void UIManager::previousViewRestored()
200 {
201         iEngine->startIdleTimeCounter();
202 }
203
204 void UIManager::progressBarCancelled()
205 {
206         if ( iProgressBar != 0 )
207         {
208                 iProgressBar->close();
209                 iEngine->startIdleTimeCounter();
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::currentRoomChanged(Room *aRoom)
227 {
228         qDebug() << "[UIManager::currentRoomChanged] <Invoked>";
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                                 iProgressBar->update( tr( "" ), tr( "Changing operation mode" ) );
254                                 iProgressBar->toggleCancellable( false );
255                                 iWindowManager->showDialog( static_cast<QDialog *>( iProgressBar ), false );
256                         }
257                         // ... and initiate the mode changing
258                         iEngine->changeDeviceMode( true );
259                         break;
260                 case PasswordDialog::Incorrect:
261                         iWindowManager->error( tr("Incorrect Password") );
262                 case PasswordDialog::Canceled:
263                         iEngine->changeDeviceMode( false );
264                         break;
265         }
266         
267         // Close the dialog after we have handled the status
268         if ( iPasswordDialog != 0 )
269         {
270                 iPasswordDialog->close();
271         }
272 }
273
274 void UIManager::updateProgressBarText(const QString &aText)
275 {
276         if ( iProgressBar != 0 )
277         {
278                 iProgressBar->update( aText );
279         }
280 }
281
282 void UIManager::hideProgressBar()
283 {
284         qDebug() << "[UIManager::hideProgressBar] <Invoked>";
285         if ( iProgressBar != 0 && iProgressBar->isVisible() )
286         {
287                 iProgressBar->close();
288         }
289 }