Skip to content

Commit 05f0348

Browse files
committed
network: update API
A few changes to the frontend Network API: - Expose all devices at the toplevel - Move wifi rfkill options to the toplevel - Add WifiSecurityType (backend agnostic) - Move WifiNetwork signal strength to a qreal from 0.0 to 1.0 - Add extensible BaseNetwork class
1 parent 62ab321 commit 05f0348

File tree

14 files changed

+283
-271
lines changed

14 files changed

+283
-271
lines changed

src/network/device.cpp

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,38 @@ namespace {
1414
QS_LOGGING_CATEGORY(logNetworkDevice, "quickshell.network.device", QtWarningMsg);
1515
} // namespace
1616

17-
NetworkDevice::NetworkDevice(QObject* parent): QObject(parent) {};
18-
19-
void NetworkDevice::disconnect() {
20-
if (this->bState == NetworkConnectionState::Disconnected) {
21-
qCCritical(logNetworkDevice) << "Device" << this << "is already disconnected";
22-
return;
23-
}
24-
if (this->bState == NetworkConnectionState::Disconnecting) {
25-
qCCritical(logNetworkDevice) << "Device" << this << "is already disconnecting";
26-
return;
27-
}
28-
qCDebug(logNetworkDevice) << "Disconnecting from device" << this;
29-
this->requestDisconnect();
30-
}
31-
32-
QString NetworkConnectionState::toString(NetworkConnectionState::Enum state) {
17+
QString DeviceConnectionState::toString(DeviceConnectionState::Enum state) {
3318
switch (state) {
3419
case Unknown: return QStringLiteral("Unknown");
3520
case Connecting: return QStringLiteral("Connecting");
3621
case Connected: return QStringLiteral("Connected");
3722
case Disconnecting: return QStringLiteral("Disconnecting");
3823
case Disconnected: return QStringLiteral("Disconnected");
39-
default: return QStringLiteral("Unknown");
4024
}
4125
}
4226

43-
} // namespace qs::network
27+
QString DeviceType::toString(DeviceType::Enum type) {
28+
switch (type) {
29+
case None: return QStringLiteral("None");
30+
case Wifi: return QStringLiteral("Wifi");
31+
}
32+
}
4433

