summaryrefslogtreecommitdiff
path: root/noncore/settings/netsystemtime/timetabwidget.cpp
blob: e6fcf7f39fa52331af2242b4b4b6e5c3c3364ccb (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
                             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 "timetabwidget.h"

#include <opie2/oresource.h>

#include <qpe/applnk.h>
#include <qpe/config.h>
#include <qpe/datebookmonth.h>
#include <qpe/global.h>
#include <qpe/tzselect.h>

#if ( defined Q_WS_QWS || defined(_WS_QWS_) ) && !defined(QT_NO_COP)
#include <qpe/qcopenvelope_qws.h>
#endif

#include <qcombobox.h>
#include <qdatetime.h>
#include <qframe.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qpushbutton.h>
#include <qscrollview.h>
#include <qspinbox.h>
#include <qmessagebox.h>
#include <qfile.h>

#include <stdlib.h>
#include <sys/time.h>

static const int ValueAM = 0;
static const int ValuePM = 1;

TimeTabWidget::TimeTabWidget( QWidget *parent )
	: QWidget( parent, 0x0, 0 )
{
	// Synchronize HW clock to systemtime
	// This app will update systemtime
	//  - if Cancel is clicked, will reset systemtime to HW clock's time
	//  - if Ok is clicked, will leave systemtime as is
	system("/sbin/hwclock --systohc --utc");

	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 );

	// Hours
	layout->addMultiCellWidget( new QLabel( tr( "Hour" ), container ), 1, 1, 0, 1 );
	sbHour = new QSpinBox( container );
	sbHour->setWrapping( TRUE );
	layout->addMultiCellWidget( sbHour, 2, 2, 0, 1 );

	// Minutes
	layout->addMultiCellWidget( new QLabel( tr( "Minute" ), container ), 1, 1, 2, 3 );
	sbMin = new QSpinBox( container );
	sbMin->setWrapping( TRUE );
	sbMin->setMinValue( 0 );
	sbMin->setMaxValue( 59 );
	layout->addMultiCellWidget( sbMin, 2, 2, 2, 3 );

	// AM/PM
	cbAmpm = new QComboBox( container );
	cbAmpm->insertItem( tr( "AM" ), ValueAM );
	cbAmpm->insertItem( tr( "PM" ), ValuePM );
	layout->addMultiCellWidget( cbAmpm, 2, 2, 4, 5 );

	// Date
	layout->addWidget( new QLabel( tr( "Date" ), container ), 4, 0 );
	btnDate = new DateButton( TRUE, container );
	layout->addMultiCellWidget( btnDate, 4, 4, 1, 5 );

	// Timezone
	layout->addMultiCellWidget( new QLabel( tr( "Time zone" ), container ), 6, 6, 0, 1 );
	selTimeZone = new TimeZoneSelector( container );
	connect( selTimeZone, SIGNAL(signalNewTz(const QString&)), this, SLOT(slotTZChanged(const QString&)) );
	layout->addMultiCellWidget( selTimeZone, 6, 6, 2, 5 );

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

	// Set NTP time button
    m_ntpBtn = new QPushButton( Opie::Core::OResource::loadPixmap( "netsystemtime/ntptab", Opie::Core::OResource::SmallIcon ),
                                tr( "Get time from the network" ), container );
    m_ntpBtn->setMinimumHeight( AppLnk::smallIconSize()+4 );
	connect( m_ntpBtn, SIGNAL(clicked()), this, SIGNAL(getNTPTime()) );
	layout->addMultiCellWidget( m_ntpBtn, 8, 8, 0, 5 );

	// Set predicted time button
	QPushButton *pb = new QPushButton( Opie::Core::OResource::loadPixmap( "netsystemtime/predicttab", Opie::Core::OResource::SmallIcon ),
                                       tr( "Set predicted time" ), container );
    pb->setMinimumHeight( AppLnk::smallIconSize()+4 );
	connect( pb, SIGNAL(clicked()), this, SIGNAL(getPredictedTime()) );
	layout->addMultiCellWidget( pb, 9, 9, 0, 5 );

	// Space filler at bottom of widget
	layout->addItem( new QSpacerItem( 1, 1, QSizePolicy::Minimum, QSizePolicy::Expanding ), 10, 0 );

	// Initialize values
	Config config( "locale" );
	config.setGroup( "Location" );
	selTimeZone->setCurrentZone( config.readEntry( "Timezone", "America/New_York" ) );
	use12HourTime = FALSE;
	setDateTime( QDateTime::currentDateTime() );
}

TimeTabWidget::~TimeTabWidget()
{
}

