panel clock (kclockapplet) patch

Helge Deller deller at gmx.de
Wed Nov 6 21:34:41 GMT 2002


On Wednesday 06 November 2002 19:20, Helge Deller wrote:
> The panel clock (kclockapplet) still has the following (imho important)
> bugs open:
>
> Bug 30869: sometimes kclockapplet shows an incorrect time
> Bug 32912: kclockapplet configuration is not dual display aware
> Bug 42702: Timezone wrong in kclock
> Bug 43586: kicker/clock: multiple applets with different timezones don't
> work

This patch seems to work somewhat more correct and includes changes as 
suggested in feedback by Melchior.

Comments ?

Helge
-------------- next part --------------
Index: clock.cpp
===================================================================
RCS file: /home/kde/kdebase/kicker/applets/clock/clock.cpp,v
retrieving revision 1.97
diff -u -p -r1.97 clock.cpp
--- clock.cpp	2002/11/05 11:06:57	1.97
+++ clock.cpp	2002/11/06 21:32:25
@@ -118,7 +118,7 @@ PlainClock::~PlainClock()
 
 int PlainClock::preferedWidthForHeight(int ) const
 {
-    return kMax(sizeHint().width()-4, 0);
+    return kMax(sizeHint().width()+4, 0);
 }
 
 
