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

0001 // SPDX-FileCopyrightText: 2023 g10 Code GmbH
0002 // SPDX-FileContributor: Carl Schwan <carl.schwan@gnupg.com>
0003 // SPDX-License-Identifier: GPL-2.0-or-later
0004 
0005 #include "messageloader.h"
0006 
0007 #include <Akonadi/ItemFetchJob>
0008 #include <Akonadi/ItemFetchScope>
0009 
0010 MessageLoader::MessageLoader(QObject *parent)
0011     : QObject(parent)
0012 {
0013 }
0014 
0015 Akonadi::Item MessageLoader::item() const
0016 {
0017     return m_item;
0018 }
0019 
0020 void MessageLoader::setItem(const Akonadi::Item &item)
0021 {
0022     if (m_item == item) {
0023         return;
0024     }
0025 
0026     m_item = item;
0027 
0028     auto job = new Akonadi::ItemFetchJob(item);
0029     job->fetchScope().fetchFullPayload();
0030     connect(job, &Akonadi::ItemFetchJob::result, this, [this](KJob *job) {
0031         auto fetchJob = qobject_cast<Akonadi::ItemFetchJob *>(job);
0032         const auto items = fetchJob->items();
0033         if (items.isEmpty()) {
0034             qWarning() << "Empty fetch job result";
0035             return;
0036         }
0037         const auto item = items.at(0);
0038         if (item.hasPayload<KMime::Message::Ptr>()) {
0039             m_message = item.payload<KMime::Message::Ptr>();
0040             Q_EMIT messageChanged();
0041         } else {
0042             qWarning() << "This is not a mime item.";
0043         }
0044     });
0045 }
0046 
0047 KMime::Message::Ptr MessageLoader::message() const
0048 {
0049     return m_message;
0050 }
0051 
0052 #include "moc_messageloader.cpp"