summaryrefslogtreecommitdiff
path: root/core/settings/citytime/citytime.cpp
Unidiff
Diffstat (limited to 'core/settings/citytime/citytime.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--core/settings/citytime/citytime.cpp272
1 files changed, 272 insertions, 0 deletions
diff --git a/core/settings/citytime/citytime.cpp b/core/settings/citytime/citytime.cpp
new file mode 100644
index 0000000..b2f9f14
--- a/dev/null
+++ b/core/settings/citytime/citytime.cpp
@@ -0,0 +1,272 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21#include "zonemap.h"
22#include "citytime.h"
23
24#include <qpe/qpeapplication.h>
25#include <qpe/config.h>
26#include <qpe/timestring.h>
27#include <qpe/tzselect.h>
28#if ( defined Q_WS_QWS || defined(_WS_QWS_) ) && !defined(QT_NO_COP)
29#include <qpe/qcopenvelope_qws.h>
30#endif
31
32
33#include <qdir.h>
34#include <qfile.h>
35#include <qlabel.h>
36#include <qmessagebox.h>
37#include <qregexp.h>
38#include <qtextstream.h>
39#include <qtoolbutton.h>
40
41#include <stdlib.h>
42
43CityTime::CityTime( QWidget *parent, const char* name,
44 WFlags fl )
45 : CityTimeBase( parent, name, fl ),
46 strRealTz(0),
47 bAdded(false)
48{
49 Config config( "qpe" );
50 bWhichClock = config.readBoolEntry( "AMPM", TRUE );
51 frmMap->changeClock( bWhichClock );
52
53 char *pEnv;
54 pEnv = NULL;
55 pEnv = getenv("TZ");
56 if ( pEnv )
57 strRealTz = pEnv;
58 pEnv = NULL;
59 pEnv = getenv("HOME");
60 if ( pEnv )
61 strHome = pEnv;
62 // append the labels to their respective lists...
63 listCities.setAutoDelete( true );
64 listTimes.setAutoDelete( true );
65
66 listCities.append( cmdCity1 );
67 listCities.append( cmdCity2 );
68 listCities.append( cmdCity3 );
69
70 listTimes.append( lblCTime1 );
71 listTimes.append( lblCTime2 );
72 listTimes.append( lblCTime3 );
73
74
75 // kludgy way of getting the screen size so we don't have to depend
76 // on a resize event...
77 QWidget *d = QApplication::desktop();
78 if ( d->width() < d->height() ) {
79 // append for that 4 down look
80
81 listCities.append( cmdCity4 );
82 listCities.append( cmdCity5 );
83 listCities.append( cmdCity6 );
84 listTimes.append( lblCTime4 );
85 listTimes.append( lblCTime5 );
86 listTimes.append( lblCTime6 );
87 lblCTime7->hide();
88 lblCTime8->hide();
89 lblCTime9->hide();
90 cmdCity7->hide();
91 cmdCity8->hide();
92 cmdCity9->hide();
93 } else {
94 listCities.append( cmdCity7 );
95 listCities.append( cmdCity8 );
96 listCities.append( cmdCity9 );
97 listTimes.append( lblCTime7 );
98 listTimes.append( lblCTime8 );
99 listTimes.append( lblCTime9 );
100 lblCTime4->hide();
101 lblCTime5->hide();
102 lblCTime6->hide();
103 cmdCity4->hide();
104 cmdCity5->hide();
105 cmdCity6->hide();
106 }
107 bAdded = true;
108 readInTimes();
109 changed = FALSE;
110 QObject::connect( qApp, SIGNAL( clockChanged(bool) ),
111 this, SLOT( changeClock(bool) ) );
112 // now start the timer so we can update the time quickly every second
113 timerEvent( 0 );
114}
115
116CityTime::~CityTime()
117{
118 if ( changed ) {
119 Config cfg("CityTime");
120 cfg.setGroup("TimeZones");
121 QListIterator<QToolButton> itCity( listCities );
122 int i;
123 bool realTzWritten = FALSE;
124 for ( i = 0, itCity.toFirst(); i < CITIES; i++, ++itCity ) {
125 if ( !strCityTz[i].isNull() ) {
126 cfg.writeEntry("Zone"+QString::number(i), strCityTz[i]);
127 cfg.writeEntry("ZoneName"+QString::number(i), itCity.current()->text());
128 if ( strCityTz[i] == strRealTz )
129 realTzWritten = TRUE;
130 }
131 }
132 if ( realTzWritten ) {
133 cfg.removeEntry("Zone"+QString::number(CITIES));
134 cfg.removeEntry("ZoneName"+QString::number(CITIES));
135 } else {
136 cfg.writeEntry("Zone"+QString::number(CITIES), strRealTz);
137 if ( nameRealTz.isEmpty() ) {
138 int i = strRealTz.find( '/' );
139 nameRealTz = strRealTz.mid( i+1 );
140 }
141 cfg.writeEntry("ZoneName"+QString::number(CITIES), nameRealTz);
142 }
143 QCopEnvelope ( "QPE/System", "timeZoneListChange()" );
144
145 changed = FALSE;
146 }
147 // restore the timezone, just in case we messed with it and
148 // are destroyed at an inoppurtune moment
149 if ( !strRealTz.isNull() ) {
150 // this should be checked, but there is not much that can done at this
151 //point if it fails
152 setenv( "TZ", strRealTz, true );
153 }
154}
155
156void CityTime::timerEvent( QTimerEvent *e )
157{
158 if ( e )
159 killTimer( timerId );
160 // change the time again!!
161 showTime();
162 int ms = 1000 - QTime::currentTime().msec();
163 timerId = startTimer( ms );
164}
165
166void CityTime::mousePressEvent( QMouseEvent * )
167{
168 // DEBUG enable this to get a look at the zone information DEBUG
169// frmMap->showZones();
170}
171
172void CityTime::showTime( void )
173{
174 int i;
175 QListIterator<QLabel> itTime(listTimes);
176
177 // traverse the list...
178 for ( i = 0, itTime.toFirst(); i < CITIES; i++, ++itTime) {
179 if ( !strCityTz[i].isNull() ) {
180 if ( setenv( "TZ", strCityTz[i], true ) == 0 ) {
181 itTime.current()->setText( TimeString::shortTime(bWhichClock) );
182 } else {
183 QMessageBox::critical( this, tr( "Time Changing" ),
184 tr( "There was a problem setting timezone %1" )
185 .arg( QString::number( i + 1 ) ) );
186 }
187 }
188 }
189 // done playing around... put it all back
190 unsetenv( "TZ" );
191 if ( !strRealTz.isNull() ) {
192 if ( setenv( "TZ", strRealTz, true ) != 0 ) {
193 QMessageBox::critical( this, tr( "Restore Time Zone" ),
194 tr( "There was a problem setting your timezone."
195 "Your time may be wrong now..." ) );
196 }
197 }
198}
199
200void CityTime::beginNewTz()
201{
202 frmMap->setFocus();
203}
204
205void CityTime::slotNewTz( const QString & strNewCountry,
206 const QString & strNewCity )
207{
208 // determine what to do based on what putton is pressed...
209 QListIterator<QToolButton> itCity(listCities);
210 int i;
211 // go through the list and make adjustments based on which button is on
212 for ( i = 0, itCity.toFirst(); itCity.current(), i < CITIES; i++, ++itCity ) {
213 QToolButton *cmdTmp = itCity.current();
214 if ( cmdTmp->isOn() ) {
215 strCityTz[i] = strNewCountry + strNewCity;
216 QString s = strNewCity;
217 cmdTmp->setText( s.replace( QRegExp("_"), " " ) );
218 cmdTmp->toggle();
219 // we can actually break, since there is only one button
220 // that is ever pressed!
221 changed = TRUE;
222 break;
223 }
224 }
225 showTime();
226}
227
228void CityTime::readInTimes( void )
229{
230 Config cfg("CityTime");
231 cfg.setGroup("TimeZones");
232 QListIterator<QToolButton> itCity( listCities );
233 int i=0;
234 nameRealTz = QString::null;
235 QString zn;
236 for ( ; i < CITIES ; i++ ) {
237 zn = cfg.readEntry("Zone"+QString::number(i), QString::null);
238 if ( zn.isNull() )
239 break;
240 QString nm = cfg.readEntry("ZoneName"+QString::number(i));
241 strCityTz[i] = zn;
242 itCity.current()->setText(nm);
243 if ( zn == strRealTz )
244 nameRealTz = nm;
245 ++itCity;
246 }
247 if ( i == 0 ) {
248 // write in our own in a shameless self promotion and some humor
249 QStringList list = timezoneDefaults();
250 int i;
251 QStringList::Iterator it = list.begin();
252 for ( i = 0, itCity.toFirst(); i < CITIES && itCity.current();
253 i++, ++itCity ) {
254 strCityTz[i] = *it++;
255 itCity.current()->setText( *it++ );
256 }
257 }
258 if ( nameRealTz.isEmpty() ) {
259 //remember the current time zone even if we don't have room
260 //to show it.
261 zn = cfg.readEntry("Zone"+QString::number(CITIES), QString::null);
262 if ( zn == strRealTz )
263 nameRealTz = cfg.readEntry("ZoneName"+QString::number(CITIES));
264 i++;
265 }
266}
267
268void CityTime::changeClock( bool newClock )
269{
270 bWhichClock = newClock;
271 showTime();
272}