internal: format network

Also use exec, onExited and StdioCollector
This commit is contained in:
2 * r + 2 * t 2025-08-04 23:15:55 +10:00
parent 5405400bd1
commit 1f4ec53f8f

View file

@ -10,42 +10,37 @@ Singleton {
readonly property list<AccessPoint> networks: [] readonly property list<AccessPoint> networks: []
readonly property AccessPoint active: networks.find(n => n.active) ?? null readonly property AccessPoint active: networks.find(n => n.active) ?? null
property bool wifiEnabled: true property bool wifiEnabled: true
property bool scanning: false readonly property bool scanning: rescanProc.running
reloadableId: "network" reloadableId: "network"
function enableWifi(enabled: bool): void { function enableWifi(enabled: bool): void {
const cmd = enabled ? "on" : "off"; const cmd = enabled ? "on" : "off";
enableWifiProcess.command = ["nmcli", "radio", "wifi", cmd]; enableWifiProc.exec(["nmcli", "radio", "wifi", cmd]);
enableWifiProcess.running = true;
} }
function toggleWifi(): void { function toggleWifi(): void {
const cmd = wifiEnabled ? "off" : "on"; const cmd = wifiEnabled ? "off" : "on";
enableWifiProcess.command = ["nmcli", "radio", "wifi", cmd]; enableWifiProc.exec(["nmcli", "radio", "wifi", cmd]);
enableWifiProcess.running = true;
} }
function rescanWifi(): void { function rescanWifi(): void {
scanning = true; rescanProc.running = true;
rescanProcess.running = true;
} }
function connectToNetwork(ssid: string, password: string): void { function connectToNetwork(ssid: string, password: string): void {
// TODO: Implement password // TODO: Implement password
connectProcess.command = ["nmcli", "conn", "up", ssid]; connectProc.exec(["nmcli", "conn", "up", ssid]);
connectProcess.running = true;
} }
function disconnectFromNetwork(): void { function disconnectFromNetwork(): void {
if (active) { if (active) {
disconnectProcess.command = ["nmcli", "connection", "down", active.ssid]; disconnectProc.exec(["nmcli", "connection", "down", active.ssid]);
disconnectProcess.running = true;
} }
} }
function getWifiStatus(): void { function getWifiStatus(): void {
wifiStatusProcess.running = true; wifiStatusProc.running = true;
} }
Process { Process {
@ -57,7 +52,9 @@ Singleton {
} }
Process { Process {
id: wifiStatusProcess id: wifiStatusProc
running: true
command: ["nmcli", "radio", "wifi"] command: ["nmcli", "radio", "wifi"]
environment: ({ environment: ({
LANG: "C", LANG: "C",
@ -68,42 +65,40 @@ Singleton {
root.wifiEnabled = text.trim() === "enabled"; root.wifiEnabled = text.trim() === "enabled";
} }
} }
Component.onCompleted: running = true
} }
Process { Process {
id: enableWifiProcess id: enableWifiProc
stdout: SplitParser {
onRead: { onExited: {
getWifiStatus(); root.getWifiStatus();
getNetworks.running = true; getNetworks.running = true;
} }
} }
}
Process { Process {
id: rescanProcess id: rescanProc
command: ["nmcli", "dev", "wifi", "list", "--rescan", "yes"] command: ["nmcli", "dev", "wifi", "list", "--rescan", "yes"]
stdout: SplitParser { onExited: {
onRead: {
scanning = false;
getNetworks.running = true; getNetworks.running = true;
} }
} }
}
Process { Process {
id: connectProcess id: connectProc
stdout: SplitParser { stdout: SplitParser {
onRead: getNetworks.running = true onRead: getNetworks.running = true
} }
stderr: SplitParser { stderr: StdioCollector {
onRead: console.warn("Network connection error:", data) onStreamFinished: console.warn("Network connection error:", text)
} }
} }
Process { Process {
id: disconnectProcess id: disconnectProc
stdout: SplitParser { stdout: SplitParser {
onRead: getNetworks.running = true onRead: getNetworks.running = true
} }
@ -111,6 +106,7 @@ Singleton {
Process { Process {
id: getNetworks id: getNetworks
running: true running: true
command: ["nmcli", "-g", "ACTIVE,SIGNAL,FREQ,SSID,BSSID,SECURITY", "d", "w"] command: ["nmcli", "-g", "ACTIVE,SIGNAL,FREQ,SSID,BSSID,SECURITY", "d", "w"]
environment: ({ environment: ({