summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Mollekopf <mollekopf@kolabsys.com>2015-05-10 14:42:37 +0200
committerChristian Mollekopf <mollekopf@kolabsys.com>2015-05-10 14:42:37 +0200
commit9cfc9c89d7fcf86bf1ccce4d1762e16595d493ee (patch)
tree1f0fed6b9f66f1a454faa290e1747d20791b39da
parentff711520f5a761091224b75107e93bc7d04ed5b8 (diff)
downloadlibkolab-9cfc9c89d7fcf86bf1ccce4d1762e16595d493ee.tar.gz
Fixed attachment handling.
-rw-r--r--kolabformat/mimeobject.cpp43
-rw-r--r--mime/mimeutils.cpp5
2 files changed, 31 insertions, 17 deletions
diff --git a/kolabformat/mimeobject.cpp b/kolabformat/mimeobject.cpp
index 13195d4..a4403ef 100644
--- a/kolabformat/mimeobject.cpp
+++ b/kolabformat/mimeobject.cpp
@@ -438,30 +438,41 @@ void MIMEObject::setVersion(Version version)
d->mDoOverrideVersion = true;
}
-std::vector<Kolab::Attachment> convertToReferences(const std::vector<Kolab::Attachment> &attachments)
+static std::string createCid()
+{
+ return QString::fromLatin1("cid:%1@%2").arg(QString::fromLatin1(KMime::uniqueString())).arg("kolab.resource.akonadi").toStdString();
+}
+
+std::vector<Kolab::Attachment> convertToReferences(const std::vector<Kolab::Attachment> &attachments, std::vector<std::string> &attachmentCids)
{
std::vector<Kolab::Attachment> attachmentsWithReferences;
Q_FOREACH (const Kolab::Attachment &a, attachments) {
Kolab::Attachment attachment;
attachment.setLabel(a.label());
- attachment.setUri((QString::fromLatin1("cid:")+QString::fromLatin1(KMime::uniqueString() + '@' + "kolab.resource.akonadi")).toStdString(), a.mimetype()); //Serialize the attachment as attachment with uri, referencing the created mime-part
+ const std::string cid = a.uri().empty() ? createCid() : a.uri();
+ attachmentCids.push_back(cid);
+ attachment.setUri(cid, a.mimetype()); //Serialize the attachment as attachment with uri, referencing the created mime-part
attachmentsWithReferences.push_back(attachment);
}
return attachmentsWithReferences;
}
template <class T>
-static T convertAttachmentsToReferences(const T &incidence)
+static T convertAttachmentsToReferences(const T &incidence, std::vector<std::string> &attachmentCids)
{
T removedAttachments = incidence;
- removedAttachments.setAttachments(convertToReferences(incidence.attachments()));
+ removedAttachments.setAttachments(convertToReferences(incidence.attachments(), attachmentCids));
return removedAttachments;
}
-static void addAttachments(KMime::Message::Ptr msg, const std::vector<Attachment> &attachments)
+static void addAttachments(KMime::Message::Ptr msg, const std::vector<Attachment> &attachments, std::vector<std::string> &attachmentCids)
{
+ int index = 0;
foreach (const Attachment &attachment, attachments) {
- msg->addContent(Mime::createAttachmentPart(Mime::fromCid(QByteArray(attachment.uri().c_str())).toLatin1(), QByteArray(attachment.mimetype().c_str()), QString::fromStdString(attachment.label()), QByteArray(attachment.data().c_str())));
+ const std::string data = attachment.data();
+ const std::string cid = attachmentCids.empty() ? attachment.uri() : attachmentCids.at(index);
+ msg->addContent(Mime::createAttachmentPart(Mime::fromCid(QByteArray(cid.c_str())).toLatin1(), QByteArray(attachment.mimetype().c_str()), QString::fromStdString(attachment.label()), QByteArray::fromRawData(data.c_str(), data.size())));
+ index++;
}
}
@@ -528,14 +539,15 @@ std::string MIMEObject::writeEvent(const Event &event, Version version, const st
KMime::Message::Ptr msg;
Kolab::XMLObject xmlObject;
+ std::vector<std::string> attachmentCids;
if (version == KolabV3) {
- const std::string xml = xmlObject.writeEvent(convertAttachmentsToReferences(event), version, productId);
+ const std::string xml = xmlObject.writeEvent(convertAttachmentsToReferences(event, attachmentCids), version, productId);
msg = Mime::createMessage(xCalMimeType(), eventKolabType(), xml, true, productId, event.organizer().email(), event.organizer().name(), event.uid());
} else if (version == KolabV2) {
const std::string xml = xmlObject.writeEvent(event, version, productId);
msg = Mime::createMessage(eventKolabType(), eventKolabType(), xml, false, productId, event.organizer().email(), event.organizer().name(), event.uid());
}
- addAttachments(msg, event.attachments());
+ addAttachments(msg, event.attachments(), attachmentCids);
msg->assemble();
return msg->encodedContent().data();
}
@@ -552,14 +564,15 @@ std::string MIMEObject::writeTodo(const Todo &todo, Version version, const std::
KMime::Message::Ptr msg;
Kolab::XMLObject xmlObject;
+ std::vector<std::string> attachmentCids;
if (version == KolabV3) {
- const std::string xml = xmlObject.writeTodo(convertAttachmentsToReferences(todo), version, productId);
+ const std::string xml = xmlObject.writeTodo(convertAttachmentsToReferences(todo, attachmentCids), version, productId);
msg = Mime::createMessage(xCalMimeType(), todoKolabType(), xml, true, productId, todo.organizer().email(), todo.organizer().name(), todo.uid());
} else if (version == KolabV2) {
const std::string xml = xmlObject.writeTodo(todo, version, productId);
msg = Mime::createMessage(todoKolabType(), todoKolabType(), xml, false, productId, todo.organizer().email(), todo.organizer().name(), todo.uid());
}
- addAttachments(msg, todo.attachments());
+ addAttachments(msg, todo.attachments(), attachmentCids);
msg->assemble();
return msg->encodedContent().data();
}
@@ -576,14 +589,15 @@ std::string MIMEObject::writeJournal(const Journal &journal, Version version, co
KMime::Message::Ptr msg;
Kolab::XMLObject xmlObject;
+ std::vector<std::string> attachmentCids;
if (version == KolabV3) {
- const std::string xml = xmlObject.writeJournal(convertAttachmentsToReferences(journal), version, productId);
+ const std::string xml = xmlObject.writeJournal(convertAttachmentsToReferences(journal, attachmentCids), version, productId);
msg = Mime::createMessage(xCalMimeType(), journalKolabType(), xml, true, productId, std::string(), std::string(), journal.uid());
} else if (version == KolabV2) {
const std::string xml = xmlObject.writeJournal(journal, version, productId);
msg = Mime::createMessage(journalKolabType(), journalKolabType(), xml, false, productId, std::string(), std::string(), journal.uid());
}
- addAttachments(msg, journal.attachments());
+ addAttachments(msg, journal.attachments(), attachmentCids);
msg->assemble();
return msg->encodedContent().data();
}
@@ -600,14 +614,15 @@ std::string MIMEObject::writeNote(const Note &note, Version version, const std::
KMime::Message::Ptr msg;
Kolab::XMLObject xmlObject;
+ std::vector<std::string> attachmentCids;
if (version == KolabV3) {
- const std::string xml = xmlObject.writeNote(convertAttachmentsToReferences(note), version, productId);
+ const std::string xml = xmlObject.writeNote(convertAttachmentsToReferences(note, attachmentCids), version, productId);
msg = Mime::createMessage(kolabMimeType(), noteKolabType(), xml, true, productId, std::string(), std::string(), note.uid());
} else if (version == KolabV2) {
const std::string xml = xmlObject.writeNote(note, version, productId);
msg = Mime::createMessage(noteKolabType(), noteKolabType(), xml, false, productId, std::string(), std::string(), note.uid());
}
- addAttachments(msg, note.attachments());
+ addAttachments(msg, note.attachments(), attachmentCids);
msg->assemble();
return msg->encodedContent().data();
}
diff --git a/mime/mimeutils.cpp b/mime/mimeutils.cpp
index 76001bb..640d3e5 100644
--- a/mime/mimeutils.cpp
+++ b/mime/mimeutils.cpp
@@ -177,9 +177,8 @@ KMime::Content* createMainPart(const QByteArray& mimeType, const QByteArray& dec
return content;
}
-KMime::Content* createAttachmentPart(const QByteArray& cid, const QByteArray& mimeType, const QString& fileName, const QByteArray& decodedContent)
+KMime::Content* createAttachmentPart(const QByteArray& cid, const QByteArray& mimeType, const QString& fileName, const QByteArray& base64EncodedContent)
{
- qDebug() << "content: " << decodedContent;
KMime::Content* content = new KMime::Content();
if (!cid.isEmpty()) {
content->contentID()->setIdentifier( cid );
@@ -189,7 +188,7 @@ KMime::Content* createAttachmentPart(const QByteArray& cid, const QByteArray& mi
content->contentTransferEncoding()->setEncoding( KMime::Headers::CEbase64 );
content->contentDisposition()->setDisposition( KMime::Headers::CDattachment );
content->contentDisposition()->setFilename( fileName );
- content->setBody( decodedContent );
+ content->setBody( base64EncodedContent );
return content;
}