← Back to team overview

ayatana-commits team mailing list archive

[Branch ~agateau/libindicate-qt/trunk] Rev 88: Moved all value decoding code to a new namespace: Decode.

 

------------------------------------------------------------
revno: 88
committer: Aurelien Gateau <aurelien.gateau@xxxxxxxxxxxxx>
branch nick: libindicate-qt
timestamp: Mon 2009-09-14 17:10:58 +0200
message:
  Moved all value decoding code to a new namespace: Decode.
added:
  src/qindicatedecode.cpp
  src/qindicatedecode.h
  tests/decodetest.cpp
  tests/decodetest.h
modified:
  examples/qlisten-and-print.cpp
  src/CMakeLists.txt
  src/qindicateindicator.cpp
  src/qindicateindicator.h
  src/qindicatelistener.cpp
  src/qindicatelistener.h
  tests/CMakeLists.txt
  tests/communicationtest.cpp
  tests/indicatortest.cpp
  tests/listenertest.cpp
  tests/listenertest.h


--
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 'examples/qlisten-and-print.cpp'
--- examples/qlisten-and-print.cpp	2009-09-14 07:34:48 +0000
+++ examples/qlisten-and-print.cpp	2009-09-14 15:10:58 +0000
@@ -12,6 +12,8 @@
 #include <QImage>
 #include <QTime>
 
+#include "qindicatedecode.h"
+
 #include "qlisten-and-print.h"
 
 void Controller::slotServerAdded(QIndicate::Listener::Server* server, const QString& type)
@@ -52,12 +54,12 @@
 {
     qDebug() << "Property" << key;
     if (key == "time") {
-        qDebug() << "  " << QIndicate::Listener::dateTimeFromValue(value);
+        qDebug() << "  " << QIndicate::Decode::dateTimeFromValue(value);
     } else if (key == "icon") {
-        QImage image = QIndicate::Listener::imageFromValue(value);
+        QImage image = QIndicate::Decode::imageFromValue(value);
         qDebug() << "  size:" << image.size() << "depth:" << image.depth();
     } else {
-        qDebug() << "  " << QIndicate::Listener::stringFromValue(value);
+        qDebug() << "  " << QIndicate::Decode::stringFromValue(value);
     }
 }
 

=== modified file 'src/CMakeLists.txt'
--- src/CMakeLists.txt	2009-09-14 12:10:15 +0000
+++ src/CMakeLists.txt	2009-09-14 15:10:58 +0000
@@ -1,4 +1,5 @@
 set(indicate_qt_SRCS
+    qindicatedecode.cpp
     qindicateserver.cpp
     qindicateindicator.cpp
     qindicatelistener.cpp

=== added file 'src/qindicatedecode.cpp'
--- src/qindicatedecode.cpp	1970-01-01 00:00:00 +0000
+++ src/qindicatedecode.cpp	2009-09-14 15:10:58 +0000
@@ -0,0 +1,117 @@
+/*
+ * Qt wrapper for libindicate
+ *
+ * Copyright 2009 Canonical Ltd.
+ *
+ * Authors:
+ * - Aurélien Gâteau <aurelien.gateau@xxxxxxxxxxxxx>
+ *
+ * License: LGPL v2.1 or LGPL v3
+ */
+#ifndef QINDICATEDECODE_H
+#define QINDICATEDECODE_H
+
+// Qt
+#include <QDateTime>
+#include <QDebug>
+#include <QImage>
+#include <QString>
+#include <QStringList>
+
+namespace QIndicate
+{
+
+namespace Decode
+{
+
+QString stringFromValue(const QByteArray& value)
+{
+    return QString::fromUtf8(value);
+}
+
+bool boolFromValue(const QByteArray& value, bool defaultValue)
+{
+    if (value == "true") {
+        return true;
+    }
+    if (value == "false") {
+        return false;
+    }
+    return defaultValue;
+}
+
+int intFromValue(const QByteArray& _value, int defaultValue)
+{
+    bool ok;
+    int value = _value.toInt(&ok);
+    return ok ? value : defaultValue;
+}
+
+QImage imageFromValue(const QByteArray& value)
+{
+    QByteArray data = QByteArray::fromBase64(value);
+    return QImage::fromData(data);
+}
+
+QDateTime dateTimeFromValue(const QByteArray& _value)
+{
+    if (_value.isEmpty()) {
+        return QDateTime();
+    }
+
+    const QString value = QString::fromUtf8(_value);
+
+    // The time is received as an ISO8601 string which looks like this:
+    // "1970-01-01T16:29:26.705000Z"
+    // 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
+
+    // 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(timeStr.left(8), "hh:mm:ss");
+    if (!time.isValid()) {
+        qWarning() << "Could not extract time from" << value;
+        return QDateTime();
+    }
+
+    // Parse the msec part, if any
+    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 QDateTime();
+        }
+        time = time.addMSecs(msec);
+    }
+
+    return QDateTime(date, time, Qt::UTC).toLocalTime();
+}
+
+} // namespace
+
+} // namespace
+
+#endif /* QINDICATEDECODE_H */
+

