File: mimeobject.cpp

package info (click to toggle)
kdepim-runtime 4%3A20.08.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 25,680 kB
  • sloc: cpp: 94,670; xml: 981; sh: 101; javascript: 60; makefile: 13
file content (822 lines) | stat: -rw-r--r-- 28,110 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
/*
 * Copyright (C) 2012  Sofia Balicka <balicka@kolabsys.com>
 * Copyright (C) 2014  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 <https://www.gnu.org/licenses/>.
 */

#include "mimeobject.h"
#include "conversion/kcalconversion.h"
#include "conversion/kolabconversion.h"
#include "conversion/kabcconversion.h"
#include "conversion/commonconversion.h"
#include "kolabformat/kolabobject.h"
#include "kolabformat/xmlobject.h"

#include "kolabformat/v2helpers.h"
#include "mime/mimeutils.h"
#include "libkolab-version.h"
#include "pimkolab_debug.h"

#include <QString>
#include <KRandom>
#include <cstring>

Q_DECLARE_METATYPE(Kolab::Event)
Q_DECLARE_METATYPE(Kolab::Todo)
Q_DECLARE_METATYPE(Kolab::Journal)
Q_DECLARE_METATYPE(Kolab::Contact)
Q_DECLARE_METATYPE(Kolab::DistList)
Q_DECLARE_METATYPE(Kolab::Note)
Q_DECLARE_METATYPE(Kolab::Freebusy)
Q_DECLARE_METATYPE(Kolab::Configuration)

static inline std::string eventKolabType()
{
    return std::string(KOLAB_TYPE_EVENT);
}

static inline std::string todoKolabType()
{
    return std::string(KOLAB_TYPE_TASK);
}

static inline std::string journalKolabType()
{
    return std::string(KOLAB_TYPE_JOURNAL);
}

static inline std::string contactKolabType()
{
    return std::string(KOLAB_TYPE_CONTACT);
}

static inline std::string distlistKolabType()
{
    return std::string(KOLAB_TYPE_DISTLIST);
}

static inline std::string distlistKolabTypeCompat()
{
    return std::string(KOLAB_TYPE_DISTLIST_V2);
}

static inline std::string noteKolabType()
{
    return std::string(KOLAB_TYPE_NOTE);
}

static inline std::string configurationKolabType()
{
    return std::string(KOLAB_TYPE_CONFIGURATION);
}

static inline std::string dictKolabType()
{
    return std::string(KOLAB_TYPE_DICT);
}

static inline std::string freebusyKolabType()
{
    return std::string(KOLAB_TYPE_FREEBUSY);
}

static inline std::string relationKolabType()
{
    return std::string(KOLAB_TYPE_RELATION);
}

static inline std::string xCalMimeType()
{
    return std::string(MIME_TYPE_XCAL);
}

static inline std::string xCardMimeType()
{
    return std::string(MIME_TYPE_XCARD);
}

static inline std::string kolabMimeType()
{
    return std::string(MIME_TYPE_KOLAB);
}

static std::string getProductId(const std::string &pId)
{
    if (pId.empty()) {
        return LIBKOLAB_LIB_VERSION_STRING;
    }
    return pId + ' ' + LIBKOLAB_LIB_VERSION_STRING;
}

namespace Kolab {
static Kolab::ObjectType getObjectType(const std::string &type)
{
    if (type == eventKolabType()) {
        return EventObject;
    } else if (type == todoKolabType()) {
        return TodoObject;
    } else if (type == journalKolabType()) {
        return JournalObject;
    } else if (type == contactKolabType()) {
        return ContactObject;
    } else if (type == distlistKolabType() || type == distlistKolabTypeCompat()) {
        return DistlistObject;
    } else if (type == noteKolabType()) {
        return NoteObject;
    } else if (type == freebusyKolabType()) {
        return FreebusyObject;
    } else if (strstr(type.c_str(), KOLAB_TYPE_DICT)) { //Previous versions appended the language to the type
        return DictionaryConfigurationObject;
    } else if (type == relationKolabType()) {
        return RelationConfigurationObject;
    }
    qCWarning(PIMKOLAB_LOG) <<"Unknown object type: " << type;
    return Kolab::InvalidObject;
}

static 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;
    case RelationConfigurationObject:
        return KOLAB_TYPE_RELATION;
    default:
        qCCritical(PIMKOLAB_LOG) << "unknown type "<< type;
    }
    return QByteArray();
}

