kolabformat/kolabobject.cpp kolabformat/kolabobject.h tests/testfiles tests/upgradetest.cpp
Christian Mollekopf
mollekopf at kolabsys.com
Fri Aug 10 19:40:53 CEST 2012
kolabformat/kolabobject.cpp | 105 +++++++++----
kolabformat/kolabobject.h | 11 +
tests/testfiles/v2/event/simple_missingTypeHeader.ics.mime | 39 ++++
tests/upgradetest.cpp | 35 ++--
4 files changed, 151 insertions(+), 39 deletions(-)
New commits:
commit a6fef072bb9ffc373784f966abfd7e8bba20d5b2
Author: Christian Mollekopf <mollekopf at kolabsys.com>
Date: Fri Aug 10 19:40:29 2012 +0200
allow overriding the autodetected objecttype/version
diff --git a/kolabformat/kolabobject.cpp b/kolabformat/kolabobject.cpp
index 69fe273..4ead6e7 100644
--- a/kolabformat/kolabobject.cpp
+++ b/kolabformat/kolabobject.cpp
@@ -66,13 +66,15 @@ class KolabObjectReader::Private
{
public:
Private()
- : mObjectType( InvalidObject ),
- mVersion( KolabV3 )
+ : mObjectType( InvalidObject ),
+ mVersion( KolabV3 ),
+ mOverrideObjectType(InvalidObject),
+ mDoOverrideVersion(false)
{
mAddressee = KABC::Addressee();
}
- ObjectType readKolabV2(const KMime::Message::Ptr &msg, Kolab::ObjectType objectType, const QString &kolabType);
+ ObjectType readKolabV2(const KMime::Message::Ptr &msg, Kolab::ObjectType objectType);
ObjectType readKolabV3(const KMime::Message::Ptr &msg, Kolab::ObjectType objectType);
KCalCore::Incidence::Ptr mIncidence;
@@ -84,6 +86,9 @@ public:
ObjectType mObjectType;
Version mVersion;
Kolab::Freebusy mFreebusy;
+ ObjectType mOverrideObjectType;
+ Version mOverrideVersion;
+ bool mDoOverrideVersion;
};
//@endcond
@@ -103,6 +108,17 @@ KolabObjectReader::~KolabObjectReader()
delete d;
}
+void KolabObjectReader::setObjectType(ObjectType type)
+{
+ d->mOverrideObjectType = type;
+}
+
+void KolabObjectReader::setVersion(Version version)
+{
+ d->mOverrideVersion = version;
+ d->mDoOverrideVersion = true;
+}
+
Kolab::ObjectType getObjectType(const QString &type)
{
if (type == eventKolabType()) {
@@ -125,6 +141,31 @@ Kolab::ObjectType getObjectType(const QString &type)
return Kolab::InvalidObject;
}
+QByteArray getTypeString(Kolab::ObjectType type)
+{
+ switch (type) {
+ case EventObject:
+ return KOLAB_TYPE_EVENT;
+ case TodoObject:
+ return KOLAB_TYPE_TASK;
+ case JournalObject:
+ return KOLAB_TYPE_JOURNAL;
+ case FreebusyObject:
+ return KOLAB_TYPE_FREEBUSY;
+ case ContactObject:
+ return KOLAB_TYPE_CONTACT;
+ case DistlistObject:
+ return KOLAB_TYPE_DISTLIST;
+ case NoteObject:
+ return KOLAB_TYPE_NOTE;
+ case DictionaryConfigurationObject:
+ return KOLAB_TYPE_CONFIGURATION;
+ default:
+ Critical() << "unknown type "<< type;
+ }
+ return QByteArray();
+}
+
QByteArray getMimeType(Kolab::ObjectType type)
{
switch (type) {
@@ -153,7 +194,7 @@ void printMessageDebugInfo(const KMime::Message::Ptr &msg)
// Debug() << msg->encodedContent();
}
-ObjectType KolabObjectReader::Private::readKolabV2(const KMime::Message::Ptr &msg, Kolab::ObjectType objectType, const QString &kolabType)
+ObjectType KolabObjectReader::Private::readKolabV2(const KMime::Message::Ptr &msg, Kolab::ObjectType objectType)
{
if (objectType == DictionaryConfigurationObject) {
KMime::Content *xmlContent = Mime::findContentByType( msg, "application/xml" );
@@ -168,9 +209,9 @@ ObjectType KolabObjectReader::Private::readKolabV2(const KMime::Message::Ptr &ms
mObjectType = objectType;
return mObjectType;
}
- KMime::Content *xmlContent = Mime::findContentByType( msg, kolabType.toLatin1() );
+ KMime::Content *xmlContent = Mime::findContentByType( msg, getTypeString(objectType) );
if ( !xmlContent ) {
- Critical() << "no part with type" << kolabType.toLatin1() << " found";
+ Critical() << "no part with type" << getTypeString(objectType) << " found";
printMessageDebugInfo(msg);
return InvalidObject;
}
@@ -292,35 +333,45 @@ ObjectType KolabObjectReader::parseMimeMessage(const KMime::Message::Ptr &msg)
{
ErrorHandler::clearErrors();
d->mObjectType = InvalidObject;
- KMime::Headers::Base *xKolabHeader = msg->getHeaderByType(X_KOLAB_TYPE_HEADER);
- if (!xKolabHeader) {
- CRITICAL("could not find the X-Kolab-Type Header");
- printMessageDebugInfo(msg);
- return InvalidObject;
- }
- const QString &kolabType = xKolabHeader->asUnicodeString(); //TODO we probably shouldn't use unicodeString
-
- KMime::Headers::Base *xKolabVersion = msg->getHeaderByType(X_KOLAB_MIME_VERSION_HEADER);
- if (!xKolabVersion) {
- //For backwards compatibility to development versions, can be removed in future versions
- xKolabVersion = msg->getHeaderByType(X_KOLAB_MIME_VERSION_HEADER_COMPAT);
- }
- if (!xKolabVersion) {
- d->mVersion = KolabV2;
- } else {
- if (xKolabVersion->asUnicodeString() != KOLAB_VERSION_V3) { //TODO version compatibility check?
- Warning() << "Kolab Version Header available but not on the same version as the implementation: " << xKolabVersion->asUnicodeString();
+ Kolab::ObjectType objectType = InvalidObject;
+ if (d->mOverrideObjectType == InvalidObject) {
+ KMime::Headers::Base *xKolabHeader = msg->getHeaderByType(X_KOLAB_TYPE_HEADER);
+ if (!xKolabHeader) {
+ CRITICAL("could not find the X-Kolab-Type Header");
+ printMessageDebugInfo(msg);
+ return InvalidObject;
}
- d->mVersion = KolabV3;
+ const QString &kolabType = xKolabHeader->asUnicodeString();
+ objectType = getObjectType(kolabType);
+ } else {
+ objectType = d->mOverrideObjectType;
}
- Kolab::ObjectType objectType = getObjectType(kolabType);
if (objectType == InvalidObject) {
Warning() << "invalid object type";
printMessageDebugInfo(msg);
return InvalidObject;
}
+
+ if (!d->mDoOverrideVersion) {
+ KMime::Headers::Base *xKolabVersion = msg->getHeaderByType(X_KOLAB_MIME_VERSION_HEADER);
+ if (!xKolabVersion) {
+ //For backwards compatibility to development versions, can be removed in future versions
+ xKolabVersion = msg->getHeaderByType(X_KOLAB_MIME_VERSION_HEADER_COMPAT);
+ }
+ if (!xKolabVersion) {
+ d->mVersion = KolabV2;
+ } else {
+ if (xKolabVersion->asUnicodeString() != KOLAB_VERSION_V3) { //TODO version compatibility check?
+ Warning() << "Kolab Version Header available but not on the same version as the implementation: " << xKolabVersion->asUnicodeString();
+ }
+ d->mVersion = KolabV3;
+ }
+ } else {
+ d->mVersion = d->mOverrideVersion;
+ }
+
if (d->mVersion == KolabV2) {
- return d->readKolabV2(msg, objectType, kolabType);
+ return d->readKolabV2(msg, objectType);
} else {
return d->readKolabV3(msg, objectType);
}
diff --git a/kolabformat/kolabobject.h b/kolabformat/kolabobject.h
index d9d0602..a17b235 100644
--- a/kolabformat/kolabobject.h
+++ b/kolabformat/kolabobject.h
@@ -66,6 +66,17 @@ public:
~KolabObjectReader();
ObjectType parseMimeMessage(const KMime::Message::Ptr &msg);
+
+ /**
+ * Set to override the autodetected object type, before parsing the message.
+ */
+ void setObjectType(ObjectType);
+
+ /**
+ * Set to override the autodetected version, before parsing the message.
+ */
+ void setVersion(Version);
+
/**
* Returns the Object type of the parsed kolab object.
*/
diff --git a/tests/testfiles/v2/event/simple_missingTypeHeader.ics.mime b/tests/testfiles/v2/event/simple_missingTypeHeader.ics.mime
new file mode 100644
index 0000000..801b184
--- /dev/null
+++ b/tests/testfiles/v2/event/simple_missingTypeHeader.ics.mime
@@ -0,0 +1,39 @@
+Date: Tue, 01 Sep 2009 13:36:50 +0200
+User-Agent: Akonadi Kolab Proxy Resource
+Content-Type: multipart/mixed; boundary="nextPart3915010.RBqxP67orN"
+Subject: KOrganizer-1353608432.168
+MIME-Version: 1.0
+
+
+--nextPart3915010.RBqxP67orN
+Content-Type: text/plain; charset="us-ascii"
+Content-Transfer-Encoding: 7Bit
+
+This is a Kolab Groupware object.
+To view this object you will need an email client that can understand the Kolab Groupware format.
+For a list of such email clients please visit
+http://www.kolab.org/get-kolab
+
+--nextPart3915010.RBqxP67orN
+Content-Type: application/x-vnd.kolab.event; name="kolab.xml"
+Content-Transfer-Encoding: quoted-printable
+Content-Disposition: attachment; filename="kolab.xml"
+
+<?xml version=3D"1.0" encoding=3D"UTF-8"?>
+<event version=3D"1.0">
+ <product-id>Libkolab-0.1.0, Kolab resource</product-id>
+ <uid>KOrganizer-1353608432.168</uid>
+ <creation-date>2009-09-01T11:36:44Z</creation-date>
+ <last-modification-date>2009-09-01T11:36:44Z</last-modification-date>
+ <sensitivity>public</sensitivity>
+ <start-date>2009-09-02T06:00:00Z</start-date>
+ <summary>Simple Event</summary>
+ <location>Here</location>
+ <organizer/>
+ <show-time-as>busy</show-time-as>
+ <end-date>2009-09-02T07:00:00Z</end-date>
+</event>
+
+--nextPart3915010.RBqxP67orN--
+
+
diff --git a/tests/upgradetest.cpp b/tests/upgradetest.cpp
index 0510f2b..c68f475 100644
--- a/tests/upgradetest.cpp
+++ b/tests/upgradetest.cpp
@@ -21,6 +21,7 @@
#include <QTest>
#include <kolabcontainers.h>
#include <kolabformat.h>
+#include <kolab/errorhandler.h>
#include "testutils.h"
#include "kolabformat/kolabobject.h"
@@ -32,22 +33,25 @@ void UpgradeTest::testIncidence_data()
{
QTest::addColumn<Kolab::ObjectType>( "type" );
QTest::addColumn<QString>( "filename" );
+ QTest::addColumn<bool>( "forceType" );
// QTest::newRow( "v2contactSimple" ) << Kolab::KolabV2 << Kolab::ContactObject << TESTFILEDIR+QString::fromLatin1("v2/contacts/simple.vcf") << TESTFILEDIR+QString::fromLatin1("v2/contacts/simple.vcf.mime");
- QTest::newRow( "v2eventSimple" ) << Kolab::EventObject << TESTFILEDIR+QString::fromLatin1("v2/event/simple.ics.mime");
- QTest::newRow( "v2eventComplex" ) << Kolab::EventObject << TESTFILEDIR+QString::fromLatin1("v2/event/complex.ics.mime");
- QTest::newRow( "v2eventAllday" ) << Kolab::EventObject << TESTFILEDIR+QString::fromLatin1("v2/event/allday.ics.mime");
- QTest::newRow( "v2eventAttachment" ) << Kolab::EventObject << TESTFILEDIR+QString::fromLatin1("v2/event/attachment.ics.mime");
- QTest::newRow( "v2eventHorde" ) << Kolab::EventObject << TESTFILEDIR+QString::fromLatin1("v2/event/horde.ics.mime");
+ QTest::newRow( "v2eventSimple" ) << Kolab::EventObject << TESTFILEDIR+QString::fromLatin1("v2/event/simple.ics.mime") << false;
+ QTest::newRow( "v2eventComplex" ) << Kolab::EventObject << TESTFILEDIR+QString::fromLatin1("v2/event/complex.ics.mime") << false;
+ QTest::newRow( "v2eventAllday" ) << Kolab::EventObject << TESTFILEDIR+QString::fromLatin1("v2/event/allday.ics.mime") << false;
+ QTest::newRow( "v2eventAttachment" ) << Kolab::EventObject << TESTFILEDIR+QString::fromLatin1("v2/event/attachment.ics.mime") << false;
+ QTest::newRow( "v2eventHorde" ) << Kolab::EventObject << TESTFILEDIR+QString::fromLatin1("v2/event/horde.ics.mime") << false;
//Still broken, although it seems that rather the event comparison is implemented wrong (not using equals)
- QTest::newRow( "v2taskSimple" ) << Kolab::TodoObject << TESTFILEDIR+QString::fromLatin1("v2/task/simple.ics.mime");
- QTest::newRow( "v2taskComplex" ) << Kolab::TodoObject << TESTFILEDIR+QString::fromLatin1("v2/task/complex.ics.mime");
- QTest::newRow( "v2taskPrio1" ) << Kolab::TodoObject << TESTFILEDIR+QString::fromLatin1("v2/task/prioritytest1.ics.mime");
- QTest::newRow( "v2taskPrio2" ) << Kolab::TodoObject << TESTFILEDIR+QString::fromLatin1("v2/task/prioritytest2.ics.mime");
+ QTest::newRow( "v2taskSimple" ) << Kolab::TodoObject << TESTFILEDIR+QString::fromLatin1("v2/task/simple.ics.mime") << false;
+ QTest::newRow( "v2taskComplex" ) << Kolab::TodoObject << TESTFILEDIR+QString::fromLatin1("v2/task/complex.ics.mime") << false;
+ QTest::newRow( "v2taskPrio1" ) << Kolab::TodoObject << TESTFILEDIR+QString::fromLatin1("v2/task/prioritytest1.ics.mime") << false;
+ QTest::newRow( "v2taskPrio2" ) << Kolab::TodoObject << TESTFILEDIR+QString::fromLatin1("v2/task/prioritytest2.ics.mime") << false;
- QTest::newRow( "v2journalSimple" ) << Kolab::JournalObject << TESTFILEDIR+QString::fromLatin1("v2/journal/simple.ics.mime");
- QTest::newRow( "v2journalComplex" ) << Kolab::JournalObject << TESTFILEDIR+QString::fromLatin1("v2/journal/complex.ics.mime");
+ QTest::newRow( "v2journalSimple" ) << Kolab::JournalObject << TESTFILEDIR+QString::fromLatin1("v2/journal/simple.ics.mime") << false;
+ QTest::newRow( "v2journalComplex" ) << Kolab::JournalObject << TESTFILEDIR+QString::fromLatin1("v2/journal/complex.ics.mime") << false;
+
+ QTest::newRow( "v2eventSimple_missingTypeHeader" ) << Kolab::EventObject << TESTFILEDIR+QString::fromLatin1("v2/event/simple_missingTypeHeader.ics.mime") << true;
}
@@ -55,14 +59,19 @@ void UpgradeTest::testIncidence()
{
QFETCH( Kolab::ObjectType, type );
QFETCH( QString, filename ); //To compare
+ QFETCH( bool, forceType );
bool ok = false;
const KMime::Message::Ptr &msg = readMimeFile( filename, ok );
QVERIFY(ok);
Kolab::KolabObjectReader reader;
+ if (forceType) {
+ reader.setObjectType(type);
+ }
Kolab::ObjectType t = reader.parseMimeMessage(msg);
QCOMPARE(t, type);
QCOMPARE(reader.getVersion(), Kolab::KolabV2);
+ QCOMPARE(Kolab::ErrorHandler::instance().error(), Kolab::ErrorHandler::Debug);
KCalCore::Incidence::Ptr v2result = reader.getIncidence();
@@ -111,6 +120,7 @@ void UpgradeTest::testIncidence()
// }
//Test comparator
QCOMPARE(*v2result, *v3result);
+ QCOMPARE(Kolab::ErrorHandler::instance().error(), Kolab::ErrorHandler::Debug);
}
void UpgradeTest::testContact_data()
@@ -118,7 +128,6 @@ void UpgradeTest::testContact_data()
QTest::addColumn<Kolab::ObjectType>( "type" );
QTest::addColumn<QString>( "filename" );
- //TODO store attachment names in kabc? See failing tests.
QTest::newRow( "v2contactSimple" )<< Kolab::ContactObject << TESTFILEDIR+QString::fromLatin1("v2/contacts/simple.vcf.mime");
QTest::newRow( "v2contactComplex" )<< Kolab::ContactObject << TESTFILEDIR+QString::fromLatin1("v2/contacts/complex.vcf.mime");
QTest::newRow( "v2contactAddress" )<< Kolab::ContactObject << TESTFILEDIR+QString::fromLatin1("v2/contacts/address.vcf.mime");
@@ -144,6 +153,7 @@ void UpgradeTest::testContact()
Kolab::ObjectType t = reader.parseMimeMessage(msg);
QCOMPARE(t, type);
QCOMPARE(reader.getVersion(), Kolab::KolabV2);
+ QCOMPARE(Kolab::ErrorHandler::instance().error(), Kolab::ErrorHandler::Debug);
KABC::Addressee v2Addressee = reader.getContact();
QVERIFY(!v2Addressee.isEmpty());
@@ -166,6 +176,7 @@ void UpgradeTest::testContact()
showDiff(converter.createVCard(v2Addressee), converter.createVCard(v3result));
QVERIFY( false );
}
+ QCOMPARE(Kolab::ErrorHandler::instance().error(), Kolab::ErrorHandler::Debug);
}
More information about the commits
mailing list