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 "opencomposerjob.h"
0008 #include "kmail_debug.h"
0009 #include "kmkernel.h"
0010 #include <KIdentityManagementCore/Identity>
0011 #include <KIdentityManagementCore/IdentityManager>
0012 #include <KLocalizedString>
0013 #include <KMessageBox>
0014 #include <MessageComposer/Composer>
0015 #include <MessageComposer/MessageHelper>
0016 #include <MessageCore/StringUtil>
0017 #include <QFile>
0018 #include <QMimeDatabase>
0019 #include <TemplateParser/TemplateParserJob>
0020 
0021 OpenComposerJob::OpenComposerJob(QObject *parent)
0022     : QObject(parent)
0023 {
0024 }
0025 
0026 void OpenComposerJob::setOpenComposerSettings(const OpenComposerSettings &openComposerSettings)
0027 {
0028     mOpenComposerSettings = openComposerSettings;
0029 }
0030 
0031 OpenComposerJob::~OpenComposerJob() = default;
0032 
0033 void OpenComposerJob::start()
0034 {
0035     mMsg = KMime::Message::Ptr(new KMime::Message);
0036     if (!mOpenComposerSettings.mIdentity.isEmpty()) {
0037         if (KMKernel::self()->identityManager()->identities().contains(mOpenComposerSettings.mIdentity)) {
0038             const KIdentityManagementCore::Identity id = KMKernel::self()->identityManager()->modifyIdentityForName(mOpenComposerSettings.mIdentity);
0039             mIdentityId = id.uoid();
0040         } else {
0041             qCWarning(KMAIL_LOG) << "Identity name doesn't exist " << mOpenComposerSettings.mIdentity;
0042         }
0043     }
0044 
0045     MessageHelper::initHeader(mMsg, KIdentityManagementCore::IdentityManager::self(), mIdentityId);
0046 
0047     mMsg->contentType()->setCharset("utf-8");
0048     if (!mOpenComposerSettings.mTo.isEmpty()) {
0049         mMsg->to()->fromUnicodeString(mOpenComposerSettings.mTo, "utf-8");
0050     }
0051 
0052     if (!mOpenComposerSettings.mCc.isEmpty()) {
0053         mMsg->cc()->fromUnicodeString(mOpenComposerSettings.mCc, "utf-8");
0054     }
0055 
0056     if (!mOpenComposerSettings.mBcc.isEmpty()) {
0057         mMsg->bcc()->fromUnicodeString(mOpenComposerSettings.mBcc, "utf-8");
0058     }
0059 
0060     if (!mOpenComposerSettings.mSubject.isEmpty()) {
0061         mMsg->subject()->fromUnicodeString(mOpenComposerSettings.mSubject, "utf-8");
0062     }
0063     if (!mOpenComposerSettings.mReplyTo.isEmpty()) {
0064         mMsg->replyTo()->fromUnicodeString(mOpenComposerSettings.mReplyTo, "utf-8");
0065     }
0066     if (!mOpenComposerSettings.mInReplyTo.isEmpty()) {
0067         mMsg->inReplyTo()->fromUnicodeString(mOpenComposerSettings.mInReplyTo, "utf-8");
0068     }
0069 
0070     if (!mOpenComposerSettings.mMessageFile.isEmpty() && QFile::exists(mOpenComposerSettings.mMessageFile)) {
0071         QFile f(mOpenComposerSettings.mMessageFile);
0072         QByteArray str;
0073         if (!f.open(QIODevice::ReadOnly)) {
0074             qCWarning(KMAIL_LOG) << "Failed to load message: " << mOpenComposerSettings.mMessageFile << " Error : " << f.errorString();
0075         } else {
0076             str = f.readAll();
0077             f.close();
0078         }
0079         if (!str.isEmpty()) {
0080             mContext = KMail::Composer::NoTemplate;
0081             mMsg->setBody(str);
0082             slotOpenComposer();
0083         } else {
0084             auto parser = new TemplateParser::TemplateParserJob(mMsg, TemplateParser::TemplateParserJob::NewMessage);
0085             connect(parser, &TemplateParser::TemplateParserJob::parsingDone, this, &OpenComposerJob::slotOpenComposer);
0086             parser->setIdentityManager(KMKernel::self()->identityManager());
0087             parser->process(mMsg);
0088         }
0089     } else if (!mOpenComposerSettings.mBody.isEmpty()) {
0090         mContext = KMail::Composer::NoTemplate;
0091         mMsg->setBody(mOpenComposerSettings.mBody.toUtf8());
0092         mMsg->assemble();
0093         mMsg->parse();
0094         slotOpenComposer();
0095     } else {
0096         auto parser = new TemplateParser::TemplateParserJob(mMsg, TemplateParser::TemplateParserJob::NewMessage);
0097         connect(parser, &TemplateParser::TemplateParserJob::parsingDone, this, &OpenComposerJob::slotOpenComposer);
0098         parser->setIdentityManager(KMKernel::self()->identityManager());
0099         parser->process(mMsg);
0100     }
0101 }
0102 
0103 void OpenComposerJob::slotOpenComposer()
0104 {
0105     KMail::Composer *cWin = KMail::makeComposer(mMsg, false, false, mContext, mIdentityId);
0106     if (!mOpenComposerSettings.mTo.isEmpty()) {
0107         cWin->setFocusToSubject();
0108     }
0109     const QList<QUrl> attachURLs = QUrl::fromStringList(mOpenComposerSettings.mAttachmentPaths);
0110     QList<QUrl>::ConstIterator endAttachment(attachURLs.constEnd());
0111     QList<KMail::Composer::AttachmentInfo> infoList;
0112     for (QList<QUrl>::ConstIterator it = attachURLs.constBegin(); it != endAttachment; ++it) {
0113         QMimeDatabase mimeDb;
0114         if (mimeDb.mimeTypeForUrl(*it).name() == QLatin1StringView("inode/directory")) {
0115             const int answer = KMessageBox::questionTwoActions(nullptr,
0116                                                                i18n("Do you want to attach this folder \"%1\"?", (*it).toDisplayString()),
0117                                                                i18nc("@title:window", "Attach Folder"),
0118                                                                KGuiItem(i18nc("@action:button", "Attach"), QStringLiteral("dialog-ok")),
0119                                                                KGuiItem(i18nc("@action:button", "Do Not Attach"), QStringLiteral("dialog-cancel")));
0120             if (answer == KMessageBox::ButtonCode::SecondaryAction) {
0121                 continue;
0122             }
0123         }
0124         KMail::Composer::AttachmentInfo info;
0125         info.url = (*it);
0126         infoList.append(std::move(info));
0127     }
0128     if (!infoList.isEmpty()) {
0129         cWin->addAttachment(infoList, true);
0130     }
0131     if (!mOpenComposerSettings.mCustomHeaders.isEmpty()) {
0132         QMap<QByteArray, QString> extraCustomHeaders;
0133         QStringList::ConstIterator end = mOpenComposerSettings.mCustomHeaders.constEnd();
0134         for (QStringList::ConstIterator it = mOpenComposerSettings.mCustomHeaders.constBegin(); it != end; ++it) {
0135             if (!(*it).isEmpty()) {
0136                 const int pos = (*it).indexOf(QLatin1Char(':'));
0137                 if (pos > 0) {
0138                     const QString header = (*it).left(pos).trimmed();
0139                     const QString value = (*it).mid(pos + 1).trimmed();
0140                     if (!header.isEmpty() && !value.isEmpty()) {
0141                         extraCustomHeaders.insert(header.toUtf8(), value);
0142                     }
0143                 }
0144             }
0145         }
0146         if (!extraCustomHeaders.isEmpty()) {
0147             cWin->addExtraCustomHeaders(extraCustomHeaders);
0148         }
0149     }
0150     if (!mOpenComposerSettings.mHidden) {
0151         cWin->showAndActivateComposer();
0152     }
0153     deleteLater();
0154 }
0155 
0156 #include "moc_opencomposerjob.cpp"