| 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 | |
| 14 | class BluetoothJob : public Purpose::Job |
| 15 | { |
| 16 | Q_OBJECT |
| 17 | public: |
| 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(key: QLatin1String("urls" )).toArray(); |
| 28 | |
| 29 | QStringList args{QStringLiteral("-u" ), data().value(key: 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(sender: process, signal: &QProcess::errorOccurred, context: this, slot: &BluetoothJob::processError); |
| 41 | connect(sender: process, signal: &QProcess::finished, context: this, slot: &BluetoothJob::jobFinished); |
| 42 | connect(sender: process, signal: &QProcess::readyRead, context: this, slot: [process]() { |
| 43 | qDebug() << "bluedevil-sendfile output:" << process->readAll(); |
| 44 | }); |
| 45 | |
| 46 | process->start(); |
| 47 | } |
| 48 | |
| 49 | void processError(QProcess::ProcessError error) |
| 50 | { |
| 51 | QProcess *process = qobject_cast<QProcess *>(object: sender()); |
| 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 | { |
| 60 | if (status != QProcess::NormalExit) { |
| 61 | qWarning() << "bluedevil-sendfile crashed" ; |
| 62 | } |
| 63 | |
| 64 | setError(code); |
| 65 | setOutput({{QStringLiteral("url" ), QString()}}); |
| 66 | emitResult(); |
| 67 | } |
| 68 | |
| 69 | private: |
| 70 | }; |
| 71 | |
| 72 | class BluetoothPlugin : public Purpose::PluginBase |
| 73 | { |
| 74 | Q_OBJECT |
| 75 | public: |
| 76 | using PluginBase::PluginBase; |
| 77 | Purpose::Job *createJob() const override |
| 78 | { |
| 79 | return new BluetoothJob(nullptr); |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | K_PLUGIN_CLASS_WITH_JSON(BluetoothPlugin, "bluetoothplugin.json" ) |
| 84 | |
| 85 | #include "bluetoothplugin.moc" |
| 86 | |