Purpose

twitterplugin.cpp
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{
16 for (const QJsonValue &val : array) {
17 ret += val.toVariant().toString();
18 }
19 return ret;
20}
21
22class TwitterJob : public Purpose::Job
23{
25public:
26 TwitterJob(QObject *parent)
27 : Purpose::Job(parent)
28 {
29 }
30 void start() override
31 {
32 const QList<QString> urls = arrayToList(data().value(QLatin1String("urls")).toArray());
33 const QString text = data().value(QLatin1String("text")).toString();
34
36
37 if (!urls.isEmpty()) {
38 query.addQueryItem(QStringLiteral("url"), urls.constFirst());
39 }
40
41 if (!text.isEmpty()) {
42 query.addQueryItem(QStringLiteral("text"), text);
43 }
44
45 auto *job = new KIO::OpenUrlJob(QUrl(QStringLiteral("https://twitter.com/intent/tweet?") + query.toString()));
46 connect(job, &KJob::finished, this, [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{
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"
void setErrorText(const QString &errorText)
void emitResult()
void finished(KJob *job)
void setError(int errorCode)
#define K_PLUGIN_CLASS_WITH_JSON(classname, jsonFile)
Job that will actually perform the sharing.
Definition job.h:34
QJsonObject data
Represents the data the job will have available to perform its task.
Definition job.h:39
Base class to implement by plugins.
Definition pluginbase.h:29
std::optional< QSqlQuery > query(const QString &queryStatement)
QJsonValue value(QLatin1StringView key) const const
QString toString() const const
const T & constFirst() const const
bool isEmpty() const const
Q_OBJECTQ_OBJECT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
bool isEmpty() 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.