From fe3d79ccbc9281401a1a28f8a6335e4bf3618116 Mon Sep 17 00:00:00 2001 From: Piyush Aggarwal Date: Tue, 18 May 2021 14:04:43 +0530 Subject: [PATCH] indicator: add connectivity action --- indicator/CMakeLists.txt | 1 + indicator/deviceindicator.cpp | 6 +++ .../systray_actions/connectivity_action.cpp | 46 +++++++++++++++++++ .../systray_actions/connectivity_action.h | 35 ++++++++++++++ indicator/systray_actions/systray_actions.h | 1 + 5 files changed, 89 insertions(+) create mode 100644 indicator/systray_actions/connectivity_action.cpp create mode 100644 indicator/systray_actions/connectivity_action.h diff --git a/indicator/CMakeLists.txt b/indicator/CMakeLists.txt index 74af8c637..f0aa178e9 100644 --- a/indicator/CMakeLists.txt +++ b/indicator/CMakeLists.txt @@ -14,6 +14,7 @@ set(indicator_SRCS deviceindicator.cpp ${kdeconnect_custom_icons_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/systray_actions/battery_action.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/systray_actions/connectivity_action.cpp ) include(ECMAddAppIcon) diff --git a/indicator/deviceindicator.cpp b/indicator/deviceindicator.cpp index 36a2a2ef1..3b63cdf27 100644 --- a/indicator/deviceindicator.cpp +++ b/indicator/deviceindicator.cpp @@ -30,6 +30,12 @@ DeviceIndicator::DeviceIndicator(DeviceDbusInterface* device) [battery](bool available) { battery->setVisible(available); } , this); + auto connectivity = new ConnectivityAction(device); + addAction(connectivity); + setWhenAvailable(device->hasPlugin(QStringLiteral("kdeconnect_connectivity_report")), + [connectivity](bool available) { connectivity->setVisible(available); } + , this); + // Browse device filesystem auto browse = addAction(QIcon::fromTheme(QStringLiteral("document-open-folder")), i18n("Browse device")); connect(browse, &QAction::triggered, device, [device](){ diff --git a/indicator/systray_actions/connectivity_action.cpp b/indicator/systray_actions/connectivity_action.cpp new file mode 100644 index 000000000..bc54aad82 --- /dev/null +++ b/indicator/systray_actions/connectivity_action.cpp @@ -0,0 +1,46 @@ +/* + * SPDX-FileCopyrightText: 2016 Aleix Pol Gonzalez + * SPDX-FileCopyrightText: 2020 Piyush Aggarwal + * + * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL + */ + +#include "connectivity_action.h" + +ConnectivityAction::ConnectivityAction(DeviceDbusInterface* device) + : QAction(nullptr) + , m_connectivityiface(device->id()) +{ + setCellularNetworkStrength(m_connectivityiface.cellularNetworkStrength()); + setCellularNetworkType(m_connectivityiface.cellularNetworkType()); + + connect(&m_connectivityiface, &ConnectivityReportDbusInterface::refreshedProxy, this, [this]{ + setCellularNetworkStrength(m_connectivityiface.cellularNetworkStrength()); + setCellularNetworkType(m_connectivityiface.cellularNetworkType()); + }); + + setIcon(QIcon::fromTheme(QStringLiteral("network-wireless"))); + + ConnectivityAction::update(); +} + +void ConnectivityAction::update() +{ + if (m_cellularNetworkStrength < 0) { + setText(i18nc("The fallback text to display in case the remote device does not have a cellular connection", "No Cellular Connectivity")); + } else { + setText(i18nc("Display the cellular connection type and an approximate percentage of signal strength","%1 | ~%2%", m_cellularNetworkType, m_cellularNetworkStrength * 25)); + } +} + +void ConnectivityAction::setCellularNetworkStrength(int cellularNetworkStrength) +{ + m_cellularNetworkStrength = cellularNetworkStrength; + update(); +} + +void ConnectivityAction::setCellularNetworkType(QString cellularNetworkType) +{ + m_cellularNetworkType = cellularNetworkType; + update(); +} diff --git a/indicator/systray_actions/connectivity_action.h b/indicator/systray_actions/connectivity_action.h new file mode 100644 index 000000000..956a1fdea --- /dev/null +++ b/indicator/systray_actions/connectivity_action.h @@ -0,0 +1,35 @@ +/* + * SPDX-FileCopyrightText: 2016 Aleix Pol Gonzalez + * SPDX-FileCopyrightText: 2020 Piyush Aggarwal + * + * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL + */ + +#ifndef CONNECTIVITYACTION_H +#define CONNECTIVITYACTION_H + +#include +#include +#include + +#include "interfaces/dbusinterfaces.h" + +#include + +class ConnectivityAction : public QAction +{ +Q_OBJECT +public: + ConnectivityAction(DeviceDbusInterface* device); + void update(); +private Q_SLOTS: + void setCellularNetworkStrength(int cellularNetworkStrength); + void setCellularNetworkType(QString cellularNetworkType); + +private: + ConnectivityReportDbusInterface m_connectivityiface; + QString m_cellularNetworkType; + int m_cellularNetworkStrength = -1; +}; + +#endif // CONNECTIVITYACTION_H \ No newline at end of file diff --git a/indicator/systray_actions/systray_actions.h b/indicator/systray_actions/systray_actions.h index 6fd285ee2..2f1a8878d 100644 --- a/indicator/systray_actions/systray_actions.h +++ b/indicator/systray_actions/systray_actions.h @@ -1,4 +1,5 @@ #ifndef SYSTRAY_ACTIONS_H #define SYSTRAY_ACTIONS_H #include +#include #endif // SYSTRAY_ACTIONS_H