File indexing completed on 2025-01-05 04:49:46

0001 /*
0002   SPDX-FileCopyrightText: 2014 Sandro Knauß <knauss@kolabsys.com>
0003 
0004   SPDX-License-Identifier: GPL-2.0-only
0005 */
0006 
0007 #include "createnotejob.h"
0008 #include "createnoteplugin_debug.h"
0009 #include <Akonadi/ItemCreateJob>
0010 #include <Akonadi/ItemModifyJob>
0011 #include <Akonadi/MessageParts>
0012 #include <Akonadi/Relation>
0013 #include <Akonadi/RelationCreateJob>
0014 
0015 #include <KMime/Message>
0016 #include <QCoreApplication>
0017 
0018 using namespace MessageViewer;
0019 
0020 CreateNoteJob::CreateNoteJob(const KMime::Message::Ptr &notePtr, const Akonadi::Collection &collection, const Akonadi::Item &item, QObject *parent)
0021     : KJob(parent)
0022     , mItem(item)
0023     , mCollection(collection)
0024     , mNote(notePtr)
0025 {
0026 }
0027 
0028 CreateNoteJob::~CreateNoteJob() = default;
0029 
0030 void CreateNoteJob::start()
0031 {
0032     mNote.setFrom(QCoreApplication::applicationName() + QLatin1Char(' ') + QCoreApplication::applicationVersion());
0033     mNote.setLastModifiedDate(QDateTime::currentDateTimeUtc());
0034     if (!mItem.relations().isEmpty()) {
0035         Akonadi::Relation relation;
0036         const auto relations = mItem.relations();
0037         for (const Akonadi::Relation &r : relations) {
0038             // assuming that GENERIC relations to emails are notes is a pretty horrific hack imo - aseigo
0039             if (r.type() == Akonadi::Relation::GENERIC && r.right().mimeType() == Akonadi::NoteUtils::noteMimeType()) {
0040                 relation = r;
0041                 break;
0042             }
0043         }
0044 
0045         if (relation.isValid()) {
0046             Akonadi::Item item = relation.right();
0047             item.setMimeType(Akonadi::NoteUtils::noteMimeType());
0048             item.setPayload(mNote.message());
0049             auto modifyJob = new Akonadi::ItemModifyJob(item);
0050             connect(modifyJob, &Akonadi::ItemModifyJob::result, this, &CreateNoteJob::noteUpdated);
0051             return;
0052         }
0053     }
0054 
0055     Akonadi::Item newNoteItem;
0056     newNoteItem.setMimeType(Akonadi::NoteUtils::noteMimeType());
0057     newNoteItem.setPayload(mNote.message());
0058     auto createJob = new Akonadi::ItemCreateJob(newNoteItem, mCollection);
0059     connect(createJob, &Akonadi::ItemCreateJob::result, this, &CreateNoteJob::noteCreated);
0060 }
0061 
0062 void CreateNoteJob::noteCreated(KJob *job)
0063 {
0064     if (job->error()) {
0065         qCWarning(CREATENOTEPLUGIN_LOG) << "Error during create new Note " << job->errorString();
0066         setError(job->error());
0067         setErrorText(job->errorText());
0068         emitResult();
0069     } else {
0070         auto createJob = static_cast<Akonadi::ItemCreateJob *>(job);
0071         Akonadi::Relation relation(Akonadi::Relation::GENERIC, mItem, createJob->item());
0072         auto rJob = new Akonadi::RelationCreateJob(relation);
0073         connect(rJob, &Akonadi::RelationCreateJob::result, this, &CreateNoteJob::relationCreated);
0074     }
0075 }
0076 
0077 void CreateNoteJob::noteUpdated(KJob *job)
0078 {
0079     if (job->error()) {
0080         setError(job->error());
0081         setErrorText(job->errorText());
0082     }
0083 
0084     emitResult();
0085 }
0086 
0087 void CreateNoteJob::relationCreated(KJob *job)
0088 {
0089     if (job->error()) {
0090         qCDebug(CREATENOTEPLUGIN_LOG) << "Error during create new Note " << job->errorString();
0091     }
0092     emitResult();
0093 }
0094 
0095 #include "moc_createnotejob.cpp"