File indexing completed on 2024-11-24 04:44:12

0001 /*
0002  * SPDX-FileCopyrightText: 2012 Christian Mollekopf <mollekopf@kolabsys.com>
0003  *
0004  * SPDX-License-Identifier: LGPL-3.0-or-later
0005  */
0006 
0007 #include "kolabconversion.h"
0008 #include "commonconversion.h"
0009 
0010 #include <Akonadi/NoteUtils>
0011 
0012 #include <QTimeZone>
0013 
0014 namespace Kolab
0015 {
0016 namespace Conversion
0017 {
0018 Note fromNote(const KMime::Message::Ptr &m)
0019 {
0020     Akonadi::NoteUtils::NoteMessageWrapper note(m);
0021     Note n;
0022     n.setSummary(toStdString(note.title()));
0023     n.setDescription(toStdString(note.text()));
0024     QDateTime created = QDateTime(note.creationDate());
0025     created.setTimeZone(QTimeZone::utc());
0026     n.setCreated(fromDate(created, false));
0027 
0028     n.setUid(toStdString(note.uid()));
0029     QDateTime lastModified = note.lastModifiedDate();
0030     lastModified.setTimeZone(QTimeZone::utc());
0031     n.setLastModified(fromDate(lastModified, false));
0032 
0033     switch (note.classification()) {
0034     case Akonadi::NoteUtils::NoteMessageWrapper::Confidential:
0035         n.setClassification(Kolab::ClassConfidential);
0036         break;
0037     case Akonadi::NoteUtils::NoteMessageWrapper::Private:
0038         n.setClassification(Kolab::ClassPrivate);
0039         break;
0040     default:
0041         n.setClassification(Kolab::ClassPublic);
0042     }
0043 
0044     std::vector<Kolab::CustomProperty> customs;
0045     QMap<QString, QString> &customsMap = note.custom();
0046     for (QMap<QString, QString>::const_iterator it = customsMap.constBegin(), end = customsMap.constEnd(); it != end; ++it) {
0047         customs.emplace_back(toStdString(it.key()), toStdString(it.value()));
0048     }
0049     n.setCustomProperties(customs);
0050 
0051     std::vector<Kolab::Attachment> attachments;
0052     attachments.reserve(note.attachments().count());
0053     const auto noteAttachments = note.attachments();
0054     for (const Akonadi::NoteUtils::Attachment &a : noteAttachments) {
0055         Kolab::Attachment attachment;
0056         if (a.url().isValid()) {
0057             attachment.setUri(toStdString(a.url().toString()), toStdString(a.mimetype()));
0058         } else {
0059             attachment.setData(toStdString(QString::fromUtf8(a.data())), toStdString(a.mimetype()));
0060         }
0061         attachment.setLabel(toStdString(a.label()));
0062         attachments.push_back(attachment);
0063     }
0064     n.setAttachments(attachments);
0065 
0066     return n;
0067 }
0068 
0069 KMime::Message::Ptr toNote(const Note &n)
0070 {
0071     Akonadi::NoteUtils::NoteMessageWrapper note;
0072     note.setTitle(fromStdString(n.summary()));
0073     note.setText(fromStdString(n.description()));
0074     note.setFrom(QStringLiteral("kolab@kde4"));
0075     note.setCreationDate(toDate(n.created()));
0076     note.setUid(fromStdString(n.uid()));
0077     note.setLastModifiedDate(toDate(n.lastModified()));
0078     switch (n.classification()) {
0079     case Kolab::ClassPrivate:
0080         note.setClassification(Akonadi::NoteUtils::NoteMessageWrapper::Private);
0081         break;
0082     case Kolab::ClassConfidential:
0083         note.setClassification(Akonadi::NoteUtils::NoteMessageWrapper::Confidential);
0084         break;
0085     default:
0086         note.setClassification(Akonadi::NoteUtils::NoteMessageWrapper::Public);
0087     }
0088 
0089     const auto attachments{n.attachments()};
0090     for (const Kolab::Attachment &a : attachments) {
0091         if (!a.uri().empty()) {
0092             Akonadi::NoteUtils::Attachment attachment(QUrl(fromStdString(a.uri())), fromStdString(a.mimetype()));
0093             attachment.setLabel(fromStdString(a.label()));
0094             note.attachments().append(attachment);
0095         } else {
0096             Akonadi::NoteUtils::Attachment attachment(fromStdString(a.data()).toLatin1(), fromStdString(a.mimetype()));
0097             attachment.setLabel(fromStdString(a.label()));
0098             note.attachments().append(attachment);
0099         }
0100     }
0101     const auto customProperties = n.customProperties();
0102     for (const Kolab::CustomProperty &a : customProperties) {
0103         note.custom().insert(fromStdString(a.identifier), fromStdString(a.value));
0104     }
0105     return note.message();
0106 }
0107 }
0108 }