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

0001 /*
0002    SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "createtodojob.h"
0008 #include "createtodoplugin_debug.h"
0009 #include <Akonadi/ItemCreateJob>
0010 #include <Akonadi/ItemFetchJob>
0011 #include <Akonadi/ItemFetchScope>
0012 #include <Akonadi/MessageParts>
0013 #include <Akonadi/RelationCreateJob>
0014 
0015 #include <KMime/Message>
0016 
0017 using namespace MessageViewer;
0018 
0019 CreateTodoJob::CreateTodoJob(const KCalendarCore::Todo::Ptr &todoPtr, const Akonadi::Collection &collection, const Akonadi::Item &item, QObject *parent)
0020     : KJob(parent)
0021     , mItem(item)
0022     , mCollection(collection)
0023     , mTodoPtr(todoPtr)
0024 {
0025 }
0026 
0027 CreateTodoJob::~CreateTodoJob()
0028 {
0029     qCDebug(CREATETODOPLUGIN_LOG) << " CreateTodoJob::~CreateTodoJob()";
0030 }
0031 
0032 void CreateTodoJob::start()
0033 {
0034     // We need the full payload to attach the mail to the incidence
0035     if (!mItem.loadedPayloadParts().contains(Akonadi::MessagePart::Body)) {
0036         auto job = new Akonadi::ItemFetchJob(mItem);
0037         job->fetchScope().fetchFullPayload();
0038         connect(job, &Akonadi::ItemFetchJob::result, this, &CreateTodoJob::slotFetchDone);
0039     } else {
0040         createTodo();
0041     }
0042 }
0043 
0044 void CreateTodoJob::slotFetchDone(KJob *job)
0045 {
0046     auto fetchJob = qobject_cast<Akonadi::ItemFetchJob *>(job);
0047     if (fetchJob->items().count() == 1) {
0048         mItem = fetchJob->items().at(0);
0049     } else {
0050         qCDebug(CREATETODOPLUGIN_LOG) << " createTodo Error during fetch: " << job->errorString();
0051         emitResult();
0052         return;
0053     }
0054     createTodo();
0055 }
0056 
0057 void CreateTodoJob::createTodo()
0058 {
0059     if (!mItem.hasPayload<KMime::Message::Ptr>()) {
0060         qCDebug(CREATETODOPLUGIN_LOG) << " item has not payload";
0061         emitResult();
0062         return;
0063     }
0064 
0065     Akonadi::Item newTodoItem;
0066     newTodoItem.setMimeType(KCalendarCore::Todo::todoMimeType());
0067     newTodoItem.setPayload<KCalendarCore::Todo::Ptr>(mTodoPtr);
0068 
0069     auto createJob = new Akonadi::ItemCreateJob(newTodoItem, mCollection);
0070     connect(createJob, &Akonadi::ItemCreateJob::result, this, &CreateTodoJob::todoCreated);
0071 }
0072 
0073 void CreateTodoJob::todoCreated(KJob *job)
0074 {
0075     if (job->error()) {
0076         qCDebug(CREATETODOPLUGIN_LOG) << "Error during create new Todo " << job->errorString();
0077         setError(job->error());
0078         setErrorText(job->errorText());
0079         emitResult();
0080     } else {
0081         auto createJob = static_cast<Akonadi::ItemCreateJob *>(job);
0082         Akonadi::Relation relation(Akonadi::Relation::GENERIC, mItem, createJob->item());
0083         auto rJob = new Akonadi::RelationCreateJob(relation);
0084         connect(rJob, &Akonadi::RelationCreateJob::result, this, &CreateTodoJob::relationCreated);
0085     }
0086 }
0087 
0088 void CreateTodoJob::relationCreated(KJob *job)
0089 {
0090     if (job->error()) {
0091         qCDebug(CREATETODOPLUGIN_LOG) << "Error during create new Todo " << job->errorString();
0092     }
0093     emitResult();
0094 }
0095 
0096 #include "moc_createtodojob.cpp"