1/*
2 SPDX-FileCopyrightText: 2014 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
3
4 SPDX-License-Identifier: LGPL-2.1-or-later
5*/
6
7#include <KPluginFactory>
8#include <QDebug>
9#include <QJsonArray>
10#include <QProcess>
11#include <QStandardPaths>
12#include <purpose/pluginbase.h>
13
14class KDEConnectJob : public Purpose::Job
15{
16 Q_OBJECT
17public:
18 KDEConnectJob(QObject *parent)
19 : Purpose::Job(parent)
20 {
21 }
22
23 QStringList arrayToList(const QJsonArray &array)
24 {
25 QStringList ret;
26 for (const QJsonValue &val : array) {
27 ret += val.toString();
28 }
29 return ret;
30 }
31
32 void start() override
33 {
34 QProcess *process = new QProcess(this);
35 process->setProgram(QStringLiteral("kdeconnect-cli"));
36 QJsonArray urlsJson = data().value(key: QLatin1String("urls")).toArray();
37 process->setArguments(QStringList(QStringLiteral("--device"))
38 << data().value(key: QLatin1String("device")).toString() << QStringLiteral("--share") << arrayToList(array: urlsJson));
39 connect(sender: process, signal: &QProcess::errorOccurred, context: this, slot: &KDEConnectJob::processError);
40 connect(sender: process, signal: &QProcess::finished, context: this, slot: &KDEConnectJob::jobFinished);
41 connect(sender: process, signal: &QProcess::readyRead, context: this, slot: [process]() {
42 qDebug() << "kdeconnect-cli output:" << process->readAll();
43 });
44
45 process->start();
46 }
47
48 void processError(QProcess::ProcessError error)
49 {
50 QProcess *process = qobject_cast<QProcess *>(object: sender());
51 qWarning() << "kdeconnect share error:" << error << process->errorString();
52 setError(1 + error);
53 setErrorText(process->errorString());
54 emitResult();
55 }
56
57 void jobFinished(int code, QProcess::ExitStatus status)
58 {
59 if (status != QProcess::NormalExit)
60 qWarning() << "kdeconnect-cli crashed";
61
62 setError(code);
63 setOutput({{QStringLiteral("url"), QString()}});
64 emitResult();
65 }
66
67private:
68};
69
70class KDEConnectPlugin : public Purpose::PluginBase
71{
72 Q_OBJECT
73public:
74 using PluginBase::PluginBase;
75 Purpose::Job *createJob() const override
76 {
77 return new KDEConnectJob(nullptr);
78 }
79};
80
81K_PLUGIN_CLASS_WITH_JSON(KDEConnectPlugin, "kdeconnectplugin.json")
82
83#include "kdeconnectplugin.moc"
84

source code of purpose/src/plugins/kdeconnect/kdeconnectplugin.cpp