File indexing completed on 2024-10-27 04:51:06

0001 /*
0002    SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "composenewmessagejob.h"
0008 #include "kmkernel.h"
0009 #include <MessageComposer/Composer>
0010 
0011 #include <KMime/Message>
0012 #include <MessageComposer/MessageHelper>
0013 #include <TemplateParser/TemplateParserJob>
0014 
0015 ComposeNewMessageJob::ComposeNewMessageJob(QObject *parent)
0016     : QObject(parent)
0017 {
0018 }
0019 
0020 ComposeNewMessageJob::~ComposeNewMessageJob() = default;
0021 
0022 void ComposeNewMessageJob::setCurrentCollection(const Akonadi::Collection &col)
0023 {
0024     mCurrentCollection = col;
0025 }
0026 
0027 static void copyAddresses(const KMime::Headers::Generics::AddressList *from, KMime::Headers::Generics::AddressList *to)
0028 {
0029     if (!from) { // no such headers to copy from
0030         return;
0031     }
0032 
0033     const KMime::Types::Mailbox::List mailboxes = from->mailboxes();
0034     for (const KMime::Types::Mailbox &mbox : mailboxes) {
0035         to->addAddress(mbox);
0036     }
0037 }
0038 
0039 void ComposeNewMessageJob::start()
0040 {
0041     mMsg = KMime::Message::Ptr(new KMime::Message());
0042 
0043     mIdentity = mFolder ? mFolder->identity() : 0;
0044     MessageHelper::initHeader(mMsg, KMKernel::self()->identityManager(), mIdentity);
0045     auto parser = new TemplateParser::TemplateParserJob(mMsg, TemplateParser::TemplateParserJob::NewMessage, this);
0046     connect(parser, &TemplateParser::TemplateParserJob::parsingDone, this, &ComposeNewMessageJob::slotOpenComposer);
0047     parser->setIdentityManager(KMKernel::self()->identityManager());
0048     if (mFolder) {
0049         parser->process(mMsg, mCurrentCollection.id());
0050     } else {
0051         parser->process(KMime::Message::Ptr());
0052     }
0053 
0054     if (mRecipientsFrom.isValid()) {
0055         // Copy the recipient list from the original message
0056         const KMime::Message::Ptr msg = MessageComposer::Util::message(mRecipientsFrom);
0057         if (msg) {
0058             copyAddresses(msg->to(false), mMsg->to());
0059             copyAddresses(msg->cc(false), mMsg->cc());
0060             copyAddresses(msg->bcc(false), mMsg->bcc());
0061             copyAddresses(msg->replyTo(false), mMsg->replyTo());
0062         } else {
0063             qCWarning(KMAIL_LOG) << "Original message" << mRecipientsFrom.id() << "not found";
0064         }
0065     }
0066 }
0067 
0068 void ComposeNewMessageJob::slotOpenComposer(bool forceCursorPosition)
0069 {
0070     KMail::Composer *win = KMail::makeComposer(mMsg, false, false, KMail::Composer::New, mIdentity);
0071     win->setCollectionForNewMessage(mCurrentCollection);
0072 
0073     if (forceCursorPosition) {
0074         win->setFocusToEditor();
0075     }
0076     win->show();
0077     deleteLater();
0078 }
0079 
0080 void ComposeNewMessageJob::setFolderSettings(const QSharedPointer<MailCommon::FolderSettings> &folder)
0081 {
0082     mFolder = folder;
0083 }
0084 
0085 void ComposeNewMessageJob::setRecipientsFromMessage(const Akonadi::Item &from)
0086 {
0087     mRecipientsFrom = from;
0088 }
0089 
0090 #include "moc_composenewmessagejob.cpp"