Page MenuHomePhorge

mimeutils.cpp
No OneTemporary

Authored By
Unknown
Size
7 KB
Referenced Files
None
Subscribers
None

mimeutils.cpp

/*
* Copyright (C) 2012 Christian Mollekopf <mollekopf@kolabsys.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mimeutils.h"
#include <quuid.h>
#include <QtCore/qfile.h>
#include <qdom.h>
#include <kdebug.h>
namespace Kolab {
namespace Mime {
KMime::Message::Ptr readMimeFile( const QString &fileName )
{
// qDebug() << fileName;
QFile file( fileName );
file.open( QFile::ReadOnly );
const QByteArray data = file.readAll();
Q_ASSERT( !data.isEmpty() );
KMime::Message *msg = new KMime::Message;
msg->setContent( data );
msg->parse();
return KMime::Message::Ptr(msg);
}
KMime::Content* findContentByType(const KMime::Message::Ptr &data, const QByteArray &type)
{
const KMime::Content::List list = data->contents();
// qDebug() << list.size();
Q_FOREACH(KMime::Content *c, list)
{
// qDebug() << c->contentType()->mimeType() << type;
if (c->contentType()->mimeType() == type)
return c;
}
return 0;
}
KMime::Content* findContentByName(const KMime::Message::Ptr &data, const QString &name, QByteArray &type)
{
const KMime::Content::List list = data->contents();
Q_FOREACH(KMime::Content *c, list)
{
if ( c->contentType()->name() == name ) {
type = c->contentType()->mimeType();
return c;
}
}
return 0;
}
//TODO replace with getAttachments
void attachmentsFromKolab(const KMime::Message::Ptr& data, const QDomDocument &xmlDoc, const KCalCore::Incidence::Ptr &incidence)
{
QDomNodeList nodes = xmlDoc.elementsByTagName("inline-attachment");
for (int i = 0; i < nodes.size(); i++ ) {
const QString name = nodes.at(i).toElement().text();
QByteArray type;
KMime::Content *content = findContentByName(data, name, type);
if (!content) // guard against malformed events with non-existent attachments
continue;
const QByteArray c = content->decodedContent().toBase64();
KCalCore::Attachment::Ptr attachment( new KCalCore::Attachment( c, QString::fromLatin1( type ) ) );
attachment->setLabel( name );
incidence->addAttachment(attachment);
kDebug() << "ATTACHEMENT NAME" << name << type;
}
}
QByteArray getXmlDocument(const KMime::Message::Ptr &data, const QByteArray &mimetype)
{
KMime::Content *xmlContent = findContentByType( data, mimetype );
if ( xmlContent ) {
return xmlContent->decodedContent();
}
kDebug() << "document not found";
return QByteArray();
}
QByteArray fromCid(const QString &cid)
{
if (cid.left(4) != QString::fromLatin1("cid:")) { //Don't set if not a cid, happens when serializing format v2
return QByteArray();
}
return cid.right(cid.size()-4).toLatin1();
}
KMime::Message::Ptr createMessage(const KCalCore::Incidence::Ptr &incidencePtr, const QString &mimetype, const QString &xKolabType, const QByteArray &xml)
{
KMime::Message::Ptr message = createMessage( xKolabType );
message->from()->addAddress( incidencePtr->organizer()->email().toUtf8(), incidencePtr->organizer()->name() );
message->subject()->fromUnicodeString( incidencePtr->uid(), "utf-8" );
KMime::Content *content = createMainPart( mimetype, xml );
message->addContent( content );
Q_FOREACH (KCalCore::Attachment::Ptr attachment, incidencePtr->attachments()) {
content = createAttachmentPart(fromCid(attachment->uri()), attachment->mimeType(), attachment->label(), attachment->decodedData() );
message->addContent( content );
}
message->assemble();
return message;
}
KMime::Content* createExplanationPart()
{
KMime::Content *content = new KMime::Content();
content->contentType()->setMimeType( "text/plain" );
content->contentType()->setCharset( "us-ascii" );
content->contentTransferEncoding()->setEncoding( KMime::Headers::CE7Bit );
content->setBody( "This is a Kolab Groupware object.\n"
"To view this object you will need an email client that can understand the Kolab Groupware format.\n"
"For a list of such email clients please visit\n"
"http://www.kolab.org/kolab2-clients.html\n" );
return content;
}
KMime::Message::Ptr createMessage(const QString& xKolabType)
{
KMime::Message::Ptr message( new KMime::Message );
message->date()->setDateTime( KDateTime::currentLocalDateTime() );
KMime::Headers::Generic *h = new KMime::Headers::Generic( "X-Kolab-Type", message.get(), xKolabType, "utf-8" );
message->appendHeader( h );
message->userAgent()->from7BitString( "Akonadi Kolab Proxy Resource" );
message->contentType()->setMimeType( "multipart/mixed" );
message->contentType()->setBoundary( KMime::multiPartBoundary() );
message->addContent( createExplanationPart() );
return message;
}
KMime::Content* createMainPart(const QString& mimeType, const QByteArray& decodedContent)
{
KMime::Content* content = new KMime::Content();
content->contentType()->setMimeType( mimeType.toLatin1() );
content->contentType()->setName( "kolab.xml", "us-ascii" );
content->contentTransferEncoding()->setEncoding( KMime::Headers::CEquPr );
content->contentDisposition()->setDisposition( KMime::Headers::CDattachment );
content->contentDisposition()->setFilename( "kolab.xml" );
content->setBody( decodedContent );
return content;
}
KMime::Content* createAttachmentPart(const QByteArray& cid, const QString& mimeType, const QString& fileName, const QByteArray& decodedContent)
{
KMime::Content* content = new KMime::Content();
if (!cid.isEmpty()) {
content->contentID()->setIdentifier( cid );
}
content->contentType()->setMimeType( mimeType.toLatin1() );
content->contentType()->setName( fileName, "us-ascii" );
content->contentTransferEncoding()->setEncoding( KMime::Headers::CEbase64 );
content->contentDisposition()->setDisposition( KMime::Headers::CDattachment );
content->contentDisposition()->setFilename( fileName );
content->setBody( decodedContent );
return content;
}
void getAttachments(KCalCore::Incidence::Ptr incidence, const QStringList &attachments, const KMime::Message::Ptr &mimeData)
{
foreach (const QString &name, attachments) {
QByteArray type;
KMime::Content *content = findContentByName(mimeData, name, type);
if (!content) { // guard against malformed events with non-existent attachments
qWarning() << "could not find attachment: "<< name;
continue;
}
const QByteArray c = content->decodedContent().toBase64();
KCalCore::Attachment::Ptr attachment( new KCalCore::Attachment( c, QString::fromLatin1( type ) ) );
attachment->setLabel( name );
incidence->addAttachment(attachment);
kDebug() << "ATTACHEMENT NAME" << name << type;
}
}
}; //Namespace
}; //Namespace

File Metadata

Mime Type
text/x-c++
Expires
Sat, Apr 4, 9:46 AM (3 w, 6 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
18727956
Default Alt Text
mimeutils.cpp (7 KB)

Event Timeline