File indexing completed on 2024-04-28 05:11:37

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Sandro Knauß <knauss@kolabsys.com>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "opencomposerjob.h"
0008 
0009 #include <KLocalizedString>
0010 
0011 #include <QDBusConnectionInterface>
0012 #include <QDBusInterface>
0013 
0014 using namespace IncidenceEditorNG;
0015 
0016 OpenComposerJob::OpenComposerJob(QObject *parent,
0017                                  const QString &to,
0018                                  const QString &cc,
0019                                  const QString &bcc,
0020                                  const KMime::Message::Ptr &message,
0021                                  const KIdentityManagementCore::Identity &identity)
0022     : KJob(parent)
0023     , mTo(to)
0024     , mCc(cc)
0025     , mBcc(bcc)
0026     , mMessage(message)
0027     , mIdentity(identity)
0028 {
0029 }
0030 
0031 OpenComposerJob::~OpenComposerJob() = default;
0032 
0033 void OpenComposerJob::start()
0034 {
0035     Q_ASSERT(mMessage);
0036 
0037     unsigned int identity = mIdentity.uoid();
0038 
0039     QString subject = mMessage->subject()->asUnicodeString();
0040     QString body = QString::fromUtf8(mMessage->contents()[0]->body());
0041 
0042     QList<QVariant> messages;
0043 
0044     if (mMessage->contents().count() == 1) {
0045         const QString messageFile;
0046         const QStringList attachmentPaths;
0047         const QStringList customHeaders;
0048         const QString replyTo;
0049         const QString inReplyTo;
0050         bool hidden = false;
0051 
0052         messages << mTo << mCc << mBcc << subject << body << hidden << messageFile << attachmentPaths << customHeaders << replyTo << inReplyTo;
0053     } else {
0054         KMime::Content *attachment(mMessage->contents().at(1));
0055         QString attachName = attachment->contentType()->name();
0056         QByteArray attachCte = attachment->contentTransferEncoding()->as7BitString(false);
0057         QByteArray attachType = attachment->contentType()->mediaType();
0058         QByteArray attachSubType = attachment->contentType()->subType();
0059         QByteArray attachContDisp = attachment->contentDisposition()->as7BitString(false);
0060         QByteArray attachCharset = attachment->contentType()->charset();
0061 
0062         QByteArray attachParamAttr = "method";
0063         QString attachParamValue = attachment->contentType()->parameter(QStringLiteral("method"));
0064         QByteArray attachData = attachment->encodedBody();
0065 
0066         messages << mTo << mCc << mBcc << subject << body << attachName << attachCte << attachData << attachType << attachSubType << attachParamAttr
0067                  << attachParamValue << attachContDisp << attachCharset << identity;
0068     }
0069 
0070     // with D-Bus autostart, this will start kmail if it's not running yet
0071     QDBusInterface kmailObj(QStringLiteral("org.kde.kmail"), QStringLiteral("/KMail"), QStringLiteral("org.kde.kmail.kmail"));
0072 
0073     QDBusReply<int> composerDbusPath = kmailObj.callWithArgumentList(QDBus::AutoDetect, QStringLiteral("openComposer"), messages);
0074 
0075     if (!composerDbusPath.isValid()) {
0076         setError(KJob::UserDefinedError);
0077         setErrorText(i18nc("errormessage: dbus is running but still no connection kmail", "Cannot connect to email service"));
0078     }
0079     emitResult();
0080 }
0081 
0082 #include "moc_opencomposerjob.cpp"