Purpose

bluetoothplugin.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 <QUrl>
12#include <purpose/pluginbase.h>
13
14class BluetoothJob : public Purpose::Job
15{
17public:
18 BluetoothJob(QObject *parent)
19 : Purpose::Job(parent)
20 {
21 }
22
23 void start() override
24 {
25 QProcess *process = new QProcess(this);
26 process->setProgram(QStringLiteral("bluedevil-sendfile"));
27 const QJsonArray urlsJson = data().value(QLatin1String("urls")).toArray();
28
29 QStringList args{QStringLiteral("-u"), data().value(QLatin1String("device")).toString()};
30
31 for (const QJsonValue &val : urlsJson) {
32 const QUrl url(val.toString());
33 if (url.isLocalFile()) {
34 args << QStringLiteral("-f") << url.toLocalFile();
35 }
36 }
37
38 process->setArguments(args);
39
40 connect(process, &QProcess::errorOccurred, this, &BluetoothJob::processError);
41 connect(process, &QProcess::finished, this, &BluetoothJob::jobFinished);
42 connect(process, &QProcess::readyRead, this, [process]() {
43 qDebug() << "bluedevil-sendfile output:" << process->readAll();
44 });
45
46 process->start();
47 }
48
49 void processError(QProcess::ProcessError error)
50 {
52 qWarning() << "bluetooth share error:" << error << process->errorString();
53 setError(1 + error);
54 setErrorText(process->errorString());
55 emitResult();
56 }
57
58 void jobFinished(int code, QProcess::ExitStatus status)
59 {
61 qWarning() << "bluedevil-sendfile crashed";
62 }
63
64 setError(code);
65 setOutput({{QStringLiteral("url"), QString()}});
66 emitResult();
67 }
68
69private:
70};
71
72class BluetoothPlugin : public Purpose::PluginBase
73{
75public:
76 using PluginBase::PluginBase;
77 Purpose::Job *createJob() const override
78 {
79 return new BluetoothJob(nullptr);
80 }
81};
82
83K_PLUGIN_CLASS_WITH_JSON(BluetoothPlugin, "bluetoothplugin.json")
84
85#include "bluetoothplugin.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()
QString errorString() const const
QByteArray readAll()
void readyRead()
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
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 Mon Nov 18 2024 12:09:06 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.