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 | |
13 | QList<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 | |
22 | class : public Purpose::Job |
23 | { |
24 | Q_OBJECT |
25 | public: |
26 | (QObject *parent) |
27 | : Purpose::Job(parent) |
28 | { |
29 | } |
30 | void () 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 | |
57 | class : public Purpose::PluginBase |
58 | { |
59 | Q_OBJECT |
60 | public: |
61 | using PluginBase::PluginBase; |
62 | Purpose::Job *() const override |
63 | { |
64 | return new TwitterJob(nullptr); |
65 | } |
66 | }; |
67 | |
68 | K_PLUGIN_CLASS_WITH_JSON(TwitterPlugin, "twitterplugin.json" ) |
69 | |
70 | #include "twitterplugin.moc" |
71 | |