User interface update
[qtmeetings] / src / UserInterface / Views / SettingsView.cpp
1 #include "SettingsView.h"
2
3 #include <QTabWidget>
4 #include <QVBoxLayout>
5 #include <QHBoxLayout>
6 #include <QPushButton>
7 #include <QGroupBox>
8 #include <QLabel>
9 #include <QLineEdit>
10 #include <QRadioButton>
11 #include <QTimeEdit>
12 #include <QListView>
13 #include <QList>
14 #include <QTime>
15 #include <QIntValidator>
16 #include <QGridLayout>
17 #include <QCheckBox>
18 #include "Configuration.h"
19 #include "Room.h"
20 #include "DisplaySettings.h"
21 #include "ConnectionSettings.h"
22 #include "StartupSettings.h"
23 #include <QScrollArea>
24
25 #include <QtDebug>
26
27 #define QT_DELETE(X) \
28         if ( X != 0 ) \
29         { \
30                 qDebug() << "delete " << X; \
31                 delete X; \
32                 X = 0; \
33         }
34
35 SettingsView::SettingsView( QWidget *aParent ) :
36                 ViewBase( ViewBase::NormalView, aParent )
37 {
38         qDebug() << "SettingsView::ctor invoked";
39         // Prepare the tabbed view
40         iTabWidget = new QTabWidget;
41
42         iSettingsTab = initSettingsTab();
43         iWeekViewTab = initWeekViewTab();
44         iResourcesTab = initResourcesTab();
45         iKioskModeTab = initKioskModeTab();
46
47         iTabWidget->addTab( iSettingsTab, tr( "Settings" ) );
48         iTabWidget->addTab( iWeekViewTab, tr( "Weekly View" ) );
49         iTabWidget->addTab( iResourcesTab, tr( "Resources" ) );
50         iTabWidget->addTab( iKioskModeTab, tr( "KIOSK Mode" ) );
51
52         // Prepare the buttons and button layout
53         QHBoxLayout *buttonLayout = new QHBoxLayout;
54         iOkButton = new QPushButton;
55         iOkButton->setText( tr( "OK" ) );
56         buttonLayout->addWidget( iOkButton );
57         iCancelButton = new QPushButton;
58         iCancelButton->setText( tr( "Cancel" ));
59         buttonLayout->addWidget( iCancelButton );
60
61         // Handle the main layout
62         QVBoxLayout *mainLayout = new QVBoxLayout;
63         mainLayout->addWidget( iTabWidget );
64         mainLayout->addLayout( buttonLayout );
65
66         setLayout( mainLayout );
67         setValues();
68
69         setAutoFillBackground( true );
70
71         // Handle component connections
72         connect( iOkButton, SIGNAL( clicked() ), this, SLOT( handleOkClicked() ) );
73         connect( iCancelButton, SIGNAL( clicked() ), this, SLOT( handleCancelClicked() ) );
74 }
75
76 SettingsView::~SettingsView()
77 {
78         QT_DELETE(iOkButton);
79         QT_DELETE(iCancelButton);
80         QT_DELETE(iUserName);
81         QT_DELETE(iPassword);
82         QT_DELETE(iServerAddress);
83         QT_DELETE(iDayStartTime);
84         QT_DELETE(iDayEndTime);
85         QT_DELETE(iFiveDays);
86         QT_DELETE(iSevenDays);
87         QT_DELETE(iRefreshInterval);
88         QT_DELETE(iPowerSaveEnabled);
89         QT_DELETE(iPowerSaveStartTime);
90         QT_DELETE(iPowerSaveEndTime);
91         QT_DELETE(iSettingsTab);
92         QT_DELETE(iWeekViewTab);
93         QT_DELETE(iResourcesTab);
94         QT_DELETE(iKioskModeTab);
95         QT_DELETE(iTabWidget);
96
97 }
98
99 QWidget *SettingsView::initSettingsTab()
100 {
101         QWidget *widget = new QWidget( iTabWidget );
102
103         // Prepare the widgets that are member variables
104         iUserName = new QLineEdit;
105         iPassword = new QLineEdit;
106         iPassword->setEchoMode( QLineEdit::Password );
107         iServerAddress = new QLineEdit;
108         iRefreshInterval = new QLineEdit;
109         QIntValidator *qiv = new QIntValidator( 0 );
110         iRefreshInterval->setValidator( qiv );
111
112         // Create the group boxes
113         QGroupBox *userInformationGroup = new QGroupBox( tr( "User Information" ) );
114         QGroupBox *serverInformationGroup = new QGroupBox( tr( "Server Information" ) );
115
116         // Prepare the user infromation group box
117         QGridLayout *ugl = new QGridLayout;
118         QLabel *userNameLabel = new QLabel( tr( "Username:" ) );
119         QLabel *passwordLabel = new QLabel( tr( "Password:" ) );
120
121         ugl->addWidget( userNameLabel, 0, 0 );
122         ugl->addWidget( iUserName, 0, 1 );
123         ugl->addWidget( passwordLabel, 1, 0 );
124         ugl->addWidget( iPassword, 1, 1 );
125
126         userInformationGroup->setLayout( ugl );
127
128         // Prepare the server information group box
129         QGridLayout *sgl = new QGridLayout;
130         QLabel *serverURLLabel = new QLabel( tr( "Server URL:" ) );
131         QLabel *refreshLabel = new QLabel( tr( "Refresh interval" ) );
132         QLabel *secondsLabel = new QLabel( tr( "seconds" ) );
133
134         sgl->addWidget( serverURLLabel, 0, 0, 1, 2 );
135         sgl->addWidget( iServerAddress, 0, 1 );
136         sgl->addWidget( refreshLabel, 1, 0 );
137         sgl->addWidget( iRefreshInterval, 1, 1 );
138         sgl->addWidget( secondsLabel, 1, 2 );
139
140         serverInformationGroup->setLayout( sgl );
141
142         // Prepare and set the main layout
143         QVBoxLayout *mainLayout = new QVBoxLayout;
144         mainLayout->addWidget( userInformationGroup );
145         mainLayout->addWidget( serverInformationGroup );
146
147         widget->setLayout( mainLayout );
148
149         QScrollArea *scroll = new QScrollArea;
150         scroll->setWidget(widget);
151         return scroll;
152 }
153
154 QWidget *SettingsView::initWeekViewTab()
155 {
156         QWidget *widget = new QWidget( iTabWidget );
157
158         // Prepare the member variable widgets
159         iFiveDays = new QRadioButton( tr( "5" ) );
160         iSevenDays = new QRadioButton( tr( "7" ) );
161         iDayStartTime = new QTimeEdit;
162         iDayEndTime = new QTimeEdit;
163
164         // Create group box and the grid layout
165         QGroupBox *weeklyInformation = new QGroupBox( tr( "Weekly View" ) );
166         QGridLayout *wgl = new QGridLayout;
167
168         // Prepare the number of days row
169         QLabel *daysLabel = new QLabel( tr( "Days:" ) );
170
171         wgl->addWidget( daysLabel, 0, 0 );
172         wgl->addWidget( iFiveDays, 0, 1 );
173         wgl->addWidget( iSevenDays, 0, 2 );
174
175         // Preare the day starts row
176         QLabel *dayStartsLabel = new QLabel( tr( "Day starts:" ) );
177
178         wgl->addWidget( dayStartsLabel, 1, 0 );
179         wgl->addWidget( iDayStartTime, 1, 1, 1, 2 );
180
181         // Prepare the day ends row
182         QLabel *dayEndsLabel = new QLabel( tr( "Day ends:" ) );
183
184         wgl->addWidget( dayEndsLabel, 2, 0 );
185         wgl->addWidget( iDayEndTime, 2, 1, 1, 2 );
186
187         weeklyInformation->setLayout( wgl );
188
189         QVBoxLayout *mainLayout = new QVBoxLayout;
190         mainLayout->addWidget( weeklyInformation );
191
192         widget->setLayout( mainLayout );
193
194         QScrollArea *scroll = new QScrollArea;
195         scroll->setWidget(widget);
196         return scroll;
197 }
198
199 QWidget *SettingsView::initResourcesTab()
200 {
201         QWidget *widget = new QWidget( iTabWidget );
202
203         QHBoxLayout *mainLayout = new QHBoxLayout;
204
205         // Available resources
206         QVBoxLayout *availableResourcesLayout = new QVBoxLayout;
207         QLabel *availableResourcesLabel = new QLabel( tr( "Available Resources:" ) );
208         QListView *availableResourcesList = new QListView;
209
210         // Fill the list
211         QList<Room*> rooms = Configuration::instance()->rooms();
212         for ( int i = 0; i < rooms.count(); i++ )
213         {
214                 Room *tmp_room = ( Room * ) rooms.at( i );
215                 qDebug() << "Room: " << tmp_room->name();
216         }
217
218         availableResourcesLayout->addWidget( availableResourcesLabel );
219         availableResourcesLayout->addWidget( availableResourcesList );
220
221         // Selected resources
222         QVBoxLayout *selectedResourcesLayout = new QVBoxLayout;
223         QLabel *selectedResourcesLabel = new QLabel( tr( "Selected Resources:" ) );
224         QListView *selectedResourcesList = new QListView;
225
226         selectedResourcesLayout->addWidget( selectedResourcesLabel );
227         selectedResourcesLayout->addWidget( selectedResourcesList );
228
229         // Button lauout
230         QVBoxLayout *controlButtonsLayout = new QVBoxLayout;
231         QPushButton *addButton = new QPushButton( tr( "->" ) );
232         QPushButton *removeButton = new QPushButton( tr( "<-" ) );
233         controlButtonsLayout->addWidget( addButton );
234         controlButtonsLayout->addWidget( removeButton );
235
236         // Prepare main layout
237         mainLayout->addLayout( availableResourcesLayout );
238         mainLayout->addLayout( controlButtonsLayout );
239         mainLayout->addLayout( selectedResourcesLayout );
240
241         widget->setLayout( mainLayout );
242
243         QScrollArea *scroll = new QScrollArea;
244         scroll->setWidget(widget);
245         return scroll;
246 }
247
248 QWidget *SettingsView::initKioskModeTab()
249 {
250         QWidget *widget = new QWidget( iTabWidget );
251
252         // Prepare member variable widgets
253         iPowerSaveEnabled = new QCheckBox( tr( "Power save enabled" ) );
254         iPowerSaveStartTime = new QTimeEdit;
255         iPowerSaveEndTime = new QTimeEdit;
256
257         if ( Configuration::instance()->startupSettings()->isPowersavingEnabled() )
258         {
259                 iPowerSaveEnabled->setChecked( true );
260         }
261         else
262         {
263                 iPowerSaveEnabled->setChecked( false );
264         }
265         iPowerSaveStartTime->setTime( Configuration::instance()->startupSettings()->turnOnAt() );
266         iPowerSaveEndTime->setTime( Configuration::instance()->startupSettings()->turnOffAt() );
267
268         // Prepare the admin password box
269         QGroupBox *adminPasswordGroup = new QGroupBox( tr( "Admin Password" ) );
270         QLabel *oldPwdLabel = new QLabel( tr( "Old password:" ) );
271         QLabel *newPwdLabel = new QLabel( tr( "New password:" ) );
272         QLabel *confirmPwdLabel = new QLabel( tr( "Confirm password:" ) );
273         QPushButton *applyPwdButton = new QPushButton( tr( "Apply" ) );
274
275         QLineEdit *oldPwdEdit = new QLineEdit;
276         QLineEdit *newPwdEdit = new QLineEdit;
277         QLineEdit *confirmPwdEdit = new QLineEdit;
278
279         oldPwdEdit->setEchoMode( QLineEdit::Password );
280         newPwdEdit->setEchoMode( QLineEdit::Password );
281         confirmPwdEdit->setEchoMode( QLineEdit::Password );
282
283         QGridLayout *agl = new QGridLayout;
284
285         agl->addWidget( oldPwdLabel, 0, 0 );
286         agl->addWidget( oldPwdEdit, 0, 1 );
287         agl->addWidget( newPwdLabel, 1, 0 );
288         agl->addWidget( newPwdEdit, 1, 1 );
289         agl->addWidget( confirmPwdLabel, 2, 0 );
290         agl->addWidget( confirmPwdEdit, 2, 1 );
291         agl->addWidget( applyPwdButton, 3, 0, 1, 2, Qt::AlignRight );
292
293         adminPasswordGroup->setLayout( agl );
294
295         // Prepare the power save options
296         QGroupBox *powerSaveGroup = new QGroupBox( tr( "Power Save" ) );
297         QLabel *switchedOnLabel = new QLabel( tr( "Switched on from:" ) );
298         QLabel *toLabel = new QLabel( tr( "to" ) );
299         QGridLayout *psgl = new QGridLayout;
300
301         psgl->addWidget( iPowerSaveEnabled, 0, 0, 1, 4, Qt::AlignLeft );
302         psgl->addWidget( switchedOnLabel, 1, 0 );
303         psgl->addWidget( iPowerSaveStartTime, 1, 1 );
304         psgl->addWidget( toLabel, 1, 2 );
305         psgl->addWidget( iPowerSaveEndTime, 1, 3 );
306
307         powerSaveGroup->setLayout( psgl );
308
309         // Prepare the main layout
310         QVBoxLayout *mainLayout = new QVBoxLayout;
311         mainLayout->addWidget( adminPasswordGroup );
312         mainLayout->addWidget( powerSaveGroup );
313
314         widget->setLayout( mainLayout );
315
316         QScrollArea *scroll = new QScrollArea;
317         scroll->setWidget(widget);
318         return scroll;
319 }
320
321 void SettingsView::handleOkClicked()
322 {
323         qDebug() << "[SettingsView::okClicked] <Invoked>";
324
325         // Collect the configration data
326         QTime calendarStart = iDayStartTime->time();
327         QTime calendarEnd = iDayEndTime->time();
328         QTime powerSaveStart = iPowerSaveStartTime->time();
329         QTime powerSaveEnd = iPowerSaveEndTime->time();
330
331         QString userName = iUserName->text();
332         QString password = iPassword->text();
333         QString serverAddress = iServerAddress->text();
334         bool ok;
335         uint refreshInterval = iRefreshInterval->text().toUInt(&ok, 10);
336
337         bool fiveDays = iFiveDays->isChecked();
338         bool sevenDays = iSevenDays->isChecked();
339         bool powerSaveEnabled = iPowerSaveEnabled->isChecked();
340
341         // set values to Configuration
342         // set user information
343         Configuration::instance()->setUsername(userName);//connectionSettings()->setUsername( userName );
344         Configuration::instance()->setPassword(password);//connectionSettings()->setPassword( password );
345         
346         // set server information
347         Configuration::instance()->setServerUrl(serverAddress);//connectionSettings()->setServerUrl( serverAddress );
348         if ( ok )
349         {
350                 Configuration::instance()->setRefreshinterval(refreshInterval);//connectionSettings()->setRefreshInterval( refreshInterval );
351         }
352         
353         // set weekly view settings
354         if ( fiveDays )
355         {
356                 Configuration::instance()->displaySettings()->setDaysInSchedule( DisplaySettings::WeekdaysInSchedule );
357         }
358         else if ( sevenDays )
359         {
360                 Configuration::instance()->displaySettings()->setDaysInSchedule( DisplaySettings::WholeWeekInSchedule );
361         }
362         Configuration::instance()->displaySettings()->setDayStartsAt( calendarStart );
363         Configuration::instance()->displaySettings()->setDayEndsAt( calendarEnd );
364         
365         // set power save settings
366         Configuration::instance()->startupSettings()->setPowersavingEnabled( powerSaveEnabled );
367         Configuration::instance()->startupSettings()->setTurnOnAt( powerSaveStart );
368         Configuration::instance()->startupSettings()->setTurnOffAt( powerSaveEnd );
369         
370         // save configuration
371         Configuration::instance()->save();
372         
373         // Emit the signal to notify that ok is pressed and data is saved.
374         setValues();
375         emit okClicked();
376 }
377
378 void SettingsView::viewResized(const QSize &newSize, const QSize &oldSize)
379 {
380         if ( oldSize.height() > newSize.height() )
381         {
382                 // Get the button's height and add that to the new size so that
383                 // the ok button is hidden under the keyboard.
384                 size().rheight() += iOkButton->size().height();
385         }
386 }
387
388 void SettingsView::handleCancelClicked()
389 {
390         setValues();
391         emit cancelClicked();
392 }
393
394 void SettingsView::setValues()
395 {
396         // set user information
397         iUserName->setText( Configuration::instance()->getUsername() );
398         iPassword->setText( Configuration::instance()->getPassword());//connectionSettings()->password() );
399         // set server information
400         iServerAddress->setText( Configuration::instance()->getServerUrl().toString());//connectionSettings()->serverUrl().toString() );
401         QString refreshIntervalStr;
402         refreshIntervalStr.setNum( Configuration::instance()->getRefreshinterval());//connectionSettings()->refreshInterval() );
403         iRefreshInterval->setText( refreshIntervalStr );
404         // set weekly view display settings
405         if ( Configuration::instance()->displaySettings()->daysInSchedule() == DisplaySettings::WeekdaysInSchedule )
406         {
407                 iFiveDays->setChecked( true );
408                 iSevenDays->setChecked( false );
409         }
410         else
411         {
412                 iFiveDays->setChecked( false );
413                 iSevenDays->setChecked( true );
414         }
415         iDayStartTime->setTime( Configuration::instance()->displaySettings()->dayStartsAt() );
416         iDayEndTime->setTime( Configuration::instance()->displaySettings()->dayEndsAt() );
417         // set startup settings
418         if ( Configuration::instance()->startupSettings()->isPowersavingEnabled() )
419         {
420                 iPowerSaveEnabled->setChecked( true );
421         }
422         else
423         {
424                 iPowerSaveEnabled->setChecked( false );
425         }
426         iPowerSaveStartTime->setTime( Configuration::instance()->startupSettings()->turnOnAt() );
427         iPowerSaveEndTime->setTime( Configuration::instance()->startupSettings()->turnOffAt() );
428 }