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

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 "newmessagejob.h"
0008 #include "kmkernel.h"
0009 #include <MessageComposer/Composer>
0010 
0011 #include <KMime/Message>
0012 #include <MessageComposer/MessageHelper>
0013 #include <TemplateParser/TemplateParserJob>
0014 
0015 NewMessageJob::NewMessageJob(QObject *parent)
0016     : QObject(parent)
0017 {
0018 }
0019 
0020 NewMessageJob::~NewMessageJob() = default;
0021 
0022 void NewMessageJob::start()
0023 {
0024     mAttachURL = QUrl::fromLocalFile(mNewMessageJobSettings.mAttachURL);
0025     mMsg = KMime::Message::Ptr(new KMime::Message);
0026     MessageHelper::initHeader(mMsg, KMKernel::self()->identityManager(), mNewMessageJobSettings.mIdentity);
0027     mMsg->contentType()->setCharset("utf-8");
0028     // set basic headers
0029     if (!mNewMessageJobSettings.mCc.isEmpty()) {
0030         mMsg->cc()->fromUnicodeString(mNewMessageJobSettings.mCc, "utf-8");
0031     }
0032     if (!mNewMessageJobSettings.mBcc.isEmpty()) {
0033         mMsg->bcc()->fromUnicodeString(mNewMessageJobSettings.mBcc, "utf-8");
0034     }
0035     if (!mNewMessageJobSettings.mTo.isEmpty()) {
0036         mMsg->to()->fromUnicodeString(mNewMessageJobSettings.mTo, "utf-8");
0037     }
0038 
0039     mMsg->assemble();
0040 
0041     mCollection = mNewMessageJobSettings.mCurrentCollection;
0042     auto parser = new TemplateParser::TemplateParserJob(mMsg, TemplateParser::TemplateParserJob::NewMessage, this);
0043     connect(parser, &TemplateParser::TemplateParserJob::parsingDone, this, &NewMessageJob::slotOpenComposer);
0044     parser->setIdentityManager(KMKernel::self()->identityManager());
0045     parser->process(mMsg, mCollection.id());
0046 }
0047 
0048 void NewMessageJob::slotOpenComposer()
0049 {
0050     KMail::Composer *win = makeComposer(mMsg, false, false, KMail::Composer::New, mNewMessageJobSettings.mIdentity);
0051 
0052     win->setCollectionForNewMessage(mCollection);
0053     // Add the attachment if we have one
0054 
0055     if (!mAttachURL.isEmpty() && mAttachURL.isValid()) {
0056         QList<KMail::Composer::AttachmentInfo> infoList;
0057         KMail::Composer::AttachmentInfo info;
0058         info.url = mAttachURL;
0059         infoList.append(std::move(info));
0060         win->addAttachment(infoList, false);
0061     }
0062 
0063     // only show window when required
0064     if (!mNewMessageJobSettings.mHidden) {
0065         win->show();
0066     }
0067     deleteLater();
0068 }
0069 
0070 void NewMessageJob::setNewMessageJobSettings(const NewMessageJobSettings &newMessageJobSettings)
0071 {
0072     mNewMessageJobSettings = newMessageJobSettings;
0073 }
0074 
0075 #include "moc_newmessagejob.cpp"