1 /*
2  * SPDX-FileCopyrightText: 2014 Sandro Knauß <knauss@kolabsys.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.0-or-later
5  */
6 
7 #include "opencomposerjob.h"
8 
9 #include <KLocalizedString>
10 
11 #include <QDBusConnectionInterface>
12 #include <QDBusInterface>
13 
14 using namespace IncidenceEditorNG;
15 
OpenComposerJob(QObject * parent,const QString & to,const QString & cc,const QString & bcc,const KMime::Message::Ptr & message,const KIdentityManagement::Identity & identity)16 OpenComposerJob::OpenComposerJob(QObject *parent,
17                                  const QString &to,
18                                  const QString &cc,
19                                  const QString &bcc,
20                                  const KMime::Message::Ptr &message,
21                                  const KIdentityManagement::Identity &identity)
22     : KJob(parent)
23     , mTo(to)
24     , mCc(cc)
25     , mBcc(bcc)
26     , mMessage(message)
27     , mIdentity(identity)
28 {
29 }
30 
~OpenComposerJob()31 OpenComposerJob::~OpenComposerJob()
32 {
33 }
34 
start()35 void OpenComposerJob::start()
36 {
37     Q_ASSERT(mMessage);
38 
39     unsigned int identity = mIdentity.uoid();
40 
41     QString subject = mMessage->subject()->asUnicodeString();
42     QString body = QString::fromUtf8(mMessage->contents()[0]->body());
43 
44     QList<QVariant> messages;
45 
46     if (mMessage->contents().count() == 1) {
47         const QString messageFile;
48         const QStringList attachmentPaths;
49         const QStringList customHeaders;
50         const QString replyTo;
51         const QString inReplyTo;
52         bool hidden = false;
53 
54         messages << mTo << mCc << mBcc << subject << body << hidden << messageFile << attachmentPaths << customHeaders << replyTo << inReplyTo;
55     } else {
56         KMime::Content *attachment(mMessage->contents().at(1));
57         QString attachName = attachment->contentType()->name();
58         QByteArray attachCte = attachment->contentTransferEncoding()->as7BitString(false);
59         QByteArray attachType = attachment->contentType()->mediaType();
60         QByteArray attachSubType = attachment->contentType()->subType();
61         QByteArray attachContDisp = attachment->contentDisposition()->as7BitString(false);
62         QByteArray attachCharset = attachment->contentType()->charset();
63 
64         QByteArray attachParamAttr = "method";
65         QString attachParamValue = attachment->contentType()->parameter(QStringLiteral("method"));
66         QByteArray attachData = attachment->encodedBody();
67 
68         messages << mTo << mCc << mBcc << subject << body << attachName << attachCte << attachData << attachType << attachSubType << attachParamAttr
69                  << attachParamValue << attachContDisp << attachCharset << identity;
70     }
71 
72     // with D-Bus autostart, this will start kmail if it's not running yet
73     QDBusInterface kmailObj(QStringLiteral("org.kde.kmail"), QStringLiteral("/KMail"), QStringLiteral("org.kde.kmail.kmail"));
74 
75     QDBusReply<int> composerDbusPath = kmailObj.callWithArgumentList(QDBus::AutoDetect, QStringLiteral("openComposer"), messages);
76 
77     if (!composerDbusPath.isValid()) {
78         setError(KJob::UserDefinedError);
79         setErrorText(i18nc("errormessage: dbus is running but still no connection kmail", "Cannot connect to email service"));
80     }
81     emitResult();
82 }
83