Skip to content

Commit c9ece29

Browse files
committed
disconnect
1 parent bdaaa5f commit c9ece29

File tree

6 files changed

+51
-5
lines changed

6 files changed

+51
-5
lines changed

src/network/api.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace qs::network {
1414

1515
namespace {
16+
Q_LOGGING_CATEGORY(logNetworkDevice, "quickshell.network.device", QtWarningMsg);
1617
Q_LOGGING_CATEGORY(logNetwork, "quickshell.network", QtWarningMsg);
1718
}
1819

@@ -48,6 +49,22 @@ void Device::setState(DeviceState::Enum state) {
4849
}
4950
}
5051

52+
void Device::disconnect() {
53+
if (this->bState == DeviceState::Disconnected) {
54+
qCCritical(logNetworkDevice) << "Device" << this << "is already disconnected";
55+
return;
56+
}
57+
58+
if (this->bState == DeviceState::Disconnecting) {
59+
qCCritical(logNetworkDevice) << "Device" << this << "is already disconnecting";
60+
return;
61+
}
62+
63+
qCDebug(logNetworkDevice) << "Disconnecting from device" << this;
64+
65+
signalDisconnect();
66+
}
67+
5168
void WirelessDevice::setLastScan(qint64 lastScan) {
5269
if (lastScan != this->bLastScan) {
5370
this->bLastScan = lastScan;

src/network/api.hpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,56 @@ class DeviceState: public QObject {
2323

2424
public:
2525
enum Enum : quint8 {
26+
/// The device state is unknown.
2627
Unknown = 0,
27-
Disconnected = 10,
28-
Connecting = 20,
29-
Connected = 30,
30-
Disconnecting = 40,
28+
/// The device is not connected.
29+
Disconnected = 1,
30+
/// The device is connected.
31+
Connected = 2,
32+
/// The device is disconnecting.
33+
Disconnecting = 3,
34+
/// The device is connecting.
35+
Connecting = 4,
3136
};
3237
Q_ENUM(Enum);
3338
Q_INVOKABLE static QString toString(DeviceState::Enum state);
3439
};
3540

41+
///! A tracked network device.
3642
class Device: public QObject {
3743
Q_OBJECT;
3844
QML_ELEMENT;
3945
QML_UNCREATABLE("Devices can only be acquired through Network");
4046

4147
// clang-format off
48+
/// The name of the device's interface.
4249
Q_PROPERTY(QString name READ default NOTIFY nameChanged BINDABLE bindableName);
50+
/// The hardware address of the device's interface in the XX:XX:XX:XX:XX:XX format.
4351
Q_PROPERTY(QString address READ default NOTIFY addressChanged BINDABLE bindableAddress);
52+
/// Connection state of the device.
4453
Q_PROPERTY(DeviceState::Enum state READ default NOTIFY stateChanged BINDABLE bindableState);
4554
// clang-format on
4655

4756
signals:
57+
void signalDisconnect();
58+
59+
// Frontend-facing signals
4860
void nameChanged();
4961
void addressChanged();
5062
void stateChanged();
5163

5264
public slots:
65+
// Slots for the backend to connect signals to
5366
void setName(const QString& name);
5467
void setAddress(const QString& address);
5568
void setState(DeviceState::Enum state);
5669

5770
public:
5871
explicit Device(QObject* parent = nullptr);
72+
73+
/// Disconnects the device and prevents it from automatically activating further connections.
74+
Q_INVOKABLE void disconnect();
75+
5976
[[nodiscard]] QBindable<QString> bindableName() const { return &this->bName; };
6077
[[nodiscard]] QBindable<QString> bindableAddress() const { return &this->bAddress; };
6178
[[nodiscard]] QBindable<DeviceState::Enum> bindableState() const { return &this->bState; };

src/network/nm_adapters.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ void NMDeviceAdapter::init(const QString& path) {
3838
this->deviceProperties.updateAllViaGetAll();
3939
}
4040

41+
void NMDeviceAdapter::disconnect() { this->proxy->Disconnect(); }
4142
bool NMDeviceAdapter::isValid() const { return this->proxy && this->proxy->isValid(); }
4243
QString NMDeviceAdapter::address() const {
4344
return this->proxy ? this->proxy->service() : QString();

src/network/nm_adapters.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ class NMDeviceAdapter: public QObject {
120120
[[nodiscard]] QString getHwAddress() { return this->bHwAddress; };
121121
[[nodiscard]] NMDeviceType::Enum getType() { return this->bType; };
122122

123+
public slots:
124+
void disconnect();
125+
123126
signals:
124127
void interfaceChanged(const QString& interface);
125128
void hwAddressChanged(const QString& hwAddress);

src/network/nm_backend.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ void NetworkManager::registerDevice(
131131
) {
132132
Device* device = createDeviceVariant(type, path);
133133

134-
// Bind backend NMDeviceAdapter to frontend Device
134+
// deviceAdapter signal -> Device slot
135135
deviceAdapter->setParent(device);
136136
QObject::connect(deviceAdapter, &NMDeviceAdapter::hwAddressChanged, device, &Device::setAddress);
137137
QObject::connect(deviceAdapter, &NMDeviceAdapter::interfaceChanged, device, &Device::setName);
@@ -142,6 +142,10 @@ void NetworkManager::registerDevice(
142142
[device](NMDeviceState::Enum state) { device->setState(NMDeviceState::translate(state)); }
143143
);
144144

145+
// Device signal -> deviceAdapter slot
146+
QObject::connect(device, &Device::signalDisconnect, deviceAdapter, &NMDeviceAdapter::disconnect);
147+
148+
// Track device
145149
this->mDeviceHash.insert(path, device);
146150
this->mDevices.insertObject(device);
147151

src/network/test/network.qml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ FloatingWindow {
3232
}
3333
Label { text: "Hardware Address: " + modelData.address }
3434
Label { text: "State: " + DeviceState.toString(modelData.state) }
35+
Button {
36+
text: "Disconnect"
37+
onClicked: modelData.disconnect()
38+
}
3539
}
3640
}
3741
}

0 commit comments

Comments
 (0)