Files
illogical-impulse/.config/quickshell/ii/services/Network.qml
T
2025-08-26 21:07:00 +07:00

122 lines
3.2 KiB
QML

pragma Singleton
pragma ComponentBehavior: Bound
import Quickshell
import Quickshell.Io
import QtQuick
/**
* Simple polled network state service.
*/
Singleton {
id: root
property bool wifi: true
property bool ethernet: false
property bool wifiEnabled: false
property string networkName: ""
property int networkStrength
property string materialSymbol: ethernet ? "lan" :
wifiEnabled ? (
Network.networkStrength > 80 ? "signal_wifi_4_bar" :
Network.networkStrength > 60 ? "network_wifi_3_bar" :
Network.networkStrength > 40 ? "network_wifi_2_bar" :
Network.networkStrength > 20 ? "network_wifi_1_bar" :
"signal_wifi_0_bar"
) : "signal_wifi_off"
// Control
function toggleWifi(): void {
const cmd = wifiEnabled ? "off" : "on";
enableWifiProc.exec(["nmcli", "radio", "wifi", cmd]);
}
Process {
id: enableWifiProc
}
// Status update
function update() {
updateConnectionType.startCheck();
wifiStatusProcess.running = true
updateNetworkName.running = true;
updateNetworkStrength.running = true;
}
Process {
id: subscriber
running: true
command: ["nmcli", "monitor"]
stdout: SplitParser {
onRead: root.update()
}
}
Process {
id: updateConnectionType
property string buffer
command: ["sh", "-c", "nmcli -t -f NAME,TYPE,DEVICE c show --active"]
running: true
function startCheck() {
buffer = "";
updateConnectionType.running = true;
}
stdout: SplitParser {
onRead: data => {
updateConnectionType.buffer += data + "\n";
}
}
onExited: (exitCode, exitStatus) => {
const lines = updateConnectionType.buffer.trim().split('\n');
let hasEthernet = false;
let hasWifi = false;
lines.forEach(line => {
if (line.includes("ethernet"))
hasEthernet = true;
else if (line.includes("wireless"))
hasWifi = true;
});
root.ethernet = hasEthernet;
root.wifi = hasWifi;
}
}
Process {
id: updateNetworkName
command: ["sh", "-c", "nmcli -t -f NAME c show --active | head -1"]
running: true
stdout: SplitParser {
onRead: data => {
root.networkName = data;
}
}
}
Process {
id: updateNetworkStrength
running: true
command: ["sh", "-c", "nmcli -f IN-USE,SIGNAL,SSID device wifi | awk '/^\*/{if (NR!=1) {print $2}}'"]
stdout: SplitParser {
onRead: data => {
root.networkStrength = parseInt(data);
}
}
}
Process {
id: wifiStatusProcess
command: ["nmcli", "radio", "wifi"]
Component.onCompleted: running = true
environment: ({
LANG: "C",
LC_ALL: "C"
})
stdout: StdioCollector {
onStreamFinished: {
root.wifiEnabled = text.trim() === "enabled";
}
}
}
}