c++/lib c++/tests

Christian Mollekopf mollekopf at kolabsys.com
Thu Feb 23 18:31:09 CET 2012


 c++/lib/CMakeLists.txt           |    4 -
 c++/lib/kolabkcalconversion.cpp  |  143 +++++++++++++++++++++++++++++++++++++--
 c++/lib/kolabkcalconversion.h    |    4 -
 c++/tests/CMakeLists.txt         |    8 +-
 c++/tests/kcalconversiontest.cpp |  135 +++++++++++++++++++++++++++---------
 c++/tests/kcalconversiontest.h   |    9 +-
 6 files changed, 253 insertions(+), 50 deletions(-)

New commits:
commit 246ea5c99dce40998284e2fdb2569986f0abd725
Author: Christian Mollekopf <mollekopf at kolabsys.com>
Date:   Thu Feb 23 18:31:02 2012 +0100

    Prepared kcalconversions

diff --git a/c++/lib/CMakeLists.txt b/c++/lib/CMakeLists.txt
index 4eda448..9cc48f1 100644
--- a/c++/lib/CMakeLists.txt
+++ b/c++/lib/CMakeLists.txt
@@ -49,8 +49,8 @@ if(KDECORE_FOUND AND KCALCORE_FOUND)
     target_link_libraries(kolabkcal kolabxml ${KCALCORE} ${KDECORE})
 
     #Library with serialization/deserialization code for KCalCore containers (deprecated)
-    add_library(kolabxmlkcal SHARED kcalkolabformat.cpp ../compiled/XMLParserWrapper.cpp ../compiled/grammar-input-stream.cxx ${SCHEMA_SOURCEFILES})
-    target_link_libraries(kolabxmlkcal xerces-c ${KCALCORE} ${KDECORE})
+#     add_library(kolabxmlkcal SHARED kcalkolabformat.cpp ../compiled/XMLParserWrapper.cpp ../compiled/grammar-input-stream.cxx ${SCHEMA_SOURCEFILES})
+#     target_link_libraries(kolabxmlkcal xerces-c ${KCALCORE} ${KDECORE})
 else()
     message(WARNING "Could not build KCalCore/KDElibs dependend libraries, because kdepimlibs/kdelibs is missing.")
 endif()
diff --git a/c++/lib/kolabkcalconversion.cpp b/c++/lib/kolabkcalconversion.cpp
index a89730c..4a54c62 100644
--- a/c++/lib/kolabkcalconversion.cpp
+++ b/c++/lib/kolabkcalconversion.cpp
@@ -20,6 +20,7 @@
 #include <kcalcore/recurrence.h>
 #include <QtCore/QBitArray>
 #include <QtCore/QVector>