void TimeTabWidget::saveSettings( bool commit )
{
	if ( commit )
	{
		// Set timezone and announce to world
		QString tz = selTimeZone->currentZone();
		Config config("locale");
		config.setGroup( "Location" );
		config.writeEntry( "Timezone", tz );
		setenv( "TZ", tz, 1 );
		QCopEnvelope setTimeZone( "QPE/System", "timeChange(QString)" );
		setTimeZone << tz;

		// If controls have a valid date & time, update systemtime
		int hour = sbHour->value();
		if ( use12HourTime && cbAmpm->currentItem() == ValuePM )
			hour += 12;
		QDateTime dt( btnDate->date(), QTime ( hour, sbMin->value(), QTime::currentTime().second() ) );
		setSystemTime( dt );
	}
	else
	{
		// Reset systemtime to hardware clock (i.e. undo any changes made by this app)
		system("/sbin/hwclock --hctosys --utc");
	}
}

void TimeTabWidget::setDateTime( const QDateTime &dt )
{
	// Set time
	QTime t = dt.time();
	if( use12HourTime )
	{
		int show_hour = t.hour();
		if ( t.hour() > 12 )
		{
			show_hour -= 12;
			cbAmpm->setCurrentItem( ValuePM );
		}
		else
		{
			cbAmpm->setCurrentItem( ValueAM );
		}
		if ( show_hour == 0 )
			show_hour = 12;
		sbHour->setValue( show_hour );
	}
	else
	{
		sbHour->setValue( t.hour() );
	}
	sbMin->setValue( t.minute() );

	// Set date
	btnDate->setDate( dt.date() );
}

void TimeTabWidget::setNTPBtnEnabled( bool enabled )
{
    m_ntpBtn->setEnabled( enabled );
}

void TimeTabWidget::setSystemTime( const QDateTime &dt )
{
	// Set system clock
	if ( dt.isValid() )
	{
		struct timeval myTv;
		int t = TimeConversion::toUTC( dt );
			myTv.tv_sec = t;
		myTv.tv_usec = 0;

		if ( myTv.tv_sec != -1 )
                    ::settimeofday( &myTv, 0 );

                /*
                 * Commit the datetime to the  'hardware'
                 * as Global::writeHWClock() is a NOOP with Opie Alarm
                 */
                system("/sbin/hwclock --systohc --utc");
	}
}

void TimeTabWidget::slotUse12HourTime( int i )
{
	use12HourTime = (i == 1);

	cbAmpm->setEnabled( use12HourTime );

	int show_hour = sbHour->value();

	if ( use12HourTime )
	{
		sbHour->setMinValue( 1 );
		sbHour->setMaxValue( 12 );

		if ( show_hour > 12 )
		{
			show_hour -= 12;
			cbAmpm->setCurrentItem( ValuePM );
		}
		else
		{
			cbAmpm->setCurrentItem( ValueAM );
		}
		if ( show_hour == 0 )
			show_hour = 12;
	}
	else
	{
		sbHour->setMinValue( 0 );
		sbHour->setMaxValue( 23 );

		if ( cbAmpm->currentItem() == ValuePM )
		{
			show_hour += 12;
			if ( show_hour == 24 )
				show_hour = 0;
		}
	}

	sbHour->setValue( show_hour );
}

void TimeTabWidget::slotDateFormatChanged( const DateFormat &df )
{
	btnDate->setDateFormat( df );
}

void TimeTabWidget::slotWeekStartChanged( int monday )
{
	btnDate->setWeekStartsMonday( monday );
}

void TimeTabWidget::slotTZChanged( const QString &newtz )
{
	// Check timezone has a valid file in /usr/share/zoneinfo
	if(!QFile::exists("/usr/share/zoneinfo/" + newtz)) {
		QMessageBox::warning(this, tr("Time zone file missing"), 
				(tr("There is no time zone file for the\nselected time zone (%1).\nYou will need to install it before the\nsystem time zone can be set correctly.")).arg(newtz));
	}
	else {
		// If controls have a valid date & time, update systemtime
		int hour = sbHour->value();
		if ( use12HourTime && cbAmpm->currentItem() == ValuePM )
			hour += 12;
		QDateTime dt( btnDate->date(), QTime ( hour, sbMin->value(), QTime::currentTime().second() ) );
		setSystemTime( dt );
		QCopEnvelope setTimeZone( "QPE/System", "timeChange(QString)" );
		setTimeZone << newtz;

		// Set system timezone
		QString currtz = getenv( "TZ" );
		setenv( "TZ", newtz, 1 );

		// Get new date/time
		hour = sbHour->value();
		if ( use12HourTime && cbAmpm->currentItem() == ValuePM )
			hour += 12;
		dt = QDateTime::currentDateTime();

		// Reset system timezone
		if ( !currtz.isNull() )
		{
			setenv( "TZ", currtz, 1 );
		}

		// Set controls to new time
		setDateTime( dt );

		emit tzChanged( newtz );
	}
}