|
10 | 10 | #include <qloggingcategory.h> |
11 | 11 | #include <qobject.h> |
12 | 12 | #include <qtmetamacros.h> |
13 | | -#include <qxmlstream.h> |
14 | 13 |
|
15 | 14 | #include "../../core/logcat.hpp" |
16 | 15 | #include "../../dbus/properties.hpp" |
17 | 16 | #include "../device.hpp" |
18 | 17 | #include "../network.hpp" |
19 | 18 | #include "../wifi.hpp" |
20 | 19 | #include "dbus_types.hpp" |
| 20 | +#include "device.hpp" |
| 21 | +#include "enums.hpp" |
21 | 22 | #include "nm/dbus_nm_backend.h" |
22 | 23 | #include "wireless.hpp" |
23 | 24 |
|
@@ -86,101 +87,132 @@ void NetworkManager::registerDevices() { |
86 | 87 | } |
87 | 88 |
|
88 | 89 | void NetworkManager::registerDevice(const QString& path) { |
89 | | - if (this->mDeviceHash.contains(path)) { |
| 90 | + if (this->mDevices.contains(path)) { |
90 | 91 | qCDebug(logNetworkManager) << "Skipping duplicate registration of device" << path; |
91 | 92 | return; |
92 | 93 | } |
93 | 94 |
|
94 | | - // Introspect to decide the device variant. (For now, only Wireless) |
95 | | - auto* introspection = new QDBusInterface( |
| 95 | + // Create a temporary interface to decide the device type which won't change. |
| 96 | + auto* temp = new QDBusInterface( |
96 | 97 | "org.freedesktop.NetworkManager", |
97 | 98 | path, |
98 | | - "org.freedesktop.DBus.Introspectable", |
| 99 | + "org.freedesktop.DBus.Properties", |
99 | 100 | QDBusConnection::systemBus(), |
100 | 101 | this |
101 | 102 | ); |
102 | 103 |
|
103 | | - auto pending = introspection->asyncCall("Introspect"); |
| 104 | + auto pending = temp->asyncCall("Get", "org.freedesktop.NetworkManager.Device", "DeviceType"); |
104 | 105 | auto* call = new QDBusPendingCallWatcher(pending, this); |
105 | 106 |
|
106 | | - auto responseCallback = [this, path, introspection](QDBusPendingCallWatcher* call) { |
107 | | - const QDBusPendingReply<QString> reply = *call; |
108 | | - |
| 107 | + auto responseCallback = [this, path, temp](QDBusPendingCallWatcher* call) { |
| 108 | + const QDBusPendingReply<QVariant> reply = *call; |
109 | 109 | if (reply.isError()) { |
110 | | - qCWarning(logNetworkManager) << "Failed to introspect device: " << reply.error().message(); |
| 110 | + qCWarning(logNetworkManager) << "Failed to get device type: " << reply.error().message(); |
111 | 111 | } else { |
112 | | - QXmlStreamReader xml(reply.value()); |
113 | | - |
114 | | - while (!xml.atEnd() && !xml.hasError()) { |
115 | | - xml.readNext(); |
116 | | - |
117 | | - if (xml.isStartElement() && xml.name() == u"interface") { |
118 | | - const QString name = xml.attributes().value("name").toString(); |
119 | | - if (name.startsWith("org.freedesktop.NetworkManager.Device.Wireless")) { |
120 | | - this->registerWifiDevice(path); |
121 | | - break; |
122 | | - } |
123 | | - } |
| 112 | + auto type = static_cast<qs::network::NMDeviceType::Enum>(reply.value().toInt()); |
| 113 | + NMDevice* dev = nullptr; |
| 114 | + |
| 115 | + switch (type) { |
| 116 | + case NMDeviceType::Wifi: dev = new NMWirelessDevice(path); break; |
| 117 | + default: return; |
| 118 | + } |
| 119 | + |
| 120 | + if (!dev->isValid()) { |
| 121 | + qCWarning(logNetworkManager) << "Ignoring invalid registration of" << path; |
| 122 | + delete dev; |
| 123 | + return; |
124 | 124 | } |
| 125 | + |
| 126 | + // Only register a frontend device while it's managed by NM. |
| 127 | + auto onManagedChanged = [this, dev, type](bool managed) { |
| 128 | + managed ? this->registerFrontendDevice(type, dev) : this->removeFrontendDevice(dev); |
| 129 | + }; |
| 130 | + // clang-format off |
| 131 | + QObject::connect(dev, &NMDevice::addAndActivateConnection, this, &NetworkManager::addAndActivateConnection); |
| 132 | + QObject::connect(dev, &NMDevice::activateConnection, this, &NetworkManager::activateConnection); |
| 133 | + QObject::connect(dev, &NMDevice::managedChanged, this, onManagedChanged); |
| 134 | + // clang-format on |
| 135 | + |
| 136 | + this->mDevices.insert(path, dev); |
| 137 | + if (dev->managed()) this->registerFrontendDevice(type, dev); |
125 | 138 | } |
126 | 139 | delete call; |
127 | | - delete introspection; |
| 140 | + delete temp; |
128 | 141 | }; |
129 | 142 |
|
130 | 143 | QObject::connect(call, &QDBusPendingCallWatcher::finished, this, responseCallback); |
131 | 144 | } |
132 | 145 |
|
133 | | -void NetworkManager::registerWifiDevice(const QString& path) { |
134 | | - auto* wireless = new NMWirelessDevice(path); |
135 | | - if (!wireless->isWirelessValid() || !wireless->isDeviceValid()) { |
136 | | - qCWarning(logNetworkManager) << "Ignoring invalid registration of" << path; |
137 | | - delete wireless; |
138 | | - return; |
| 146 | +void NetworkManager::registerFrontendDevice(NMDeviceType::Enum type, NMDevice* dev) { |
| 147 | + NetworkDevice* frontendDev = nullptr; |
| 148 | + switch (type) { |
| 149 | + case NMDeviceType::Wifi: { |
| 150 | + auto* frontendWifiDev = new WifiDevice(dev); |
| 151 | + auto* wifiDev = qobject_cast<NMWirelessDevice*>(dev); |
| 152 | + // Bind WifiDevice-specific properties |
| 153 | + auto translateMode = [wifiDev]() { |
| 154 | + switch (wifiDev->mode()) { |
| 155 | + case NM80211Mode::Unknown: return WifiDeviceMode::Unknown; |
| 156 | + case NM80211Mode::Adhoc: return WifiDeviceMode::AdHoc; |
| 157 | + case NM80211Mode::Infra: return WifiDeviceMode::Station; |
| 158 | + case NM80211Mode::Ap: return WifiDeviceMode::AccessPoint; |
| 159 | + case NM80211Mode::Mesh: return WifiDeviceMode::Mesh; |
| 160 | + } |
| 161 | + }; |
| 162 | + // clang-format off |
| 163 | + frontendWifiDev->bindableMode().setBinding(translateMode); |
| 164 | + wifiDev->bindableScanning().setBinding([frontendWifiDev]() { return frontendWifiDev->wifiScanner()->enabled(); }); |
| 165 | + QObject::connect(wifiDev, &NMWirelessDevice::networkAdded, frontendWifiDev, &WifiDevice::networkAdded); |
| 166 | + QObject::connect(wifiDev, &NMWirelessDevice::networkRemoved, frontendWifiDev, &WifiDevice::networkRemoved); |
| 167 | + // clang-format on |
| 168 | + frontendDev = frontendWifiDev; |
| 169 | + break; |
| 170 | + } |
| 171 | + default: return; |
139 | 172 | } |
140 | 173 |
|
141 | | - auto* device = new WifiDevice(this); |
142 | | - auto* scanner = device->wifiScanner(); |
143 | | - wireless->setParent(device); |
144 | | - this->mDeviceHash.insert(path, device); |
145 | | - |
146 | | - device->bindableName().setBinding([wireless]() { return wireless->interface(); }); |
147 | | - device->bindableAddress().setBinding([wireless]() { return wireless->hwAddress(); }); |
148 | | - device->bindableNmState().setBinding([wireless]() { return wireless->state(); }); |
149 | | - device->bindableState().setBinding([wireless]() { |
150 | | - switch (wireless->state()) { |
| 174 | + // Bind generic NetworkDevice properties |
| 175 | + auto translateState = [dev]() { |
| 176 | + switch (dev->state()) { |
151 | 177 | case 0 ... 20: return DeviceConnectionState::Unknown; |
152 | 178 | case 30: return DeviceConnectionState::Disconnected; |
153 | 179 | case 40 ... 90: return DeviceConnectionState::Connecting; |
154 | 180 | case 100: return DeviceConnectionState::Connected; |
155 | 181 | case 110 ... 120: return DeviceConnectionState::Disconnecting; |
156 | 182 | } |
157 | | - }); |
158 | | - // clang-format off |
159 | | - QObject::connect(wireless, &NMWirelessDevice::addAndActivateConnection, this, &NetworkManager::addAndActivateConnection); |
160 | | - QObject::connect(wireless, &NMWirelessDevice::activateConnection, this, &NetworkManager::activateConnection); |
161 | | - QObject::connect(wireless, &NMWirelessDevice::networkAdded, device, &WifiDevice::networkAdded); |
162 | | - QObject::connect(wireless, &NMWirelessDevice::networkRemoved, device, &WifiDevice::networkRemoved); |
163 | | - QObject::connect(scanner, &WifiScanner::requestEnabled, wireless, &NMWirelessDevice::handleScanner); |
164 | | - QObject::connect(device, &WifiDevice::requestDisconnect, wireless, &NMWirelessDevice::disconnect); |
165 | | - // clang-format on |
| 183 | + }; |
| 184 | + frontendDev->bindableName().setBinding([dev]() { return dev->interface(); }); |
| 185 | + frontendDev->bindableAddress().setBinding([dev]() { return dev->hwAddress(); }); |
| 186 | + frontendDev->bindableNmState().setBinding([dev]() { return dev->state(); }); |
| 187 | + frontendDev->bindableState().setBinding(translateState); |
| 188 | + QObject::connect(frontendDev, &WifiDevice::requestDisconnect, dev, &NMDevice::disconnect); |
| 189 | + |
| 190 | + this->mFrontendDevices.insert(dev->path(), frontendDev); |
| 191 | + emit this->deviceAdded(frontendDev); |
| 192 | +} |
166 | 193 |
|
167 | | - emit this->deviceAdded(device); |
| 194 | +void NetworkManager::removeFrontendDevice(NMDevice* dev) { |
| 195 | + auto* frontendDev = this->mFrontendDevices.take(dev->path()); |
| 196 | + if (frontendDev) { |
| 197 | + emit this->deviceRemoved(frontendDev); |
| 198 | + delete frontendDev; |
| 199 | + } |
168 | 200 | } |
169 | 201 |
|
170 | 202 | void NetworkManager::onDevicePathAdded(const QDBusObjectPath& path) { |
171 | 203 | this->registerDevice(path.path()); |
172 | 204 | } |
173 | 205 |
|
174 | 206 | void NetworkManager::onDevicePathRemoved(const QDBusObjectPath& path) { |
175 | | - auto iter = this->mDeviceHash.find(path.path()); |
176 | | - if (iter == this->mDeviceHash.end()) { |
| 207 | + auto iter = this->mDevices.find(path.path()); |
| 208 | + if (iter == this->mDevices.end()) { |
177 | 209 | qCWarning(logNetworkManager) << "Sent removal signal for" << path.path() |
178 | 210 | << "which is not registered."; |
179 | 211 | } else { |
180 | | - auto* device = iter.value(); |
181 | | - this->mDeviceHash.erase(iter); |
182 | | - emit this->deviceRemoved(device); |
183 | | - delete device; |
| 212 | + auto* dev = iter.value(); |
| 213 | + this->removeFrontendDevice(dev); |
| 214 | + this->mDevices.erase(iter); |
| 215 | + delete dev; |
184 | 216 | } |
185 | 217 | } |
186 | 218 |
|
|
0 commit comments