=== added file 'src/qindicatedecode.h'
--- src/qindicatedecode.h	1970-01-01 00:00:00 +0000
+++ src/qindicatedecode.h	2009-09-14 15:10:58 +0000
@@ -0,0 +1,33 @@
+/*
+ * Qt wrapper for libindicate
+ *
+ * Copyright 2009 Canonical Ltd.
+ *
+ * Authors:
+ * - Aurélien Gâteau <aurelien.gateau@xxxxxxxxxxxxx>
+ *
+ * License: LGPL v2.1 or LGPL v3
+ */
+#ifndef QINDICATEDECODE_H
+#define QINDICATEDECODE_H
+
+namespace QIndicate
+{
+
+/**
+ * This namespace provides functions to decode the value of serialized properties
+ */
+namespace Decode
+{
+
+QString stringFromValue(const QByteArray&);
+int intFromValue(const QByteArray&, int defaultValue=0);
+bool boolFromValue(const QByteArray&, bool defaultValue=false);
+QImage imageFromValue(const QByteArray&);
+QDateTime dateTimeFromValue(const QByteArray&);
+
+} // namespace
+
+} // namespace
+
+#endif /* QINDICATEDECODE_H */

=== modified file 'src/qindicateindicator.cpp'
--- src/qindicateindicator.cpp	2009-09-14 09:52:28 +0000
+++ src/qindicateindicator.cpp	2009-09-14 15:10:58 +0000
@@ -15,6 +15,7 @@
 #include <QBuffer>
 #include <QByteArray>
 #include <QDateTime>
+#include <QDebug>
 #include <QHash>
 #include <QImage>
 #include <QStringList>
@@ -23,6 +24,7 @@
 #include <libindicate/indicator.h>
 
 // Local
+#include "qindicatedecode.h"
 
 namespace QIndicate
 {
@@ -68,6 +70,13 @@
     delete d;
 }
 
