diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2018-03-21 17:34:05 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2018-03-23 16:32:01 +0100 |
commit | 0c881f0c1b77cf8876094e3647d1732210b954d1 (patch) | |
tree | 418396b4f2aae86c1cbec2cee9bcd6d36b87dda0 /extensions/api/src/extensionapi.cpp | |
parent | d0029fbe0a503edcf36e6ad072b87c53ad0715eb (diff) | |
download | kube-0c881f0c1b77cf8876094e3647d1732210b954d1.tar.gz kube-0c881f0c1b77cf8876094e3647d1732210b954d1.zip |
An extension mechanism load qml files at generic extension points.
and forward the email via an extension api.
Diffstat (limited to 'extensions/api/src/extensionapi.cpp')
-rw-r--r-- | extensions/api/src/extensionapi.cpp | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/extensions/api/src/extensionapi.cpp b/extensions/api/src/extensionapi.cpp new file mode 100644 index 00000000..3e6689b9 --- /dev/null +++ b/extensions/api/src/extensionapi.cpp | |||
@@ -0,0 +1,89 @@ | |||
1 | /* | ||
2 | Copyright (c) 2018 Christian Mollekopf <mollekopf@kolabsys.com> | ||
3 | |||
4 | This library is free software; you can redistribute it and/or modify it | ||
5 | under the terms of the GNU Library General Public License as published by | ||
6 | the Free Software Foundation; either version 2 of the License, or (at your | ||
7 | option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, but WITHOUT | ||
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||
12 | License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this library; see the file COPYING.LIB. If not, write to the | ||
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
17 | 02110-1301, USA. | ||
18 | */ | ||
19 | #include "extensionapi.h" | ||
20 | |||
21 | #include <mailtemplates.h> | ||
22 | #include <KMime/KMimeMessage> | ||
23 | #include <sink/store.h> | ||
24 | #include <sink/log.h> | ||
25 | |||
26 | #include <QDebug> | ||
27 | |||
28 | static void send(const QByteArray &message, const QByteArray &accountId) | ||
29 | { | ||
30 | using namespace Sink; | ||
31 | using namespace Sink::ApplicationDomain; | ||
32 | |||
33 | Q_ASSERT(!accountId.isEmpty()); | ||
34 | Query query; | ||
35 | query.containsFilter<SinkResource::Capabilities>(ResourceCapabilities::Mail::transport); | ||
36 | query.filter<SinkResource::Account>(accountId); | ||
37 | auto job = Store::fetchAll<SinkResource>(query) | ||
38 | .then([=](const QList<SinkResource::Ptr> &resources) { | ||
39 | if (!resources.isEmpty()) { | ||
40 | auto resourceId = resources[0]->identifier(); | ||
41 | SinkLog() << "Sending message via resource: " << resourceId; | ||
42 | Mail mail(resourceId); | ||
43 | mail.setMimeMessage(message); | ||
44 | return Store::create(mail) | ||
45 | .then<void>([=] { | ||
46 | //Trigger a sync, but don't wait for it. | ||
47 | Store::synchronize(Sink::SyncScope{}.resourceFilter(resourceId)).exec(); | ||
48 | }); | ||
49 | } | ||
50 | SinkWarning() << "Failed to find a mailtransport resource"; | ||
51 | return KAsync::error<void>(0, "Failed to find a MailTransport resource."); | ||
52 | }) | ||
53 | .then([&] (const KAsync::Error &) { | ||
54 | SinkLog() << "Message was sent: "; | ||
55 | }); | ||
56 | job.exec(); | ||
57 | } | ||
58 | |||
59 | static QStringList toStringList(const QVariantList &list) | ||
60 | { | ||
61 | QStringList s; | ||
62 | for (const auto &e : list) { | ||
63 | s << e.toString(); | ||
64 | } | ||
65 | return s; | ||
66 | } | ||
67 | |||
68 | Q_INVOKABLE void ExtensionApi::forwardMail(const QVariantMap &map) | ||
69 | { | ||
70 | SinkLog() << "Forwarding mail " << map; | ||
71 | auto mailObject = map.value("mail").value<Sink::ApplicationDomain::Mail::Ptr>(); | ||
72 | Q_ASSERT(mailObject); | ||
73 | KMime::Message::Ptr msg(new KMime::Message); | ||
74 | msg->setContent(KMime::CRLFtoLF(mailObject->getMimeMessage())); | ||
75 | msg->parse(); | ||
76 | |||
77 | MailTemplates::forward(msg, [map] (const KMime::Message::Ptr &fwdMessage) { | ||
78 | auto msg = fwdMessage; | ||
79 | msg->subject()->fromUnicodeString(map.value("subject").toString(), "utf8"); | ||
80 | auto list = toStringList(map.value("to").toList()); | ||
81 | for (const auto &address : list) { | ||
82 | KMime::Types::Mailbox mb; | ||
83 | mb.fromUnicodeString(address); | ||
84 | msg->to()->addAddress(mb); | ||
85 | } | ||
86 | msg->assemble(); | ||
87 | send(msg->encodedContent(true), map.value("accountId").toByteArray()); | ||
88 | }); | ||
89 | } | ||