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 "handleclickedurljob.h"
0008 #include "kmail_debug.h"
0009 #include "kmkernel.h"
0010 #include <KMime/Message>
0011 #include <MessageComposer/Composer>
0012 #include <MessageComposer/MessageHelper>
0013 #include <MessageCore/StringUtil>
0014 #include <TemplateParser/TemplateParserJob>
0015 
0016 HandleClickedUrlJob::HandleClickedUrlJob(QObject *parent)
0017     : QObject(parent)
0018 {
0019 }
0020 
0021 HandleClickedUrlJob::~HandleClickedUrlJob() = default;
0022 
0023 void HandleClickedUrlJob::start()
0024 {
0025     mMsg = KMime::Message::Ptr(new KMime::Message);
0026     mIdentity = !mFolder.isNull() ? mFolder->identity() : 0;
0027     MessageHelper::initHeader(mMsg, KMKernel::self()->identityManager(), mIdentity);
0028     mMsg->contentType()->setCharset("utf-8");
0029 
0030     const QList<QPair<QString, QString>> fields = MessageCore::StringUtil::parseMailtoUrl(mUrl);
0031     for (int i = 0; i < fields.count(); ++i) {
0032         const QPair<QString, QString> element = fields.at(i);
0033         if (element.first == QLatin1StringView("to")) {
0034             mMsg->to()->fromUnicodeString(element.second, "utf-8");
0035         } else if (element.first == QLatin1StringView("subject")) {
0036             const QString subject = element.second;
0037             if (!subject.isEmpty()) {
0038                 mMsg->subject()->fromUnicodeString(subject, "utf-8");
0039             }
0040         } else if (element.first == QLatin1StringView("body")) {
0041             const QString body = element.second;
0042             if (!body.isEmpty()) {
0043                 mMsg->setBody(body.toUtf8());
0044             }
0045         } else if (element.first == QLatin1StringView("cc")) {
0046             const QString cc = element.second;
0047             if (!cc.isEmpty()) {
0048                 mMsg->cc()->fromUnicodeString(cc, "utf-8");
0049             }
0050         } else if (element.first == QLatin1StringView("bcc")) {
0051             const QString bcc = element.second;
0052             if (!bcc.isEmpty()) {
0053                 mMsg->bcc()->fromUnicodeString(bcc, "utf-8");
0054             }
0055         } else if (element.first == QLatin1StringView("attach")) {
0056             const QString attach = element.second;
0057             if (!attach.isEmpty()) {
0058                 qCDebug(KMAIL_LOG) << "Attachment not supported yet";
0059             }
0060         }
0061     }
0062 
0063     auto parser = new TemplateParser::TemplateParserJob(mMsg, TemplateParser::TemplateParserJob::NewMessage, this);
0064     connect(parser, &TemplateParser::TemplateParserJob::parsingDone, this, &HandleClickedUrlJob::slotOpenComposer);
0065     parser->setIdentityManager(KMKernel::self()->identityManager());
0066     parser->process(mMsg, mCurrentCollection.id());
0067 }
0068 
0069 void HandleClickedUrlJob::slotOpenComposer()
0070 {
0071     KMail::Composer *win = KMail::makeComposer(mMsg, false, false, KMail::Composer::New, mIdentity);
0072     win->setFocusToSubject();
0073     win->setCollectionForNewMessage(mCurrentCollection);
0074     win->show();
0075     deleteLater();
0076 }
0077 
0078 void HandleClickedUrlJob::setUrl(const QUrl &url)
0079 {
0080     mUrl = url;
0081 }
0082 
0083 void HandleClickedUrlJob::setFolder(const QSharedPointer<MailCommon::FolderSettings> &folder)
0084 {
0085     mFolder = folder;
0086 }
0087 
0088 void HandleClickedUrlJob::setCurrentCollection(const Akonadi::Collection &currentCollection)
0089 {
0090     mCurrentCollection = currentCollection;
0091 }
0092 
0093 #include "moc_handleclickedurljob.cpp"