+void Indicator::setIndicatorProperty(const QString& key, const char* value)
+{
+    indicate_indicator_set_property(INDICATE_INDICATOR(d->mGIndicator),
+        key.toUtf8().data(),
+        value);
+}
+
 void Indicator::setIndicatorProperty(const QString& key, const QString& value)
 {
     indicate_indicator_set_property(INDICATE_INDICATOR(d->mGIndicator),
@@ -97,7 +106,7 @@
 
 void Indicator::setIndicatorProperty(const QString& key, bool value)
 {
-    setIndicatorProperty(key, value ? "true" : "false");
+    setIndicatorProperty(key, QString(value ? "true" : "false"));
 }
 
 void Indicator::setIndicatorProperty(const QString& key, int value)
@@ -105,29 +114,10 @@
     setIndicatorProperty(key, QString::number(value));
 }
 
-QString Indicator::indicatorProperty(const QString& key) const
+QByteArray Indicator::indicatorProperty(const QString& key) const
 {
     const gchar* value = indicate_indicator_get_property(d->mGIndicator, key.toUtf8().data());
-    return QString::fromUtf8(value);
-}
-
-bool Indicator::indicatorProperty(const QString& key, bool defaultValue) const
-{
-    QString str = indicatorProperty(key);
-    if (str == "true") {
-        return true;
-    }
-    if (str == "false") {
-        return false;
-    }
-    return defaultValue;
-}
-
-int Indicator::indicatorProperty(const QString& key, int defaultValue) const
-{
-    bool ok = false;
-    int value = indicatorProperty(key).toInt(&ok);
-    return ok ? value : defaultValue;
+    return QByteArray(value);
 }
 
 static void propertyList_helper(gchar* _item, QStringList* list)
@@ -161,7 +151,7 @@
 
 int Indicator::countProperty() const
 {
-    return indicatorProperty("count", 0);
+    return Decode::intFromValue(indicatorProperty("count"));
 }
 
 void Indicator::setCountProperty(int value)
@@ -176,7 +166,7 @@
 
 bool Indicator::drawAttentionProperty() const
 {
-    return indicatorProperty("draw_attention", false);
+    return Decode::boolFromValue(indicatorProperty("draw_attention"));
 }
 
 void Indicator::emitDisplay()

=== modified file 'src/qindicateindicator.h'
--- src/qindicateindicator.h	2009-09-14 09:52:28 +0000
+++ src/qindicateindicator.h	2009-09-14 15:10:58 +0000
@@ -35,17 +35,14 @@
     Indicator(QObject* parent=0);
     ~Indicator();
 
+    void setIndicatorProperty(const QString& key, const char* value);
     void setIndicatorProperty(const QString& key, const QString& value);
-    /**
-     * @since 0.2.0
-     */
     void setIndicatorProperty(const QString& key, const QDateTime& value);
     void setIndicatorProperty(const QString& key, const QImage& value);
     void setIndicatorProperty(const QString& key, int value);
     void setIndicatorProperty(const QString& key, bool value);
-    QString indicatorProperty(const QString& key) const;
-    int indicatorProperty(const QString& key, int defaultValue) const;
-    bool indicatorProperty(const QString& key, bool defaultValue) const;
+
+    QByteArray indicatorProperty(const QString& key) const;
 
     /**
      * Convenience method to define count

=== modified file 'src/qindicatelistener.cpp'
--- src/qindicatelistener.cpp	2009-09-14 09:27:22 +0000
+++ src/qindicatelistener.cpp	2009-09-14 15:10:58 +0000
@@ -12,13 +12,8 @@
 #include "qindicatelistener.h"
 
 // Qt
-#include <QDate>
-#include <QDateTime>
 #include <QDebug>
-#include <QImage>
 #include <QPointer>
-#include <QStringList>
-#include <QTime>
 
 // libindicate
 #include <libindicate/listener.h>
@@ -283,73 +278,6 @@
     }
 }
 
-QString Listener::stringFromValue(const QByteArray& value)
-{
-    return QString::fromUtf8(value);
-}
-
-QImage Listener::imageFromValue(const QByteArray& value)
-{
-    QByteArray data = QByteArray::fromBase64(value);
-    return QImage::fromData(data);
-}
-
-QDateTime Listener::dateTimeFromValue(const QByteArray& _value)
-{
-    if (_value.isEmpty()) {
-        return QDateTime();
-    }
-
-    const QString value = QString::fromUtf8(_value);
-
-    // The time is received as an ISO8601 string which looks like this:
-    // "1970-01-01T16:29:26.705000Z"
-    // 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
-
-    // 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(timeStr.left(8), "hh:mm:ss");
-    if (!time.isValid()) {
-        qWarning() << "Could not extract time from" << value;
-        return QDateTime();
-    }
-
-    // Parse the msec part, if any
-    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 QDateTime();
-        }
-        time = time.addMSecs(msec);
-    }
-
-    return QDateTime(date, time, Qt::UTC).toLocalTime();
-}
-
 } // namespace
 
 #include "qindicatelistener.moc"

=== modified file 'src/qindicatelistener.h'
--- src/qindicatelistener.h	2009-09-14 09:27:22 +0000
+++ src/qindicatelistener.h	2009-09-14 15:10:58 +0000
@@ -96,26 +96,6 @@
 
     void setInterest(QIndicate::Listener::Server*, QIndicate::Interest, bool value);
 
-    /**
-     * Converts a value received from getServerType, getServerDesktopFile or
-     * getIndicatorProperty into a QString
-     */
-    static QString stringFromValue(const QByteArray& value);
-
-    /**
-     * Converts a value received from getServerType, getServerDesktopFile or
-     * getIndicatorProperty into a QImage
-     */
-    static QImage imageFromValue(const QByteArray& value);
-
-    /**
-     * Converts a value received from getServerType, getServerDesktopFile or
-     * getIndicatorProperty into a localtime QDateTime
-     *
-     * @since 0.2.0
-     */
-    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/CMakeLists.txt'
--- tests/CMakeLists.txt	2009-08-06 20:40:39 +0000
+++ tests/CMakeLists.txt	2009-09-14 15:10:58 +0000
@@ -82,5 +82,18 @@
     ${test_LIBRARIES}
     )
 
