Purpose

emailplugin.cpp
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
23namespace
24{
25class EmailJob : public Purpose::Job
26{
27 Q_OBJECT
28public:
29 explicit EmailJob(QObject *parent = nullptr)
30 : Purpose::Job(parent)
31 {
32 }
33
34 void start() override
35 {
37
38 QList<QUrl> attachments;
39 QStringList bodyPieces;
40
41 const auto urls = data().value(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(QLatin1Char('\n')));
53 job->setSubject(data().value(QLatin1String("title")).toString());
54
55 connect(job, &KJob::result, this, [this](KJob *job) {
56 setError(job->error());
57 setErrorText(job->errorText());
58 emitResult();
59 });
60
61 job->start();
62 }
63};
64
65}
66
67class EmailPlugin : public Purpose::PluginBase
68{
70public:
71 using PluginBase::PluginBase;
72 Purpose::Job *createJob() const override
73 {
74 return new EmailJob(nullptr);
75 }
76};
77
78K_PLUGIN_CLASS_WITH_JSON(EmailPlugin, "emailplugin.json")
79
80#include "emailplugin.moc"
void setAttachments(const QList< QUrl > &urls)
void setSubject(const QString &subject)
void setBody(const QString &body)
int error() const
void result(KJob *job)
virtual Q_SCRIPTABLE void start()=0
QString errorText() const
#define K_PLUGIN_CLASS_WITH_JSON(classname, jsonFile)
Job that will actually perform the sharing.
Definition job.h:34
Base class to implement by plugins.
Definition pluginbase.h:29
Q_SCRIPTABLE Q_NOREPLY void start()
char * toString(const EngineQuery &query)
T value(qsizetype i) const const
Q_OBJECTQ_OBJECT
QString join(QChar separator) const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
bool isLocalFile() const const
QString toString(FormattingOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:03:06 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.