From 0131710c23e63e93299eb3d7432c7139a91905d0 Mon Sep 17 00:00:00 2001 From: Devin Lin Date: Sat, 12 Feb 2022 23:07:09 -0500 Subject: [PATCH] mmplugin: Use NM autoconnect instead of wwanEnabled for mobile data Add supported function fix fix fix fix fix fix fix fix cleanup fix fix fix fix --- components/mmplugin/signalindicator.cpp | 75 ++++++++++++++++--- components/mmplugin/signalindicator.h | 17 ++++- .../quicksettings/SettingsModel.qml | 13 +++- 3 files changed, 88 insertions(+), 17 deletions(-) diff --git a/components/mmplugin/signalindicator.cpp b/components/mmplugin/signalindicator.cpp index 8b176103..cb985463 100644 --- a/components/mmplugin/signalindicator.cpp +++ b/components/mmplugin/signalindicator.cpp @@ -2,16 +2,13 @@ // SPDX-FileCopyrightText: 2022 Devin Lin // SPDX-License-Identifier: GPL-2.0-or-later +#include #include #include "signalindicator.h" SignalIndicator::SignalIndicator() { - connect(NetworkManager::notifier(), &NetworkManager::Notifier::wwanEnabledChanged, this, [this](bool) { - Q_EMIT wwanEnabledChanged(); - }); - connect(ModemManager::notifier(), &ModemManager::Notifier::modemAdded, this, &SignalIndicator::updateModem); connect(ModemManager::notifier(), &ModemManager::Notifier::modemRemoved, this, &SignalIndicator::updateModem); updateModem(); @@ -43,14 +40,50 @@ bool SignalIndicator::available() const return !ModemManager::modemDevices().isEmpty(); } -bool SignalIndicator::wwanEnabled() const +bool SignalIndicator::mobileDataSupported() const { - return NetworkManager::isWwanEnabled(); + return m_nmModem && m_modemDevice->sim(); } -void SignalIndicator::setWwanEnabled(bool wwanEnabled) +bool SignalIndicator::mobileDataEnabled() const { - NetworkManager::setWwanEnabled(wwanEnabled); + if (!m_nmModem) { + return false; + } + + return m_nmModem->state() == NetworkManager::Device::Activated || m_nmModem->autoconnect(); +} + +void SignalIndicator::setMobileDataEnabled(bool enabled) +{ + if (!m_nmModem) { + return; + } + + if (!enabled) { + m_nmModem->setAutoconnect(false); + + // before disconnecting, we ensure the current active connection is set to autoconnect + for (NetworkManager::Connection::Ptr con : m_nmModem->availableConnections()) { + if (con->uuid() == m_nmModem->activeConnection()->uuid()) { + con->settings()->setAutoconnect(true); + } else { + con->settings()->setAutoconnect(false); + } + } + + m_nmModem->disconnectInterface().waitForFinished(); + } else { + m_nmModem->setAutoconnect(true); + + // activate the connection that is set to autoconnect + for (NetworkManager::Connection::Ptr con : m_nmModem->availableConnections()) { + if (con->settings()->autoconnect()) { + NetworkManager::activateConnection(con->path(), m_nmModem->uni(), ""); + break; + } + } + } } void SignalIndicator::updateModem() @@ -59,11 +92,31 @@ void SignalIndicator::updateModem() qWarning() << "No modems available"; return; } - m_modem = ModemManager::modemDevices()[0]->modemInterface(); - m_3gppModem = ModemManager::modemDevices()[0]->interface(ModemManager::ModemDevice::GsmInterface).objectCast(); - Q_EMIT nameChanged(); + + // we assume that there is a single modem + m_modemDevice = ModemManager::modemDevices()[0]; + m_modem = m_modemDevice->modemInterface(); + m_3gppModem = m_modemDevice->interface(ModemManager::ModemDevice::GsmInterface).objectCast(); + + // find networkmanager modem + for (NetworkManager::Device::Ptr nmDevice : NetworkManager::networkInterfaces()) { + if (nmDevice->udi() == m_modemDevice->uni()) { + m_nmModem = nmDevice.objectCast(); + + connect(m_nmModem.get(), &NetworkManager::Device::autoconnectChanged, this, [this]() { + Q_EMIT mobileDataEnabledChanged(); + }); + connect(m_nmModem.get(), &NetworkManager::Device::stateChanged, this, [this](auto, auto, auto) { + Q_EMIT mobileDataEnabledChanged(); + }); + } + } + connect(m_modem.get(), &ModemManager::Modem::signalQualityChanged, this, &SignalIndicator::strengthChanged); connect(m_3gppModem.get(), &ModemManager::Modem3gpp::operatorNameChanged, this, &SignalIndicator::nameChanged); connect(m_modem.get(), &ModemManager::Modem::unlockRequiredChanged, this, &SignalIndicator::simLockedChanged); + + Q_EMIT mobileDataSupportedChanged(); + Q_EMIT nameChanged(); Q_EMIT availableChanged(); } diff --git a/components/mmplugin/signalindicator.h b/components/mmplugin/signalindicator.h index f2d1d2d7..a522530b 100644 --- a/components/mmplugin/signalindicator.h +++ b/components/mmplugin/signalindicator.h @@ -7,6 +7,9 @@ #include #include +#include +#include + #include // We make the assumption that there is only one modem. @@ -18,7 +21,8 @@ class SignalIndicator : public QObject Q_PROPERTY(QString name READ name NOTIFY nameChanged) Q_PROPERTY(bool simLocked READ simLocked NOTIFY simLockedChanged) Q_PROPERTY(bool available READ available NOTIFY availableChanged) - Q_PROPERTY(bool wwanEnabled READ wwanEnabled WRITE setWwanEnabled NOTIFY wwanEnabledChanged) + Q_PROPERTY(bool mobileDataSupported READ mobileDataSupported NOTIFY mobileDataSupportedChanged) + Q_PROPERTY(bool mobileDataEnabled READ mobileDataEnabled WRITE setMobileDataEnabled NOTIFY mobileDataEnabledChanged) public: SignalIndicator(); @@ -27,19 +31,24 @@ public: QString name() const; bool simLocked() const; bool available() const; - bool wwanEnabled() const; + bool mobileDataSupported() const; + bool mobileDataEnabled() const; - void setWwanEnabled(bool wwanEnabled); + void setMobileDataEnabled(bool enabled); Q_SIGNALS: void strengthChanged(); void nameChanged(); void simLockedChanged(); void availableChanged(); - void wwanEnabledChanged(); + void mobileDataSupportedChanged(); + void mobileDataEnabledChanged(); private: + NetworkManager::ModemDevice::Ptr m_nmModem; + ModemManager::ModemDevice::Ptr m_modemDevice; ModemManager::Modem::Ptr m_modem; ModemManager::Modem3gpp::Ptr m_3gppModem; + void updateModem(); }; diff --git a/components/mobileshell/qml/actiondrawer/quicksettings/SettingsModel.qml b/components/mobileshell/qml/actiondrawer/quicksettings/SettingsModel.qml index 4f853267..0999f5b4 100644 --- a/components/mobileshell/qml/actiondrawer/quicksettings/SettingsModel.qml +++ b/components/mobileshell/qml/actiondrawer/quicksettings/SettingsModel.qml @@ -32,6 +32,7 @@ MobileShell.QuickSettingsModel { enabled: false settingsCommand: "plasma-open-settings" } + MobileShell.QuickSetting { PlasmaNM.Handler { id: nmHandler @@ -49,6 +50,7 @@ MobileShell.QuickSettingsModel { } enabled: enabledConnections.wirelessEnabled } + MobileShell.QuickSetting { text: i18n("Bluetooth") icon: "network-bluetooth" @@ -64,15 +66,17 @@ MobileShell.QuickSettingsModel { } enabled: BluezQt.Manager.bluetoothOperational } + MobileShell.QuickSetting { text: i18n("Mobile Data") icon: "network-modem" settingsCommand: "plasma-open-settings kcm_mobile_broadband" - enabled: PlasmaMM.SignalIndicator.wwanEnabled + enabled: PlasmaMM.SignalIndicator.mobileDataEnabled function toggle() { - PlasmaMM.SignalIndicator.wwanEnabled = !PlasmaMM.SignalIndicator.wwanEnabled + PlasmaMM.SignalIndicator.mobileDataEnabled = !PlasmaMM.SignalIndicator.mobileDataEnabled } } + MobileShell.QuickSetting { text: i18n("Flashlight") icon: "flashlight-on" @@ -81,11 +85,13 @@ MobileShell.QuickSettingsModel { MobileShell.ShellUtil.toggleTorch() } } + MobileShell.QuickSetting { text: i18n("Location") icon: "gps" enabled: false } + MobileShell.QuickSetting { text: i18n("Screenshot") status: i18n("Tap to screenshot") @@ -113,6 +119,7 @@ MobileShell.QuickSettingsModel { onTriggered: MobileShell.ShellUtil.takeScreenshot() } } + MobileShell.QuickSetting { text: i18n("Auto-rotate") icon: "rotation-allowed" @@ -122,6 +129,7 @@ MobileShell.QuickSettingsModel { MobileShell.ShellUtil.autoRotateEnabled = !enabled } } + MobileShell.QuickSetting { text: i18n("Battery") status: i18n("%1%", MobileShell.BatteryProvider.percent) @@ -129,6 +137,7 @@ MobileShell.QuickSettingsModel { enabled: false settingsCommand: "plasma-open-settings kcm_mobile_power" } + MobileShell.QuickSetting { text: i18n("Sound") icon: "audio-speakers-symbolic"