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

@ -51,7 +51,7 @@ NPanel {
id: wifiSwitch id: wifiSwitch
checked: Settings.data.network.wifiEnabled checked: Settings.data.network.wifiEnabled
onToggled: checked => NetworkService.setWifiEnabled(checked) onToggled: checked => NetworkService.setWifiEnabled(checked)
baseSize: Style.baseWidgetSize * 0.7 * scaling baseSize: Style.baseWidgetSize * 0.65 * scaling
} }
NIconButton { NIconButton {
@ -122,9 +122,11 @@ NPanel {
ColumnLayout { ColumnLayout {
visible: !Settings.data.network.wifiEnabled visible: !Settings.data.network.wifiEnabled
anchors.fill: parent anchors.fill: parent
spacing: Style.marginL * scaling spacing: Style.marginM * scaling
Item { Layout.fillHeight: true } Item {
Layout.fillHeight: true
}
NIcon { NIcon {
text: "wifi_off" text: "wifi_off"
@ -141,22 +143,27 @@ NPanel {
} }
NText { NText {
text: "Enable Wi-Fi to see available networks" text: "Enable Wi-Fi to see available networks."
font.pointSize: Style.fontSizeS * scaling font.pointSize: Style.fontSizeS * scaling
color: Color.mOnSurfaceVariant color: Color.mOnSurfaceVariant
Layout.alignment: Qt.AlignHCenter Layout.alignment: Qt.AlignHCenter
} }
Item { Layout.fillHeight: true } Item {
Layout.fillHeight: true
}
} }
// Scanning state // Scanning state
ColumnLayout { ColumnLayout {
visible: Settings.data.network.wifiEnabled && NetworkService.scanning && Object.keys(NetworkService.networks).length === 0 visible: Settings.data.network.wifiEnabled && NetworkService.scanning && Object.keys(
NetworkService.networks).length === 0
anchors.fill: parent anchors.fill: parent
spacing: Style.marginL * scaling spacing: Style.marginL * scaling
Item { Layout.fillHeight: true } Item {
Layout.fillHeight: true
}
NBusyIndicator { NBusyIndicator {
running: true running: true
@ -172,12 +179,15 @@ NPanel {
Layout.alignment: Qt.AlignHCenter Layout.alignment: Qt.AlignHCenter
} }
Item { Layout.fillHeight: true } Item {
Layout.fillHeight: true
}
} }
// Networks list container // Networks list container
ScrollView { ScrollView {
visible: Settings.data.network.wifiEnabled && (!NetworkService.scanning || Object.keys(NetworkService.networks).length > 0) visible: Settings.data.network.wifiEnabled && (!NetworkService.scanning || Object.keys(
NetworkService.networks).length > 0)
anchors.fill: parent anchors.fill: parent
ScrollBar.horizontal.policy: ScrollBar.AlwaysOff ScrollBar.horizontal.policy: ScrollBar.AlwaysOff
ScrollBar.vertical.policy: ScrollBar.AsNeeded ScrollBar.vertical.policy: ScrollBar.AsNeeded
@ -190,20 +200,23 @@ NPanel {
// Network list // Network list
Repeater { Repeater {
model: { model: {
if (!Settings.data.network.wifiEnabled) return [] if (!Settings.data.network.wifiEnabled)
return []
const nets = Object.values(NetworkService.networks) const nets = Object.values(NetworkService.networks)
return nets.sort((a, b) => { return nets.sort((a, b) => {
if (a.connected !== b.connected) return b.connected - a.connected if (a.connected !== b.connected)
return b.signal - a.signal return b.connected - a.connected
}) return b.signal - a.signal
})
} }
Rectangle { Rectangle {
Layout.fillWidth: true Layout.fillWidth: true
implicitHeight: netColumn.implicitHeight + (Style.marginM * scaling * 2) implicitHeight: netColumn.implicitHeight + (Style.marginM * scaling * 2)
radius: Style.radiusM * scaling radius: Style.radiusM * scaling
color: modelData.connected ? Qt.rgba(Color.mPrimary.r, Color.mPrimary.g, Color.mPrimary.b, 0.05) : Color.mSurface color: modelData.connected ? Qt.rgba(Color.mPrimary.r, Color.mPrimary.g, Color.mPrimary.b,
0.05) : Color.mSurface
border.width: Math.max(1, Style.borderS * scaling) border.width: Math.max(1, Style.borderS * scaling)
border.color: modelData.connected ? Color.mPrimary : Color.mOutline border.color: modelData.connected ? Color.mPrimary : Color.mOutline
@ -262,7 +275,6 @@ NPanel {
Item { Item {
Layout.preferredWidth: Style.marginXXS * scaling Layout.preferredWidth: Style.marginXXS * scaling
} }
Rectangle { Rectangle {
visible: modelData.connected visible: modelData.connected
@ -302,7 +314,7 @@ NPanel {
// Action area // Action area
RowLayout { RowLayout {
spacing: Style.marginXS * scaling spacing: Style.marginS * scaling
NBusyIndicator { NBusyIndicator {
visible: NetworkService.connectingTo === modelData.ssid visible: NetworkService.connectingTo === modelData.ssid
@ -311,11 +323,23 @@ NPanel {
size: Style.baseWidgetSize * 0.5 * scaling size: Style.baseWidgetSize * 0.5 * scaling
} }
NIconButton {
visible: (modelData.existing || modelData.cached) && !modelData.connected
&& NetworkService.connectingTo !== modelData.ssid
icon: "delete"
tooltipText: "Forget network"
sizeRatio: 0.7
onClicked: expandedSsid = expandedSsid === modelData.ssid ? "" : modelData.ssid
}
NButton { NButton {
visible: !modelData.connected && NetworkService.connectingTo !== modelData.ssid && passwordSsid !== modelData.ssid visible: !modelData.connected && NetworkService.connectingTo !== modelData.ssid
&& passwordSsid !== modelData.ssid
text: { text: {
if (modelData.existing || modelData.cached) return "Connect" if (modelData.existing || modelData.cached)
if (!NetworkService.isSecured(modelData.security)) return "Connect" return "Connect"
if (!NetworkService.isSecured(modelData.security))
return "Connect"
return "Password" return "Password"
} }
outlined: !hovered outlined: !hovered
@ -339,13 +363,6 @@ NPanel {
backgroundColor: Color.mError backgroundColor: Color.mError
onClicked: NetworkService.disconnect(modelData.ssid) onClicked: NetworkService.disconnect(modelData.ssid)
} }
NIconButton {
visible: (modelData.existing || modelData.cached) && !modelData.connected && NetworkService.connectingTo !== modelData.ssid
icon: "more_vert"
sizeRatio: 0.7
onClicked: expandedSsid = expandedSsid === modelData.ssid ? "" : modelData.ssid
}
} }
} }
@ -353,21 +370,24 @@ NPanel {
Rectangle { Rectangle {
visible: passwordSsid === modelData.ssid visible: passwordSsid === modelData.ssid
Layout.fillWidth: true Layout.fillWidth: true
height: 40 * scaling height: passwordRow.implicitHeight + Style.marginS * scaling * 2
color: Color.mSurfaceVariant color: Color.mSurfaceVariant
border.color: Color.mOutline
border.width: Math.max(1, Style.borderS * scaling)
radius: Style.radiusS * scaling radius: Style.radiusS * scaling
RowLayout { RowLayout {
id: passwordRow
anchors.fill: parent anchors.fill: parent
anchors.margins: Style.marginXS * scaling anchors.margins: Style.marginS * scaling
spacing: Style.marginXS * scaling spacing: Style.marginM * scaling
Rectangle { Rectangle {
Layout.fillWidth: true Layout.fillWidth: true
Layout.fillHeight: true Layout.fillHeight: true
radius: Style.radiusXS * scaling radius: Style.radiusXS * scaling
color: Color.mSurface color: Color.mSurface
border.color: pwdInput.activeFocus ? Color.mPrimary : Color.mOutline border.color: pwdInput.activeFocus ? Color.mSecondary : Color.mOutline
border.width: Math.max(1, Style.borderS * scaling) border.width: Math.max(1, Style.borderS * scaling)
TextInput { TextInput {
@ -382,7 +402,10 @@ NPanel {
echoMode: TextInput.Password echoMode: TextInput.Password
selectByMouse: true selectByMouse: true
focus: visible focus: visible
passwordCharacter: "●"
onTextChanged: passwordInput = text onTextChanged: passwordInput = text
onVisibleChanged: if (visible)
forceActiveFocus()
onAccepted: { onAccepted: {
if (text) { if (text) {
NetworkService.connect(passwordSsid, text) NetworkService.connect(passwordSsid, text)
@ -405,6 +428,7 @@ NPanel {
text: "Connect" text: "Connect"
fontSize: Style.fontSizeXXS * scaling fontSize: Style.fontSizeXXS * scaling
enabled: passwordInput.length > 0 enabled: passwordInput.length > 0
outlined: true
onClicked: { onClicked: {
NetworkService.connect(passwordSsid, passwordInput) NetworkService.connect(passwordSsid, passwordInput)
passwordSsid = "" passwordSsid = ""
@ -414,7 +438,7 @@ NPanel {
NIconButton { NIconButton {
icon: "close" icon: "close"
sizeRatio: 0.6 sizeRatio: 0.8
onClicked: { onClicked: {
passwordSsid = "" passwordSsid = ""
passwordInput = "" passwordInput = ""
@ -423,38 +447,43 @@ NPanel {
} }
} }
// Options menu // Forget network
Rectangle { Rectangle {
visible: expandedSsid === modelData.ssid visible: expandedSsid === modelData.ssid
Layout.fillWidth: true Layout.fillWidth: true
height: forgetRow.implicitHeight + Style.marginS * 2 height: forgetRow.implicitHeight + Style.marginS * 2 * scaling
color: Color.mSurfaceVariant color: Color.mSurfaceVariant
radius: Style.radiusS * scaling radius: Style.radiusS * scaling
border.width: Math.max(1, Style.borderS * scaling) border.width: Math.max(1, Style.borderS * scaling)
border.color: Color.mError border.color: Color.mOutline
RowLayout { RowLayout {
id: forgetRow id: forgetRow
anchors.fill: parent anchors.fill: parent
anchors.margins: Style.marginS * scaling anchors.margins: Style.marginS * scaling
spacing: Style.marginM * scaling
NIcon { RowLayout {
text: "delete_outline" NIcon {
font.pointSize: Style.fontSizeM * scaling text: "delete_outline"
color: Color.mError font.pointSize: Style.fontSizeL * scaling
} color: Color.mError
}
NText { NText {
text: "Forget this network?" text: "Forget this network?"
font.pointSize: Style.fontSizeS * scaling font.pointSize: Style.fontSizeS * scaling
color: Color.mError color: Color.mError
Layout.fillWidth: true Layout.fillWidth: true
}
} }
NButton { NButton {
id: forgetButton
text: "Forget" text: "Forget"
fontSize: Style.fontSizeXXS * scaling fontSize: Style.fontSizeXXS * scaling
backgroundColor: Color.mError backgroundColor: Color.mError
outlined: forgetButton.hovered ? false : true
onClicked: { onClicked: {
NetworkService.forget(modelData.ssid) NetworkService.forget(modelData.ssid)
expandedSsid = "" expandedSsid = ""
@ -463,7 +492,7 @@ NPanel {
NIconButton { NIconButton {
icon: "close" icon: "close"
sizeRatio: 0.6 sizeRatio: 0.8
onClicked: expandedSsid = "" onClicked: expandedSsid = ""
} }
} }
@ -476,11 +505,14 @@ NPanel {
// Empty state when no networks // Empty state when no networks
ColumnLayout { ColumnLayout {
visible: Settings.data.network.wifiEnabled && !NetworkService.scanning && Object.keys(NetworkService.networks).length === 0 visible: Settings.data.network.wifiEnabled && !NetworkService.scanning && Object.keys(
NetworkService.networks).length === 0
anchors.fill: parent anchors.fill: parent
spacing: Style.marginL * scaling spacing: Style.marginL * scaling
Item { Layout.fillHeight: true } Item {
Layout.fillHeight: true
}
NIcon { NIcon {
text: "wifi_find" text: "wifi_find"
@ -503,9 +535,11 @@ NPanel {
onClicked: NetworkService.scan() onClicked: NetworkService.scan()
} }
Item { Layout.fillHeight: true } Item {
Layout.fillHeight: true
}
} }
} }
} }
} }
} }

View file

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