1/*
2 SPDX-FileCopyrightText: 2021 Nicolas Fella <nicolas.fella@gmx.de>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include <KPluginFactory>
7#include <purpose/pluginbase.h>
8
9#include <KIO/OpenUrlJob>
10
11#include <QUrlQuery>
12
13QList<QString> arrayToList(const QJsonArray &array)
14{
15 QList<QString> ret;
16 for (const QJsonValue &val : array) {
17 ret += val.toVariant().toString();
18 }
19 return ret;
20}
21
22class TwitterJob : public Purpose::Job
23{
24 Q_OBJECT
25public:
26 TwitterJob(QObject *parent)
27 : Purpose::Job(parent)
28 {
29 }
30 void start() override
31 {
32 const QList<QString> urls = arrayToList(array: data().value(key: QLatin1String("urls")).toArray());
33 const QString text = data().value(key: QLatin1String("text")).toString();
34
35 QUrlQuery query;
36
37 if (!urls.isEmpty()) {
38 query.addQueryItem(QStringLiteral("url"), value: urls.constFirst());
39 }
40
41 if (!text.isEmpty()) {
42 query.addQueryItem(QStringLiteral("text"), value: text);
43 }
44
45 auto *job = new KIO::OpenUrlJob(QUrl(QStringLiteral("https://twitter.com/intent/tweet?") + query.toString()));
46 connect(sender: job, signal: &KJob::finished, context: this, slot: [this, job] {
47 if (job->error()) {
48 setError(job->error());
49 setErrorText(job->errorText());
50 }
51 emitResult();
52 });
53 job->start();
54 }
55};
56
57class TwitterPlugin : public Purpose::PluginBase
58{
59 Q_OBJECT
60public:
61 using PluginBase::PluginBase;
62 Purpose::Job *createJob() const override
63 {
64 return new TwitterJob(nullptr);
65 }
66};
67
68K_PLUGIN_CLASS_WITH_JSON(TwitterPlugin, "twitterplugin.json")
69
70#include "twitterplugin.moc"
71

source code of purpose/src/plugins/twitter/twitterplugin.cpp