File indexing completed on 2025-01-05 04:49:45
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 "createeventjob.h" 0008 #include "createeventplugin_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 CreateEventJob::CreateEventJob(const KCalendarCore::Event::Ptr &eventPtr, const Akonadi::Collection &collection, const Akonadi::Item &item, QObject *parent) 0020 : KJob(parent) 0021 , mItem(item) 0022 , mCollection(collection) 0023 , mEventPtr(eventPtr) 0024 { 0025 } 0026 0027 CreateEventJob::~CreateEventJob() = default; 0028 0029 void CreateEventJob::start() 0030 { 0031 // We need the full payload to attach the mail to the incidence 0032 if (!mItem.loadedPayloadParts().contains(Akonadi::MessagePart::Body)) { 0033 auto job = new Akonadi::ItemFetchJob(mItem); 0034 job->fetchScope().fetchFullPayload(); 0035 connect(job, &Akonadi::ItemFetchJob::result, this, &CreateEventJob::slotFetchDone); 0036 } else { 0037 createEvent(); 0038 } 0039 } 0040 0041 void CreateEventJob::slotFetchDone(KJob *job) 0042 { 0043 auto fetchJob = qobject_cast<Akonadi::ItemFetchJob *>(job); 0044 if (fetchJob->items().count() == 1) { 0045 mItem = fetchJob->items().at(0); 0046 } else { 0047 emitResult(); 0048 return; 0049 } 0050 createEvent(); 0051 } 0052 0053 void CreateEventJob::createEvent() 0054 { 0055 if (!mItem.hasPayload<KMime::Message::Ptr>()) { 0056 qCDebug(CREATEEVENTPLUGIN_LOG) << " item has not payload"; 0057 emitResult(); 0058 return; 0059 } 0060 0061 Akonadi::Item newEventItem; 0062 newEventItem.setMimeType(KCalendarCore::Event::eventMimeType()); 0063 newEventItem.setPayload<KCalendarCore::Event::Ptr>(mEventPtr); 0064 0065 auto createJob = new Akonadi::ItemCreateJob(newEventItem, mCollection); 0066 connect(createJob, &Akonadi::ItemCreateJob::result, this, &CreateEventJob::slotEventCreated); 0067 } 0068 0069 void CreateEventJob::slotEventCreated(KJob *job) 0070 { 0071 if (job->error()) { 0072 qCDebug(CREATEEVENTPLUGIN_LOG) << "Error during create new Event " << job->errorString(); 0073 setError(job->error()); 0074 setErrorText(job->errorText()); 0075 emitResult(); 0076 } else { 0077 auto createJob = static_cast<Akonadi::ItemCreateJob *>(job); 0078 Akonadi::Relation relation(Akonadi::Relation::GENERIC, mItem, createJob->item()); 0079 auto rJob = new Akonadi::RelationCreateJob(relation); 0080 connect(rJob, &Akonadi::RelationCreateJob::result, this, &CreateEventJob::slotRelationCreated); 0081 } 0082 } 0083 0084 void CreateEventJob::slotRelationCreated(KJob *job) 0085 { 0086 if (job->error()) { 0087 qCDebug(CREATEEVENTPLUGIN_LOG) << "Error during create new Event " << job->errorString(); 0088 } 0089 emitResult(); 0090 } 0091 0092 #include "moc_createeventjob.cpp"