static QByteArray getMimeType(Kolab::ObjectType type)
{
    switch (type) {
    case EventObject:
    case TodoObject:
    case JournalObject:
    case FreebusyObject:
        return MIME_TYPE_XCAL;
    case ContactObject:
    case DistlistObject:
        return MIME_TYPE_XCARD;
    case NoteObject:
    case DictionaryConfigurationObject:
    case RelationConfigurationObject:
        return MIME_TYPE_KOLAB;
    default:
        qCCritical(PIMKOLAB_LOG) << "unknown type "<< type;
    }
    return QByteArray();
}

static Kolab::ObjectType detectType(const KMime::Message::Ptr &msg)
{
    Q_FOREACH (const QByteArray &type, Mime::getContentMimeTypeList(msg)) {
        Kolab::ObjectType t = getObjectType(type.toStdString()); //works for v2 types
        if (t != InvalidObject) {
            return t;
        }
    }
    return InvalidObject;
}

static void printMessageDebugInfo(const KMime::Message::Ptr &msg)
{
    //TODO replace by Debug stream for Mimemessage
    qCDebug(PIMKOLAB_LOG) << "MessageId: " << msg->messageID()->asUnicodeString();
    qCDebug(PIMKOLAB_LOG) << "Subject: " << msg->subject()->asUnicodeString();
//     Debug() << msg->encodedContent();
}

//@cond PRIVATE
class MIMEObject::Private
{
public:
    Private()
    {
    }

    QVariant readKolabV2(const KMime::Message::Ptr &msg, Kolab::ObjectType objectType);
    QVariant readKolabV3(const KMime::Message::Ptr &msg, Kolab::ObjectType objectType);
    QVariant parseMimeMessage(const KMime::Message::Ptr &msg);
    QVariant parseMimeMessage(const std::string &s);

    ObjectType mObjectType = InvalidObject;
    Version mVersion = KolabV3;
    ObjectType mOverrideObjectType = InvalidObject;
    Version mOverrideVersion = KolabV3;
    bool mDoOverrideVersion = false;
    QVariant mObject;
};
//@endcond

static std::vector<Kolab::Attachment> getAttachments(const std::vector<Kolab::Attachment> &attachments, const KMime::Message::Ptr &msg)
{
    std::vector<Kolab::Attachment> allAttachments;
    foreach (const Kolab::Attachment &attachment, attachments) {
        if (!attachment.uri().empty()) {
            const Kolab::Attachment extracted = Mime::getAttachment(attachment.uri(), msg);
            if (extracted.isValid()) {
                allAttachments.push_back(extracted);
            }
        } else {
            allAttachments.push_back(attachment);
        }
    }
    return allAttachments;
}

static std::vector<Kolab::Attachment> getAttachments(const QStringList &attachmentNames, const KMime::Message::Ptr &msg)
{
    std::vector<Kolab::Attachment> allAttachments;
    foreach (const QString &name, attachmentNames) {
        const Kolab::Attachment extracted = Mime::getAttachmentByName(name, msg);
        if (extracted.isValid()) {
            allAttachments.push_back(extracted);
        }
    }
    return allAttachments;
}

