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