diff --git a/src/plugins/CMakeLists.txt b/src/plugins/CMakeLists.txt --- a/src/plugins/CMakeLists.txt +++ b/src/plugins/CMakeLists.txt @@ -51,3 +51,4 @@ add_subdirectory(reviewboard) add_subdirectory(phabricator) add_subdirectory(email) +add_subdirectory(telegram) diff --git a/src/plugins/telegram/CMakeLists.txt b/src/plugins/telegram/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/src/plugins/telegram/CMakeLists.txt @@ -0,0 +1 @@ +add_share_plugin(telegramplugin telegramplugin.cpp) diff --git a/src/plugins/telegram/Messages.sh b/src/plugins/telegram/Messages.sh new file mode 100644 --- /dev/null +++ b/src/plugins/telegram/Messages.sh @@ -0,0 +1,4 @@ +#!/bin/sh +$EXTRACTRC `find . -name \*.rc` `find . -name \*.ui` >> rc.cpp +$XGETTEXT `find . -not -path \*/tests/\* -name \*.cpp -o -name \*.cc -o -name \*.h` -o $podir/purpose_telegram.pot +rm -f rc.cpp diff --git a/src/plugins/telegram/debug.h b/src/plugins/telegram/debug.h new file mode 100644 --- /dev/null +++ b/src/plugins/telegram/debug.h @@ -0,0 +1,2 @@ +#include +Q_DECLARE_LOGGING_CATEGORY(PLUGIN_TELEGRAM) diff --git a/src/plugins/telegram/telegramplugin.cpp b/src/plugins/telegram/telegramplugin.cpp new file mode 100644 --- /dev/null +++ b/src/plugins/telegram/telegramplugin.cpp @@ -0,0 +1,138 @@ +/* + Copyright 2018 Nicolas Fella + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see . +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "debug.h" + +Q_LOGGING_CATEGORY(PLUGIN_TELEGRAM, "org.kde.purpose.plugin.telegram") + +EXPORT_SHARE_VERSION + +class TelegramJob : public Purpose::Job +{ + Q_OBJECT + public: + TelegramJob(QObject* parent) + : Purpose::Job(parent) + {} + + QStringList arrayToList(const QJsonArray& array) + { + QStringList ret; + foreach(const QJsonValue& val, array) { + QUrl url(val.toString()); + if(url.isLocalFile()) { + ret += url.toLocalFile(); + } + } + return ret; + } + + void start() override + { + // Try executing via telegram-desktop command. If it fails, try again with flatpak commands. + // TODO Add snap command + tryClassic(); + } + + void tryClassic() + { + QJsonArray urlsJson = data().value(QStringLiteral("urls")).toArray(); + QString classicCommand(QStringLiteral("telegram-desktop")); + QStringList classicArgs(QStringLiteral("-sendpath")); + classicArgs << arrayToList(urlsJson); + + QProcess* process = new QProcess(this); + process->setProgram(classicCommand); + process->setArguments(classicArgs); + connect(process, &QProcess::errorOccurred, this, &TelegramJob::tryFlatpak); + connect(process, QOverload::of(&QProcess::finished), this, &TelegramJob::jobFinishedClassic); + connect(process, &QProcess::readyRead, this, [process](){ qDebug() << "telegram-desktop output:" << process->readAll(); }); + + process->start(); + } + + void tryFlatpak() + { + QJsonArray urlsJson = data().value(QStringLiteral("urls")).toArray(); + QString classicCommand(QStringLiteral("/usr/bin/flatpak")); + QStringList classicArgs(QStringLiteral("run")); + classicArgs << QStringLiteral("--file-forwarding") << QStringLiteral("org.telegram.desktop") << QStringLiteral("-sendpath") + << QStringLiteral("@@") << arrayToList(urlsJson) << QStringLiteral("@@"); + + QProcess* process = new QProcess(this); + process->setProgram(classicCommand); + process->setArguments(classicArgs); + connect(process, &QProcess::errorOccurred, this, &TelegramJob::processError); + connect(process, QOverload::of(&QProcess::finished), this, &TelegramJob::jobFinished); + connect(process, &QProcess::readyRead, this, [process](){ qDebug() << "telegram-desktop output:" << process->readAll(); }); + + process->start(); + } + + void jobFinishedClassic(int code, QProcess::ExitStatus status) + { + if (status != QProcess::NormalExit) { + return; + } + jobFinished(code, status); + } + + void processError(QProcess::ProcessError error) + { + QProcess* process = qobject_cast(sender()); + qWarning() << "telegram share error:" << error << process->errorString(); + setError(1 + error); + setErrorText(process->errorString()); + emitResult(); + } + + void jobFinished(int code, QProcess::ExitStatus status) + { + if (status != QProcess::NormalExit) + qWarning() << "Telegram not found or crashing"; + + setError(code); + setOutput( {{ QStringLiteral("url"), QString() }}); + emitResult(); + } + + private: +}; + +class Q_DECL_EXPORT TelegramPlugin : public Purpose::PluginBase +{ + Q_OBJECT + public: + TelegramPlugin(QObject* p, const QVariantList& ) : Purpose::PluginBase(p) {} + + Purpose::Job* createJob() const override + { + return new TelegramJob(nullptr); + } +}; + +K_PLUGIN_FACTORY_WITH_JSON(Telegram, "telegramplugin.json", registerPlugin();) + +#include "telegramplugin.moc" diff --git a/src/plugins/telegram/telegramplugin.json b/src/plugins/telegram/telegramplugin.json new file mode 100644 --- /dev/null +++ b/src/plugins/telegram/telegramplugin.json @@ -0,0 +1,22 @@ +{ + "KPlugin": { + "Authors": [ + { + "Name": "Nicolas Fella", + "Name[x-test]": "xxNicolas Fellaxx" + } + ], + "Category": "Utilities", + "Description": "Send via Telegram", + "Icon": "telegram", + "License": "GPL", + "Name": "Send via Telegram..." + }, + "X-Purpose-Configuration": [ + ], + "X-Purpose-Constraints": [ + ], + "X-Purpose-PluginTypes": [ + "Export" + ] +}