QVariant MIMEObject::Private::readKolabV2(const KMime::Message::Ptr &msg, Kolab::ObjectType objectType)
{
    if (objectType == DictionaryConfigurationObject) {
        KMime::Content *xmlContent = Mime::findContentByType(msg, "application/xml");
        if (!xmlContent) {
            qCCritical(PIMKOLAB_LOG) << "no application/xml part found";
            printMessageDebugInfo(msg);
            return InvalidObject;
        }
        const QByteArray &xmlData = xmlContent->decodedContent();
        QString dictionaryLanguage;
        const QStringList entries = Kolab::readLegacyDictionaryConfiguration(xmlData, dictionaryLanguage);
        mObjectType = objectType;
        Kolab::Dictionary dictionary(Conversion::toStdString(dictionaryLanguage));
        std::vector<std::string> convertedEntries;
        convertedEntries.reserve(entries.count());
        foreach (const QString &value, entries) {
            convertedEntries.push_back(Conversion::toStdString(value));
        }
        dictionary.setEntries(convertedEntries);
        return QVariant::fromValue(Kolab::Configuration(dictionary));
    }
    KMime::Content *xmlContent = Mime::findContentByType(msg, getTypeString(objectType));
    if (!xmlContent) {
        qCCritical(PIMKOLAB_LOG) << "no part with type" << getTypeString(objectType) << " found";
        printMessageDebugInfo(msg);
        return QVariant();
    }
    const QByteArray &xmlData = xmlContent->decodedContent();
    if (xmlData.isEmpty()) {
        qCCritical(PIMKOLAB_LOG) << "no content in message part with type" << getTypeString(objectType);
        printMessageDebugInfo(msg);
        return QVariant();
    }

    QVariant variant;
    switch (objectType) {
    case EventObject:
    {
        QStringList attachments;
        KCalendarCore::Event::Ptr kEvent = fromXML<KCalendarCore::Event::Ptr, KolabV2::Event>(xmlData, attachments);
        if (kEvent) {
            Kolab::Event event = Kolab::Conversion::fromKCalendarCore(*kEvent);
            event.setAttachments(getAttachments(attachments, msg));
            variant = QVariant::fromValue(event);
        }
        break;
    }
    case TodoObject:
    {
        QStringList attachments;
        KCalendarCore::Todo::Ptr kTodo = fromXML<KCalendarCore::Todo::Ptr, KolabV2::Task>(xmlData, attachments);
        if (kTodo) {
            Kolab::Todo todo = Kolab::Conversion::fromKCalendarCore(*kTodo);
            todo.setAttachments(getAttachments(attachments, msg));
            variant = QVariant::fromValue(todo);
        }
        break;
    }
    case JournalObject:
    {
        QStringList attachments;
        KCalendarCore::Journal::Ptr kJournal = fromXML<KCalendarCore::Journal::Ptr, KolabV2::Journal>(xmlData, attachments);
        if (kJournal) {
            Kolab::Journal journal = Kolab::Conversion::fromKCalendarCore(*kJournal);
            journal.setAttachments(getAttachments(attachments, msg));
            variant = QVariant::fromValue(journal);
        }
        break;
    }
    case ContactObject:
    {
        KContacts::Addressee kContact = addresseeFromKolab(xmlData, msg);
        Kolab::Contact contact = Kolab::Conversion::fromKABC(kContact);
        variant = QVariant::fromValue(contact);
        break;
    }
    case DistlistObject:
    {
        KContacts::ContactGroup kContactGroup = contactGroupFromKolab(xmlData);
        Kolab::DistList distlist = Kolab::Conversion::fromKABC(kContactGroup);
        variant = QVariant::fromValue(distlist);
        break;
    }
    case NoteObject:
    {
        KMime::Message::Ptr kNote = noteFromKolab(xmlData, msg->date()->dateTime());
        Kolab::Note note = Kolab::Conversion::fromNote(kNote);
        variant = QVariant::fromValue(note);
        break;
    }
    default:
        CRITICAL(QStringLiteral("no kolab object found "));
        break;
    }
    if (ErrorHandler::errorOccured()) {
        printMessageDebugInfo(msg);
        return QVariant();
    }
    mObjectType = objectType;
    return variant;
}

