Page MenuHomePhorge

No OneTemporary

Authored By
Unknown
Size
18 KB
Referenced Files
None
Subscribers
None
diff --git a/kolabformat/kolabobject.cpp b/kolabformat/kolabobject.cpp
index c3603a5..3e4bdb6 100644
--- a/kolabformat/kolabobject.cpp
+++ b/kolabformat/kolabobject.cpp
@@ -1,391 +1,447 @@
/*
* Copyright (C) 2011 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 "kolabobject.h"
#include <kolabbase.h>
#include <kolabformatV2/journal.h>
#include <kolabformatV2/task.h>
#include <kolabformatV2/event.h>
#include <kolabformatV2/contact.h>
#include <kolabformatV2/distributionlist.h>
+#include <kolabformatV2/note.h>
#include <mime/mimeutils.h>
#include <conversion/kcalconversion.h>
#include <qdom.h>
#include <kdebug.h>
#include <qbuffer.h>
+#include <akonadi/notes/noteutils.h>
#include <kolab/kolabformat.h>
namespace Kolab {
static QString eventKolabType() { return QString::fromLatin1("application/x-vnd.kolab.event"); };
static QString todoKolabType() { return QString::fromLatin1("application/x-vnd.kolab.task"); };
static QString journalKolabType() { return QString::fromLatin1("application/x-vnd.kolab.journal"); };
static QString contactKolabType() { return QString::fromLatin1("application/x-vnd.kolab.contact"); };
static QString distlistKolabType() { return QString::fromLatin1("application/x-vnd.kolab.contact.distlist"); }
+static QString noteKolabType() { return QString::fromLatin1("application/x-vnd.kolab.note"); }
+
static QString xCalMimeType() { return QString::fromLatin1("application/calendar+xml"); };
/*
* Parse XML, create KCalCore container and extract attachments
*/
//TODO V2 specific
template <typename KCalPtr, typename Container>
static KCalPtr fromXML(const QByteArray &xmlData, QStringList &attachments)
{
const QDomDocument xmlDoc = KolabV2::KolabBase::loadDocument( QString::fromUtf8(xmlData) ); //TODO extract function from V2 format
Q_ASSERT ( !xmlDoc.isNull() );
const KCalPtr i = Container::fromXml( xmlDoc, QString() ); //For parsing we don't need the timezone, so we don't set one
Q_ASSERT ( i );
QDomNodeList nodes = xmlDoc.elementsByTagName("inline-attachment");
for (int i = 0; i < nodes.size(); i++ ) {
attachments.append(nodes.at(i).toElement().text());
}
return i;
}
//TODO V2 specific
template <typename IncidencePtr, typename Converter>
static inline IncidencePtr incidenceFromKolabImpl( const KMime::Message::Ptr &data, const QByteArray &mimetype, const QString &timezoneId )
{
KMime::Content *xmlContent = Mime::findContentByType( data, mimetype );
if ( !xmlContent ) {
qWarning() << "couldn't find part";
return IncidencePtr();
}
const QByteArray &xmlData = xmlContent->decodedContent();
QStringList attachments;
IncidencePtr ptr = fromXML<IncidencePtr, Converter>(xmlData, attachments); //TODO do we care about timezone?
Mime::getAttachments(ptr, attachments, data);
return ptr;
}
KABC::Addressee addresseFromKolab( const QByteArray &xmlData, const KMime::Message::Ptr &data)
{
KABC::Addressee addressee;
// kDebug() << "xmlData " << xmlData;
KolabV2::Contact contact(QString::fromUtf8(xmlData));
const QString pictureAttachmentName = contact.pictureAttachmentName();
if (!pictureAttachmentName.isEmpty()) {
QByteArray type;
KMime::Content *imgContent = Mime::findContentByName(data, "kolab-picture.png", type);
if (imgContent) {
QByteArray imgData = imgContent->decodedContent();
QBuffer buffer(&imgData);
buffer.open(QIODevice::ReadOnly);
QImage image;
image.load(&buffer, "PNG");
contact.setPicture(image);
}
}
QString logoAttachmentName = contact.logoAttachmentName();
if (!logoAttachmentName.isEmpty()) {
QByteArray type;
KMime::Content *imgContent = Mime::findContentByName(data, "kolab-logo.png", type);
if (imgContent) {
QByteArray imgData = imgContent->decodedContent();
QBuffer buffer(&imgData);
buffer.open(QIODevice::ReadOnly);
QImage image;
image.load(&buffer, "PNG");
contact.setLogo(image);
}
}
QString soundAttachmentName = contact.soundAttachmentName();
if (!soundAttachmentName.isEmpty()) {
QByteArray type;
KMime::Content *content = Mime::findContentByName(data, "sound", type);
if (content) {
QByteArray sData = content->decodedContent();
contact.setSound(sData);
}
}
contact.saveTo(&addressee);
return addressee;
}
KABC::ContactGroup contactGroupFromKolab(const QByteArray &xmlData)
{
KABC::ContactGroup contactGroup;
// kDebug() << "xmlData " << xmlData;
KolabV2::DistributionList distList(QString::fromUtf8(xmlData));
distList.saveTo(&contactGroup);
return contactGroup;
}
KMime::Message::Ptr contactToKolabFormat(const KolabV2::Contact& contact)
{
KMime::Message::Ptr message = Mime::createMessage( contactKolabType() ); //TODO v3 mimetype
message->subject()->fromUnicodeString( contact.uid(), "utf-8" );
message->from()->fromUnicodeString( contact.fullEmail(), "utf-8" );
KMime::Content* content = Mime::createMainPart( contactKolabType(), contact.saveXML().toUtf8() ); //TODO v3 mimetype
message->addContent( content );
if ( !contact.picture().isNull() ) {
QByteArray pic;
QBuffer buffer(&pic);
buffer.open(QIODevice::WriteOnly);
contact.picture().save(&buffer, "PNG");
buffer.close();
content = Mime::createAttachmentPart(QByteArray(), "image/png", "kolab-picture.png", pic );
message->addContent(content);
}
if ( !contact.logo().isNull() ) {
QByteArray pic;
QBuffer buffer(&pic);
buffer.open(QIODevice::WriteOnly);
contact.logo().save(&buffer, "PNG");
buffer.close();
content = Mime::createAttachmentPart(QByteArray(), "image/png", "kolab-logo.png", pic );
message->addContent(content);
}
if ( !contact.sound().isEmpty() ) {
content = Mime::createAttachmentPart(QByteArray(), "audio/unknown", "sound", contact.sound() );
message->addContent(content);
}
message->assemble();
return message;
}
+KMime::Message::Ptr noteFromKolab(const QByteArray &xmlData, const KMime::Message::Ptr &data)
+{
+ KolabV2::Note j;
+ if ( !j.load( xmlData ) ) {
+ kWarning() << "failed to read note";
+ return KMime::Message::Ptr();
+ }
+
+ Akonadi::NoteUtils::NoteMessageWrapper note;
+ note.setTitle(j.summary());
+ note.setText(j.body().toUtf8());
+ note.setFrom("kolab@kde4");
+ note.setCreationDate(data->date()->dateTime());
+ return note.message();
+}
+
+KMime::Message::Ptr noteToKolab(const KMime::Message::Ptr& msg)
+{
+ Akonadi::NoteUtils::NoteMessageWrapper note(msg);
+ KolabV2::Note j;
+ j.setSummary( note.title() );
+ j.setBody( note.text() );
+
+ KMime::Message::Ptr message = Mime::createMessage( noteKolabType() );
+ message->subject()->fromUnicodeString( j.summary(), "utf-8" );
+ KMime::Content* content = Mime::createMainPart( noteKolabType(), j.saveXML().toUtf8() );
+ message->addContent( content );
+
+ message->assemble();
+ return message;
+}
+
+
//Normalize incidences before serializing them
void normalizeIncidence(KCalCore::Incidence::Ptr i)
{
Q_FOREACH (KCalCore::Attendee::Ptr a, i->attendees()) {
a->setUid(QString()); //KCalCore sets the pointer as uid, we don't want that (if we wanted one, we'd want a real, globally unique one), so we clear the uid
}
//TODO Do we really not need the attachment uri for something else?
Q_FOREACH (KCalCore::Attachment::Ptr attachment, i->attachments()) {
attachment->setUri(QString::fromLatin1("cid:")+QString::fromLatin1(KMime::uniqueString() + '@' + "kolab.resource.akonadi")); //Serialize the attachment as attachment with uri, referencing the created mime-part
}
}
KCalCore::Event::Ptr readV2EventXML(const QByteArray& xmlData, QStringList& attachments)
{
return fromXML<KCalCore::Event::Ptr, KolabV2::Event>(xmlData, attachments);
}
//@cond PRIVATE
class KolabObjectReader::Private
{
public:
Private()
: mObjectType( InvalidObject ),
mVersion( KolabV3 )
{}
KCalCore::Incidence::Ptr mIncidence;
KABC::Addressee mAddressee;
KABC::ContactGroup mContactGroup;
+ KMime::Message::Ptr mNote;
ObjectType mObjectType;
Version mVersion;
};
//@endcond
KolabObjectReader::KolabObjectReader()
: d( new KolabObjectReader::Private )
{
}
KolabObjectReader::KolabObjectReader(const KMime::Message::Ptr& msg)
: d( new KolabObjectReader::Private )
{
parseMimeMessage(msg);
}
KolabObjectReader::~KolabObjectReader()
{
delete d;
}
ObjectType KolabObjectReader::parseMimeMessage(const KMime::Message::Ptr &msg)
{
d->mObjectType = InvalidObject;
KMime::Headers::Base *xKolabHeader = msg->getHeaderByType("X-Kolab-Type");
if (!xKolabHeader) {
kWarning() << "could not find xKolabHeader";
return InvalidObject;
}
const QString &kolabType = xKolabHeader->asUnicodeString(); //TODO we probably shouldn't use unicodeString
KMime::Content *xmlContent = Mime::findContentByType( msg, kolabType.toLocal8Bit() );
if ( !xmlContent ) {
qWarning() << "no part found";
return InvalidObject;
}
const QByteArray &xmlData = xmlContent->decodedContent();
QStringList attachments;
KCalCore::Incidence::Ptr i;
kDebug() << kolabType;
d->mVersion = KolabV2;
if (kolabType == eventKolabType()) { //Event
kDebug() << "event";
d->mIncidence = fromXML<KCalCore::Event::Ptr, KolabV2::Event>(xmlData, attachments);
d->mObjectType = EventObject;
} else if (kolabType == todoKolabType()) { //Todo
kDebug() << "todo";
d->mIncidence = fromXML<KCalCore::Todo::Ptr, KolabV2::Task>(xmlData, attachments);
d->mObjectType = TodoObject;
} else if (kolabType == journalKolabType()) { //Journal
kDebug() << "journal";
d->mIncidence = fromXML<KCalCore::Journal::Ptr, KolabV2::Journal>(xmlData, attachments);
d->mObjectType = JournalObject;
} else if (kolabType == contactKolabType()) { //Contact
kDebug() << "contact";
d->mAddressee = addresseFromKolab(xmlData, msg);
d->mObjectType = ContactObject;
} else if (kolabType == distlistKolabType()) { //Distlist
kDebug() << "distlist";
d->mContactGroup = contactGroupFromKolab(xmlData);
d->mObjectType = DistlistObject;
+ } else if (kolabType == noteKolabType()) { //Note
+ kDebug() << "notes";
+ d->mNote = noteFromKolab(xmlData, msg);
+ d->mObjectType = NoteObject;
} else {
kWarning() << "no kolab object found " << kolabType;
}
if (d->mIncidence) {
Mime::getAttachments(d->mIncidence, attachments, msg);
}
return d->mObjectType;
}
Version KolabObjectReader::getVersion() const
{
return d->mVersion;
}
ObjectType KolabObjectReader::getType() const
{
return d->mObjectType;
}
KCalCore::Event::Ptr KolabObjectReader::getEvent() const
{
return d->mIncidence.dynamicCast<KCalCore::Event>();
}
KCalCore::Todo::Ptr KolabObjectReader::getTodo() const
{
return d->mIncidence.dynamicCast<KCalCore::Todo>();
}
KCalCore::Journal::Ptr KolabObjectReader::getJournal() const
{
return d->mIncidence.dynamicCast<KCalCore::Journal>();
}
KCalCore::Incidence::Ptr KolabObjectReader::getIncidence() const
{
return d->mIncidence;
}
KABC::Addressee KolabObjectReader::getContact() const
{
return d->mAddressee;
}
KABC::ContactGroup KolabObjectReader::getDistlist() const
{
return d->mContactGroup;
}
+KMime::Message::Ptr KolabObjectReader::getNote() const
+{
+ return d->mNote;
+}
+
KMime::Message::Ptr KolabObjectWriter::writeEvent(const KCalCore::Event::Ptr &i, Version v, const QString &tz)
{
if (v == KolabV3) {
normalizeIncidence(i);
const Kolab::Event &incidence = Kolab::Conversion::fromKCalCore(*i);
const std::string &v3String = Kolab::writeEvent(incidence);
return Mime::createMessage(i, xCalMimeType(), eventKolabType(), QString::fromStdString(v3String).toLocal8Bit());
}
const QString &xml = KolabV2::Event::eventToXML(i, tz);
return Mime::createMessage(i, xCalMimeType(), eventKolabType(), xml.toLocal8Bit());
}
KMime::Message::Ptr KolabObjectWriter::writeTodo(const KCalCore::Todo::Ptr &i, Version v, const QString &tz)
{
if (v == KolabV3) {
normalizeIncidence(i);
const Kolab::Todo &incidence = Kolab::Conversion::fromKCalCore(*i);
const std::string &v3String = Kolab::writeTodo(incidence);
return Mime::createMessage(i, xCalMimeType(), todoKolabType(), QString::fromStdString(v3String).toLocal8Bit());
}
const QString &xml = KolabV2::Task::taskToXML(i, tz);
return Mime::createMessage(i, xCalMimeType(), todoKolabType(), xml.toLocal8Bit());
}
KMime::Message::Ptr KolabObjectWriter::writeJournal(const KCalCore::Journal::Ptr &i, Version v, const QString &tz)
{
if (v == KolabV3) {
normalizeIncidence(i);
const Kolab::Journal &incidence = Kolab::Conversion::fromKCalCore(*i);
const std::string &v3String = Kolab::writeJournal(incidence);
return Mime::createMessage(i, xCalMimeType(), journalKolabType(), QString::fromStdString(v3String).toLocal8Bit());
}
const QString &xml = KolabV2::Journal::journalToXML(i, tz);
return Mime::createMessage(i, xCalMimeType(), journalKolabType(), xml.toLocal8Bit());
}
KMime::Message::Ptr KolabObjectWriter::writeContact(const KABC::Addressee &addressee, Version v)
{
if (v == KolabV3) {
//TODO
}
KolabV2::Contact contact(&addressee);
return contactToKolabFormat(contact);
}
KMime::Message::Ptr distListToKolabFormat(const KolabV2::DistributionList& distList)
{
KMime::Message::Ptr message = Mime::createMessage( distlistKolabType() );
message->subject()->fromUnicodeString( distList.uid(), "utf-8" );
message->from()->fromUnicodeString( distList.uid(), "utf-8" );
KMime::Content* content = Mime::createMainPart( distlistKolabType(), distList.saveXML().toUtf8() );
message->addContent( content );
message->assemble();
return message;
}
KMime::Message::Ptr KolabObjectWriter::writeDistlist(const KABC::ContactGroup &distlist, Version v)
{
if (v == KolabV3) {
//TODO
}
KolabV2::DistributionList d(&distlist);
return distListToKolabFormat(d);
}
+KMime::Message::Ptr KolabObjectWriter::writeNote(const KMime::Message::Ptr &note, Version v)
+{
+ if (v == KolabV3) {
+ //TODO
+ }
+ return noteToKolab(note);
+}
+
+
}; //Namespace
diff --git a/kolabformat/kolabobject.h b/kolabformat/kolabobject.h
index a20ce99..3c00cbb 100644
--- a/kolabformat/kolabobject.h
+++ b/kolabformat/kolabobject.h
@@ -1,91 +1,95 @@
/*
* Copyright (C) 2011 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/>.
*/
#ifndef KOLABOBJECT_H
#define KOLABOBJECT_H
#include "kolab_export.h"
#include <kabc/addressee.h>
#include <kabc/contactgroup.h>
#include <kcalcore/incidence.h>
#include <kcalcore/event.h>
#include <kcalcore/journal.h>
#include <kcalcore/todo.h>
#include <kmime/kmime_message.h>
namespace Kolab {
enum Version {
KolabV2,
KolabV3
};
enum ObjectType {
InvalidObject,
EventObject,
TodoObject,
JournalObject,
ContactObject,
- DistlistObject
+ DistlistObject,
+ NoteObject
};
KOLAB_EXPORT KCalCore::Event::Ptr readV2EventXML(const QByteArray &xmlData, QStringList &attachments);
/**
* Class to read Kolab Mime files
*
* It implements the Kolab specifics of Mime message handling
*
*/
class KOLAB_EXPORT KolabObjectReader {
public:
KolabObjectReader();
explicit KolabObjectReader(const KMime::Message::Ptr &msg);
~KolabObjectReader();
ObjectType parseMimeMessage(const KMime::Message::Ptr &msg);
ObjectType getType() const;
Version getVersion() const;
KCalCore::Event::Ptr getEvent() const;
KCalCore::Todo::Ptr getTodo() const;
KCalCore::Journal::Ptr getJournal() const;
KCalCore::Incidence::Ptr getIncidence() const;
KABC::Addressee getContact() const;
KABC::ContactGroup getDistlist() const;
+ KMime::Message::Ptr getNote() const;
+
private:
//@cond PRIVATE
class Private;
Private *const d;
//@endcond
};
class KOLAB_EXPORT KolabObjectWriter {
public:
static KMime::Message::Ptr writeEvent(const KCalCore::Event::Ptr &, Version v = KolabV3, const QString &tz = QString());
static KMime::Message::Ptr writeTodo(const KCalCore::Todo::Ptr &, Version v = KolabV3, const QString &tz = QString());
static KMime::Message::Ptr writeJournal(const KCalCore::Journal::Ptr &, Version v = KolabV3, const QString &tz = QString());
static KMime::Message::Ptr writeContact(const KABC::Addressee &, Version v = KolabV3);
static KMime::Message::Ptr writeDistlist(const KABC::ContactGroup &, Version v = KolabV3);
+ static KMime::Message::Ptr writeNote(const KMime::Message::Ptr &, Version v = KolabV3);
};
}; //Namespace
#endif // KOLABOBJECT_H

File Metadata

Mime Type
text/x-diff
Expires
Fri, Apr 24, 1:59 PM (6 d, 5 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
18767106
Default Alt Text
(18 KB)

Event Timeline