00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "zonemap.h"
00024 #include "citytime.h"
00025
00026
00027 #include <qpe/qpeapplication.h>
00028 #include <qpe/config.h>
00029 #include <qpe/tzselect.h>
00030 #if !defined(QT_NO_COP)
00031 #include <qpe/qcopenvelope_qws.h>
00032 #endif
00033 #include <opie2/odebug.h>
00034
00035
00036 #include <qlabel.h>
00037 #include <qmessagebox.h>
00038 #include <qtoolbutton.h>
00039 #include <qlayout.h>
00040
00041
00042 #include <stdlib.h>
00043
00044 CityTime::CityTime( QWidget *parent, const char* name,
00045 WFlags fl )
00046 : CityTimeBase( parent, name, fl ),
00047 strRealTz(0),
00048 bAdded(false)
00049 {
00050 Config config( "qpe" );
00051 config.setGroup( "Time" );
00052 bWhichClock = config.readBoolEntry( "AMPM", TRUE );
00053 odebug << QString("%1").arg(bWhichClock) << oendl;
00054 frmMap->changeClock( bWhichClock );
00055
00056 char *pEnv;
00057 pEnv = NULL;
00058 pEnv = getenv("TZ");
00059 if ( pEnv )
00060 strRealTz = pEnv;
00061 pEnv = NULL;
00062 pEnv = getenv("HOME");
00063 if ( pEnv )
00064 strHome = pEnv;
00065
00066 listCities.setAutoDelete( true );
00067 listTimes.setAutoDelete( true );
00068
00069 listCities.append( cmdCity1 );
00070 listCities.append( cmdCity2 );
00071 listCities.append( cmdCity3 );
00072
00073 listTimes.append( lblCTime1 );
00074 listTimes.append( lblCTime2 );
00075 listTimes.append( lblCTime3 );
00076
00077
00078
00079
00080 QWidget *d = QApplication::desktop();
00081 if ( d->width() < d->height() ) {
00082
00083 listCities.append( cmdCity4 );
00084 listCities.append( cmdCity5 );
00085 listCities.append( cmdCity6 );
00086 listTimes.append( lblCTime4 );
00087 listTimes.append( lblCTime5 );
00088 listTimes.append( lblCTime6 );
00089 lblCTime7->hide();
00090 lblCTime8->hide();
00091 lblCTime9->hide();
00092 cmdCity7->hide();
00093 cmdCity8->hide();
00094 cmdCity9->hide();
00095 } else {
00096 listCities.append( cmdCity7 );
00097 listCities.append( cmdCity8 );
00098 listCities.append( cmdCity9 );
00099 listTimes.append( lblCTime7 );
00100 listTimes.append( lblCTime8 );
00101 listTimes.append( lblCTime9 );
00102 lblCTime4->hide();
00103 lblCTime5->hide();
00104 lblCTime6->hide();
00105 cmdCity4->hide();
00106 cmdCity5->hide();
00107 cmdCity6->hide();
00108 }
00109
00110 selWidget = frmMap->selectionWidget( this );
00111 selWidget->hide();
00112 CityTimeBaseLayout->addWidget( selWidget );
00113 bAdded = true;
00114 readInTimes();
00115 changed = FALSE;
00116 QObject::connect( qApp, SIGNAL( clockChanged(bool) ),
00117 this, SLOT( changeClock(bool) ) );
00118
00119 timerEvent( 0 );
00120 }
00121
00122 CityTime::~CityTime()
00123 {
00124 if ( changed ) {
00125 Config cfg("CityTime");
00126 cfg.setGroup("TimeZones");
00127 QListIterator<QToolButton> itCity( listCities );
00128 int i;
00129 bool realTzWritten = FALSE;
00130 for ( i = 0, itCity.toFirst(); i < CITIES; i++, ++itCity ) {
00131 if ( !strCityTz[i].isNull() ) {
00132 cfg.writeEntry("Zone"+QString::number(i), strCityTz[i]);
00133 cfg.writeEntry("ZoneName"+QString::number(i), itCity.current()->text());
00134 if ( strCityTz[i] == strRealTz )
00135 realTzWritten = TRUE;
00136 }
00137 }
00138 if ( realTzWritten ) {
00139 cfg.removeEntry("Zone"+QString::number(CITIES));
00140 cfg.removeEntry("ZoneName"+QString::number(CITIES));
00141 } else {
00142 cfg.writeEntry("Zone"+QString::number(CITIES), strRealTz);
00143 if ( nameRealTz.isEmpty() ) {
00144 int i = strRealTz.find( '/' );
00145 nameRealTz = strRealTz.mid( i+1 );
00146 }
00147 cfg.writeEntry("ZoneName"+QString::number(CITIES), nameRealTz);
00148 }
00149 QCopEnvelope ( "QPE/System", "timeZoneListChange()" );
00150
00151 changed = FALSE;
00152 }
00153
00154
00155 if ( !strRealTz.isNull() ) {
00156
00157
00158 setenv( "TZ", strRealTz, true );
00159 }
00160 }
00161
00162 void CityTime::timerEvent( QTimerEvent *e )
00163 {
00164 if ( e )
00165 killTimer( timerId );
00166
00167 showTime();
00168 int ms = 1000 - QTime::currentTime().msec();
00169 timerId = startTimer( ms );
00170 }
00171
00172 void CityTime::mousePressEvent( QMouseEvent * )
00173 {
00174
00175
00176 }
00177
00178 void CityTime::showTime( void )
00179 {
00180 int i;
00181 QListIterator<QLabel> itTime(listTimes);
00182
00183
00184 for ( i = 0, itTime.toFirst(); i < CITIES; i++, ++itTime) {
00185 if ( !strCityTz[i].isNull() ) {
00186 if ( setenv( "TZ", strCityTz[i], true ) == 0 ) {
00187 itTime.current()->setText( TimeString::shortTime( bWhichClock ) );
00188 } else {
00189 QMessageBox::critical( this, tr( "Time Changing" ),
00190 tr( "There was a problem setting timezone %1" )
00191 .arg( QString::number( i + 1 ) ) );
00192 }
00193 }
00194 }
00195
00196 unsetenv( "TZ" );
00197 if ( !strRealTz.isNull() ) {
00198 if ( setenv( "TZ", strRealTz, true ) != 0 ) {
00199 QMessageBox::critical( this, tr( "Restore Time Zone" ),
00200 tr( "There was a problem setting your timezone."
00201 "Your time may be wrong now..." ) );
00202 }
00203 }
00204 }
00205
00206 void CityTime::beginNewTz()
00207 {
00208 buttonWidget->hide();
00209 frmMap->setFocus();
00210 selWidget->show();
00211
00212 }
00213
00214 void CityTime::slotNewTz( const QString & strNewCountry,
00215 const QString & strNewCity )
00216 {
00217
00218 QListIterator<QToolButton> itCity(listCities);
00219 int i;
00220
00221 for ( i = 0, itCity.toFirst(); itCity.current(), i < CITIES; i++, ++itCity ) {
00222 QToolButton *cmdTmp = itCity.current();
00223 if ( cmdTmp->isOn() ) {
00224 strCityTz[i] = strNewCountry + strNewCity;
00225 QString s = strNewCity;
00226 cmdTmp->setText( s.replace( QRegExp("_"), " " ) );
00227 cmdTmp->toggle();
00228
00229
00230 changed = TRUE;
00231 break;
00232 }
00233 }
00234 showTime();
00235 buttonWidget->show();
00236 selWidget->hide();
00237 }
00238
00239 void CityTime::readInTimes( void )
00240 {
00241 Config cfg("CityTime");
00242 cfg.setGroup("TimeZones");
00243 QListIterator<QToolButton> itCity( listCities );
00244 int i=0;
00245 nameRealTz = QString::null;
00246 QString zn;
00247 for ( ; i < CITIES ; i++ ) {
00248 zn = cfg.readEntry("Zone"+QString::number(i), QString::null);
00249 if ( zn.isNull() )
00250 break;
00251 QString nm = cfg.readEntry("ZoneName"+QString::number(i));
00252 strCityTz[i] = zn;
00253 itCity.current()->setText(nm);
00254 if ( zn == strRealTz )
00255 nameRealTz = nm;
00256 ++itCity;
00257 }
00258 if ( i == 0 ) {
00259
00260 QStringList list = timezoneDefaults();
00261 int i;
00262 QStringList::Iterator it = list.begin();
00263 for ( i = 0, itCity.toFirst(); i < CITIES && itCity.current();
00264 i++, ++itCity ) {
00265 strCityTz[i] = *it++;
00266 itCity.current()->setText( *it++ );
00267 }
00268 }
00269 if ( nameRealTz.isEmpty() ) {
00270
00271
00272 zn = cfg.readEntry("Zone"+QString::number(CITIES), QString::null);
00273 if ( zn == strRealTz )
00274 nameRealTz = cfg.readEntry("ZoneName"+QString::number(CITIES));
00275 i++;
00276 }
00277 }
00278
00279 void CityTime::changeClock( bool newClock )
00280 {
00281 bWhichClock = newClock;
00282 showTime();
00283 }