QVariant MIMEObject::Private::readKolabV3(const KMime::Message::Ptr &msg, Kolab::ObjectType objectType)
{
    KMime::Content *const xmlContent = Mime::findContentByType(msg, getMimeType(objectType));
    if (!xmlContent) {
        qCCritical(PIMKOLAB_LOG) << "no " << getMimeType(objectType) << " part found";
        printMessageDebugInfo(msg);
        return InvalidObject;
    }
    const QByteArray &content = xmlContent->decodedContent();
    const std::string xml = std::string(content.data(), content.size());
    QVariant variant;
    switch (objectType) {
    case EventObject:
    {
        Kolab::Event event = Kolab::readEvent(xml, false);
        event.setAttachments(getAttachments(event.attachments(), msg));
        variant = QVariant::fromValue<Kolab::Event>(event);
        break;
    }
    case TodoObject:
    {
        Kolab::Todo todo = Kolab::readTodo(xml, false);
        todo.setAttachments(getAttachments(todo.attachments(), msg));
        variant = QVariant::fromValue<Kolab::Todo>(todo);
        break;
    }
    case JournalObject:
    {
        Kolab::Journal journal = Kolab::readJournal(xml, false);
        journal.setAttachments(getAttachments(journal.attachments(), msg));
        variant = QVariant::fromValue<Kolab::Journal>(journal);
        break;
    }
    case ContactObject:
        variant = QVariant::fromValue<Kolab::Contact>(Kolab::readContact(xml, false));
        break;
    case DistlistObject:
        variant = QVariant::fromValue<Kolab::DistList>(Kolab::readDistlist(xml, false));
        break;
    case NoteObject:
        variant = QVariant::fromValue<Kolab::Note>(Kolab::readNote(xml, false));
        break;
    case FreebusyObject:
        variant = QVariant::fromValue<Kolab::Freebusy>(Kolab::readFreebusy(xml, false));
        break;
    case DictionaryConfigurationObject:
    case RelationConfigurationObject:
        variant = QVariant::fromValue<Kolab::Configuration>(Kolab::readConfiguration(xml, false));
        break;
    default:
        qCCritical(PIMKOLAB_LOG) << "no kolab object found ";
        printMessageDebugInfo(msg);
        break;
    }

    if (ErrorHandler::errorOccured()) {
        printMessageDebugInfo(msg);
        return QVariant();
    }
    mObjectType = objectType;
    return variant;
}

QVariant MIMEObject::Private::parseMimeMessage(const KMime::Message::Ptr &msg)
{
    ErrorHandler::clearErrors();
    mObjectType = InvalidObject;
    if (msg->contents().isEmpty()) {
        qCCritical(PIMKOLAB_LOG) << "message has no contents (we likely failed to parse it correctly)";
        printMessageDebugInfo(msg);
        return QVariant();
    }
    Kolab::ObjectType objectType = InvalidObject;
    if (mOverrideObjectType == InvalidObject) {
        if (KMime::Headers::Base *xKolabHeader = msg->headerByType(X_KOLAB_TYPE_HEADER)) {
            objectType = getObjectType(xKolabHeader->asUnicodeString().trimmed().toStdString());
        } else {
            qCWarning(PIMKOLAB_LOG) <<"could not find the X-Kolab-Type Header, trying autodetection";
            //This works only for v2 messages atm.
            objectType = detectType(msg);
        }
    } else {
        objectType = mOverrideObjectType;
    }
    if (objectType == InvalidObject) {
        qCCritical(PIMKOLAB_LOG) << "unable to detect object type";
        printMessageDebugInfo(msg);
        return QVariant();
    }

    if (!mDoOverrideVersion) {
        KMime::Headers::Base *xKolabVersion = msg->headerByType(X_KOLAB_MIME_VERSION_HEADER);
        if (!xKolabVersion) {
            //For backwards compatibility to development versions, can be removed in future versions
            xKolabVersion = msg->headerByType(X_KOLAB_MIME_VERSION_HEADER_COMPAT);
        }
        if (!xKolabVersion || xKolabVersion->asUnicodeString() == KOLAB_VERSION_V2) {
            mVersion = KolabV2;
        } else {
            if (xKolabVersion->asUnicodeString() != KOLAB_VERSION_V3) { //TODO version compatibility check?
                qCWarning(PIMKOLAB_LOG) <<"Kolab Version Header available but not on the same version as the implementation: " << xKolabVersion->asUnicodeString();
            }
            mVersion = KolabV3;
        }
    } else {
        mVersion = mOverrideVersion;
    }

    if (mVersion == KolabV2) {
        return readKolabV2(msg, objectType);
    }
    return readKolabV3(msg, objectType);
}

QVariant MIMEObject::Private::parseMimeMessage(const std::string &s)
{
    KMime::Message::Ptr msg(new KMime::Message);
    msg->setContent(QByteArray(s.c_str()));
    msg->parse();
    return parseMimeMessage(msg);
}

MIMEObject::MIMEObject()
    : d(new MIMEObject::Private)
{
}

MIMEObject::~MIMEObject()
{
    delete d;
}

void MIMEObject::setObjectType(ObjectType type)
{
    d->mOverrideObjectType = type;
}

void MIMEObject::setVersion(Version version)
{
    d->mOverrideVersion = version;
    d->mDoOverrideVersion = true;
}

