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