Purpose

telegramplugin.cpp
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
22Q_LOGGING_CATEGORY(PLUGIN_TELEGRAM, "kf.purpose.plugins.telegram")
23
24class TelegramJob : public Purpose::Job
25{
26 Q_OBJECT
27public:
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(0, this, [this]() {
58 setError(KJob::UserDefinedError + 1);
59 setErrorText(i18n("Could not find telegram"));
60 setOutput({{QStringLiteral("url"), QString()}});
61 emitResult();
62 });
63 return;
64 }
65
66 QString exec = service->exec();
67 exec.replace(QLatin1String("%u"), QLatin1String("%f"));
68 exec.replace(QLatin1String("@@u"), QLatin1String("@@"));
69 exec.replace(QLatin1String(" -- "), QLatin1String(" -sendpath "));
70 service->setExec(exec);
71
72 auto *job = new KIO::ApplicationLauncherJob(service);
73 job->setUrls(arrayToList(data().value(QLatin1String("urls")).toArray()));
74 connect(job, &KIO::ApplicationLauncherJob::result, this, [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
89class TelegramPlugin : public Purpose::PluginBase
90{
92public:
93 using PluginBase::PluginBase;
94 Purpose::Job *createJob() const override
95 {
96 return new TelegramJob(nullptr);
97 }
98};
99
100K_PLUGIN_CLASS_WITH_JSON(TelegramPlugin, "telegramplugin.json")
101
102#include "telegramplugin.moc"
virtual QString errorString() const
int error() const
void result(KJob *job)
virtual Q_SCRIPTABLE void start()=0
#define K_PLUGIN_CLASS_WITH_JSON(classname, jsonFile)
static Ptr serviceByDesktopName(const QString &_name)
Job that will actually perform the sharing.
Definition job.h:34
Base class to implement by plugins.
Definition pluginbase.h:29
Q_SCRIPTABLE Q_NOREPLY void start()
QString i18n(const char *text, const TYPE &arg...)
Q_OBJECTQ_OBJECT
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:08:47 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.