static std::string createCid()
{
    return QStringLiteral("cid:%1@%2").arg(KRandom::randomString(16), QStringLiteral("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());
        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, std::vector<std::string> &attachmentCids)
{
    T removedAttachments = incidence;
    removedAttachments.setAttachments(convertToReferences(incidence.attachments(), attachmentCids));
    return removedAttachments;
}

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) {
        const std::string data = attachment.data();
        const std::string cid = attachmentCids.empty() ? attachment.uri() : attachmentCids.at(index);
        msg->addContent(Mime::createAttachmentPart(Mime::fromCid(QString::fromStdString(cid.c_str())).toLatin1(), QByteArray(attachment.mimetype().c_str()), QString::fromStdString(attachment.label()), QByteArray(data.c_str(), data.size())));
        index++;
    }
}

ObjectType MIMEObject::parseMessage(const std::string &msg)
{
    d->mObject = d->parseMimeMessage(msg);
    return d->mObjectType;
}

ObjectType MIMEObject::getType() const
{
    return d->mObjectType;
}

Version MIMEObject::getVersion() const
{
    return d->mVersion;
}

Kolab::Event MIMEObject::getEvent() const
{
    return d->mObject.value<Kolab::Event>();
}

Kolab::Todo MIMEObject::getTodo() const
{
    return d->mObject.value<Kolab::Todo>();
}

Kolab::Journal MIMEObject::getJournal() const
{
    return d->mObject.value<Kolab::Journal>();
}

Kolab::Note MIMEObject::getNote() const
{
    return d->mObject.value<Kolab::Note>();
}

Kolab::Contact MIMEObject::getContact() const
{
    return d->mObject.value<Kolab::Contact>();
}

Kolab::DistList MIMEObject::getDistlist() const
{
    return d->mObject.value<Kolab::DistList>();
}

Kolab::Freebusy MIMEObject::getFreebusy() const
{
    return d->mObject.value<Kolab::Freebusy>();
}

Kolab::Configuration MIMEObject::getConfiguration() const
{
    return d->mObject.value<Kolab::Configuration>();
}

std::string MIMEObject::writeEvent(const Event &event, Version version, const std::string &pId)
{
    ErrorHandler::clearErrors();
    const std::string productId = getProductId(pId);

    KMime::Message::Ptr msg;
    Kolab::XMLObject xmlObject;
    std::vector<std::string> attachmentCids;
    if (version == KolabV3) {
        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(), attachmentCids);
    msg->assemble();
    return msg->encodedContent().data();
}

Event MIMEObject::readEvent(const std::string &s)
{
    return d->parseMimeMessage(s).value<Kolab::Event>();
}

std::string MIMEObject::writeTodo(const Todo &todo, Version version, const std::string &pId)
{
    ErrorHandler::clearErrors();
    const std::string productId = getProductId(pId);

    KMime::Message::Ptr msg;
    Kolab::XMLObject xmlObject;
    std::vector<std::string> attachmentCids;
    if (version == KolabV3) {
        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(), attachmentCids);
    msg->assemble();
    return msg->encodedContent().data();
}

Todo MIMEObject::readTodo(const std::string &s)
{
    return d->parseMimeMessage(s).value<Kolab::Todo>();
}

std::string MIMEObject::writeJournal(const Journal &journal, Version version, const std::string &pId)
{
    ErrorHandler::clearErrors();
    const std::string productId = getProductId(pId);

    KMime::Message::Ptr msg;
    Kolab::XMLObject xmlObject;
    std::vector<std::string> attachmentCids;
    if (version == KolabV3) {
        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(), attachmentCids);
    msg->assemble();
    return msg->encodedContent().data();
}

Journal MIMEObject::readJournal(const std::string &s)
{
    return d->parseMimeMessage(s).value<Kolab::Journal>();
}

std::string MIMEObject::writeNote(const Note &note, Version version, const std::string &pId)
{
    ErrorHandler::clearErrors();
    const std::string productId = getProductId(pId);

    KMime::Message::Ptr msg;
    Kolab::XMLObject xmlObject;
    std::vector<std::string> attachmentCids;
    if (version == KolabV3) {
        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(), attachmentCids);
    msg->assemble();
    return msg->encodedContent().data();
}

Note MIMEObject::readNote(const std::string &s)
{
    return d->parseMimeMessage(s).value<Kolab::Note>();
}

