← Back to team overview

ayatana-commits team mailing list archive

[Branch ~agateau/libindicate-qt/trunk] Rev 74: Take date into account in time properties.

 

------------------------------------------------------------
revno: 74
committer: Aurelien Gateau <aurelien.gateau@xxxxxxxxxxxxx>
branch nick: libindicate-qt
timestamp: Thu 2009-09-10 11:44:01 +0200
message:
  Take date into account in time properties.
  
  Introduced Indicator::setProperty(QString, QDateTime)
  and Listener::dateTimeFromValue(QString)
modified:
  src/qindicateindicator.cpp
  src/qindicateindicator.h
  src/qindicatelistener.cpp
  src/qindicatelistener.h
  tests/listenertest.cpp


--
lp:libindicate-qt
https://code.launchpad.net/~agateau/libindicate-qt/trunk

Your team ayatana-commits is subscribed to branch lp:libindicate-qt.
To unsubscribe from this branch go to https://code.launchpad.net/~agateau/libindicate-qt/trunk/+edit-subscription.
=== modified file 'src/qindicateindicator.cpp'
--- src/qindicateindicator.cpp	2009-07-28 14:19:05 +0000
+++ src/qindicateindicator.cpp	2009-09-10 09:44:01 +0000
@@ -75,15 +75,19 @@
         value.toUtf8().data());
 }
 
-void Indicator::setProperty(const QString& key, const QTime& value)
+void Indicator::setProperty(const QString& key, const QDateTime& value)
 {
     GTimeVal gTime;
-    QTime midnight(0, 0);
-    gTime.tv_sec = midnight.secsTo(value);
-    gTime.tv_usec = 1000 * value.msec();
+    gTime.tv_sec = value.toTime_t();
+    gTime.tv_usec = 1000 * value.time().msec();
     indicate_indicator_set_property_time(INDICATE_INDICATOR(d->mGIndicator), key.toUtf8().data(), &gTime);
 }
 
+void Indicator::setProperty(const QString& key, const QTime& value)
+{
+    setProperty(key, QDateTime(QDate::currentDate(), value));
+}
+
 void Indicator::setProperty(const QString& key, const QImage& image)
 {
     QBuffer buffer;

=== modified file 'src/qindicateindicator.h'
--- src/qindicateindicator.h	2009-07-28 14:19:05 +0000
+++ src/qindicateindicator.h	2009-09-10 09:44:01 +0000
@@ -16,6 +16,7 @@
 
 // Local
 
+class QDateTime;
 class QImage;
 class QStringList;
 class QTime;
@@ -34,6 +35,7 @@
     ~Indicator();
 
     void setProperty(const QString& key, const QString& value);
+    void setProperty(const QString& key, const QDateTime& value);
     void setProperty(const QString& key, const QTime& value);
     void setProperty(const QString& key, const QImage& value);
     QString property(const QString& key) const;

=== modified file 'src/qindicatelistener.cpp'
--- src/qindicatelistener.cpp	2009-07-31 08:45:01 +0000
+++ src/qindicatelistener.cpp	2009-09-10 09:44:01 +0000
@@ -12,9 +12,12 @@
 #include "qindicatelistener.h"
 
 // Qt
+#include <QDate>
+#include <QDateTime>
 #include <QDebug>
 #include <QImage>
 #include <QPointer>
+#include <QStringList>
 #include <QTime>
 
 // libindicate
@@ -271,10 +274,15 @@
     return QImage::fromData(data);
 }
 