+# decodetest
+set(decodetest_SRCS
+    decodetest.cpp
+    )
+
+qt4_automoc(${decodetest_SRCS})
+
+add_test_executable(decodetest ${decodetest_SRCS})
+
+target_link_libraries(decodetest
+    ${test_LIBRARIES}
+    )
+
 # Keep this at the end
 create_check_target()

=== modified file 'tests/communicationtest.cpp'
--- tests/communicationtest.cpp	2009-09-14 09:44:41 +0000
+++ tests/communicationtest.cpp	2009-09-14 15:10:58 +0000
@@ -18,6 +18,7 @@
 #include <QtTest>
 
 // Local
+#include "qindicatedecode.h"
 
 const QString SERVER_TYPE = "message";
 
@@ -130,7 +131,7 @@
     QCOMPARE(receiver.server, mListenerServer);
     QCOMPARE(receiver.indicator, mListenerIndicator);
     QCOMPARE(receiver.key, key1);
-    QCOMPARE(QIndicate::Listener::stringFromValue(receiver.value.toByteArray()), v1);
+    QCOMPARE(QIndicate::Decode::stringFromValue(receiver.value.toByteArray()), v1);
 
     // key2
     mListener->getIndicatorProperty(mListenerServer, mListenerIndicator, key2,
@@ -146,7 +147,7 @@
     QCOMPARE(receiver.server, mListenerServer);
     QCOMPARE(receiver.indicator, mListenerIndicator);
     QCOMPARE(receiver.key, key2);
-    QCOMPARE(QIndicate::Listener::imageFromValue(receiver.value.toByteArray()), v2);
+    QCOMPARE(QIndicate::Decode::imageFromValue(receiver.value.toByteArray()), v2);
 
     // key3
     mListener->getIndicatorProperty(mListenerServer, mListenerIndicator, key3,
@@ -162,7 +163,7 @@
     QCOMPARE(receiver.server, mListenerServer);
     QCOMPARE(receiver.indicator, mListenerIndicator);
     QCOMPARE(receiver.key, key3);
-    QCOMPARE(QIndicate::Listener::dateTimeFromValue(receiver.value.toByteArray()), v3);
+    QCOMPARE(QIndicate::Decode::dateTimeFromValue(receiver.value.toByteArray()), v3);
 }
 
 void CommunicationTest::testGetServerDesktop()
@@ -185,7 +186,7 @@
     QTest::qWait(500);
 
     QCOMPARE(receiver.server, mListenerServer);
-    QCOMPARE(QIndicate::Listener::stringFromValue(receiver.value.toByteArray()), value);
+    QCOMPARE(QIndicate::Decode::stringFromValue(receiver.value.toByteArray()), value);
 }
 
 void CommunicationTest::testGetServerType()
@@ -203,7 +204,7 @@
     QTest::qWait(500);
 
     QCOMPARE(receiver.server, mListenerServer);
-    QCOMPARE(QIndicate::Listener::stringFromValue(receiver.value.toByteArray()), SERVER_TYPE);
+    QCOMPARE(QIndicate::Decode::stringFromValue(receiver.value.toByteArray()), SERVER_TYPE);
 }
 
 void CommunicationTest::testGetServerCount()