45-
QDebug operator<<(QDebug debug, const qs::network::NetworkDevice* device) {
46-
auto saver = QDebugStateSaver(debug);
34+
NetworkDevice::NetworkDevice(DeviceType::Enum type, QObject* parent)
35+
: QObject(parent)
36+
, mType(type) {};
4737

48-
if (device) {
49-
debug.nospace() << "NetworkDevice(" << static_cast<const void*>(device)
50-
<< ", name=" << device->name() << ")";
51-
} else {
52-
debug << "BluetoothDevice(nullptr)";
38+
void NetworkDevice::disconnect() {
39+
if (this->bState == DeviceConnectionState::Disconnected) {
40+
qCCritical(logNetworkDevice) << "Device" << this << "is already disconnected";
41+
return;
5342
}
54-
55-
return debug;
43+
if (this->bState == DeviceConnectionState::Disconnecting) {
44+
qCCritical(logNetworkDevice) << "Device" << this << "is already disconnecting";
45+
return;
46+
}
47+
qCDebug(logNetworkDevice) << "Disconnecting from device" << this;
48+
this->requestDisconnect();
5649
}
50+
51+
} // namespace qs::network

src/network/device.hpp

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
namespace qs::network {
1212

13-
///! Connection state.
14-
class NetworkConnectionState: public QObject {
13+
///! Connection state of a NetworkDevice.
14+
class DeviceConnectionState: public QObject {
1515
Q_OBJECT;
1616
QML_ELEMENT;
1717
QML_SINGLETON;
@@ -25,37 +25,56 @@ class NetworkConnectionState: public QObject {
2525
Disconnected = 4,
2626
};
2727
Q_ENUM(Enum);
28-
Q_INVOKABLE static QString toString(NetworkConnectionState::Enum state);
28+
Q_INVOKABLE static QString toString(DeviceConnectionState::Enum state);
2929
};
3030

31-
///! A Network device.
31+
///! Type of network device.
32+
class DeviceType: public QObject {
33+
Q_OBJECT;
34+
QML_ELEMENT;
35+
QML_SINGLETON;
36+
37+
public:
38+
enum Enum : quint8 {
39+
None = 0,
40+
Wifi = 1,
41+
};
42+
Q_ENUM(Enum);
43+
Q_INVOKABLE static QString toString(DeviceType::Enum type);
44+
};
45+
46+
47+
///! A network device.
3248
class NetworkDevice: public QObject {
3349
Q_OBJECT;
3450
QML_ELEMENT;
3551
QML_UNCREATABLE("Devices can only be acquired through Network");
3652
// clang-format off
53+
/// The device type.
54+
Q_PROPERTY(DeviceType::Enum type READ type CONSTANT);
3755
/// The name of the device's control interface.
3856
Q_PROPERTY(QString name READ name NOTIFY nameChanged BINDABLE bindableName);
3957
/// The hardware address of the device in the XX:XX:XX:XX:XX:XX format.
4058
Q_PROPERTY(QString address READ address NOTIFY addressChanged BINDABLE bindableAddress);
4159
/// Connection state of the device.
42-
Q_PROPERTY(qs::network::NetworkConnectionState::Enum state READ state NOTIFY stateChanged BINDABLE bindableState);
60+
Q_PROPERTY(qs::network::DeviceConnectionState::Enum state READ state NOTIFY stateChanged BINDABLE bindableState);
4361
/// A more specific device state when the backend is NetworkManager.
4462
Q_PROPERTY(qs::network::NMDeviceState::Enum nmState READ nmState NOTIFY nmStateChanged BINDABLE bindableNmState);
4563
// clang-format on
4664

4765
public:
48-
explicit NetworkDevice(QObject* parent = nullptr);
66+
explicit NetworkDevice(DeviceType::Enum type, QObject* parent = nullptr);
4967

5068
/// Disconnects the device and prevents it from automatically activating further connections.
5169
Q_INVOKABLE void disconnect();
5270

71+
[[nodiscard]] DeviceType::Enum type() const { return this->mType; };
5372
QBindable<QString> bindableName() { return &this->bName; };
5473
[[nodiscard]] QString name() const { return this->bName; };
5574
QBindable<QString> bindableAddress() { return &this->bAddress; };
5675
[[nodiscard]] QString address() const { return this->bAddress; };
57-
QBindable<NetworkConnectionState::Enum> bindableState() { return &this->bState; };
58-
[[nodiscard]] NetworkConnectionState::Enum state() const { return this->bState; };
76+
QBindable<DeviceConnectionState::Enum> bindableState() { return &this->bState; };
77+
[[nodiscard]] DeviceConnectionState::Enum state() const { return this->bState; };
5978
QBindable<NMDeviceState::Enum> bindableNmState() { return &this->bNmState; };
6079
[[nodiscard]] NMDeviceState::Enum nmState() const { return this->bNmState; };
6180

@@ -67,14 +86,13 @@ class NetworkDevice: public QObject {
6786
void nmStateChanged();
6887

6988
private:
89+
DeviceType::Enum mType;
7090
// clang-format off
7191
Q_OBJECT_BINDABLE_PROPERTY(NetworkDevice, QString, bName, &NetworkDevice::nameChanged);
7292
Q_OBJECT_BINDABLE_PROPERTY(NetworkDevice, QString, bAddress, &NetworkDevice::addressChanged);
73-
Q_OBJECT_BINDABLE_PROPERTY(NetworkDevice, NetworkConnectionState::Enum, bState, &NetworkDevice::stateChanged);
93+
Q_OBJECT_BINDABLE_PROPERTY(NetworkDevice, DeviceConnectionState::Enum, bState, &NetworkDevice::stateChanged);
7494
Q_OBJECT_BINDABLE_PROPERTY(NetworkDevice, NMDeviceState::Enum, bNmState, &NetworkDevice::nmStateChanged);
7595
// clang-format on
7696
};
7797

7898
} // namespace qs::network
79-
80-
QDebug operator<<(QDebug debug, const qs::network::NetworkDevice* device);

src/network/network.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,22 @@
66

77
#include "../core/logcat.hpp"
88
#include "nm/backend.hpp"
9-
#include "wifi.hpp"
109

1110
namespace qs::network {
1211

1312
namespace {
1413
QS_LOGGING_CATEGORY(logNetwork, "quickshell.network", QtWarningMsg);
1514
} // namespace
1615

17-
Network::Network(QObject* parent): QObject(parent), mWifi(new Wifi(this)) {
16+
Network::Network(QObject* parent): QObject(parent) {
1817
// NetworkManager
1918
auto* nm = new NetworkManager(this);
2019
if (nm->isAvailable()) {
21-
QObject::connect(nm, &NetworkManager::wifiDeviceAdded, this->wifi(), &Wifi::onDeviceAdded);
22-
QObject::connect(nm, &NetworkManager::wifiDeviceRemoved, this->wifi(), &Wifi::onDeviceRemoved);
23-
this->wifi()->bindableEnabled().setBinding([nm]() { return nm->wifiEnabled(); });
24-
this->wifi()->bindableHardwareEnabled().setBinding([nm]() { return nm->wifiHardwareEnabled(); }
25-
);
26-
QObject::connect(this->wifi(), &Wifi::requestSetEnabled, nm, &NetworkManager::setWifiEnabled);
20+
QObject::connect(nm, &NetworkManager::deviceAdded, this, &Network::onDeviceAdded);
21+
QObject::connect(nm, &NetworkManager::deviceRemoved, this, &Network::onDeviceRemoved);
22+
this->bindableWifiEnabled().setBinding([nm]() { return nm->wifiEnabled(); });
23+
this->bindableWifiHardwareEnabled().setBinding([nm]() { return nm->wifiHardwareEnabled(); });
24+
QObject::connect(this, &Network::requestSetWifiEnabled, nm, &NetworkManager::setWifiEnabled);
2725
this->mBackend = nm;
2826
this->mBackendType = NetworkBackendType::NetworkManager;
2927
return;
@@ -34,4 +32,18 @@ Network::Network(QObject* parent): QObject(parent), mWifi(new Wifi(this)) {
3432
qCCritical(logNetwork) << "Network will not work. Could not find an available backend.";
3533
}
3634

35+
void Network::onDeviceAdded(NetworkDevice* dev) { this->mDevices.insertObject(dev); }
36+
void Network::onDeviceRemoved(NetworkDevice* dev) { this->mDevices.removeObject(dev); }
37+
38+
void Network::setWifiEnabled(bool enabled) {
39+
if (this->bWifiEnabled == enabled) {
40+
const QString state = enabled ? "enabled" : "disabled";
41+
qCCritical(logNetwork) << "Wifi is already software" << state;
42+
} else {
43+
emit this->requestSetWifiEnabled(enabled);
44+
}
45+
}
46+
47+
BaseNetwork::BaseNetwork(QString name, QObject* parent): QObject(parent), mName(std::move(name)) {};
48+
3749
} // namespace qs::network

src/network/network.hpp

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
#include <qtmetamacros.h>
77
#include <qtypes.h>
88

9-
#include "wifi.hpp"
9+
#include "../core/model.hpp"
10+
#include "device.hpp"
1011

1112
namespace qs::network {
1213

@@ -41,22 +42,72 @@ class Network: public QObject {
4142
Q_OBJECT;
4243
QML_SINGLETON;
4344
QML_NAMED_ELEMENT(Network);
44-
45-
/// The wifi device service.
46-
Q_PROPERTY(qs::network::Wifi* wifi READ wifi CONSTANT);
45+
// clang-format off
46+
/// A list of all network devices.
47+
Q_PROPERTY(UntypedObjectModel* devices READ devices CONSTANT);
48+
QSDOC_TYPE_OVERRIDE(ObjectModel<qs::Network::Device>*);
4749
/// The backend being used to power the Network service.
4850
Q_PROPERTY(qs::network::NetworkBackendType::Enum backend READ backend CONSTANT);
51+
/// Master switch for the status of the rfkill software block of all wireless devices.
52+
Q_PROPERTY(bool wifiEnabled READ wifiEnabled WRITE setWifiEnabled NOTIFY wifiEnabledChanged);
53+
/// Master switch for the status of the rfkill hardware block of all wireless devices.
54+
Q_PROPERTY(bool wifiHardwareEnabled READ wifiHardwareEnabled NOTIFY wifiHardwareEnabledChanged BINDABLE bindableWifiHardwareEnabled);
55+
// clang-format on
4956

5057
public:
5158
explicit Network(QObject* parent = nullptr);
5259

53-
[[nodiscard]] Wifi* wifi() const { return this->mWifi; };
60+
[[nodiscard]] ObjectModel<NetworkDevice>* devices() { return &this->mDevices; };
5461
[[nodiscard]] NetworkBackendType::Enum backend() const { return this->mBackendType; };
62+
QBindable<bool> bindableWifiEnabled() { return &this->bWifiEnabled; };
63+
[[nodiscard]] bool wifiEnabled() const { return this->bWifiEnabled; };
64+
void setWifiEnabled(bool enabled);
65+
QBindable<bool> bindableWifiHardwareEnabled() { return &this->bWifiHardwareEnabled; };
66+
[[nodiscard]] bool wifiHardwareEnabled() const { return this->bWifiHardwareEnabled; };
67+
68+
signals:
69+
void requestSetWifiEnabled(bool enabled);
70+
void wifiEnabledChanged();
71+
void wifiHardwareEnabledChanged();
72+
73+
public slots:
74+
void onDeviceAdded(NetworkDevice* dev);
75+
void onDeviceRemoved(NetworkDevice* dev);
5576

5677
private:
57-
Wifi* mWifi;
78+
ObjectModel<NetworkDevice> mDevices {this};
5879
NetworkBackend* mBackend = nullptr;
5980
NetworkBackendType::Enum mBackendType = NetworkBackendType::None;
81+
// clang-format off
82+
Q_OBJECT_BINDABLE_PROPERTY(Network, bool, bWifiEnabled, &Network::wifiEnabledChanged);
83+
Q_OBJECT_BINDABLE_PROPERTY(Network, bool, bWifiHardwareEnabled, &Network::wifiHardwareEnabledChanged);
84+
// clang-format on
85+
};
86+
87+
///! A base network.
88+
class BaseNetwork: public QObject {
89+
Q_OBJECT;
90+
QML_ELEMENT;
91+
QML_UNCREATABLE("BaseNetwork can only be aqcuired through network devices");
92+
93+
/// The name of the network
94+
Q_PROPERTY(QString name READ name CONSTANT);
95+
/// True if the network is connected to.
96+
Q_PROPERTY(bool connected READ connected NOTIFY connectedChanged BINDABLE bindableConnected);
97+
98+
public:
99+
explicit BaseNetwork(QString name, QObject* parent = nullptr);
100+
101+
[[nodiscard]] QString name() const { return this->mName; };
102+
QBindable<bool> bindableConnected() { return &this->bConnected; }
103+
[[nodiscard]] bool connected() const { return this->bConnected; };
104+
105+
signals:
106+
void connectedChanged();
107+
108+
protected:
109+
QString mName;
110+
Q_OBJECT_BINDABLE_PROPERTY(BaseNetwork, bool, bConnected, &BaseNetwork::connectedChanged);
60111
};
61112

62113
} // namespace qs::network

src/network/nm/backend.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,6 @@ void NetworkManager::registerDevice(const QString& path) {
131131
QObject::connect(call, &QDBusPendingCallWatcher::finished, this, responseCallback);
132132
}
133133

134-
NetworkConnectionState::Enum NetworkManager::toNetworkDeviceState(NMDeviceState::Enum state) {
135-
switch (state) {
136-
case 0 ... 20: return NetworkConnectionState::Unknown;
137-
case 30: return NetworkConnectionState::Disconnected;
138-
case 40 ... 90: return NetworkConnectionState::Connecting;
139-
case 100: return NetworkConnectionState::Connected;
140-
case 110 ... 120: return NetworkConnectionState::Disconnecting;
141-
}
142-
}
143-
144134
void NetworkManager::registerWifiDevice(const QString& path) {
145135
auto* wireless = new NMWirelessDevice(path);
146136
if (!wireless->isWirelessValid() || !wireless->isDeviceValid()) {
@@ -157,7 +147,13 @@ void NetworkManager::registerWifiDevice(const QString& path) {
157147
device->bindableAddress().setBinding([wireless]() { return wireless->hwAddress(); });
158148
device->bindableNmState().setBinding([wireless]() { return wireless->state(); });
159149
device->bindableState().setBinding([wireless]() {
160-
return qs::network::NetworkManager::toNetworkDeviceState(wireless->state());
150+
switch (wireless->state()) {
151+
case 0 ... 20: return DeviceConnectionState::Unknown;
152+
case 30: return DeviceConnectionState::Disconnected;
153+
case 40 ... 90: return DeviceConnectionState::Connecting;
154+
case 100: return DeviceConnectionState::Connected;
155+
case 110 ... 120: return DeviceConnectionState::Disconnecting;
156+
}
161157
});
162158
device->bindableScanning().setBinding([wireless]() { return wireless->scanning(); });
163159
// clang-format off
@@ -169,7 +165,7 @@ void NetworkManager::registerWifiDevice(const QString& path) {
169165
QObject::connect(device, &WifiDevice::requestDisconnect, wireless, &NMWirelessDevice::disconnect);
170166
// clang-format on
171167

172-
emit this->wifiDeviceAdded(device);
168+
emit this->deviceAdded(device);
173169
}
174170

175171
void NetworkManager::onDevicePathAdded(const QDBusObjectPath& path) {
@@ -184,7 +180,7 @@ void NetworkManager::onDevicePathRemoved(const QDBusObjectPath& path) {
184180
} else {
185181
auto* device = iter.value();
186182
this->mDeviceHash.erase(iter);
187-
if (auto* wifi = qobject_cast<WifiDevice*>(device)) emit this->wifiDeviceRemoved(wifi);
183+
emit this->deviceRemoved(device);
188184
delete device;
189185
}
190186
}

src/network/nm/backend.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class NetworkManager: public NetworkBackend {
2525
[[nodiscard]] bool wifiHardwareEnabled() const { return this->bWifiHardwareEnabled; };
2626

2727
signals:
28-
void wifiDeviceAdded(WifiDevice* device);
29-
void wifiDeviceRemoved(WifiDevice* device);
28+
void deviceAdded(NetworkDevice* device);
29+
void deviceRemoved(NetworkDevice* device);
3030
void wifiEnabledChanged(bool enabled);
3131
void wifiHardwareEnabledChanged(bool enabled);
3232

@@ -44,7 +44,6 @@ private slots:
4444
);
4545

4646
private:
47-
static NetworkConnectionState::Enum toNetworkDeviceState(NMDeviceState::Enum state);
4847
void init();
4948
void registerDevices();
5049
void registerDevice(const QString& path);

0 commit comments

Comments
 (0)