summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Mollekopf <mollekopf@kolabsys.com>2012-10-26 15:04:15 (GMT)
committerChristian Mollekopf <mollekopf@kolabsys.com>2012-10-26 15:04:15 (GMT)
commit49273576d059c21a3aba58ee4f371b40f12eaf14 (patch)
tree8ef3b1029eab38f62d1c48b68daf956070142638
parente21838326486e1a382ecd214e1052b75493ea453 (diff)
downloadkolab-utils-49273576d059c21a3aba58ee4f371b40f12eaf14.tar.gz
Moved the writing to the kolabaccount class which frees the way for writing kolabv2 as well.
-rw-r--r--fbdaemon/tests/fbaggregatortest.cpp4
-rw-r--r--fbdaemon/tests/fbgeneratortest.cpp4
-rw-r--r--lib/kolabaccount.cpp35
-rw-r--r--lib/object.h24
-rw-r--r--migrationutility/sourceaccount.cpp45
-rw-r--r--migrationutility/tests/exchangeimaptest.cpp22
-rw-r--r--migrationutility/tests/migrationscenariotest.cpp4
7 files changed, 90 insertions, 48 deletions
diff --git a/fbdaemon/tests/fbaggregatortest.cpp b/fbdaemon/tests/fbaggregatortest.cpp
index 1a0c308..56a66e1 100644
--- a/fbdaemon/tests/fbaggregatortest.cpp
+++ b/fbdaemon/tests/fbaggregatortest.cpp
@@ -58,13 +58,13 @@ public:
fb.setOrganizer(Kolab::ContactReference("mail@example.com", "john doe"));
fb.setStart(Kolab::Conversion::fromDate(KDateTime::currentUtcDateTime()));
fb.setEnd(Kolab::Conversion::fromDate(KDateTime::currentUtcDateTime().addDays(60)));
- fbObj1.message = Kolab::KolabObjectWriter::writeFreebusy(fb, Kolab::KolabV3, "fbtest");
+ fbObj1.object = QVariant::fromValue(Kolab::KolabObjectWriter::writeFreebusy(fb, Kolab::KolabV3, "fbtest"));
Object fbObj2;
Kolab::Freebusy fb2;
fb2.setStart(Kolab::Conversion::fromDate(KDateTime::currentUtcDateTime()));
fb2.setEnd(Kolab::Conversion::fromDate(KDateTime::currentUtcDateTime().addDays(60)));
- fbObj2.message = Kolab::KolabObjectWriter::writeFreebusy(fb2, Kolab::KolabV3, "fbtest");
+ fbObj2.object = QVariant::fromValue(Kolab::KolabObjectWriter::writeFreebusy(fb2, Kolab::KolabV3, "fbtest"));
folders << Folder("Freebusy", Kolab::FreebusyType, QList<Object>() << fbObj1 << fbObj2);
}
diff --git a/fbdaemon/tests/fbgeneratortest.cpp b/fbdaemon/tests/fbgeneratortest.cpp
index b02aeda..4064b13 100644
--- a/fbdaemon/tests/fbgeneratortest.cpp
+++ b/fbdaemon/tests/fbgeneratortest.cpp
@@ -54,7 +54,7 @@ public:
event->setUid("uid1");
event->setDtStart(KDateTime::currentUtcDateTime());
event->setDtEnd(KDateTime::currentUtcDateTime().addSecs(3600));
- calObj1.message = Kolab::KolabObjectWriter::writeEvent(event, Kolab::KolabV3, "fbtest");
+ calObj1.object = QVariant::fromValue(Kolab::KolabObjectWriter::writeEvent(event, Kolab::KolabV3, "fbtest"));
folders << Folder("Calendar", Kolab::EventType, QList<Object>() << calObj1);
// folders << Folder("Freebusy", Kolab::FreebusyType, QList<Object>()/* << fbObj1*/);
@@ -120,7 +120,7 @@ private:
Kolab::Freebusy fb;
fb.setStart(Kolab::Conversion::fromDate(KDateTime::currentUtcDateTime()));
fb.setEnd(Kolab::Conversion::fromDate(KDateTime::currentUtcDateTime().addDays(60)));
- fbObj1.message = Kolab::KolabObjectWriter::writeFreebusy(fb, Kolab::KolabV3, "fbtest");
+ fbObj1.object = QVariant(Kolab::KolabObjectWriter::writeFreebusy(fb, Kolab::KolabV3, "fbtest"));
QList<Folder> targetFolders;
targetFolders << Folder("Freebusy", Kolab::FreebusyType, QList<Object>() << fbObj1);
diff --git a/lib/kolabaccount.cpp b/lib/kolabaccount.cpp
index 0d98ede..8d744a1 100644
--- a/lib/kolabaccount.cpp
+++ b/lib/kolabaccount.cpp
@@ -34,6 +34,7 @@
#include <kolab/errorhandler.h>
#include <kolab/kolabdefinitions.h>
#include <formathelpers.h>
+#include <kolabobject.h>
KolabAccount::KolabAccount(QObject* parent)
@@ -115,6 +116,30 @@ bool KolabAccount::init()
const char* FlagRecent2 = "\\Recent";
+
+
+KMime::Message::Ptr writeObject(const Object &obj)
+{
+ KMime::Message::Ptr msg;
+ if (obj.object.canConvert<KMime::Message::Ptr>()) {
+ msg = obj.object.value<KMime::Message::Ptr>();
+ } else if (obj.object.canConvert<KCalCore::Incidence::Ptr>()) {
+ msg = Kolab::KolabObjectWriter::writeIncidence(obj.object.value<KCalCore::Incidence::Ptr>());
+ } else if (obj.object.canConvert<KABC::Addressee>()) {
+ msg = Kolab::KolabObjectWriter::writeContact(obj.object.value<KABC::Addressee>());
+ } else if (obj.object.canConvert<KABC::ContactGroup>()) {
+ msg = Kolab::KolabObjectWriter::writeDistlist(obj.object.value<KABC::ContactGroup>());
+ } else if (obj.object.canConvert<Note>()) {
+ msg = Kolab::KolabObjectWriter::writeNote(obj.object.value<Note>().msg);
+ } else if (obj.object.canConvert<Dictionary>()) {
+ const Dictionary dict = obj.object.value<Dictionary>();
+ msg = Kolab::KolabObjectWriter::writeDictionary(dict.dict, dict.lang);
+ } else {
+ Error() << "Type not handled";
+ }
+ return msg;
+}
+
void KolabAccount::appendObject(Object obj, const QString& folder)
{
if (mDryRun) {
@@ -122,14 +147,16 @@ void KolabAccount::appendObject(Object obj, const QString& folder)
return;
}
Q_ASSERT(mFolders.contains(folder));
- if (!obj.message) {
+
+ KMime::Message::Ptr message = writeObject(obj);
+
+ if (!message) {
Error() << "got empty message";
return;
}
- Q_ASSERT(obj.message.get());
KIMAP::AppendJob *job = new KIMAP::AppendJob( mSession );
job->setMailBox( folder );
- job->setContent( obj.message->encodedContent( true ) );
+ job->setContent( message->encodedContent( true ) );
//The Recent flag is a special case which is not allowed in the append command
obj.flags.removeAll(QByteArray(FlagRecent2));
job->setFlags( obj.flags );
@@ -321,7 +348,7 @@ QList<Object> KolabAccount::getObjects(const QString& folder)
QList<Object> messages;
foreach (const KMime::Message::Ptr &msg, fetchJob->getMessages()) {
Object obj;
- obj.message = msg;
+ obj.object = QVariant::fromValue(msg);
obj.flags = fetchJob->getFlags(msg);
messages.append(obj);
}
diff --git a/lib/object.h b/lib/object.h
index 1d34e1a..60fb74e 100644
--- a/lib/object.h
+++ b/lib/object.h
@@ -17,12 +17,30 @@
#ifndef OBJECT_H
#define OBJECT_H
+
#include <kmime/kmime_message.h>
+#include <kcalcore/incidence.h>
+
+Q_DECLARE_METATYPE( KCalCore::Incidence::Ptr )
+Q_DECLARE_METATYPE( KMime::Message::Ptr )
+
+//Little helper class to distinguish notes from plain Mime messages
+struct Note
+{
+ KMime::Message::Ptr msg;
+};
+Q_DECLARE_METATYPE( Note )
+
+struct Dictionary
+{
+ QString lang;
+ QStringList dict;
+};
+Q_DECLARE_METATYPE( Dictionary )
-class Object
+struct Object
{
-public:
- KMime::Message::Ptr message;
+ QVariant object;
QList<QByteArray> flags;
};
diff --git a/migrationutility/sourceaccount.cpp b/migrationutility/sourceaccount.cpp
index 49b30f8..7966454 100644
--- a/migrationutility/sourceaccount.cpp
+++ b/migrationutility/sourceaccount.cpp
@@ -143,35 +143,33 @@ void KolabSourceAccount::mailBoxesReceived(const QList<KIMAP::MailBoxDescriptor>
// }
}
-KMime::Message::Ptr upgradeMessage(KMime::Message::Ptr msg, Kolab::ObjectType overrideObjectType)
+QVariant upgradeMessage(KMime::Message::Ptr msg)
{
Kolab::KolabObjectReader reader;
- if (overrideObjectType != Kolab::InvalidObject) {
- reader.setObjectType(overrideObjectType);
- }
switch (reader.parseMimeMessage(msg)) {
case Kolab::EventObject:
- return Kolab::KolabObjectWriter::writeEvent(reader.getEvent());
case Kolab::TodoObject:
- return Kolab::KolabObjectWriter::writeTodo(reader.getTodo());
case Kolab::JournalObject:
- return Kolab::KolabObjectWriter::writeJournal(reader.getJournal());
+ return QVariant::fromValue(reader.getIncidence());
case Kolab::ContactObject:
- return Kolab::KolabObjectWriter::writeContact(reader.getContact());
+ return QVariant::fromValue(reader.getContact());
case Kolab::DistlistObject:
- return Kolab::KolabObjectWriter::writeDistlist(reader.getDistlist());
- case Kolab::NoteObject:
- return Kolab::KolabObjectWriter::writeNote(reader.getNote());
+ return QVariant::fromValue(reader.getDistlist());
+ case Kolab::NoteObject: {
+ Note note;
+ note.msg = reader.getNote();
+ return QVariant::fromValue(note);
+ }
case Kolab::DictionaryConfigurationObject: {
- QString lang;
- const QStringList &dict = reader.getDictionary(lang);
- return Kolab::KolabObjectWriter::writeDictionary(dict, lang);
+ Dictionary dictionary;
+ dictionary.dict = reader.getDictionary(dictionary.lang);
+ return QVariant::fromValue(dictionary);
}
case Kolab::InvalidObject:
//TODO handle configuration objects
Error() << "failed to read mime file";
}
- return KMime::Message::Ptr();
+ return QVariant();
}
QList<Object> KolabSourceAccount::getObjects(const QString& folder)
@@ -186,15 +184,14 @@ QList<Object> KolabSourceAccount::getObjects(const QString& folder)
Debug() << fetchJob->getMessages().size();
QList<Object> messages;
foreach (const KMime::Message::Ptr &msg, fetchJob->getMessages()) {
- //TODO writing the message again would rather belong to the KolabAccount, but for that we'd need some Kolab object container to pass around (aka QPair<Type,Object>)
Object obj;
if (isKolabType) {
- obj.message = upgradeMessage(msg, Kolab::InvalidObject);
+ obj.object = upgradeMessage(msg);
} else {
- obj.message = msg;
+ obj.object = QVariant::fromValue(msg);
}
obj.flags = fetchJob->getFlags(msg);
- if (!obj.message) {
+ if (!obj.object.isValid()) {
Error() << "got empty message";
continue;
}
@@ -289,7 +286,7 @@ QList<Object> ExchangeIMAPSourceAccount::getObjects(const QString& folder)
Debug() << type.first;
if (type.first == Kolab::MailType) {
Object obj;
- obj.message = msg;
+ obj.object = QVariant::fromValue(msg);
obj.flags = fetchJob->getFlags(msg);
messages.append(obj);
} else if (type.first == Kolab::EventObject || type.first == Kolab::TodoObject || type.first == Kolab::JournalObject) {
@@ -302,9 +299,9 @@ QList<Object> ExchangeIMAPSourceAccount::getObjects(const QString& folder)
//We're simply assuming here that we never have different incidence types mixed in a folder
foreach (const KCalCore::Incidence::Ptr &inc, calendar->incidences()) {
Object obj;
- obj.message = Kolab::KolabObjectWriter::writeIncidence(inc, Kolab::KolabV3, "kolab-migration");
+ obj.object = QVariant::fromValue(inc);
obj.flags = fetchJob->getFlags(msg);
- if (!obj.message) {
+ if (!obj.object.isValid()) {
Error() << "got empty message";
continue;
}
@@ -317,9 +314,9 @@ QList<Object> ExchangeIMAPSourceAccount::getObjects(const QString& folder)
const KABC::Addressee addressee = format.parseVCard(content);
Object obj;
- obj.message = Kolab::KolabObjectWriter::writeContact(addressee, Kolab::KolabV3, "kolab-migration");
+ obj.object = QVariant::fromValue(addressee);
obj.flags = fetchJob->getFlags(msg);
- if (!obj.message) {
+ if (!obj.object.isValid()) {
Error() << "got empty message";
continue;
}
diff --git a/migrationutility/tests/exchangeimaptest.cpp b/migrationutility/tests/exchangeimaptest.cpp
index 93fe80a..281485c 100644
--- a/migrationutility/tests/exchangeimaptest.cpp
+++ b/migrationutility/tests/exchangeimaptest.cpp
@@ -74,11 +74,11 @@ class ExchangeIMAPTest: public QObject
msg->assemble();
Object calObj1;
- calObj1.message = msg;
+ calObj1.object = QVariant::fromValue(msg);
calObj1.flags << QByteArray("\\Seen");
Object targetObject;
- targetObject.message = Kolab::KolabObjectWriter::writeEvent(event);
+ targetObject.object = QVariant::fromValue(Kolab::KolabObjectWriter::writeEvent(event));
targetObject.flags << QByteArray("\\Seen");
return QPair<Object,Object>(calObj1, targetObject);
@@ -108,11 +108,11 @@ class ExchangeIMAPTest: public QObject
msg->assemble();
Object calObj1;
- calObj1.message = msg;
+ calObj1.object = QVariant::fromValue(msg);
calObj1.flags << QByteArray("\\Seen");
Object targetObject;
- targetObject.message = Kolab::KolabObjectWriter::writeTodo(event);
+ targetObject.object = QVariant::fromValue(Kolab::KolabObjectWriter::writeTodo(event));
targetObject.flags << QByteArray("\\Seen");
return QPair<Object,Object>(calObj1, targetObject);
@@ -141,11 +141,11 @@ class ExchangeIMAPTest: public QObject
msg->assemble();
Object calObj1;
- calObj1.message = msg;
+ calObj1.object = QVariant::fromValue(msg);
calObj1.flags << QByteArray("\\Seen");
Object targetObject;
- targetObject.message = Kolab::KolabObjectWriter::writeContact(addressee);
+ targetObject.object = QVariant::fromValue(Kolab::KolabObjectWriter::writeContact(addressee));
targetObject.flags << QByteArray("\\Seen");
return QPair<Object,Object>(calObj1, targetObject);
}
@@ -159,7 +159,7 @@ class ExchangeIMAPTest: public QObject
msg->subject()->fromUnicodeString( subject, "utf-8" );
msg->assemble();
Object targetObject;
- targetObject.message = msg;
+ targetObject.object = QVariant::fromValue(msg);
targetObject.flags << QByteArray("\\Seen");
return targetObject;
}
@@ -248,14 +248,14 @@ private:
QList<Object>::const_iterator recObjIt = objects.constBegin();
QList<Object>::const_iterator objIt = foldersIt->objects.constBegin();
for (;objIt != foldersIt->objects.constEnd() && recObjIt != objects.constEnd(); ++objIt, ++recObjIt) {
- Kolab::KolabObjectReader reader(objIt->message);
+ Kolab::KolabObjectReader reader(objIt->object.value<KMime::Message::Ptr>());
Kolab::ObjectType type = reader.getType();
if (type == Kolab::EventObject) {
- QCOMPARE(*Kolab::KolabObjectReader(recObjIt->message).getEvent(), *Kolab::KolabObjectReader(objIt->message).getEvent());
+ QCOMPARE(*recObjIt->object.value<KCalCore::Incidence::Ptr>().dynamicCast<KCalCore::Event>(), *Kolab::KolabObjectReader(objIt->object.value<KMime::Message::Ptr>()).getEvent());
} else if (type == Kolab::TodoObject) {
- QCOMPARE(*Kolab::KolabObjectReader(recObjIt->message).getTodo(), *Kolab::KolabObjectReader(objIt->message).getTodo());
+ QCOMPARE(*recObjIt->object.value<KCalCore::Incidence::Ptr>().dynamicCast<KCalCore::Todo>(), *Kolab::KolabObjectReader(objIt->object.value<KMime::Message::Ptr>()).getTodo());
} else if (type == Kolab::ContactObject) {
- QCOMPARE(Kolab::KolabObjectReader(recObjIt->message).getContact().name(), Kolab::KolabObjectReader(objIt->message).getContact().name());
+ QCOMPARE(recObjIt->object.value<KABC::Addressee>().name(), Kolab::KolabObjectReader(objIt->object.value<KMime::Message::Ptr>()).getContact().name());
} else {
// QCOMPARE(Kolab::KolabObjectReader(recObjIt->message).getEvent(), *Kolab::KolabObjectReader(objIt->message).getEvent());
}
diff --git a/migrationutility/tests/migrationscenariotest.cpp b/migrationutility/tests/migrationscenariotest.cpp
index b5a20a8..33f3ff2 100644
--- a/migrationutility/tests/migrationscenariotest.cpp
+++ b/migrationutility/tests/migrationscenariotest.cpp
@@ -52,7 +52,7 @@ class MigrationScenarioTest: public QObject
KCalCore::Event::Ptr event(new KCalCore::Event());
event->setUid(uid);
event->setDtStart(KDateTime::currentUtcDateTime());
- calObj1.message = Kolab::KolabObjectWriter::writeEvent(event, Kolab::KolabV2, "migrationtest");
+ calObj1.object = QVariant::fromValue(Kolab::KolabObjectWriter::writeEvent(event, Kolab::KolabV2, "migrationtest"));
calObj1.flags << QByteArray("\\Seen");
return calObj1;
@@ -133,7 +133,7 @@ private:
QList<Object>::const_iterator recObjIt = objects.constBegin();
QList<Object>::const_iterator objIt = foldersIt->objects.constBegin();
for (;objIt != foldersIt->objects.constEnd() && recObjIt != objects.constEnd(); ++objIt, ++recObjIt) {
- QCOMPARE(*Kolab::KolabObjectReader(recObjIt->message).getEvent(), *Kolab::KolabObjectReader(objIt->message).getEvent());
+ QCOMPARE(*recObjIt->object.value<KCalCore::Incidence::Ptr>().dynamicCast<KCalCore::Event>(), *Kolab::KolabObjectReader(objIt->object.value<KMime::Message::Ptr>()).getEvent());
foreach (const QByteArray &flag, objIt->flags) {
QVERIFY(recObjIt->flags.contains(flag));
}