File indexing completed on 2025-01-19 04:46:50

0001 /*
0002    SPDX-FileCopyrightText: 2017 Volker Krause <vkrause@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "itinerarykdeconnecthandler.h"
0008 
0009 #include <QDBusConnection>
0010 #include <QDBusInterface>
0011 #include <QDBusMessage>
0012 #include <QDBusReply>
0013 #include <QList>
0014 #include <QUrl>
0015 
0016 ItineraryKDEConnectHandler::ItineraryKDEConnectHandler(QObject *parent)
0017     : QObject(parent)
0018 {
0019 }
0020 
0021 QList<ItineraryKDEConnectHandler::Device> ItineraryKDEConnectHandler::devices() const
0022 {
0023     // TODO we might want to do all this asynchronously by watching change signals and cache the device list
0024 
0025     auto msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"),
0026                                               QStringLiteral("/modules/kdeconnect"),
0027                                               QStringLiteral("org.kde.kdeconnect.daemon"),
0028                                               QStringLiteral("devices"));
0029     msg.setArguments({true, true});
0030     QDBusPendingReply<QStringList> reply = QDBusConnection::sessionBus().asyncCall(msg);
0031     reply.waitForFinished();
0032 
0033     if (!reply.isValid()) {
0034         return {};
0035     }
0036 
0037     QList<Device> devices;
0038     const auto values = reply.value();
0039     for (const QString &deviceId : values) {
0040         QDBusInterface deviceIface(QStringLiteral("org.kde.kdeconnect"),
0041                                    QStringLiteral("/modules/kdeconnect/devices/") + deviceId,
0042                                    QStringLiteral("org.kde.kdeconnect.device"));
0043         QDBusReply<bool> pluginReply = deviceIface.call(QStringLiteral("hasPlugin"), QLatin1StringView("kdeconnect_share"));
0044 
0045         if (pluginReply.value()) {
0046             devices.push_back({deviceId, deviceIface.property("name").toString()});
0047         }
0048     }
0049 
0050     return devices;
0051 }
0052 
0053 void ItineraryKDEConnectHandler::sendToDevice(const QString &fileName, const QString &deviceId)
0054 {
0055     const QString method = QStringLiteral("openFile");
0056 
0057     QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"),
0058                                                       QStringLiteral("/modules/kdeconnect/devices/") + deviceId + QStringLiteral("/share"),
0059                                                       QStringLiteral("org.kde.kdeconnect.device.share"),
0060                                                       method);
0061     msg.setArguments({QUrl::fromLocalFile(fileName).toString()});
0062 
0063     QDBusConnection::sessionBus().send(msg);
0064 }
0065 
0066 #include "moc_itinerarykdeconnecthandler.cpp"