File indexing completed on 2024-05-19 04:03:04
0001 /* 0002 SPDX-FileCopyrightText: 2021 Nicolas Fella <nicolas.fella@gmx.de> 0003 SPDX-License-Identifier: LGPL-2.0-or-later 0004 */ 0005 0006 #include <KPluginFactory> 0007 #include <purpose/pluginbase.h> 0008 0009 #include <KIO/OpenUrlJob> 0010 0011 #include <QUrlQuery> 0012 0013 QList<QString> arrayToList(const QJsonArray &array) 0014 { 0015 QList<QString> ret; 0016 for (const QJsonValue &val : array) { 0017 ret += val.toVariant().toString(); 0018 } 0019 return ret; 0020 } 0021 0022 class TwitterJob : public Purpose::Job 0023 { 0024 Q_OBJECT 0025 public: 0026 TwitterJob(QObject *parent) 0027 : Purpose::Job(parent) 0028 { 0029 } 0030 void start() override 0031 { 0032 const QList<QString> urls = arrayToList(data().value(QLatin1String("urls")).toArray()); 0033 const QString text = data().value(QLatin1String("text")).toString(); 0034 0035 QUrlQuery query; 0036 0037 if (!urls.isEmpty()) { 0038 query.addQueryItem(QStringLiteral("url"), urls.constFirst()); 0039 } 0040 0041 if (!text.isEmpty()) { 0042 query.addQueryItem(QStringLiteral("text"), text); 0043 } 0044 0045 auto *job = new KIO::OpenUrlJob(QUrl(QStringLiteral("https://twitter.com/intent/tweet?") + query.toString())); 0046 connect(job, &KJob::finished, this, [this, job] { 0047 if (job->error()) { 0048 setError(job->error()); 0049 setErrorText(job->errorText()); 0050 } 0051 emitResult(); 0052 }); 0053 job->start(); 0054 } 0055 }; 0056 0057 class TwitterPlugin : public Purpose::PluginBase 0058 { 0059 Q_OBJECT 0060 public: 0061 using PluginBase::PluginBase; 0062 Purpose::Job *createJob() const override 0063 { 0064 return new TwitterJob(nullptr); 0065 } 0066 }; 0067 0068 K_PLUGIN_CLASS_WITH_JSON(TwitterPlugin, "twitterplugin.json") 0069 0070 #include "twitterplugin.moc"