| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2018 Nicolas Fella <nicolas.fella@gmx.de> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 | */ |
| 6 | |
| 7 | #include "debug.h" |
| 8 | #include <KConfigGroup> |
| 9 | #include <KDesktopFile> |
| 10 | #include <KIO/ApplicationLauncherJob> |
| 11 | #include <KLocalizedString> |
| 12 | #include <KPluginFactory> |
| 13 | #include <KShell> |
| 14 | #include <QDebug> |
| 15 | #include <QJsonArray> |
| 16 | #include <QProcess> |
| 17 | #include <QStandardPaths> |
| 18 | #include <QTimer> |
| 19 | #include <QUrl> |
| 20 | #include <purpose/pluginbase.h> |
| 21 | |
| 22 | Q_LOGGING_CATEGORY(PLUGIN_TELEGRAM, "kf.purpose.plugins.telegram" ) |
| 23 | |
| 24 | class TelegramJob : public Purpose::Job |
| 25 | { |
| 26 | Q_OBJECT |
| 27 | public: |
| 28 | TelegramJob(QObject *parent) |
| 29 | : Purpose::Job(parent) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | QList<QUrl> arrayToList(const QJsonArray &array) |
| 34 | { |
| 35 | QList<QUrl> ret; |
| 36 | for (const QJsonValue &val : array) { |
| 37 | QUrl url(val.toString()); |
| 38 | ret << url; |
| 39 | } |
| 40 | return ret; |
| 41 | } |
| 42 | |
| 43 | void start() override |
| 44 | { |
| 45 | KService::Ptr service = KService::serviceByDesktopName(QStringLiteral("org.telegram.desktop" )); |
| 46 | |
| 47 | if (!service) { |
| 48 | service = KService::serviceByDesktopName(QStringLiteral("telegramdesktop" )); |
| 49 | } |
| 50 | |
| 51 | if (!service) { |
| 52 | service = KService::serviceByDesktopName(QStringLiteral("telegram-desktop" )); |
| 53 | } |
| 54 | |
| 55 | if (!service) { |
| 56 | // Failed to find the application |
| 57 | QTimer::singleShot(interval: 0, receiver: this, slot: [this]() { |
| 58 | setError(KJob::UserDefinedError + 1); |
| 59 | setErrorText(i18n(text: "Could not find telegram" )); |
| 60 | setOutput({{QStringLiteral("url" ), QString()}}); |
| 61 | emitResult(); |
| 62 | }); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | QString exec = service->exec(); |
| 67 | exec.replace(before: QLatin1String("%u" ), after: QLatin1String("%f" )); |
| 68 | exec.replace(before: QLatin1String("@@u" ), after: QLatin1String("@@" )); |
| 69 | exec.replace(before: QLatin1String(" -- " ), after: QLatin1String(" -sendpath " )); |
| 70 | service->setExec(exec); |
| 71 | |
| 72 | auto *job = new KIO::ApplicationLauncherJob(service); |
| 73 | job->setUrls(arrayToList(array: data().value(key: QLatin1String("urls" )).toArray())); |
| 74 | connect(sender: job, signal: &KIO::ApplicationLauncherJob::result, context: this, slot: [this](KJob *job) { |
| 75 | if (job->error()) { |
| 76 | qWarning() << "telegram share error:" << job->error() << job->errorString(); |
| 77 | setError(job->error()); |
| 78 | setErrorText(job->errorString()); |
| 79 | } else { |
| 80 | setOutput({{QStringLiteral("url" ), QString()}}); |
| 81 | } |
| 82 | |
| 83 | emitResult(); |
| 84 | }); |
| 85 | job->start(); |
| 86 | } |
| 87 | }; |
| 88 | |
| 89 | class TelegramPlugin : public Purpose::PluginBase |
| 90 | { |
| 91 | Q_OBJECT |
| 92 | public: |
| 93 | using PluginBase::PluginBase; |
| 94 | Purpose::Job *createJob() const override |
| 95 | { |
| 96 | return new TelegramJob(nullptr); |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | K_PLUGIN_CLASS_WITH_JSON(TelegramPlugin, "telegramplugin.json" ) |
| 101 | |
| 102 | #include "telegramplugin.moc" |
| 103 | |