Purpose

kdeconnectplugin.cpp
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{
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(QLatin1String("urls")).toArray();
37 process->setArguments(QStringList(QStringLiteral("--device"))
38 << data().value(QLatin1String("device")).toString() << QStringLiteral("--share") << arrayToList(urlsJson));
39 connect(process, &QProcess::errorOccurred, this, &KDEConnectJob::processError);
40 connect(process, &QProcess::finished, this, &KDEConnectJob::jobFinished);
41 connect(process, &QProcess::readyRead, this, [process]() {
42 qDebug() << "kdeconnect-cli output:" << process->readAll();
43 });
44
45 process->start();
46 }
47
48 void processError(QProcess::ProcessError error)
49 {
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 {
60 qWarning() << "kdeconnect-cli crashed";
61 }
62
63 setError(code);
64 setOutput({{QStringLiteral("url"), QString()}});
65 emitResult();
66 }
67
68private:
69};
70
71class KDEConnectPlugin : public Purpose::PluginBase
72{
74public:
75 using PluginBase::PluginBase;
76 Purpose::Job *createJob() const override
77 {
78 return new KDEConnectJob(nullptr);
79 }
80};
81
82K_PLUGIN_CLASS_WITH_JSON(KDEConnectPlugin, "kdeconnectplugin.json")
83
84#include "kdeconnectplugin.moc"
void setErrorText(const QString &errorText)
void emitResult()
int error() const
void setError(int errorCode)
#define K_PLUGIN_CLASS_WITH_JSON(classname, jsonFile)
Job that will actually perform the sharing.
Definition job.h:34
QJsonObject data
Represents the data the job will have available to perform its task.
Definition job.h:39
Base class to implement by plugins.
Definition pluginbase.h:29
Q_SCRIPTABLE CaptureState status()
char * toString(const EngineQuery &query)
QString errorString() const const
QByteArray readAll()
void readyRead()
QJsonValue value(QLatin1StringView key) const const
QJsonArray toArray() const const
Q_OBJECTQ_OBJECT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
T qobject_cast(QObject *object)
QObject * sender() const const
void errorOccurred(QProcess::ProcessError error)
void finished(int exitCode, QProcess::ExitStatus exitStatus)
void setArguments(const QStringList &arguments)
void setProgram(const QString &program)
void start(OpenMode mode)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:03:06 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.