File indexing completed on 2023-05-30 09:17:23

0001 /*
0002  * SPDX-FileCopyrightText: 2016 Aleix Pol Gonzalez <aleixpol@kde.org>
0003  * SPDX-FileCopyrightText: 2020 Piyush Aggarwal <piyushaggarwal002@gmail.com>
0004  *
0005  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006  */
0007 
0008 #include "deviceindicator.h"
0009 #include <KLocalizedString>
0010 #include <QDateTime>
0011 #include <QFileDialog>
0012 #include <QStandardPaths>
0013 
0014 #include "interfaces/dbusinterfaces.h"
0015 
0016 #include <dbushelper.h>
0017 #include <dbushelpers.h>
0018 #include <systray_actions.h>
0019 
0020 DeviceIndicator::DeviceIndicator(DeviceDbusInterface *device)
0021     : QMenu(device->name(), nullptr)
0022     , m_device(device)
0023     , m_remoteCommandsInterface(new RemoteCommandsDbusInterface(m_device->id()))
0024 {
0025     setIcon(QIcon::fromTheme(device->iconName()));
0026 
0027     connect(device, SIGNAL(nameChanged(QString)), this, SLOT(setText(QString)));
0028 
0029     // Battery status
0030     auto battery = new BatteryAction(device);
0031     addAction(battery);
0032     setWhenAvailable(
0033         device->hasPlugin(QStringLiteral("kdeconnect_battery")),
0034         [battery](bool available) {
0035             battery->setVisible(available);
0036             battery->setDisabled(available);
0037         },
0038         this);
0039 
0040     auto connectivity = new ConnectivityAction(device);
0041     addAction(connectivity);
0042     setWhenAvailable(
0043         device->hasPlugin(QStringLiteral("kdeconnect_connectivity_report")),
0044         [connectivity](bool available) {
0045             connectivity->setVisible(available);
0046             connectivity->setDisabled(available);
0047         },
0048         this);
0049 
0050     this->addSeparator();
0051 
0052     // Browse device filesystem
0053     auto browse = addAction(QIcon::fromTheme(QStringLiteral("document-open-folder")), i18n("Browse device"));
0054     connect(browse, &QAction::triggered, device, [device]() {
0055         SftpDbusInterface *sftpIface = new SftpDbusInterface(device->id(), device);
0056         sftpIface->startBrowsing();
0057         sftpIface->deleteLater();
0058     });
0059     setWhenAvailable(
0060         device->hasPlugin(QStringLiteral("kdeconnect_sftp")),
0061         [browse](bool available) {
0062             browse->setVisible(available);
0063         },
0064         this);
0065 
0066     // Find device
0067     auto findDevice = addAction(QIcon::fromTheme(QStringLiteral("irc-voice")), i18n("Ring device"));
0068     connect(findDevice, &QAction::triggered, device, [device]() {
0069         FindMyPhoneDeviceDbusInterface *iface = new FindMyPhoneDeviceDbusInterface(device->id(), device);
0070         iface->ring();
0071         iface->deleteLater();
0072     });
0073     setWhenAvailable(
0074         device->hasPlugin(QStringLiteral("kdeconnect_findmyphone")),
0075         [findDevice](bool available) {
0076             findDevice->setVisible(available);
0077         },
0078         this);
0079 
0080     // Get a photo
0081     auto getPhoto = addAction(QIcon::fromTheme(QStringLiteral("camera-photo")), i18n("Get a photo"));
0082     connect(getPhoto, &QAction::triggered, this, [device]() {
0083         QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"),
0084                                                           QStringLiteral("/modules/kdeconnect/devices/") + device->id() + QStringLiteral("/photo"),
0085                                                           QStringLiteral("org.kde.kdeconnect.device.photo"),
0086                                                           QStringLiteral("requestPhoto"));
0087         msg.setArguments({QStandardPaths::standardLocations(QStandardPaths::DownloadLocation).first()
0088                           + QDateTime::currentDateTime().toString(QStringLiteral("/dd-MM-yy_hh-mm-ss.png"))});
0089         blockOnReply(QDBusConnection::sessionBus().asyncCall(msg));
0090     });
0091     setWhenAvailable(
0092         device->hasPlugin(QStringLiteral("kdeconnect_photo")),
0093         [getPhoto](bool available) {
0094             getPhoto->setVisible(available);
0095         },
0096         this);
0097 
0098     // Send file
0099     const QString kdeconnectHandlerExecutable = QStandardPaths::findExecutable(QStringLiteral("kdeconnect-handler"), {QCoreApplication::applicationDirPath()});
0100     if (!kdeconnectHandlerExecutable.isEmpty()) {
0101         auto handlerApp = addAction(QIcon::fromTheme(QStringLiteral("document-share")), i18n("Send a file/URL"));
0102         QObject::connect(handlerApp, &QAction::triggered, device, [device, kdeconnectHandlerExecutable]() {
0103             QProcess::startDetached(kdeconnectHandlerExecutable, {QStringLiteral("--device"), device->id()});
0104         });
0105         handlerApp->setVisible(true);
0106     }
0107 
0108     // SMS Messages
0109     const QString kdeconnectsmsExecutable = QStandardPaths::findExecutable(QStringLiteral("kdeconnect-sms"), {QCoreApplication::applicationDirPath()});
0110     if (!kdeconnectsmsExecutable.isEmpty()) {
0111         auto smsapp = addAction(QIcon::fromTheme(QStringLiteral("message-new")), i18n("SMS Messages..."));
0112         QObject::connect(smsapp, &QAction::triggered, device, [device, kdeconnectsmsExecutable]() {
0113             QProcess::startDetached(kdeconnectsmsExecutable, {QStringLiteral("--device"), device->id()});
0114         });
0115         setWhenAvailable(
0116             device->hasPlugin(QStringLiteral("kdeconnect_sms")),
0117             [smsapp](bool available) {
0118                 smsapp->setVisible(available);
0119             },
0120             this);
0121     }
0122 
0123     // Run command
0124     QMenu *remoteCommandsMenu = new QMenu(i18n("Run command"), this);
0125     QAction *menuAction = remoteCommandsMenu->menuAction();
0126     QAction *addCommandAction = remoteCommandsMenu->addAction(QIcon::fromTheme(QStringLiteral("list-add")), i18n("Add commands"));
0127     connect(addCommandAction, &QAction::triggered, m_remoteCommandsInterface, &RemoteCommandsDbusInterface::editCommands);
0128 
0129     addAction(menuAction);
0130     setWhenAvailable(
0131         device->hasPlugin(QStringLiteral("kdeconnect_remotecommands")),
0132         [this, remoteCommandsMenu, menuAction](bool available) {
0133             menuAction->setVisible(available);
0134 
0135             if (!available)
0136                 return;
0137 
0138             const auto cmds = QJsonDocument::fromJson(m_remoteCommandsInterface->commands()).object();
0139 
0140             for (auto it = cmds.constBegin(), itEnd = cmds.constEnd(); it != itEnd; ++it) {
0141                 const QJsonObject cont = it->toObject();
0142                 QString key = it.key();
0143                 QAction *action = remoteCommandsMenu->addAction(cont.value(QStringLiteral("name")).toString());
0144                 connect(action, &QAction::triggered, [this, key] {
0145                     m_remoteCommandsInterface->triggerCommand(key);
0146                 });
0147             }
0148         },
0149         this);
0150 }