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
14#include <QDBusConnection>
15#include <QDBusMessage>
16#include <QDBusPendingCallWatcher>
17#include <QDBusPendingReply>
18
19using namespace Qt::StringLiterals;
20
21class KDEConnectJob : public Purpose::Job
22{
24public:
25 KDEConnectJob(QObject *parent)
26 : Purpose::Job(parent)
27 {
28 }
29
30 QStringList arrayToList(const QJsonArray &array)
31 {
32 QStringList ret;
33 for (const QJsonValue &val : array) {
34 ret += val.toString();
35 }
36 return ret;
37 }
38
39 void start() override
40 {
41 const QString deviceId = data().value(QLatin1String("device")).toString();
42 QJsonArray urlsJson = data().value(QLatin1String("urls")).toArray();
43
44 QDBusMessage msg = QDBusMessage::createMethodCall(u"org.kde.kdeconnect"_s,
45 "/modules/kdeconnect/devices/"_L1 + deviceId + "/share"_L1,
46 u"org.kde.kdeconnect.device.share"_s,
47 u"shareUrls"_s);
48 msg.setArguments({arrayToList(urlsJson)});
49
51
53 connect(watcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *watcher) {
54 QDBusPendingReply<> reply = *watcher;
55 if (reply.isError()) {
56 qWarning() << "kdeconnect share error:" << reply.error().message();
57 setError(2);
58 setErrorText(reply.error().message());
59 emitResult();
60 } else {
61 setError(0);
62 setOutput({{QStringLiteral("url"), QString()}});
63 emitResult();
64 }
65 });
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()
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
QDBusPendingCall asyncCall(const QDBusMessage &message, int timeout) const const
QDBusConnection sessionBus()
QString message() const const
QDBusMessage createMethodCall(const QString &service, const QString &path, const QString &interface, const QString &method)
void setArguments(const QList< QVariant > &arguments)
void finished(QDBusPendingCallWatcher *self)
QDBusError error() const const
bool isError() const const
QJsonValue value(QLatin1StringView key) const const
QJsonArray toArray() const const
QString toString() const const
Q_OBJECTQ_OBJECT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 3 2025 11:49:11 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.