Wifi: more clean ups and improvements

This commit is contained in:
LemmyCook 2025-09-06 00:36:29 -04:00
parent a57bfeba31
commit 2398961473
2 changed files with 181 additions and 116 deletions

View file

@ -14,7 +14,8 @@ Singleton {
property bool connecting: false
property string connectingTo: ""
property string lastError: ""
property bool ethernet: false
// Persistent cache
property string cacheFile: Settings.cacheDir + "network.json"
readonly property string cachedLastConnected: cacheAdapter.lastConnected
@ -24,13 +25,13 @@ Singleton {
FileView {
id: cacheFileView
path: root.cacheFile
JsonAdapter {
id: cacheAdapter
property var knownNetworks: ({})
property string lastConnected: ""
}
onLoadFailed: {
cacheAdapter.knownNetworks = ({})
cacheAdapter.lastConnected = ""
@ -79,26 +80,29 @@ Singleton {
function setWifiEnabled(enabled) {
Settings.data.network.wifiEnabled = enabled
wifiToggleProcess.action = enabled ? "on" : "off"
wifiToggleProcess.running = true
}
function scan() {
if (scanning) return
if (scanning)
return
scanning = true
lastError = ""
scanProcess.running = true
ethernetStateProcess.running = true
}
function connect(ssid, password = "") {
if (connecting) return
if (connecting)
return
connecting = true
connectingTo = ssid
lastError = ""
// Check if we have a saved connection
if (networks[ssid]?.existing || cachedNetworks[ssid]) {
connectProcess.mode = "saved"
@ -109,7 +113,7 @@ Singleton {
connectProcess.ssid = ssid
connectProcess.password = password
}
connectProcess.running = true
}
@ -123,13 +127,13 @@ Singleton {
let known = cacheAdapter.knownNetworks
delete known[ssid]
cacheAdapter.knownNetworks = known
if (cacheAdapter.lastConnected === ssid) {
cacheAdapter.lastConnected = ""
}
saveCache()
// Remove from system
forgetProcess.ssid = ssid
forgetProcess.running = true
@ -137,10 +141,14 @@ Singleton {
// Helper functions
function signalIcon(signal) {
if (signal >= 80) return "network_wifi"
if (signal >= 60) return "network_wifi_3_bar"
if (signal >= 40) return "network_wifi_2_bar"
if (signal >= 20) return "network_wifi_1_bar"
if (signal >= 80)
return "network_wifi"
if (signal >= 60)
return "network_wifi_3_bar"
if (signal >= 40)
return "network_wifi_2_bar"
if (signal >= 20)
return "network_wifi_1_bar"
return "signal_wifi_0_bar"
}
@ -149,14 +157,32 @@ Singleton {
}
// Processes
Process {
id: ethernetStateProcess
running: false
command: ["nmcli", "-t", "-f", "DEVICE,TYPE,STATE", "device"]
stdout: StdioCollector {
onStreamFinished: {
root.ethernet = text.split("\n").some(line => {
const parts = line.split(":")
return parts[1] === "ethernet" && parts[2] === "connected"
})
Logger.log("Network", "Ethernet connected:", root.ethernet)
}
}
}
Process {
id: wifiStateProcess
running: false
command: ["nmcli", "radio", "wifi"]
stdout: StdioCollector {
onStreamFinished: {
const enabled = text.trim() === "enabled"
Logger.log("Network", "Wifi enabled:", enabled)
if (Settings.data.network.wifiEnabled !== enabled) {
Settings.data.network.wifiEnabled = enabled
}
@ -169,19 +195,20 @@ Singleton {
property string action: "on"
running: false
command: ["nmcli", "radio", "wifi", action]
onRunningChanged: {
if (!running) {
if (action === "on") {
// Clear networks immediately and start delayed scan
root.networks = ({})
delayedScanTimer.interval = 8000
delayedScanTimer.restart()
} else {
root.networks = ({})
}
}
}
stderr: StdioCollector {
onStreamFinished: {
if (text.trim()) {
@ -197,66 +224,69 @@ Singleton {
command: ["sh", "-c", `
# Get existing profiles
profiles=$(nmcli -t -f NAME,TYPE connection show | grep ':802-11-wireless' | cut -d: -f1)
# Get WiFi networks
nmcli -t -f SSID,SECURITY,SIGNAL,IN-USE device wifi list | while read line; do
ssid=$(echo "$line" | cut -d: -f1)
security=$(echo "$line" | cut -d: -f2)
signal=$(echo "$line" | cut -d: -f3)
in_use=$(echo "$line" | cut -d: -f4)
# Skip empty SSIDs
if [ -z "$ssid" ]; then
continue
fi
existing=false
if echo "$profiles" | grep -q "^$ssid$"; then
existing=true
fi
echo "$ssid|$security|$signal|$in_use|$existing"
ssid=$(echo "$line" | cut -d: -f1)
security=$(echo "$line" | cut -d: -f2)
signal=$(echo "$line" | cut -d: -f3)
in_use=$(echo "$line" | cut -d: -f4)
# Skip empty SSIDs
if [ -z "$ssid" ]; then
continue
fi
existing=false
if echo "$profiles" | grep -q "^$ssid$"; then
existing=true
fi
echo "$ssid|$security|$signal|$in_use|$existing"
done
`]
`]
stdout: StdioCollector {
onStreamFinished: {
const nets = {}
const lines = text.split("\n").filter(l => l.trim())
for (const line of lines) {
const parts = line.split("|")
if (parts.length < 5) continue
if (parts.length < 5)
continue
const ssid = parts[0]
if (!ssid || ssid.trim() === "") continue
if (!ssid || ssid.trim() === "")
continue
const network = {
ssid: ssid,
security: parts[1] || "--",
signal: parseInt(parts[2]) || 0,
connected: parts[3] === "*",
existing: parts[4] === "true",
cached: ssid in cacheAdapter.knownNetworks
"ssid": ssid,
"security": parts[1] || "--",
"signal": parseInt(parts[2]) || 0,
"connected": parts[3] === "*",
"existing": parts[4] === "true",
"cached": ssid in cacheAdapter.knownNetworks
}
// Track connected network
if (network.connected && cacheAdapter.lastConnected !== ssid) {
cacheAdapter.lastConnected = ssid
saveCache()
}
// Keep best signal for duplicate SSIDs
if (!nets[ssid] || network.signal > nets[ssid].signal) {
nets[ssid] = network
}
}
root.networks = nets
root.scanning = false
Logger.log("Network", "Discovered", Object.keys(root.networks).length, "Wi-Fi networks")
}
}
stderr: StdioCollector {
onStreamFinished: {
root.scanning = false
@ -278,7 +308,7 @@ Singleton {
property string ssid: ""
property string password: ""
running: false
command: {
if (mode === "saved") {
return ["nmcli", "connection", "up", "id", ssid]
@ -290,38 +320,39 @@ Singleton {
return cmd
}
}
stdout: StdioCollector {
onStreamFinished: {
// Success - update cache
let known = cacheAdapter.knownNetworks
known[connectProcess.ssid] = {
profileName: connectProcess.ssid,
lastConnected: Date.now()
"profileName": connectProcess.ssid,
"lastConnected": Date.now()
}
cacheAdapter.knownNetworks = known
cacheAdapter.lastConnected = connectProcess.ssid
saveCache()
root.connecting = false
root.connectingTo = ""
Logger.log("Network", "Connected to " + connectProcess.ssid)
// Rescan to update status
delayedScanTimer.interval = 1000
delayedScanTimer.restart()
}
}
stderr: StdioCollector {
onStreamFinished: {
root.connecting = false
root.connectingTo = ""
if (text.trim()) {
// Parse common errors
if (text.includes("Secrets were required") || text.includes("no secrets provided")) {
root.lastError = "Incorrect password"
forget(connectProcess.ssid)
} else if (text.includes("No network with SSID")) {
root.lastError = "Network not found"
} else if (text.includes("Timeout")) {
@ -329,7 +360,7 @@ Singleton {
} else {
root.lastError = text.split("\n")[0].trim()
}
Logger.warn("Network", "Connect error: " + text)
}
}
@ -341,7 +372,7 @@ Singleton {
property string ssid: ""
running: false
command: ["nmcli", "connection", "down", "id", ssid]
onRunningChanged: {
if (!running) {
delayedScanTimer.interval = 1000
@ -355,7 +386,7 @@ Singleton {
property string ssid: ""
running: false
command: ["nmcli", "connection", "delete", "id", ssid]
onRunningChanged: {
if (!running) {
delayedScanTimer.interval = 1000
@ -363,4 +394,4 @@ Singleton {
}
}
}
}
}