qtmeetings sources to Maemo garage
[qtmeetings] / src / UserInterface / WindowManager.cpp
1 #include "WindowManager.h"
2
3 #include <QApplication>
4 #include <QTimer>
5 #include "Configuration.h"
6 #include "DisplaySettings.h"
7 #include "Meeting.h"
8 #include "Room.h"
9 #include "Engine.h"
10 #include "Clock.h"
11 #include "WeeklyViewWidget.h"
12 #include "RoomStatusIndicatorWidget.h"
13 #include "MeetingInfoDialog.h"
14 #include "PopUpMessageBox.h"
15 #include "DeviceManager.h"
16 #include "SettingsView.h"
17
18 #include <QtDebug>
19
20 const int IDLE_TIME_MULTIPLIER = 60000; // Multiplies milliseconds to minutes
21
22 WindowManager::WindowManager() :
23                 QObject(),
24                 iApplicationName( tr( "Qt Meetings" ) ),
25                 iWeeklyView( 0 ),
26                 iRoomStatusView( 0 ),
27                 iMeetingInfo( 0 )
28 {
29         iEngine = new Engine;
30         connect( iEngine, SIGNAL( initializationFailed() ), this, SLOT( closeApplication() ) );
31         connect( this, SIGNAL( roomStatusInfoNeeded( Room * ) ), iEngine, SLOT( roomStatusInfoNeeded( Room * ) ) );
32         connect( iEngine, SIGNAL( roomStatusChanged( Room *, Room::Status, QTime ) ), this, SLOT( roomStatusChanged( Room *, Room::Status, QTime ) ) );
33         connect( iEngine->clock(), SIGNAL( tick( QDateTime ) ), this, SLOT( distributeDateTimeInfo( QDateTime ) ) );
34         connect( iEngine, SIGNAL( error( QString ) ), this, SLOT( error( QString ) ) );
35         connect( iEngine->deviceManager(), SIGNAL( changeModeOrdered( DeviceManager::OperationMode ) ), this, SLOT( changeModeOrdered( DeviceManager::OperationMode ) ) );
36         
37         iWeeklyView = new WeeklyViewWidget( QDateTime::currentDateTime(), iEngine->configuration() );
38         iWeeklyView->setWindowTitle( iApplicationName );
39         connect( iEngine, SIGNAL( meetingAdded( Meeting * ) ), iWeeklyView, SLOT( insertMeeting( Meeting * ) ) );
40         connect( iEngine, SIGNAL( meetingDeleted( Meeting * ) ), iWeeklyView, SLOT( deleteMeeting( Meeting * ) ) );
41         connect( iWeeklyView, SIGNAL( observedEventDetected() ), this, SLOT( observedEventDetected() ) );
42         connect( iWeeklyView, SIGNAL( meetingActivated( Meeting * ) ), iEngine, SLOT( fetchMeetingDetails( Meeting* ) ) );
43         connect( iEngine, SIGNAL( meetingDetailsFetched( Meeting* ) ), this, SLOT( showMeetingInfo( Meeting * ) ) );
44         connect( iWeeklyView, SIGNAL( currentRoomChanged( Room * ) ), iEngine, SLOT( currentRoomChanged( Room * ) ) );
45         connect( iWeeklyView, SIGNAL( currentRoomChanged( Room * ) ), this, SLOT( fetchMeetings( Room * ) ) );
46         // TODO: fetch meetings for specific week
47         connect( iWeeklyView, SIGNAL( shownWeekChanged( QDate ) ), this, SLOT( fetchMeetings( QDate ) ) );
48
49         iIdleTimeCounter = new QTimer();
50         iIdleTimeCounter->setSingleShot( true );
51         iIdleTimeCounter->setInterval( IDLE_TIME_MULTIPLIER * iEngine->configuration()->displaySettings()->screensaver() );
52         iIdleTimeCounter->start();
53         connect( iIdleTimeCounter, SIGNAL( timeout() ), this, SLOT( showRoomStatus() ) );
54
55         if( iEngine->deviceManager()->currentOperationMode() == DeviceManager::KioskMode )
56                 iWeeklyView->setWindowState( Qt::WindowFullScreen );
57         else
58                 iWeeklyView->setWindowState( Qt::WindowMaximized );
59         showWeeklyView();
60
61         //QTimer::singleShot( 0, this, SLOT( closeApplication() ) );
62 }
63
64 WindowManager::~WindowManager()
65 {
66         if ( iWeeklyView != 0 )
67         {
68                 delete iWeeklyView;
69                 iWeeklyView = 0;
70         }
71
72         if ( iRoomStatusView != 0 )
73         {
74                 delete iRoomStatusView;
75                 iRoomStatusView = 0;
76         }
77
78         if ( iMeetingInfo != 0 )
79         {
80                 delete iMeetingInfo;
81                 iMeetingInfo = 0;
82         }
83
84         if ( iIdleTimeCounter )
85         {
86                 iIdleTimeCounter->stop();
87                 delete iIdleTimeCounter;
88                 iIdleTimeCounter = 0;
89         }
90 }
91
92 void WindowManager::closeApplication()
93 {
94         qDebug() << "WindowManager::closeApplication\tclose application";
95         // closes application after 1 second
96         QTimer::singleShot( 1000, QApplication::instance(), SLOT( quit() ) );
97 }
98
99 void WindowManager::distributeDateTimeInfo( QDateTime aCurrentDateTime )
100 {
101         if ( iRoomStatusView != 0 && iRoomStatusView->isActiveWindow() )
102         {
103                 iRoomStatusView->setCurrentTime( aCurrentDateTime.time() );
104         }
105
106         if ( iWeeklyView != 0 && iWeeklyView->isActiveWindow() )
107         {
108                 iWeeklyView->setCurrentDateTime( aCurrentDateTime );
109         }
110 }
111
112 void WindowManager::roomStatusChanged( Room *aRoom, Room::Status aStatus, QTime aTime )
113 {
114         // currently works only for default room
115         if ( aRoom->equals( *(iEngine->defaultRoom()) ) )
116         {
117                 if ( iRoomStatusView == 0 )
118                 {
119                         iRoomStatusView = new RoomStatusIndicatorWidget( aRoom, aStatus, aTime, iEngine->configuration()->displaySettings()->timeFormat() );
120                         iRoomStatusView->setWindowTitle( iApplicationName );
121                         connect( iRoomStatusView, SIGNAL( observedEventDetected() ), this, SLOT( observedEventDetected() ) );
122                         if( iEngine->deviceManager()->currentOperationMode() == DeviceManager::KioskMode )
123                                 iRoomStatusView->setWindowState( Qt::WindowFullScreen );
124                         else
125                                 iRoomStatusView->setWindowState( Qt::WindowMaximized );
126                 }
127                 else
128                 {
129                         iRoomStatusView->statusChanged( aStatus, aTime );
130                 }
131
132                 if ( !iWeeklyView->isVisible() && !iRoomStatusView->isVisible() )
133                 {
134                         showRoomStatus();
135                 }
136         }
137 }
138
139 void WindowManager::showRoomStatus()
140 {
141         qDebug() << "WindowManager::showRoomStatus";
142
143         if ( iRoomStatusView == 0 )
144         {
145                 iEngine->roomStatusInfoNeeded( iWeeklyView->currentRoom() );
146         }
147         else
148         {
149                 iRoomStatusView->show();
150                 if ( iWeeklyView->isVisible() )
151                 {
152                         iWeeklyView->hide();
153                 }
154                 /* Causes SEGMENTATION FAULT
155                 if ( iSettingsView->isVisible() )
156                 {
157                         iSettingsView->hide();
158                 }
159                 */
160         }
161
162         // closing/deleting meeting info dialog
163         if ( iMeetingInfo != 0 )
164         {
165                 iMeetingInfo->hide();
166         }
167 }
168
169 void WindowManager::showWeeklyView()
170 {
171         qDebug() << "WindowManager::showWeeklyView";
172         if ( iRoomStatusView != 0 && iRoomStatusView->isVisible() )
173         {
174                 iRoomStatusView->hide();
175         }
176
177         iWeeklyView->show();
178 }
179
180 void WindowManager::showMeetingInfo( Meeting *aMeeting )
181 {
182         iMeetingInfo = new MeetingInfoDialog( aMeeting );
183         // Display modal dialog
184         iMeetingInfo->exec();
185
186         delete iMeetingInfo;
187         iMeetingInfo = 0;
188 }
189
190 void WindowManager::showSettingsView()
191 {
192         // TODO : give the Torspo for the person who was responsible to write this method
193 }
194
195 void WindowManager::error( const QString &aErrorMessage )
196 {
197         qDebug() << "WindowManager::showErrorPopup";
198
199         PopUpMessageBox::error( 0, aErrorMessage );
200 }
201
202 void WindowManager::observedEventDetected()
203 {
204         // if event was detected on room status view
205         if ( iRoomStatusView != 0 && iRoomStatusView->isVisible() )
206         {
207                 // show weekly view
208                 showWeeklyView();
209         }
210         // otherwise
211         else
212         {
213                 // prepare to restart idle counter
214                 if ( iIdleTimeCounter->isActive() )
215                 {
216                         iIdleTimeCounter->stop();
217                 }
218         }
219         // (re)start idle counter
220         iIdleTimeCounter->start();
221 }
222
223 void WindowManager::fetchMeetings( Room * aRoom )
224 {
225         QDateTime from( iWeeklyView->beginnigOfShownWeek() );
226         QDateTime to( from.addDays( 8 ) );
227         qDebug() << "WindowManager::fetchMeetings from " << from.toString( "d.m. h:mm" )
228         << " to " << to.toString( "d.m. h:mm" );
229         iEngine->fetchMeetings( from, to, aRoom );
230 }
231
232 void WindowManager::fetchMeetings( QDate aFrom )
233 {
234         QDateTime from( aFrom );
235         QDateTime to( aFrom.addDays( 7 ), QTime( 23, 59 ) );
236         qDebug() << "WindowManager::fetchMeetings from " << from.toString( "d.m. h:mm" )
237         << " to " << to.toString( "d.m. h:mm" );
238         iEngine->fetchMeetings( from, to, iWeeklyView->currentRoom() );
239 }
240
241 void WindowManager::changeModeOrdered( DeviceManager::OperationMode aMode )
242 {
243         QString message = tr( "You are about to change operation mode to %1." )
244                                 .arg( iEngine->deviceManager()->operationModeToString( aMode ) );
245         PasswordDialog *dlg = PasswordDialog::query( 0, iEngine->configuration()->adminPassword(), message );
246         qDebug() << "WindowManager::changeModeOrdered/tpassword: " << iEngine->configuration()->adminPassword();
247         //TODO make this modal!!!
248         connect( dlg, SIGNAL( passwordEntered( PasswordDialog::PasswordStatus ) ),
249                    this, SLOT( passwordEntered( PasswordDialog::PasswordStatus ) ) );
250 }
251
252 void WindowManager::passwordEntered( PasswordDialog::PasswordStatus aPasswordStatus )
253 {
254         switch ( aPasswordStatus )
255         {
256                 case PasswordDialog::Correct :
257                 {
258                         iEngine->deviceManager()->changeMode( true );
259                         break;
260                 }
261                 case PasswordDialog::Incorrect :
262                 {
263                         error( tr( "Incorrect password." ) );
264                         iEngine->deviceManager()->changeMode( false );
265                         break;
266                 }
267                 default : //case PasswordDialog::Canceled
268                 {
269                         iEngine->deviceManager()->changeMode( false );
270                 }
271         }
272 }