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