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 "opencomposerhiddenjob.h"
0008 #include "kmkernel.h"
0009 #include <MessageComposer/Composer>
0010 
0011 #include <MessageComposer/MessageHelper>
0012 
0013 #include <TemplateParser/TemplateParserJob>
0014 
0015 OpenComposerHiddenJob::OpenComposerHiddenJob(QObject *parent)
0016     : QObject(parent)
0017 {
0018 }
0019 
0020 OpenComposerHiddenJob::~OpenComposerHiddenJob() = default;
0021 
0022 void OpenComposerHiddenJob::start()
0023 {
0024     mMsg = KMime::Message::Ptr(new KMime::Message);
0025     MessageHelper::initHeader(mMsg, KMKernel::self()->identityManager());
0026     mMsg->contentType()->setCharset("utf-8");
0027     if (!mSettings.mCc.isEmpty()) {
0028         mMsg->cc()->fromUnicodeString(mSettings.mCc, "utf-8");
0029     }
0030     if (!mSettings.mBcc.isEmpty()) {
0031         mMsg->bcc()->fromUnicodeString(mSettings.mBcc, "utf-8");
0032     }
0033     if (!mSettings.mSubject.isEmpty()) {
0034         mMsg->subject()->fromUnicodeString(mSettings.mSubject, "utf-8");
0035     }
0036     if (!mSettings.mTo.isEmpty()) {
0037         mMsg->to()->fromUnicodeString(mSettings.mTo, "utf-8");
0038     }
0039     if (!mSettings.mBody.isEmpty()) {
0040         mMsg->setBody(mSettings.mBody.toUtf8());
0041         slotOpenComposer();
0042     } else {
0043         auto parser = new TemplateParser::TemplateParserJob(mMsg, TemplateParser::TemplateParserJob::NewMessage, this);
0044         connect(parser, &TemplateParser::TemplateParserJob::parsingDone, this, &OpenComposerHiddenJob::slotOpenComposer);
0045         parser->setIdentityManager(KMKernel::self()->identityManager());
0046         parser->process(KMime::Message::Ptr());
0047     }
0048 }
0049 
0050 void OpenComposerHiddenJob::slotOpenComposer()
0051 {
0052     mMsg->assemble();
0053     const KMail::Composer::TemplateContext context = mSettings.mBody.isEmpty() ? KMail::Composer::New : KMail::Composer::NoTemplate;
0054     KMail::Composer *cWin = KMail::makeComposer(mMsg, false, false, context);
0055     if (!mSettings.mHidden) {
0056         cWin->showAndActivateComposer();
0057     } else {
0058         // Always disable word wrap when we don't show the composer; otherwise,
0059         // *TextEdit gets the widget size wrong and wraps much too early.
0060         cWin->disableWordWrap();
0061         cWin->slotSendNow();
0062     }
0063     deleteLater();
0064 }
0065 
0066 void OpenComposerHiddenJob::setSettings(const OpenComposerHiddenJobSettings &settings)
0067 {
0068     mSettings = settings;
0069 }
0070 
0071 #include "moc_opencomposerhiddenjob.cpp"