File indexing completed on 2024-02-25 05:45:28

0001 // SPDX-FileCopyrightText: 2023 Yari Polla <skilvingr@gmail.com>
0002 // SPDX-License-Identifier: LGPL-2.0-or-later
0003 
0004 #include "wirelessstatus.h"
0005 
0006 #include <NetworkManagerQt/ActiveConnection>
0007 #include <NetworkManagerQt/Manager>
0008 #include <NetworkManagerQt/WirelessSetting>
0009 
0010 WirelessStatus::WirelessStatus(QObject *parent)
0011     : QObject(parent)
0012 {
0013     connect(NetworkManager::notifier(), &NetworkManager::Notifier::activeConnectionsChanged, this, &WirelessStatus::activeConnectionsChanged);
0014 
0015     activeConnectionsChanged();
0016 }
0017 
0018 WirelessStatus::~WirelessStatus() = default;
0019 
0020 QString WirelessStatus::wifiSSID() const
0021 {
0022     return m_wifiSSID;
0023 }
0024 
0025 QString WirelessStatus::hotspotSSID() const
0026 {
0027     return m_hotspotSSID;
0028 }
0029 
0030 void WirelessStatus::activeConnectionsChanged()
0031 {
0032     for (const NetworkManager::ActiveConnection::Ptr &active : NetworkManager::activeConnections()) {
0033         connect(active.data(), &NetworkManager::ActiveConnection::stateChanged, this, &WirelessStatus::stateChanged);
0034 
0035         Q_EMIT active.data()->stateChanged(active->state());
0036     }
0037 }
0038 
0039 void WirelessStatus::stateChanged()
0040 {
0041     auto connection = qobject_cast<NetworkManager::ActiveConnection *>(QObject::sender());
0042 
0043     if (connection->type() == NetworkManager::ConnectionSettings::ConnectionType::Wireless) {
0044         NetworkManager::WirelessSetting::Ptr wifiSetting =
0045             connection->connection()->settings()->setting(NetworkManager::Setting::Wireless).dynamicCast<NetworkManager::WirelessSetting>();
0046 
0047         bool connected = connection->state() == NetworkManager::ActiveConnection::Activated;
0048 
0049         if (wifiSetting->mode() == NetworkManager::WirelessSetting::Ap || wifiSetting->mode() == NetworkManager::WirelessSetting::Adhoc) {
0050             if (connected) {
0051                 m_hotspotSSID = connection->connection()->name();
0052             } else {
0053                 m_hotspotSSID = "";
0054             }
0055 
0056             Q_EMIT hotspotSSIDChanged(m_hotspotSSID);
0057         } else {
0058             if (connected) {
0059                 m_wifiSSID = connection->connection()->name();
0060             } else {
0061                 m_wifiSSID = "";
0062             }
0063 
0064             Q_EMIT wifiSSIDChanged(m_wifiSSID);
0065         }
0066     }
0067 }
0068 
0069 #include "moc_wirelessstatus.cpp"