1 | /* |
2 | SPDX-FileCopyrightText: 2017 Klarälvdalens Datakonsult AB a KDAB Group company info@kdab.com |
3 | SPDX-FileContributor: Daniel Vrátil <daniel.vratil@kdab.com> |
4 | |
5 | SPDX-License-Identifier: LGPL-2.1-or-later |
6 | */ |
7 | |
8 | #include <purpose/pluginbase.h> |
9 | |
10 | #include <KApplicationTrader> |
11 | #include <KEMailClientLauncherJob> |
12 | #include <KLocalizedString> |
13 | #include <KPluginFactory> |
14 | |
15 | #include <QDebug> |
16 | #include <QDesktopServices> |
17 | #include <QJsonArray> |
18 | #include <QProcess> |
19 | #include <QStandardPaths> |
20 | #include <QUrl> |
21 | #include <QUrlQuery> |
22 | |
23 | namespace |
24 | { |
25 | class EmailJob : public Purpose::Job |
26 | { |
27 | Q_OBJECT |
28 | public: |
29 | explicit EmailJob(QObject *parent = nullptr) |
30 | : Purpose::Job(parent) |
31 | { |
32 | } |
33 | |
34 | void start() override |
35 | { |
36 | KEMailClientLauncherJob *job = new KEMailClientLauncherJob; |
37 | |
38 | QList<QUrl> attachments; |
39 | QStringList bodyPieces; |
40 | |
41 | const auto urls = data().value(key: QLatin1String("urls" )).toArray(); |
42 | for (const QJsonValue &val : urls) { |
43 | const QUrl url = val.toVariant().toUrl(); |
44 | if (url.isLocalFile()) { |
45 | attachments << url; |
46 | } else { |
47 | bodyPieces << url.toString(); |
48 | } |
49 | } |
50 | |
51 | job->setAttachments(attachments); |
52 | job->setBody(bodyPieces.join(sep: QLatin1Char('\n'))); |
53 | job->setSubject(data().value(key: QLatin1String("title" )).toString()); |
54 | |
55 | connect(sender: job, signal: &KJob::result, context: this, slot: [this](KJob *job) { |
56 | setError(job->error()); |
57 | setErrorText(job->errorText()); |
58 | emitResult(); |
59 | }); |
60 | |
61 | job->start(); |
62 | } |
63 | }; |
64 | |
65 | } |
66 | |
67 | class EmailPlugin : public Purpose::PluginBase |
68 | { |
69 | Q_OBJECT |
70 | public: |
71 | using PluginBase::PluginBase; |
72 | Purpose::Job *createJob() const override |
73 | { |
74 | return new EmailJob(nullptr); |
75 | } |
76 | }; |
77 | |
78 | K_PLUGIN_CLASS_WITH_JSON(EmailPlugin, "emailplugin.json" ) |
79 | |
80 | #include "emailplugin.moc" |
81 | |