@@ -131,7 +131,7 @@ int PlainClock::preferedHeightForWidth(i
 void PlainClock::updateClock()
 {
     QString newStr =
-      KGlobal::locale()->formatTime(QTime::currentTime(),
+      KGlobal::locale()->formatTime(_applet->clockGetTime(),
 		      		_settings->showSeconds());
 
     if (newStr != _timeStr) {
@@ -152,7 +152,7 @@ void PlainClock::forceUpdate()
 DigitalClock::DigitalClock(ClockApplet *applet, ClockSettings* settings, QWidget *parent, const char *name)
     : QLCDNumber(parent, name), ClockWidget(applet, settings)
 {
-    setFrameStyle(Panel | (_settings->showFrame() ? Sunken : 0));
+    setFrameStyle(Panel | Sunken | (_settings->showFrame() ? Box : 0));
     setMargin( 4 );
     setSegmentStyle(QLCDNumber::Flat);
 
@@ -186,7 +186,7 @@ int DigitalClock::preferedWidthForHeight
 int DigitalClock::preferedHeightForWidth(int w) const
 {
     if (w < 0) w = 0;
-   return((w / numDigits() * 2) + 6);
+    return((w / numDigits() * 2) + 6);
 }
 
 
@@ -194,7 +194,7 @@ void DigitalClock::updateClock()
 {
     static bool colon = true;
     QString newStr;
-    QTime t(QTime::currentTime());
+    QTime t(_applet->clockGetTime());
 
     int h = t.hour();
     int m = t.minute();
@@ -309,7 +309,7 @@ AnalogClock::AnalogClock(ClockApplet *ap
         setBackgroundMode(NoBackground); //prevent flicker
     }
 
-    _time = QTime::currentTime();
+    _time = _applet->clockGetTime();
     _spPx = new QPixmap(size().width() * settings->antialiasFactor(),
                        size().height() * settings->antialiasFactor());
 
@@ -345,10 +345,10 @@ void AnalogClock::initBackgroundPixmap()
 void AnalogClock::updateClock()
 {
     if (!_settings->showSeconds())
-         if (_time.minute() == QTime::currentTime().minute())
+         if (_time.minute() == _applet->clockGetTime().minute())
             return;
 
-    _time = QTime::currentTime();
+    _time = _applet->clockGetTime();
     repaint( false ); //don't erase on redraw
 }
 
@@ -542,7 +542,7 @@ FuzzyClock::FuzzyClock(ClockApplet *appl
             << i18n("Noon") << i18n("Afternoon") << i18n("Evening")
             << i18n("Late evening");
 
-    _time = QTime::currentTime();
+    _time = _applet->clockGetTime();
     repaint();
 }
 
@@ -568,11 +568,11 @@ int FuzzyClock::preferedHeightForWidth(i
 
 void FuzzyClock::updateClock()
 {
-  if (_time.hour() == QTime::currentTime().hour() &&
-      _time.minute() == QTime::currentTime().minute())
+  if (_time.hour() == _applet->clockGetTime().hour() &&
+      _time.minute() == _applet->clockGetTime().minute())
      return;
 
-  _time = QTime::currentTime();
+  _time = _applet->clockGetTime();
   repaint();
 }
 
@@ -627,7 +627,7 @@ void FuzzyClock::drawContents(QPainter *
     } else if (_settings->fuzzyness() == 3) {
         newTimeStr = dayTime[_time.hour() / 3];
     } else {
-        int dow = QDate::currentDate().dayOfWeek();
+        int dow = _applet->clockGetDate().dayOfWeek();
 
         if (dow == 1)
             newTimeStr = i18n("Start of week");
@@ -686,6 +686,7 @@ ClockApplet::~ClockApplet()
 {
     if (_calendar)
         _calendar->close();
+    config()->sync();
     delete _settings;
 }
 
@@ -769,7 +770,7 @@ void ClockApplet::slotApplySettings()
 
     if (!_settings->showDate())
 	    _settings->resetZone();
-    setCurrentTimeZone(_settings->zone());
+    TZoffset = _settings->calc_TZ_offset( _settings->zone() );
 
     switch (_settings->type()) {
         case ClockSettings::Plain:
@@ -809,7 +810,7 @@ void ClockApplet::slotApplySettings()
 
 void ClockApplet::slotUpdate()
 {
-  if (_lastDate != QDate::currentDate())
+  if (_lastDate != clockGetDate())
     updateDateLabel();
 
   _clock->updateClock();
@@ -871,6 +872,7 @@ void ClockApplet::openContextMenu()
 
     KLocale *loc = KGlobal::locale();
     QDateTime dt = QDateTime::currentDateTime();
+    dt = dt.addSecs(TZoffset);
 
     KPopupMenu *copyMenu = new KPopupMenu( menu );
     copyMenu->insertItem(loc->formatDateTime(dt), 201);
@@ -953,13 +955,24 @@ void ClockApplet::slotCopyMenuActivated(
     QApplication::clipboard()->setText(s);
 }
 
+QTime ClockApplet::clockGetTime()
+{
+    return QTime::currentTime().addSecs(TZoffset);
+}
+
+QDate ClockApplet::clockGetDate()
+{
+    return QDate::currentDate().addDays(TZoffset/(24*60*60));
+}
+	
 void ClockApplet::showZone(int z)
 {
     _settings->resetZone(z);
-    setCurrentTimeZone(_settings->zone());
+    TZoffset = _settings->calc_TZ_offset( _settings->zone() );
     updateDateLabel();
     _clock->forceUpdate(); /* force repaint */
     slotUpdate();
+    _settings->writeSettings();
 }
 
 void ClockApplet::nextZone()
@@ -1027,7 +1040,7 @@ void ClockApplet::slotUpdateToolTip()
 
 void ClockApplet::updateDateLabel()
 {
-    _lastDate = QDate::currentDate();
+    _lastDate = clockGetDate();
 
     if (_settings->zoneIndex())
     {
Index: clock.h
===================================================================
RCS file: /home/kde/kdebase/kicker/applets/clock/clock.h,v
retrieving revision 1.31
diff -u -p -r1.31 clock.h
--- clock.h	2002/05/23 14:58:17	1.31
+++ clock.h	2002/11/06 21:32:25
@@ -168,7 +168,6 @@ protected:
 class ClockApplet : public KPanelApplet
 {
   Q_OBJECT
-//  friend class ClockSettings;
 
 public:
 	ClockApplet(const QString& configFile, Type t = Normal, int actions = 0,
@@ -180,6 +179,10 @@ public:
 	void preferences();
 	Orientation getOrientation()    { return orientation(); }
 	void resizeRequest()            { emit(updateLayout()); }
+
+	int TZoffset;
+	QTime clockGetTime();
+	QDate clockGetDate();
 
 protected slots:
 	void slotApplySettings();
Index: settings.cpp
===================================================================
RCS file: /home/kde/kdebase/kicker/applets/clock/settings.cpp,v
retrieving revision 1.6
diff -u -p -r1.6 settings.cpp
--- settings.cpp	2002/09/21 11:20:04	1.6
+++ settings.cpp	2002/11/06 21:32:26
@@ -52,12 +52,13 @@ CONNECTION WITH THE SOFTWARE OR THE USE 
 #include <qstringlist.h>
 #include <qtabwidget.h>
 #include <qtooltip.h>
+#include <qdatetime.h>
 
 #include <stdlib.h>
 #include <time.h>
+#include <unistd.h>
 
-void
-setCurrentTimeZone( const QString & zone )
+static void setCurrentTimeZone(const QString& zone)
 {
   // set TZ variable for this program
   // Ever occuring error:
@@ -73,14 +74,10 @@ setCurrentTimeZone( const QString & zone
 }
 
 ClockSettings::ClockSettings(QWidget* app, KConfig* conf)
-    : applet(app), config(conf), confDlg(0), _zoneIndex(0)
+    : applet(app), config(conf), confDlg(0)
 {
+    _defaultTZ = ::getenv("TZ");
     tzset();
-    QFile f( "/etc/timezone" );
-    if ( f.open( IO_ReadOnly ) ) {
-        QTextStream ts( &f );
-        ts >> _defaultTZ;
-    }
 
     config->setGroup("General");
 
@@ -100,6 +97,7 @@ ClockSettings::ClockSettings(QWidget* ap
 		    "Australias/Sydney,Asia/Tokyo,"
 		    "Europe/Berlin,Europe/London,Europe/Moscow");
     _remotezonelist = QStringList::split(QRegExp(","), tzList);
+    _zoneIndex = config->readNumEntry("Initial_Zone_Index", 0);
 
     config->setGroup("Date");
     _useColDate = config->readBoolEntry("Use_Custom_Colors",false);
@@ -156,7 +154,6 @@ ClockSettings::ClockSettings(QWidget* ap
 
 ClockSettings::~ClockSettings()
 {
-    setCurrentTimeZone(_defaultTZ);
     delete confDlg;
 }
 
@@ -165,6 +162,23 @@ QString ClockSettings::zone(int z) const
     return (z==0 ? _defaultTZ : _remotezonelist[z-1]);
 }
 
+int ClockSettings::calc_TZ_offset(const QString& zone)
+{
+  static int lock = 0; /* a very simple lock algoritm */
+  
+  while (lock)
+	/* wait */;
+
+  lock++;
+  setCurrentTimeZone(zone);
+  QDateTime t1( QDateTime::currentDateTime() );
+  setCurrentTimeZone(_defaultTZ);
+  int diff = QDateTime::currentDateTime().secsTo(t1);
+  lock--;
+
+  return diff;
+}
+
 void ClockSettings::readZoneList( const QStringList & tzDefaults )
 {
   QFile f("/usr/share/zoneinfo/zone.tab");
@@ -285,6 +299,7 @@ void ClockSettings::writeSettings()
     }
 
     config->writeEntry("RemoteZones", _remotezonelist.join(","));
+    config->writeEntry("Initial_Zone_Index",_zoneIndex);
 
     config->setGroup("Date");
     config->writeEntry("Use_Custom_Colors",_useColDate);
Index: settings.h
===================================================================
RCS file: /home/kde/kdebase/kicker/applets/clock/settings.h,v
retrieving revision 1.2
diff -u -p -r1.2 settings.h
--- settings.h	2002/05/25 09:32:01	1.2
+++ settings.h	2002/11/06 21:32:26
@@ -32,9 +32,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE 
 class KConfig;
 class ClockConfDialog;
 
-// needed by clock.cpp and settings.cpp (Werner)
-void setCurrentTimeZone( const QString & zone );
-
 class ClockSettings : public QObject
 {
   Q_OBJECT
@@ -75,6 +72,8 @@ public:
 	void prevZone();
 	unsigned int zoneIndex() { return _zoneIndex; }
 	void resetZone(int z=0) { _zoneIndex = z; }
+
+	int calc_TZ_offset(const QString& zone);
 
 signals:
 	void newSettings();


More information about the kde-core-devel mailing list