File indexing completed on 2024-05-12 05:04:14

0001 /*
0002     SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include <KIO/CommandLauncherJob>
0008 #include <KPluginFactory>
0009 #include <Purpose/PluginBase>
0010 
0011 class TokodonJob : public Purpose::Job
0012 {
0013     Q_OBJECT
0014 public:
0015     explicit TokodonJob(QObject *parent)
0016         : Purpose::Job(parent)
0017     {
0018     }
0019 
0020     QStringList arrayToList(const QJsonArray &array)
0021     {
0022         QStringList ret;
0023         for (const auto &val : array) {
0024             ret += val.toString();
0025         }
0026         return ret;
0027     }
0028 
0029     void start() override
0030     {
0031         const QJsonArray urlsJson = data().value(QStringLiteral("urls")).toArray();
0032         const QString title = data().value(QStringLiteral("title")).toString();
0033         const QString message = QStringLiteral("%1 - %2").arg(title, arrayToList(urlsJson).join(QLatin1Char(' ')));
0034 
0035         auto *job = new KIO::CommandLauncherJob(QStringLiteral("tokodon"), {QStringLiteral("--share"), message});
0036         connect(job, &KJob::finished, this, &TokodonJob::emitResult);
0037         job->start();
0038     }
0039 };
0040 
0041 class Q_DECL_EXPORT PurposePlugin : public Purpose::PluginBase
0042 {
0043     Q_OBJECT
0044 public:
0045     PurposePlugin(QObject *p, const QVariantList &)
0046         : Purpose::PluginBase(p)
0047     {
0048     }
0049 
0050     Purpose::Job *createJob() const override
0051     {
0052         return new TokodonJob(nullptr);
0053     }
0054 };
0055 
0056 K_PLUGIN_CLASS_WITH_JSON(PurposePlugin, "purposeplugin.json")
0057 
0058 #include "purposeplugin.moc"