1/*
2 SPDX-FileCopyrightText: 2009, 2011 Will Stephenson <wstephenson@kde.org>
3 SPDX-FileCopyrightText: 2013 Jan Grulich <jgrulich@redhat.com>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6*/
7
8#include "wirelessnetwork.h"
9#include "wirelessnetwork_p.h"
10
11#include "manager.h"
12#include "wirelessdevice.h"
13
14NetworkManager::WirelessNetworkPrivate::WirelessNetworkPrivate(WirelessNetwork *q, WirelessDevice *device)
15 : q_ptr(q)
16 , wirelessNetworkInterface(device)
17{
18 QObject::connect(sender: device, SIGNAL(accessPointAppeared(QString)), receiver: q, SLOT(accessPointAppeared(QString)));
19 QObject::connect(sender: device, SIGNAL(accessPointDisappeared(QString)), receiver: q, SLOT(accessPointDisappeared(QString)));
20 QObject::connect(sender: device, SIGNAL(activeAccessPointChanged(QString)), receiver: q, SLOT(updateStrength()));
21}
22
23NetworkManager::WirelessNetworkPrivate::~WirelessNetworkPrivate()
24{
25}
26
27void NetworkManager::WirelessNetworkPrivate::addAccessPointInternal(const NetworkManager::AccessPoint::Ptr &accessPoint)
28{
29 Q_Q(WirelessNetwork);
30
31 QObject::connect(sender: accessPoint.data(), SIGNAL(signalStrengthChanged(int)), receiver: q, SLOT(updateStrength()));
32 aps.insert(key: accessPoint->uni(), value: accessPoint);
33 updateStrength();
34}
35
36void NetworkManager::WirelessNetworkPrivate::accessPointAppeared(const QString &uni)
37{
38 if (!aps.contains(key: uni) && wirelessNetworkInterface) {
39 NetworkManager::AccessPoint::Ptr accessPoint = wirelessNetworkInterface->findAccessPoint(uni);
40 if (accessPoint && accessPoint->ssid() == ssid) {
41 addAccessPointInternal(accessPoint);
42 }
43 }
44}
45
46void NetworkManager::WirelessNetworkPrivate::accessPointDisappeared(const QString &uni)
47{
48 Q_Q(WirelessNetwork);
49 aps.remove(key: uni);
50 if (aps.isEmpty()) {
51 Q_EMIT q->disappeared(ssid);
52 } else {
53 updateStrength();
54 }
55}
56
57void NetworkManager::WirelessNetworkPrivate::updateStrength()
58{
59 Q_Q(WirelessNetwork);
60
61 int selectedStrength = -1;
62 NetworkManager::AccessPoint::Ptr selectedAp;
63
64 if (!wirelessNetworkInterface) {
65 return;
66 }
67
68 NetworkManager::AccessPoint::Ptr activeAp = wirelessNetworkInterface->activeAccessPoint();
69 if (activeAp && activeAp->ssid() == ssid) {
70 // If the network has an active access point, use it as the referenceAp
71 selectedStrength = activeAp->signalStrength();
72 selectedAp = activeAp;
73 } else {
74 // Otherwise, choose the access point with the strongest signal
75 for (const NetworkManager::AccessPoint::Ptr &iface : std::as_const(t&: aps)) {
76 if (selectedStrength <= iface->signalStrength()) {
77 selectedStrength = iface->signalStrength();
78 selectedAp = iface;
79 }
80 }
81 }
82
83 if (selectedStrength != strength) {
84 strength = selectedStrength;
85 Q_EMIT q->signalStrengthChanged(strength);
86 }
87
88 if (selectedAp && referenceAp != selectedAp) {
89 referenceAp = selectedAp;
90 Q_EMIT q->referenceAccessPointChanged(apUni: referenceAp->uni());
91 }
92 // TODO: update the networks delayed
93 // kDebug() << "update strength" << ssid << strength;
94}
95
96NetworkManager::WirelessNetwork::WirelessNetwork(const AccessPoint::Ptr &accessPoint, WirelessDevice *device)
97 : d_ptr(new WirelessNetworkPrivate(this, device))
98{
99 Q_D(WirelessNetwork);
100
101 d->strength = -1;
102 d->ssid = accessPoint->ssid();
103 d->addAccessPointInternal(accessPoint);
104}
105
106NetworkManager::WirelessNetwork::~WirelessNetwork()
107{
108 delete d_ptr;
109}
110
111QString NetworkManager::WirelessNetwork::ssid() const
112{
113 Q_D(const WirelessNetwork);
114 return d->ssid;
115}
116
117int NetworkManager::WirelessNetwork::signalStrength() const
118{
119 Q_D(const WirelessNetwork);
120 return d->strength;
121}
122
123NetworkManager::AccessPoint::Ptr NetworkManager::WirelessNetwork::referenceAccessPoint() const
124{
125 Q_D(const WirelessNetwork);
126 return d->referenceAp;
127}
128
129NetworkManager::AccessPoint::List NetworkManager::WirelessNetwork::accessPoints() const
130{
131 Q_D(const WirelessNetwork);
132 return d->aps.values();
133}
134
135QString NetworkManager::WirelessNetwork::device() const
136{
137 Q_D(const WirelessNetwork);
138
139 if (d->wirelessNetworkInterface) {
140 return d->wirelessNetworkInterface->uni();
141 } else {
142 return QString();
143 }
144}
145
146#include "moc_wirelessnetwork.cpp"
147

source code of networkmanager-qt/src/wirelessnetwork.cpp