summaryrefslogtreecommitdiff
path: root/noncore/settings/netsystemtime/settingstabwidget.cpp
blob: 5b52a0783b14e69eb2d1fbb01040cc47d64710d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
                             This file is part of the Opie Project

                             Copyright (C) Opie Team <opie-devel@handhelds.org>
              =.
            .=l.
           .>+-=
 _;:,     .>    :=|.         This program is free software; you can
.> <`_,   >  .   <=          redistribute it and/or  modify it under
:`=1 )Y*s>-.--   :           the terms of the GNU Library General Public
.="- .-=="i,     .._         License as published by the Free Software
 - .   .-<_>     .<>         Foundation; either version 2 of the License,
     ._= =}       :          or (at your option) any later version.
    .%`+i>       _;_.
    .i_,=:_.      -<s.       This program is distributed in the hope that
     +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
    : ..    .:,     . . .    without even the implied warranty of
    =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
  _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
..}^=.=       =       ;      Library General Public License for more
++=   -.     .`     .:       details.
:     =  ...= . :.=-
 -.   .:....=;==+<;          You should have received a copy of the GNU
  -_. . .   )=.  =           Library General Public License along with
    --        :-=`           this library; see the file COPYING.LIB.
                             If not, write to the Free Software Foundation,
                             Inc., 59 Temple Place - Suite 330,
                             Boston, MA 02111-1307, USA.
*/

#include "settingstabwidget.h"

#include <qpe/config.h>
#include <qpe/qpeapplication.h>

#include <qcheckbox.h>
#include <qcombobox.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qscrollview.h>
#include <qspinbox.h>

SettingsTabWidget::SettingsTabWidget( QWidget *parent )
	: QWidget( parent, 0x0, 0 )
{
	QVBoxLayout *tmpvb = new QVBoxLayout( this );
	QScrollView *sv = new QScrollView( this );
	tmpvb->addWidget( sv, 0, 0 );
	sv->setResizePolicy( QScrollView::AutoOneFit );
	sv->setFrameStyle( QFrame::NoFrame );
	QWidget *container = new QWidget( sv->viewport() );
	sv->addChild( container );
    
	QGridLayout *layout = new QGridLayout( container );
	layout->setMargin( 2 );
	layout->setSpacing( 4 );

	// Time server selector
	layout->addWidget( new QLabel( tr( "Time server" ), container ), 0, 0 );
	cbTimeServer = new QComboBox( TRUE, container );
	layout->addMultiCellWidget( cbTimeServer, 1, 1, 0, 1 );

	// Lookup delay selector
	layout->addWidget( new QLabel( tr( "minutes between time updates" ), container ), 2, 1 );
	sbNtpDelay = new QSpinBox( 1, 9999999, 1, container );
	sbNtpDelay->setWrapping( TRUE );
	sbNtpDelay->setMaximumWidth( 50 );
	connect( sbNtpDelay, SIGNAL(valueChanged(int)), this, SIGNAL(ntpDelayChanged(int)) );
	layout->addWidget( sbNtpDelay, 2, 0 );

	// Prediction delay selector
	layout->addWidget( new QLabel( tr( "minutes between prediction updates" ), container ), 3, 1 );
	sbPredictDelay = new QSpinBox( 42, 9999999, 1, container );
	sbPredictDelay->setWrapping( TRUE );
	sbPredictDelay->setMaximumWidth( 50 );
	layout->addWidget( sbPredictDelay, 3, 0 );

	// Space filler
	layout->addItem( new QSpacerItem( 1, 1, QSizePolicy::Minimum, QSizePolicy::Expanding ), 4, 0 );

	// Show system time options on every restart
	chShowOnRestart = new QCheckBox( tr( "Show time settings on every restart" ), container );
	layout->addMultiCellWidget( chShowOnRestart, 5, 5, 0, 1 );

	// Display time server information selector
	chNtpTab = new QCheckBox( tr( "Display time server information" ), container );
	connect( chNtpTab, SIGNAL( toggled(bool) ), this, SIGNAL( displayNTPTab(bool) ) );
	layout->addMultiCellWidget( chNtpTab, 6, 6, 0, 1 );

	// Display time prediction information selector
	chPredictTab = new QCheckBox( tr( "Display time prediction information" ), container );
	connect( chPredictTab, SIGNAL( toggled(bool) ), this, SIGNAL( displayPredictTab(bool) ) );
	layout->addMultiCellWidget( chPredictTab, 7, 7, 0, 1 );

	// Space filler
	layout->addItem( new QSpacerItem( 1, 1, QSizePolicy::Minimum, QSizePolicy::Expanding ), 8, 0 );

	// Initialize values
	QString ntpSrvsFile = QPEApplication::qpeDir();
	ntpSrvsFile.append( "etc/ntpservers" );
	Config ntpSrvs( ntpSrvsFile, Config::File );
	ntpSrvs.setGroup( "servers" );
	int srvCount = ntpSrvs.readNumEntry( "count", 0 );
	for ( int i = 0; i < srvCount; i++ )
	{
		ntpSrvs.setGroup( QString::number( i ) );
		cbTimeServer->insertItem( ntpSrvs.readEntry( "name" ) );
	}
	if ( srvCount==0 ) {
		cbTimeServer->insertItem( "0.pool.ntp.org" );
    cbTimeServer->insertItem( "1.pool.ntp.org" );
    cbTimeServer->insertItem( "2.pool.ntp.org" );
  }
	Config config( "ntp" );
	config.setGroup( "settings" );
	sbPredictDelay->setValue( config.readNumEntry( "minLookupDiff", 720 ) );
	sbNtpDelay->setValue( config.readNumEntry( "ntpRefreshFreq", 1440 ) );
	cbTimeServer->setCurrentItem( config.readNumEntry( "ntpServer", 0 ) );
	chNtpTab->setChecked( config.readBoolEntry( "displayNtpTab", FALSE ) );
	chPredictTab->setChecked( config.readBoolEntry( "displayPredictTab", FALSE ) );

	Config config_qpe( "qpe" );
	config_qpe.setGroup( "Startup" );
	chShowOnRestart->setChecked( config_qpe.readBoolEntry( "ShowTimeSettings", FALSE ) );
}

SettingsTabWidget::~SettingsTabWidget()
{
}

void SettingsTabWidget::saveSettings()
{
	int srvCount = cbTimeServer->count();
	bool serversChanged = TRUE;
	int curSrv = cbTimeServer->currentItem();
	QString edit = cbTimeServer->currentText();
	for ( int i = 0; i < srvCount; i++ )
	{
		if ( edit == cbTimeServer->text( i ) )
			serversChanged = FALSE;
	}
	if ( serversChanged )
	{
		QString ntpSrvsFile = QPEApplication::qpeDir();
		ntpSrvsFile.append( "etc/ntpservers" );
		Config ntpSrvs( ntpSrvsFile, Config::File );
		ntpSrvs.setGroup( "servers" );
		ntpSrvs.writeEntry( "count", ++srvCount );
		ntpSrvs.setGroup( "0" );
		ntpSrvs.writeEntry( "name", edit );
		curSrv = 0;
		for ( int i = 1; i < srvCount; i++ )
		{
//			odebug << "ntpSrvs[" << i << "/" << srvCount << "]=" << cbTimeServer->text( i ).latin1() << "" << oendl; 
			ntpSrvs.setGroup( QString::number( i ) );
			ntpSrvs.writeEntry( "name", cbTimeServer->text( i-1 ) );
		}
	}
	Config config( "ntp", Config::User );
	config.setGroup( "settings" );
	config.writeEntry( "ntpServer", curSrv );
	config.writeEntry( "minLookupDiff", sbPredictDelay->value() );
	config.writeEntry( "ntpRefreshFreq", sbNtpDelay->value() );
	config.writeEntry( "displayNtpTab", chNtpTab->isChecked() );
	config.writeEntry( "displayPredictTab", chPredictTab->isChecked() );

	Config config_qpe( "qpe" );
	config_qpe.setGroup( "Startup" );
	config_qpe.writeEntry( "ShowTimeSettings", chShowOnRestart->isChecked() );
}

QString SettingsTabWidget::ntpServer()
{
	return cbTimeServer->currentText();
}