Warning, file /network/kdeconnect-kde/indicator/deviceindicator.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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     // Clipboard
0067     auto clipboard = addAction(QIcon::fromTheme(QStringLiteral("klipper")), i18n("Send clipboard"));
0068     connect(clipboard, &QAction::triggered, device, [device]() {
0069         ClipboardDbusInterface *clipboardIface = new ClipboardDbusInterface(device->id(), device);
0070         clipboardIface->sendClipboard();
0071         clipboardIface->deleteLater();
0072     });
0073     setWhenAvailable(
0074         device->hasPlugin(QStringLiteral("kdeconnect_clipboard")),
0075         [clipboard](bool available) {
0076             clipboard->setVisible(available);
0077         },
0078         this);
0079 
0080     // Find device
0081     auto findDevice = addAction(QIcon::fromTheme(QStringLiteral("irc-voice")), i18n("Ring device"));
0082     connect(findDevice, &QAction::triggered, device, [device]() {
0083         FindMyPhoneDeviceDbusInterface *iface = new FindMyPhoneDeviceDbusInterface(device->id(), device);
0084         iface->ring();
0085         iface->deleteLater();
0086     });
0087     setWhenAvailable(
0088         device->hasPlugin(QStringLiteral("kdeconnect_findmyphone")),
0089         [findDevice](bool available) {
0090             findDevice->setVisible(available);
0091         },
0092         this);
0093 
0094     // Get a photo
0095     auto getPhoto = addAction(QIcon::fromTheme(QStringLiteral("camera-photo")), i18n("Get a photo"));
0096     connect(getPhoto, &QAction::triggered, this, [device]() {
0097         QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kdeconnect"),
0098                                                           QStringLiteral("/modules/kdeconnect/devices/") + device->id() + QStringLiteral("/photo"),
0099                                                           QStringLiteral("org.kde.kdeconnect.device.photo"),
0100                                                           QStringLiteral("requestPhoto"));
0101         msg.setArguments({QStandardPaths::standardLocations(QStandardPaths::DownloadLocation).first()
0102                           + QDateTime::currentDateTime().toString(QStringLiteral("/dd-MM-yy_hh-mm-ss.png"))});
0103         blockOnReply(QDBusConnection::sessionBus().asyncCall(msg));
0104     });
0105     setWhenAvailable(
0106         device->hasPlugin(QStringLiteral("kdeconnect_photo")),
0107         [getPhoto](bool available) {
0108             getPhoto->setVisible(available);
0109         },
0110         this);
0111 
0112     // Send file
0113     const QString kdeconnectHandlerExecutable = QStandardPaths::findExecutable(QStringLiteral("kdeconnect-handler"), {QCoreApplication::applicationDirPath()});
0114     if (!kdeconnectHandlerExecutable.isEmpty()) {
0115         auto handlerApp = addAction(QIcon::fromTheme(QStringLiteral("document-share")), i18n("Send a file/URL"));
0116         QObject::connect(handlerApp, &QAction::triggered, device, [device, kdeconnectHandlerExecutable]() {
0117             QProcess::startDetached(kdeconnectHandlerExecutable, {QStringLiteral("--device"), device->id()});
0118         });
0119         handlerApp->setVisible(true);
0120     }
0121 
0122     // SMS Messages
0123     const QString kdeconnectsmsExecutable = QStandardPaths::findExecutable(QStringLiteral("kdeconnect-sms"), {QCoreApplication::applicationDirPath()});
0124     if (!kdeconnectsmsExecutable.isEmpty()) {
0125         auto smsapp = addAction(QIcon::fromTheme(QStringLiteral("message-new")), i18n("SMS Messages..."));
0126         QObject::connect(smsapp, &QAction::triggered, device, [device, kdeconnectsmsExecutable]() {
0127             QProcess::startDetached(kdeconnectsmsExecutable, {QStringLiteral("--device"), device->id()});
0128         });
0129         setWhenAvailable(
0130             device->hasPlugin(QStringLiteral("kdeconnect_sms")),
0131             [smsapp](bool available) {
0132                 smsapp->setVisible(available);
0133             },
0134             this);
0135     }
0136 
0137     // Run command
0138     QMenu *remoteCommandsMenu = new QMenu(i18n("Run command"), this);
0139     QAction *menuAction = remoteCommandsMenu->menuAction();
0140     QAction *addCommandAction = remoteCommandsMenu->addAction(QIcon::fromTheme(QStringLiteral("list-add")), i18n("Add commands"));
0141     connect(addCommandAction, &QAction::triggered, m_remoteCommandsInterface, &RemoteCommandsDbusInterface::editCommands);
0142 
0143     addAction(menuAction);
0144     setWhenAvailable(
0145         device->hasPlugin(QStringLiteral("kdeconnect_remotecommands")),
0146         [this, remoteCommandsMenu, menuAction](bool available) {
0147             menuAction->setVisible(available);
0148 
0149             if (!available)
0150                 return;
0151 
0152             const auto cmds = QJsonDocument::fromJson(m_remoteCommandsInterface->commands()).object();
0153 
0154             for (auto it = cmds.constBegin(), itEnd = cmds.constEnd(); it != itEnd; ++it) {
0155                 const QJsonObject cont = it->toObject();
0156                 QString key = it.key();
0157                 QAction *action = remoteCommandsMenu->addAction(cont.value(QStringLiteral("name")).toString());
0158                 connect(action, &QAction::triggered, [this, key] {
0159                     m_remoteCommandsInterface->triggerCommand(key);
0160                 });
0161             }
0162         },
0163         this);
0164 }