=== added file 'tests/decodetest.cpp'
--- tests/decodetest.cpp	1970-01-01 00:00:00 +0000
+++ tests/decodetest.cpp	2009-09-14 15:10:58 +0000
@@ -0,0 +1,37 @@
+/*
+ * Qt wrapper for libindicate
+ *
+ * Copyright 2009 Canonical Ltd.
+ *
+ * Authors:
+ * - Aurélien Gâteau <aurelien.gateau@xxxxxxxxxxxxx>
+ *
+ * License: LGPL v2.1 or LGPL v3
+ */
+// Self
+#include "decodetest.h"
+
+// Qt
+#include <QtTest>
+
+// Local
+#include <qindicatedecode.h>
+
+QTEST_MAIN(DecodeTest)
+
+static QDateTime localDateTimeFromUtcTime(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();
+}
+
+void DecodeTest::testDateTimeFromValue()
+{
+    QCOMPARE(QIndicate::Decode::dateTimeFromValue("1970-01-01T23:45:56.001000Z"), localDateTimeFromUtcTime(23, 45, 56, 1));
+    QCOMPARE(QIndicate::Decode::dateTimeFromValue("1970-01-01T23:45:56Z"), localDateTimeFromUtcTime(23, 45, 56));
+    QCOMPARE(QIndicate::Decode::dateTimeFromValue("John Doe"), QDateTime());
+    QCOMPARE(QIndicate::Decode::dateTimeFromValue("1970-01-01T23:45:56.aZ"), QDateTime());
+}
+
+#include "decodetest.moc"

=== added file 'tests/decodetest.h'
--- tests/decodetest.h	1970-01-01 00:00:00 +0000
+++ tests/decodetest.h	2009-09-14 15:10:58 +0000
@@ -0,0 +1,25 @@
+/*
+ * Qt wrapper for libindicate
+ *
+ * Copyright 2009 Canonical Ltd.
+ *
+ * Authors:
+ * - Aurélien Gâteau <aurelien.gateau@xxxxxxxxxxxxx>
+ *
+ * License: LGPL v2.1 or LGPL v3
+ */
+#ifndef DECODETEST_H
+#define DECODETEST_H
+
+// Qt
+#include <QObject>
+
+// Local
+class DecodeTest : public QObject
+{
+Q_OBJECT
+private Q_SLOTS:
+    void testDateTimeFromValue();
+};
+
+#endif /* DECODETEST_H */

=== modified file 'tests/indicatortest.cpp'
--- tests/indicatortest.cpp	2009-09-14 09:44:41 +0000
+++ tests/indicatortest.cpp	2009-09-14 15:10:58 +0000
@@ -15,8 +15,9 @@
 #include <QtTest>
 
 // Local
+#include <qindicatedecode.h>
+#include <qindicateindicator.h>
 #include <qindicateserver.h>
-#include <qindicateindicator.h>
 
 QTEST_MAIN(IndicatorTest)
 
@@ -70,7 +71,7 @@
     QString value = "value";
     indicator.setIndicatorProperty(key, value);
 
-    QCOMPARE(indicator.indicatorProperty(key), value);
+    QCOMPARE(QIndicate::Decode::stringFromValue(indicator.indicatorProperty(key)), value);
 
     QStringList lst = indicator.propertyList();
     QStringList expected = QStringList() << key;

=== modified file 'tests/listenertest.cpp'
--- tests/listenertest.cpp	2009-09-14 07:34:48 +0000
+++ tests/listenertest.cpp	2009-09-14 15:10:58 +0000
@@ -77,19 +77,4 @@
     QCOMPARE(removedSpy.count(), 1);
 }
 
-static QDateTime localDateTimeFromUtcTime(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();
-}
-
-void ListenerTest::testDateTimeFromValue()
-{
-    QCOMPARE(QIndicate::Listener::dateTimeFromValue("1970-01-01T23:45:56.001000Z"), localDateTimeFromUtcTime(23, 45, 56, 1));
-    QCOMPARE(QIndicate::Listener::dateTimeFromValue("1970-01-01T23:45:56Z"), localDateTimeFromUtcTime(23, 45, 56));
-    QCOMPARE(QIndicate::Listener::dateTimeFromValue("John Doe"), QDateTime());
-    QCOMPARE(QIndicate::Listener::dateTimeFromValue("1970-01-01T23:45:56.aZ"), QDateTime());
-}
-
 #include "listenertest.moc"

=== modified file 'tests/listenertest.h'
--- tests/listenertest.h	2009-09-14 07:34:48 +0000
+++ tests/listenertest.h	2009-09-14 15:10:58 +0000
@@ -22,7 +22,6 @@
     void initTestCase();
     void testServerAddedRemoved();
     void testIndicatorAddedRemoved();
-    void testDateTimeFromValue();
 };
 
 #endif /* LISTENERTEST_H */