std::string MIMEObject::writeContact(const Contact &contact, Version version, const std::string &pId)
{
    ErrorHandler::clearErrors();
    const std::string productId = getProductId(pId);

    KMime::Message::Ptr msg;
    Kolab::XMLObject xmlObject;
    const std::string xml = xmlObject.writeContact(contact, version, productId);

    Email preferredEmail = !contact.emailAddresses().empty() ? contact.emailAddresses().at(contact.emailAddressPreferredIndex()) : Email();
    QPair<std::string, std::string> pair = Conversion::fromMailto(preferredEmail.address());
    std::string name = pair.second;
    std::string email = pair.first;
    if (name.empty()) {
        name = contact.name();
    }

    if (version == KolabV3) {
        msg = Mime::createMessage(xCardMimeType(), contactKolabType(), xml, true, productId, email, name, contact.uid());
    } else if (version == KolabV2) {
        msg = Mime::createMessage(contactKolabType(), contactKolabType(), xml, false, productId, email, name, contact.uid());
    }
    msg->assemble();
    return msg->encodedContent().data();
}

Contact MIMEObject::readContact(const std::string &s)
{
    return d->parseMimeMessage(s).value<Kolab::Contact>();
}

std::string MIMEObject::writeDistlist(const DistList &distlist, Version version, const std::string &pId)
{
    ErrorHandler::clearErrors();
    const std::string productId = getProductId(pId);

    KMime::Message::Ptr msg;
    Kolab::XMLObject xmlObject;
    const std::string xml = xmlObject.writeDistlist(distlist, version, productId);
    if (version == KolabV3) {
        msg = Mime::createMessage(xCardMimeType(), distlistKolabType(), xml, true, productId, std::string(), std::string(), distlist.uid());
    } else if (version == KolabV2) {
        msg = Mime::createMessage(distlistKolabType(), distlistKolabType(), xml, false, productId, std::string(), std::string(), distlist.uid());
    }
    msg->assemble();
    return msg->encodedContent().data();
}

DistList MIMEObject::readDistlist(const std::string &s)
{
    return d->parseMimeMessage(s).value<Kolab::DistList>();
}

std::string MIMEObject::writeConfiguration(const Configuration &configuration, Version version, const std::string &pId)
{
    ErrorHandler::clearErrors();
    const std::string productId = getProductId(pId);

    KMime::Message::Ptr msg;
    Kolab::XMLObject xmlObject;
    const std::string xml = xmlObject.writeConfiguration(configuration, version, productId);
    std::string kolabType;
    switch (configuration.type()) {
    case Kolab::Configuration::TypeDictionary:
        kolabType = dictKolabType();
        break;
    case Kolab::Configuration::TypeRelation:
        kolabType = relationKolabType();
        break;
    case Kolab::Configuration::TypeSnippet:
        kolabType = configurationKolabType();
        break;
    case Kolab::Configuration::TypeFileDriver:
        kolabType = configurationKolabType();
        break;
    case Kolab::Configuration::TypeCategoryColor:
        kolabType = configurationKolabType();
        break;
    default:
        break;
    }
    if (version == KolabV3) {
        msg = Mime::createMessage(kolabMimeType(), kolabType, xml, true, productId, std::string(), std::string(), configuration.uid());
    } else if (version == KolabV2) {
        qCCritical(PIMKOLAB_LOG) << "Not available in KolabV2";
    }
    msg->assemble();
    return msg->encodedContent().data();
}

Configuration MIMEObject::readConfiguration(const std::string &s)
{
    return d->parseMimeMessage(s).value<Kolab::Configuration>();
}

std::string MIMEObject::writeFreebusy(const Freebusy &freebusy, Version version, const std::string &pId)
{
    ErrorHandler::clearErrors();
    const std::string productId = getProductId(pId);

    KMime::Message::Ptr msg;
    Kolab::XMLObject xmlObject;
    const std::string xml = xmlObject.writeFreebusy(freebusy, version, productId);
    if (version == KolabV3) {
        msg = Mime::createMessage(xCalMimeType(), freebusyKolabType(), xml, true, productId, std::string(), std::string(), freebusy.uid());
    } else if (version == KolabV2) {
        qCCritical(PIMKOLAB_LOG) << "Not available in KolabV2";
    }
    msg->assemble();
    return msg->encodedContent().data();
}

Freebusy MIMEObject::readFreebusy(const std::string &s)
{
    return d->parseMimeMessage(s).value<Kolab::Freebusy>();
}
}