File indexing completed on 2024-05-12 07:56:08

0001 /*
0002     SPDX-FileCopyrightText: 2009, 2011 Will Stephenson <wstephenson@kde.org>
0003     SPDX-FileCopyrightText: 2013 Jan Grulich <jgrulich@redhat.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include "wirelessnetwork.h"
0009 #include "wirelessnetwork_p.h"
0010 
0011 #include "manager.h"
0012 #include "wirelessdevice.h"
0013 
0014 NetworkManager::WirelessNetworkPrivate::WirelessNetworkPrivate(WirelessNetwork *q, WirelessDevice *device)
0015     : q_ptr(q)
0016     , wirelessNetworkInterface(device)
0017 {
0018     QObject::connect(device, SIGNAL(accessPointAppeared(QString)), q, SLOT(accessPointAppeared(QString)));
0019     QObject::connect(device, SIGNAL(accessPointDisappeared(QString)), q, SLOT(accessPointDisappeared(QString)));
0020     QObject::connect(device, SIGNAL(activeAccessPointChanged(QString)), q, SLOT(updateStrength()));
0021 }
0022 
0023 NetworkManager::WirelessNetworkPrivate::~WirelessNetworkPrivate()
0024 {
0025 }
0026 
0027 void NetworkManager::WirelessNetworkPrivate::addAccessPointInternal(const NetworkManager::AccessPoint::Ptr &accessPoint)
0028 {
0029     Q_Q(WirelessNetwork);
0030 
0031     QObject::connect(accessPoint.data(), SIGNAL(signalStrengthChanged(int)), q, SLOT(updateStrength()));
0032     aps.insert(accessPoint->uni(), accessPoint);
0033     updateStrength();
0034 }
0035 
0036 void NetworkManager::WirelessNetworkPrivate::accessPointAppeared(const QString &uni)
0037 {
0038     if (!aps.contains(uni) && wirelessNetworkInterface) {
0039         NetworkManager::AccessPoint::Ptr accessPoint = wirelessNetworkInterface->findAccessPoint(uni);
0040         if (accessPoint && accessPoint->ssid() == ssid) {
0041             addAccessPointInternal(accessPoint);
0042         }
0043     }
0044 }
0045 
0046 void NetworkManager::WirelessNetworkPrivate::accessPointDisappeared(const QString &uni)
0047 {
0048     Q_Q(WirelessNetwork);
0049     aps.remove(uni);
0050     if (aps.isEmpty()) {
0051         Q_EMIT q->disappeared(ssid);
0052     } else {
0053         updateStrength();
0054     }
0055 }
0056 
0057 void NetworkManager::WirelessNetworkPrivate::updateStrength()
0058 {
0059     Q_Q(WirelessNetwork);
0060 
0061     int selectedStrength = -1;
0062     NetworkManager::AccessPoint::Ptr selectedAp;
0063 
0064     if (!wirelessNetworkInterface) {
0065         return;
0066     }
0067 
0068     NetworkManager::AccessPoint::Ptr activeAp = wirelessNetworkInterface->activeAccessPoint();
0069     if (activeAp && activeAp->ssid() == ssid) {
0070         // If the network has an active access point, use it as the referenceAp
0071         selectedStrength = activeAp->signalStrength();
0072         selectedAp = activeAp;
0073     } else {
0074         // Otherwise, choose the access point with the strongest signal
0075         for (const NetworkManager::AccessPoint::Ptr &iface : std::as_const(aps)) {
0076             if (selectedStrength <= iface->signalStrength()) {
0077                 selectedStrength = iface->signalStrength();
0078                 selectedAp = iface;
0079             }
0080         }
0081     }
0082 
0083     if (selectedStrength != strength) {
0084         strength = selectedStrength;
0085         Q_EMIT q->signalStrengthChanged(strength);
0086     }
0087 
0088     if (selectedAp && referenceAp != selectedAp) {
0089         referenceAp = selectedAp;
0090         Q_EMIT q->referenceAccessPointChanged(referenceAp->uni());
0091     }
0092     // TODO: update the networks delayed
0093     // kDebug() << "update strength" << ssid << strength;
0094 }
0095 
0096 NetworkManager::WirelessNetwork::WirelessNetwork(const AccessPoint::Ptr &accessPoint, WirelessDevice *device)
0097     : d_ptr(new WirelessNetworkPrivate(this, device))
0098 {
0099     Q_D(WirelessNetwork);
0100 
0101     d->strength = -1;
0102     d->ssid = accessPoint->ssid();
0103     d->addAccessPointInternal(accessPoint);
0104 }
0105 
0106 NetworkManager::WirelessNetwork::~WirelessNetwork()
0107 {
0108     delete d_ptr;
0109 }
0110 
0111 QString NetworkManager::WirelessNetwork::ssid() const
0112 {
0113     Q_D(const WirelessNetwork);
0114     return d->ssid;
0115 }
0116 
0117 int NetworkManager::WirelessNetwork::signalStrength() const
0118 {
0119     Q_D(const WirelessNetwork);
0120     return d->strength;
0121 }
0122 
0123 NetworkManager::AccessPoint::Ptr NetworkManager::WirelessNetwork::referenceAccessPoint() const
0124 {
0125     Q_D(const WirelessNetwork);
0126     return d->referenceAp;
0127 }
0128 
0129 NetworkManager::AccessPoint::List NetworkManager::WirelessNetwork::accessPoints() const
0130 {
0131     Q_D(const WirelessNetwork);
0132     return d->aps.values();
0133 }
0134 
0135 QString NetworkManager::WirelessNetwork::device() const
0136 {
0137     Q_D(const WirelessNetwork);
0138 
0139     if (d->wirelessNetworkInterface) {
0140         return d->wirelessNetworkInterface->uni();
0141     } else {
0142         return QString();
0143     }
0144 }
0145 
0146 #include "moc_wirelessnetwork.cpp"