+#include <QtCore/QDebug>
 #include <vector>
 
 namespace Kolab {
@@ -28,40 +29,174 @@ namespace KCalConversion {
 KDateTime toDate(const Kolab::DateTime &dt)
 {
     KDateTime date;
-    if (dt.isDateOnly()) {
+    if (dt.isDateOnly()) { //Date only
         date.setDateOnly(true);
         date.setDate(QDate(dt.year(), dt.month(), dt.day()));
     } else {
         date.setDate(QDate(dt.year(), dt.month(), dt.day()));
         date.setTime(QTime(dt.hour(), dt.minute(), dt.second()));
-        if (dt.isUTC()) {
+        if (dt.isUTC()) { //UTC
             date.setTimeSpec(KDateTime::Spec(KDateTime::UTC));
-        } else if (dt.timezone().empty()) {
+        } else if (!dt.timezone().empty()) { //Timezone
             date.setTimeSpec(KDateTime::Spec(KTimeZone(QString::fromStdString(dt.timezone()))));
+        } //Floating
+    }
+    return date;
+}
+
+DateTime fromDate(const KDateTime &dt)
+{
+    DateTime date;
+    if (dt.isDateOnly()) { //Date only
+        const QDate &d = dt.date();
+        date.setDate(d.year(), d.month(), d.day());
+    } else {
+        const QDate &d = dt.date();
+        date.setDate(d.year(), d.month(), d.day());
+        const QTime &t = dt.time();
+        date.setTime(t.hour(), t.minute(), t.second());
+        if (dt.timeType() == KDateTime::UTC) { //UTC
+            date.setUTC(true);
+        } else if (dt.timeType() == KDateTime::TimeZone) { //Timezone
+            //TODO handle local timezone?
+            date.setTimezone(dt.timeZone().name().toStdString()); //FIXME use system independent name according to spec
+        } else if (dt.timeType() != KDateTime::ClockTime) {
+            qWarning() << "invalid timespec, assuming floating time" << dt.timeType();
         }
     }
     return date;
 }
 
+KCalCore::Duration toDuration(const Kolab::Duration &d)
+{
+    KCalCore::Duration d1(d.weeks()*7+d.days(), KCalCore::Duration::Days);
+    KCalCore::Duration d2(d.hours()*60*60+d.minutes()*60+d.seconds());
+    //TODO handle negative durations?
+    return d1+d2;
+}
+
+KCalCore::Incidence::Secrecy toSecrecy(Kolab::Classification c)
+{
+    switch(c) {
+        case Kolab::Public:
+            return KCalCore::Incidence::SecrecyPublic;
+        case Kolab::Private:
+            return KCalCore::Incidence::SecrecyPrivate;
+        case Kolab::Confidential:
+            return KCalCore::Incidence::SecrecyConfidential;
+        default:
+            qWarning() << "unhandled";
+            Q_ASSERT(0);
+    }
+    return KCalCore::Incidence::SecrecyPublic;
+}
+
+int toPriority(int priority)
+{
+    //Same mapping
+    return priority;
+}
+
+KCalCore::Incidence::Status toStatus(Kolab::Status s)
+{
+    switch (s) {
+        case UndefinedStatus:
+            return  KCalCore::Incidence::StatusNone;
+        case NeedsAction:
+            return  KCalCore::Incidence::StatusNeedsAction;
+        case Completed:
+            return  KCalCore::Incidence::StatusCompleted;
+        case InProcess:
+            return  KCalCore::Incidence::StatusInProcess;
+        case Cancelled:
+            return  KCalCore::Incidence::StatusCanceled;
+        case Tentative:
+            return  KCalCore::Incidence::StatusTentative;
+        case Confirmed:
+            return  KCalCore::Incidence::StatusConfirmed;
+        case Draft:
+            return  KCalCore::Incidence::StatusDraft;
+        case Final:
+            return  KCalCore::Incidence::StatusFinal;
+        default:
+            qWarning() << "unhandled";
+            Q_ASSERT(0);
+    }
+    return KCalCore::Incidence::StatusNone;
+}
+
+QStringList toStringList(const std::vector<std::string> &l)
+{
+    QStringList list;
+    foreach(const std::string &s, l) {
+        list.append(QString::fromStdString(s));
+    }
+    return list;
+}
+
 void setIncidence(KCalCore::Incidence &i, const Kolab::Event &e)
 {
     if (!e.uid().empty()) {
         i.setUid(QString::fromStdString(e.uid()));
     }
     
+    i.setCreated(toDate(e.created()));
+    i.setLastModified(toDate(e.lastModified()));
+    i.setRevision(e.sequence());
+    i.setSecrecy(toSecrecy(e.classification()));
+    i.setCategories(toStringList(e.categories()));
+    
     if (e.start().isValid()) {
         i.setDtStart(toDate(e.start()));
     }
+    if (e.duration().isValid()) {
+        i.setDuration(toDuration(e.duration()));
+    }
+    
+    i.setSummary(QString::fromStdString(e.summary())); //TODO detect richtext
+    i.setDescription(QString::fromStdString(e.description())); //TODO detect richtext
+    i.setPriority(toPriority(e.priority()));
+    i.setStatus(toStatus(e.status()));
+    i.setLocation(QString::fromStdString(e.location())); //TODO detect richtext
+//     i.setOrganizer();
+//     i.addAttendee();
+//     i.addAttachment();
+//     i.addAlarm();
+//     i.setCustomProperties();
+}
 
+void getIncidence(Event &i, const KCalCore::Event &e)
+{
+    i.setUid(e.uid().toStdString());
+//     i.setCreated(fromDate(e.created()));
 }
 
+
+
 KCalCore::Event::Ptr toKCalCore(const Kolab::Event &event)
 {
     KCalCore::Event::Ptr e(new KCalCore::Event);
-    setIncidence(*e, event);    
+    setIncidence(*e, event);
+    if (event.end().isValid()) {
+        e->setDtEnd(toDate(event.end()));
+    }
+    if (event.transparency()) {
+        e->setTransparency(KCalCore::Event::Transparent);
+    } else {
+        e->setTransparency(KCalCore::Event::Opaque);
+    }
+//     e->recurrence();
+
     return e;
 }
 
+Event fromKCalCore(const KCalCore::Event &event)
+{
+    Event e;
+    getIncidence(e, event);
+
+    return e;
+}
 
 
 }
diff --git a/c++/lib/kolabkcalconversion.h b/c++/lib/kolabkcalconversion.h
index 68f43dd..60c5e3f 100644
--- a/c++/lib/kolabkcalconversion.h
+++ b/c++/lib/kolabkcalconversion.h
@@ -18,8 +18,6 @@
 #ifndef KOLABKCALCONVERSION_H
 #define KOLABKCALCONVERSION_H
 
-#include <kcalcore/recurrence.h>
-#include "kolabcontainers.h"
 #include "kolabevent.h"
 #include <kcalcore/event.h>
 
@@ -28,7 +26,7 @@ namespace Kolab {
         
         
         KCalCore::Event::Ptr toKCalCore(const Kolab::Event &);
-//         Kolab::Event writeEvent(KCalCore::Event::Ptr);
+        Kolab::Event fromKCalCore(const KCalCore::Event &);
         
         //TODO converters in both directions for all containers
         
diff --git a/c++/tests/CMakeLists.txt b/c++/tests/CMakeLists.txt
index a3b7899..04b2b3a 100644
--- a/c++/tests/CMakeLists.txt
+++ b/c++/tests/CMakeLists.txt
@@ -8,10 +8,10 @@ if (QT4_FOUND AND KDECORE_FOUND AND KCALCORE_FOUND)
     QT4_AUTOMOC(bindingstest.cpp)
     add_executable(bindingstest bindingstest.cpp ${CMAKE_CURRENT_BINARY_DIR}/${BINDINGSTEST_MOC})
     target_link_libraries(bindingstest ${QT_QTTEST_LIBRARY} ${QT_QTCORE_LIBRARY} kolabxml kolabkcal ${XERCES_C})
-# 
-#     QT4_AUTOMOC(kcalconversiontest.cpp)
-#     add_executable(kcalconversiontest kcalconversiontest.cpp ${CMAKE_CURRENT_BINARY_DIR}/${KCALCONVERSIONTEST_MOC})
-#     target_link_libraries(kcalconversiontest ${QT_QTTEST_LIBRARY} ${QT_QTCORE_LIBRARY} kolabxmlkcal ${XERCES_C})
+
+    QT4_AUTOMOC(kcalconversiontest.cpp)
+    add_executable(kcalconversiontest kcalconversiontest.cpp ${CMAKE_CURRENT_BINARY_DIR}/${KCALCONVERSIONTEST_MOC})
+    target_link_libraries(kcalconversiontest ${QT_QTTEST_LIBRARY} ${QT_QTCORE_LIBRARY} kolabkcal ${XERCES_C})
 
     QT4_AUTOMOC(conversiontest.cpp)
     add_executable(conversiontest conversiontest.cpp ${CMAKE_CURRENT_BINARY_DIR}/${CONVERSIONTEST_MOC})
diff --git a/c++/tests/kcalconversiontest.cpp b/c++/tests/kcalconversiontest.cpp
index ee5061d..80adff7 100644
--- a/c++/tests/kcalconversiontest.cpp
+++ b/c++/tests/kcalconversiontest.cpp
@@ -4,26 +4,115 @@
 #include <QtTest/QtTest>
 #include <kcalcore/recurrence.h>
 
-// #include <lib/kcalconversions.h>
-#include <lib/kcalkolabformat.h>
+#include <lib/kolabkcalconversion.h>
+#include <lib/kolabkcalconversion.cpp>
 
+#include "serializers.h"
+
+using namespace Kolab::KCalConversion;
+
+Q_DECLARE_METATYPE(Kolab::Duration);
+Q_DECLARE_METATYPE(Kolab::DateTime);
+Q_DECLARE_METATYPE(Kolab::Event);
+Q_DECLARE_METATYPE(KCalCore::Event);
+
+namespace QTest {
+    
+    template<>
+    char *toString(const KDateTime &dt)
+    {
+        QByteArray ba = "KDateTime(";
+        ba += dt.toString().toAscii();
+        ba += dt.timeZone().name();
+        ba += ")";
+        return qstrdup(ba.data());
+    }
+}
+
+void KCalConversionTest::testDate_data()
+{
+    QTest::addColumn<Kolab::DateTime>( "input" );
+    QTest::addColumn<KDateTime>( "result" );
+
+    QTest::newRow( "datetime with tz" ) << Kolab::DateTime("US/Eastern",2006,1,8,12,0,0) << KDateTime(KDateTime(QDate(2006, 1, 8), QTime(12, 0, 0), KTimeZone(QString::fromLatin1("US/Eastern"))));
+    QTest::newRow( "floating datetime" ) << Kolab::DateTime(2006,1,8,12,0,0, false) << KDateTime(QDate(2006, 1, 8), QTime(12, 0, 0), KDateTime::Spec(KDateTime::ClockTime));
+    QTest::newRow( "utc datetime" ) << Kolab::DateTime(2006,1,8,12,0,0, true) << KDateTime(KDateTime(QDate(2006, 1, 8), QTime(12, 0, 0), KDateTime::UTC));
+    QTest::newRow( "date only" ) << Kolab::DateTime(2006,1,8) << KDateTime(QDate(2006, 1, 8));
+}
 
 
 void KCalConversionTest::testDate()
 {
-    KDateTime dt1(KDateTime(QDate(2006, 1, 8), QTime(12, 0, 0), KTimeZone(QString::fromLatin1("US/Eastern"))));
-    xml_schema::date_time date1 = Kolab::DateTimeConverter<KDateTime>::fromDateTime(dt1);
-    QCOMPARE(date1.zone_present(), true);
+    QFETCH(Kolab::DateTime, input);
+    QFETCH(KDateTime, result);
+    
+    const KDateTime &r = Kolab::KCalConversion::toDate(input);
+    QCOMPARE(r, result);
+    
+    const Kolab::DateTime &r2 = Kolab::KCalConversion::fromDate(result);
+    QCOMPARE(r2, input);
+}
+
+
+void KCalConversionTest::testConversion_data()
+{
+    QTest::addColumn<KCalCore::Event>( "kcal" );
+    QTest::addColumn<Kolab::Event>( "kolab" );
+    
+    Kolab::DateTime date(2011,2,2,12,11,10);
+    
+    KCalCore::Event kcal;
+    kcal.setUid("uid");
+    kcal.setCreated(toDate(date));
+    kcal.setLastModified(toDate(date));
+    kcal.setRevision(3);
+    kcal.setSecrecy(KCalCore::Incidence::SecrecyConfidential);
+    kcal.setDtStart(toDate(date));
+    
+    Kolab::Event kolab;
+    kolab.setUid("uid");
+    kolab.setCreated(date);
+    kolab.setLastModified(date);
+    kolab.setSequence(3);
+    kolab.setClassification(Kolab::Confidential);
+    kolab.setStart(date);
+    
+    QTest::newRow( "test1" ) << kcal << kolab;
+    
+}
+    
+void KCalConversionTest::testConversion()
+{
+    QFETCH(KCalCore::Event, kcal);
+    QFETCH(Kolab::Event, kolab);
     
-    KDateTime dt2(KDateTime(QDate(2006, 1, 8), QTime(12, 0, 0)));
-    xml_schema::date_time date2 = Kolab::DateTimeConverter<KDateTime>::fromDateTime(dt2);
-    QCOMPARE(date2.zone_present(), false);
+    const KCalCore::Event::Ptr e = toKCalCore(kolab);
     
-    KDateTime dt3(KDateTime(QDate(2006, 1, 8), QTime(12, 0, 0), KDateTime::UTC));
-    xml_schema::date_time date3 = Kolab::DateTimeConverter<KDateTime>::fromDateTime(dt3);
-    QCOMPARE(date3.zone_present(), false);
+    QCOMPARE(e->uid(), kcal.uid());
+    QCOMPARE(e->created(), kcal.created());
+    QCOMPARE(e->lastModified(), kcal.lastModified());
+    QCOMPARE(e->revision(), kcal.revision());
+    QCOMPARE(e->secrecy(), kcal.secrecy());
+    QCOMPARE(e->categories(), kcal.categories());
+    QCOMPARE(e->dtStart(), kcal.dtStart());
+    QCOMPARE(e->dtEnd(), kcal.dtEnd());
+    QCOMPARE(e->duration(), kcal.duration());
+    QCOMPARE(e->transparency(), kcal.transparency());
+    QCOMPARE(e->recurrence(), kcal.recurrence());
+    QCOMPARE(e->recurrenceId(), kcal.recurrenceId());
+    QCOMPARE(e->recurrenceType(), kcal.recurrenceType());
+    QCOMPARE(e->summary(), kcal.summary());
+    QCOMPARE(e->description(), kcal.description());
+    QCOMPARE(e->priority(), kcal.priority());
+    QCOMPARE(e->status(), kcal.status());
+    QCOMPARE(e->location(), kcal.location());
+    QCOMPARE(e->organizer(), kcal.organizer());
+    QCOMPARE(e->attendees(), kcal.attendees());
+    QCOMPARE(e->attachments(), kcal.attachments());
+    QCOMPARE(e->alarms(), kcal.alarms());
 }
 
+/*
 void KCalConversionTest::BenchmarkRoundtripKCAL()
 {
     std::ifstream ifs;
@@ -155,29 +244,9 @@ void KCalConversionTest::BenchmarkRoundtrip()
         result = Kolab::KCal::writeEvent(event);
         Kolab::KCal::readEvent(result, false);
     }
-}
+}*/
+
 
-void KCalConversionTest::testConversion_data()
-{
-    /*
-    QTest::addColumn<KCalCore::Recurrence>( "kcalrecurrence" );
-    QTest::addColumn<QString>( "result" );
-    
-    KCalCore::Recurrence recurrence;
-    recurrence.setYearly(2);
-    
-    QTest::newRow( "test1" ) << recurrence;
-    */
-}
-    
-void KCalConversionTest::testConversion()
-{
-  /*  QFETCH(KCalCore::Recurrence, kcalrecurrence);
-    
-    KolabXSD::Recurrence ret = Kolab::KCalConversion::fromKCal( &kcalrecurrence );
-    QCOMPARE(frequency(ret), kcalrecurrence.rRules().first()->frequency());
-    */
-}
 
 #if 0
 class KCalConversionTest : public QObject
diff --git a/c++/tests/kcalconversiontest.h b/c++/tests/kcalconversiontest.h
index 6545e5c..d029e11 100644
--- a/c++/tests/kcalconversiontest.h
+++ b/c++/tests/kcalconversiontest.h
@@ -10,18 +10,19 @@ class KCalConversionTest : public QObject
   private slots:
       
       
-    void BenchmarkRoundtripKCAL();
-
+//     void BenchmarkRoundtripKCAL();
+// 
     void testConversion_data();
     void testConversion();
 
+    void testDate_data();
     void testDate();
-    
+/*    
     void readEvent();
     void writeEvent();
     void roundtripEvent();
     void roundtripReverseEvent();
-    void BenchmarkRoundtrip();
+    void BenchmarkRoundtrip();*/
 };
 
 #endif
\ No newline at end of file





More information about the commits mailing list