File indexing completed on 2024-03-24 04:59:44

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, &DeviceDbusInterface::nameChanged, this, &DeviceIndicator::setText);
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")), i18nc("@action:inmenu play bell sound", "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     // Send file
0095     const QString kdeconnectHandlerExecutable = QStandardPaths::findExecutable(QStringLiteral("kdeconnect-handler"), {QCoreApplication::applicationDirPath()});
0096     if (!kdeconnectHandlerExecutable.isEmpty()) {
0097         auto handlerApp = addAction(QIcon::fromTheme(QStringLiteral("document-share")), i18n("Send a file/URL"));
0098         QObject::connect(handlerApp, &QAction::triggered, device, [device, kdeconnectHandlerExecutable]() {
0099             QProcess::startDetached(kdeconnectHandlerExecutable, {QStringLiteral("--device"), device->id()});
0100         });
0101         handlerApp->setVisible(true);
0102     }
0103 
0104     // SMS Messages
0105     const QString kdeconnectsmsExecutable = QStandardPaths::findExecutable(QStringLiteral("kdeconnect-sms"), {QCoreApplication::applicationDirPath()});
0106     if (!kdeconnectsmsExecutable.isEmpty()) {
0107         auto smsapp = addAction(QIcon::fromTheme(QStringLiteral("message-new")), i18n("SMS Messages..."));
0108         QObject::connect(smsapp, &QAction::triggered, device, [device, kdeconnectsmsExecutable]() {
0109             QProcess::startDetached(kdeconnectsmsExecutable, {QStringLiteral("--device"), device->id()});
0110         });
0111         setWhenAvailable(
0112             device->hasPlugin(QStringLiteral("kdeconnect_sms")),
0113             [smsapp](bool available) {
0114                 smsapp->setVisible(available);
0115             },
0116             this);
0117     }
0118 
0119     // Run command
0120     QMenu *remoteCommandsMenu = new QMenu(i18n("Run command"), this);
0121     QAction *menuAction = remoteCommandsMenu->menuAction();
0122     QAction *addCommandAction = remoteCommandsMenu->addAction(QIcon::fromTheme(QStringLiteral("list-add")), i18n("Add commands"));
0123     connect(addCommandAction, &QAction::triggered, m_remoteCommandsInterface, &RemoteCommandsDbusInterface::editCommands);
0124 
0125     addAction(menuAction);
0126     setWhenAvailable(
0127         device->hasPlugin(QStringLiteral("kdeconnect_remotecommands")),
0128         [this, remoteCommandsMenu, menuAction](bool available) {
0129             menuAction->setVisible(available);
0130 
0131             if (!available)
0132                 return;
0133 
0134             const auto cmds = QJsonDocument::fromJson(m_remoteCommandsInterface->commands()).object();
0135 
0136             for (auto it = cmds.constBegin(), itEnd = cmds.constEnd(); it != itEnd; ++it) {
0137                 const QJsonObject cont = it->toObject();
0138                 QString key = it.key();
0139                 QAction *action = remoteCommandsMenu->addAction(cont.value(QStringLiteral("name")).toString());
0140                 connect(action, &QAction::triggered, [this, key] {
0141                     m_remoteCommandsInterface->triggerCommand(key);
0142                 });
0143             }
0144         },
0145         this);
0146 }
0147 
0148 #include "moc_deviceindicator.cpp"