-QTime Listener::timeFromValue(const QByteArray& _value)
+QTime Listener::timeFromValue(const QByteArray& value)
+{
+    return dateTimeFromValue(value).time();
+}
+
+QDateTime Listener::dateTimeFromValue(const QByteArray& _value)
 {
     if (_value.isEmpty()) {
-        return QTime();
+        return QDateTime();
     }
 
     const QString value = QString::fromUtf8(_value);
@@ -284,32 +292,47 @@
     // or like this if ms is 0:
     // "1970-01-01T16:29:26Z"
     //
+    // The ending "Z" indicates this is UTC time
+    //
     // Unfortunately this does not match with Qt::ISODate, so we parse it
     // ourself
 
-    // Skip the date part
-    QString str = value.section('T', 1);
+    // Split date and time
+    QStringList tokens = value.split('T');
+    if (tokens.count() != 2) {
+        qWarning() << "Could not separate date and time from" << value;
+        return QDateTime();
+    }
+    QString dateStr = tokens[0];
+    QString timeStr = tokens[1];
+
+    // Parse the date
+    QDate date = QDate::fromString(dateStr, "yyyy-MM-dd");
+    if (!date.isValid()) {
+        qWarning () << "Could not extract date from" << value;
+        return QDateTime();
+    }
 
     // Parse the HMS part
-    QTime time = QTime::fromString(str.left(8), "hh:mm:ss");
+    QTime time = QTime::fromString(timeStr.left(8), "hh:mm:ss");
     if (!time.isValid()) {
         qWarning() << "Could not extract time from" << value;
-        return QTime();
+        return QDateTime();
     }
 
     // Parse the msec part, if any
-    str = str.section('.', 1);
+    QString str = timeStr.section('.', 1);
     if (!str.isEmpty()) {
         bool ok;
         int msec = str.left(3).toInt(&ok);
         if (!ok) {
             qWarning() << "Invalid msec value in" << value;
-            return QTime();
+            return QDateTime();
         }
         time = time.addMSecs(msec);
     }
 
-    return time;
+    return QDateTime(date, time, Qt::UTC).toLocalTime();
 }
 
 } // namespace

=== modified file 'src/qindicatelistener.h'
--- src/qindicatelistener.h	2009-07-28 15:19:33 +0000
+++ src/qindicatelistener.h	2009-09-10 09:44:01 +0000
@@ -100,10 +100,16 @@
 
     /**
      * Converts a value received from getServerType, getServerDesktopFile or
-     * getIndicatorProperty into a QTime
+     * getIndicatorProperty into a localtime QTime
      */
     static QTime timeFromValue(const QByteArray& value);
 
+    /**
+     * Converts a value received from getServerType, getServerDesktopFile or
+     * getIndicatorProperty into a localtime QDateTime
+     */
+    static QDateTime dateTimeFromValue(const QByteArray& value);
+
 Q_SIGNALS:
     void serverAdded(QIndicate::Listener::Server* server, const QString& type);
     void serverRemoved(QIndicate::Listener::Server* server, const QString& type);

=== modified file 'tests/listenertest.cpp'
--- tests/listenertest.cpp	2009-07-28 15:19:33 +0000
+++ tests/listenertest.cpp	2009-09-10 09:44:01 +0000
@@ -79,10 +79,17 @@
     QCOMPARE(removedSpy.takeFirst()[2].toString(), SERVER_TYPE);
 }
 
+static QTime localTimeFromUtcTime(int hour, int minute, int second, int msec=0)
+{
+    QTime utcTime(hour, minute, second, msec);
+    QDateTime utcDateTime(QDate(1970, 1, 1), utcTime, Qt::UTC);
+    return utcDateTime.toLocalTime().time();
+}
+
 void ListenerTest::testTimeFromValue()
 {
-    QCOMPARE(QIndicate::Listener::timeFromValue("1970-01-01T23:45:56.001000Z"), QTime(23, 45, 56, 1));
-    QCOMPARE(QIndicate::Listener::timeFromValue("1970-01-01T23:45:56Z"), QTime(23, 45, 56));
+    QCOMPARE(QIndicate::Listener::timeFromValue("1970-01-01T23:45:56.001000Z"), localTimeFromUtcTime(23, 45, 56, 1));
+    QCOMPARE(QIndicate::Listener::timeFromValue("1970-01-01T23:45:56Z"), localTimeFromUtcTime(23, 45, 56));
     QCOMPARE(QIndicate::Listener::timeFromValue("John Doe"), QTime());
     QCOMPARE(QIndicate::Listener::timeFromValue("1970-01-01T23:45:56.aZ"), QTime());
 }