tzselect.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #define QTOPIA_INTERNAL_TZSELECT_INC_LOCAL
00022
00023 #include "tzselect.h"
00024 #include "resource.h"
00025 #include "global.h"
00026 #include "config.h"
00027 #include <qtoolbutton.h>
00028 #include <qfile.h>
00029 #include <stdlib.h>
00030
00031 #include <qcopchannel_qws.h>
00032 #include <qpe/qpeapplication.h>
00033 #include <qmessagebox.h>
00034
00043 class TimeZoneSelectorPrivate
00044 {
00045 public:
00046 TimeZoneSelectorPrivate() : includeLocal(FALSE) {}
00047 bool includeLocal;
00048 };
00049
00050 TZCombo::TZCombo( QWidget *p, const char* n )
00051 : QComboBox( p, n )
00052 {
00053 updateZones();
00054
00055 QString tz = getenv("TZ");
00056 if (parent()->inherits("TimeZoneSelector")) {
00057 if ( ((TimeZoneSelector *)parent())->localIncluded() ) {
00058
00059 tz = "None";
00060 }
00061 }
00062 if ( !tz.isNull() ) {
00063 int n = 0,
00064 index = 0;
00065 for ( QStringList::Iterator it=identifiers.begin();
00066 it!=identifiers.end(); ++it) {
00067 if ( *it == tz )
00068 index = n;
00069 n++;
00070 }
00071 setCurrentItem(index);
00072 } else {
00073 setCurrentItem(0);
00074 }
00075
00076
00077
00078
00079 #if !defined(QT_NO_COP)
00080 QCopChannel *channel = new QCopChannel( "QPE/System", this );
00081 connect( channel, SIGNAL(received(const QCString&, const QByteArray&)),
00082 this, SLOT(handleSystemChannel(const QCString&, const QByteArray&)) );
00083 #endif
00084
00085
00086 }
00087
00088 TZCombo::~TZCombo()
00089 {
00090 }
00091
00092 void TZCombo::updateZones()
00093 {
00094 QString cur = currentText();
00095 clear();
00096 identifiers.clear();
00097 int curix=0;
00098 QString tz = getenv("TZ");
00099 bool tzFound = FALSE;
00100 Config cfg("CityTime");
00101 cfg.setGroup("TimeZones");
00102 int listIndex = 0;
00103 if (parent()->inherits("TimeZoneSelector")) {
00104 if ( ((TimeZoneSelector *)parent())->localIncluded() ) {
00105
00106 identifiers.append( "None" );
00107 insertItem( tr("None") );
00108 if ( cur == tr("None"))
00109 curix = 0;
00110 listIndex++;
00111 }
00112 }
00113 int cfgIndex = 0;
00114 while (1) {
00115 QString zn = cfg.readEntry("Zone"+QString::number(cfgIndex), QString::null);
00116 if ( zn.isNull() )
00117 break;
00118 if ( zn == tz )
00119 tzFound = TRUE;
00120 QString nm = cfg.readEntry("ZoneName"+QString::number(cfgIndex));
00121 identifiers.append(zn);
00122 insertItem(nm);
00123 if ( nm == cur )
00124 curix = listIndex;
00125 ++cfgIndex;
00126 ++listIndex;
00127 }
00128 if ( !listIndex ) {
00129 QStringList list = timezoneDefaults();
00130 for ( QStringList::Iterator it = list.begin(); it!=list.end(); ++it ) {
00131 QString zn = *it;
00132 QString nm = *++it;
00133 if ( zn == tz )
00134 tzFound = TRUE;
00135 if ( nm == cur )
00136 curix = listIndex;
00137 identifiers.append(zn);
00138 insertItem(nm);
00139 ++listIndex;
00140 }
00141 }
00142 for (QStringList::Iterator it=extras.begin(); it!=extras.end(); ++it) {
00143 insertItem(*it);
00144 identifiers.append(*it);
00145 if ( *it == cur )
00146 curix = listIndex;
00147 ++listIndex;
00148 }
00149 if ( !tzFound && !tz.isEmpty()) {
00150 int i = tz.find( '/' );
00151 QString nm = tz.mid( i+1 ).replace(QRegExp("_"), " ");
00152 identifiers.append(tz);
00153 insertItem(nm);
00154 if ( nm == cur )
00155 curix = listIndex;
00156 ++listIndex;
00157 }
00158 setCurrentItem(curix);
00159 }
00160
00161
00162 void TZCombo::keyPressEvent( QKeyEvent *e )
00163 {
00164
00165
00166 QComboBox::keyPressEvent(e);
00167 }
00168
00169 void TZCombo::mousePressEvent(QMouseEvent*e)
00170 {
00171
00172
00173 QComboBox::mousePressEvent(e);
00174 }
00175
00176 QString TZCombo::currZone() const
00177 {
00178 return identifiers[currentItem()];
00179 }
00180
00181 void TZCombo::setCurrZone( const QString& id )
00182 {
00183 for (int i=0; i< count(); i++) {
00184 if ( identifiers[i] == id ) {
00185 setCurrentItem(i);
00186 return;
00187 }
00188 }
00189 insertItem(id);
00190 setCurrentItem( count() - 1);
00191 identifiers.append(id);
00192 extras.append(id);
00193 }
00194
00195
00196
00197 void TZCombo::handleSystemChannel(const QCString&msg, const QByteArray&)
00198 {
00199 if ( msg == "timeZoneListChange()" ) {
00200 updateZones();
00201 }
00202 }
00203
00209 TimeZoneSelector::TimeZoneSelector(QWidget* p, const char* n) :
00210 QHBox(p,n)
00211 {
00212 d = new TimeZoneSelectorPrivate();
00213
00214 cmbTz = new TZCombo( this, "timezone combo" );
00215
00216 cmdTz = new QToolButton( this, "timezone button" );
00217 cmdTz->setIconSet( Resource::loadIconSet( "citytime_icon" ) );
00218 cmdTz->setMaximumSize( cmdTz->sizeHint() );
00219
00220
00221
00222 QObject::connect( cmbTz, SIGNAL( activated( int ) ),
00223 this, SLOT( slotTzActive( int ) ) );
00224 QObject::connect( cmdTz, SIGNAL( clicked() ),
00225 this, SLOT( slotExecute() ) );
00226 }
00227
00231 TimeZoneSelector::~TimeZoneSelector()
00232 {
00233 }
00234
00235 void TimeZoneSelector::setLocalIncluded(bool b)
00236 {
00237 d->includeLocal = b;
00238 cmbTz->updateZones();
00239 }
00240
00241 bool TimeZoneSelector::localIncluded() const
00242 {
00243 return d->includeLocal;
00244 }
00245
00250 QString TimeZoneSelector::currentZone() const
00251 {
00252 return cmbTz->currZone();
00253 }
00254
00258 void TimeZoneSelector::setCurrentZone( const QString& id )
00259 {
00260 cmbTz->setCurrZone( id );
00261 }
00268 void TimeZoneSelector::slotTzActive( int )
00269 {
00270 emit signalNewTz( cmbTz->currZone() );
00271 }
00272
00273 void TimeZoneSelector::slotExecute( void )
00274 {
00275
00276 if (QFile::exists(QPEApplication::qpeDir()+"bin/citytime"))
00277 Global::execute( "citytime" );
00278 else
00279 QMessageBox::warning(this,tr("citytime executable not found"),
00280 tr("In order to choose the time zones,\nplease install citytime."));
00281 }
00282
00283 QStringList timezoneDefaults( void )
00284 {
00285 QStringList tzs;
00286
00287 tzs.append( "America/New_York" );
00288 tzs.append( "New York" );
00289 tzs.append( "America/Los_Angeles" );
00290 tzs.append( "Los Angeles" );
00291 tzs.append( "Australia/Brisbane" );
00292 tzs.append( "Brisbane" );
00293 tzs.append( "Europe/Berlin" );
00294 tzs.append( "Berlin" );
00295 tzs.append( "Asia/Tokyo" );
00296 tzs.append( "Tokyo" );
00297 tzs.append( "America/Denver" );
00298 tzs.append( "Denver" );
00299
00300 return tzs;
00301 }
00302
00303
This file is part of the documentation for OPIE Version 1.5.5.