commits
tags
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import TvLauncher
// Wi‑Fi via NetworkManager (nmcli).
Item {
id: page
readonly property bool available: shell.has("nmcli")
property bool serviceOk: true
property bool enabled: false
property var networks: []
property var known: []
property bool scanning: false
property string busySsid: ""
property string status: ""
function focusFirst() { list.focusFirst() }
// nmcli -t: fält separeras med ':' och ':' i värden skrivs '\:'.
function splitTerse(line) {
const out = []
let cur = ""
for (let i = 0; i < line.length; ++i) {
const ch = line[i]
if (ch === "\\" && i + 1 < line.length) cur += line[++i]
else if (ch === ":") { out.push(cur); cur = "" }
else cur += ch
}
out.push(cur)
return out
}
function refresh(rescan) {
if (!available) return
shell.run("nmcli", ["radio", "wifi"], (code, out) => {
serviceOk = code === 0
enabled = out.trim() === "enabled"
}, 8000)
shell.run("nmcli", ["-t", "-f", "NAME,TYPE", "connection", "show"], (code, out) => {
known = out.split("\n").map(splitTerse).filter(f => f[1] === "802-11-wireless").map(f => f[0])
}, 8000)
if (rescan) scanning = true
shell.run("nmcli", ["-t", "-f", "IN-USE,SIGNAL,SECURITY,SSID", "device", "wifi", "list", "--rescan", rescan ? "yes" : "auto"],
(code, out) => {
scanning = false
if (code !== 0) return
const bySsid = {}
for (const line of out.split("\n")) {
if (line.trim() === "") continue
const f = splitTerse(line)
const ssid = f.slice(3).join(":")
if (ssid === "") continue
const n = { ssid: ssid, inUse: f[0] === "*", signal: parseInt(f[1]) || 0,
secured: f[2] !== "" && f[2] !== "--" }
const old = bySsid[ssid]
if (!old || n.inUse || (!old.inUse && n.signal > old.signal)) bySsid[ssid] = n
}
networks = Object.values(bySsid).sort((a, b) => (b.inUse - a.inUse) || (b.signal - a.signal))
}, 30000)
}
function bars(signal) {
return signal > 75 ? "▂▄▆█" : signal > 50 ? "▂▄▆" : signal > 25 ? "▂▄" : "▂"
}
function finish(ssid, code, out, err) {
busySsid = ""
status = code === 0 ? "Ansluten till " + ssid
: "Kunde inte ansluta: " + ((err || out).trim().split("\n").pop() || "okänt fel")
refresh(false)
}
function activate(n) {
if (busySsid !== "" || n.inUse) return
if (page.known.indexOf(n.ssid) >= 0) {
busySsid = n.ssid
status = "Ansluter till " + n.ssid + " ..."
shell.run("nmcli", ["connection", "up", "id", n.ssid], (c, o, e) => finish(n.ssid, c, o, e), 45000)
} else if (!n.secured) {
busySsid = n.ssid
status = "Ansluter till " + n.ssid + " ..."
shell.run("nmcli", ["device", "wifi", "connect", n.ssid], (c, o, e) => finish(n.ssid, c, o, e), 45000)
} else {
passwordDialog.ssid = n.ssid
passwordDialog.open()
}
}
function connectWithPassword(ssid, password) {
busySsid = ssid
status = "Ansluter till " + ssid + " ..."
shell.run("nmcli", ["device", "wifi", "connect", ssid, "password", password],
(c, o, e) => finish(ssid, c, o, e), 45000)
}
function forget(n) {
if (page.known.indexOf(n.ssid) < 0) return
shell.run("nmcli", ["connection", "delete", "id", n.ssid], () => { status = "Glömde " + n.ssid; refresh(false) }, 8000)
}
Component.onCompleted: refresh(false)
Timer { interval: 10000; running: page.available && page.visible; repeat: true; onTriggered: page.refresh(false) }
CardList {
id: list
anchors.fill: parent
SettingsCard {
visible: !page.available
icon: "wifi"
title: "NetworkManager saknas"
subtitle: "Installera med:\nsudo apt install network-manager\nOm Wi‑Fi är inställt i /etc/network/interfaces behöver det tas bort därifrån."
}
SettingsCard {
visible: page.available && !page.serviceOk
icon: "wifi"
title: "NetworkManager svarar inte"
subtitle: "Starta den med: sudo systemctl enable --now NetworkManager"
onActivated: page.refresh(false)
}
SettingsCard {
visible: page.available && page.serviceOk
icon: "wifi"
title: "Wi‑Fi"
subtitle: page.enabled ? "På" : "Av"
toggle: page.enabled
onActivated: shell.run("nmcli", ["radio", "wifi", page.enabled ? "off" : "on"], () => page.refresh(false))
}
SettingsCard {
visible: page.available && page.serviceOk && page.enabled
icon: "refresh"
title: page.scanning ? "Söker ..." : "Sök efter nätverk"
busy: page.scanning
onActivated: page.refresh(true)
}
Text {
width: parent.width
visible: page.status !== ""
text: page.status
color: Theme.accent
wrapMode: Text.Wrap
topPadding: Theme.dp(10)
font.family: Theme.fontFamily
font.pixelSize: Theme.dp(24)
}
SectionTitle {
visible: page.enabled && page.networks.length > 0
text: "Nätverk"
}
Repeater {
model: page.enabled ? page.networks : []
delegate: SettingsCard {
required property var modelData
icon: modelData.secured ? "lock" : "wifi"
title: modelData.ssid
subtitle: modelData.inUse ? "Ansluten"
: page.known.indexOf(modelData.ssid) >= 0 ? "Sparat · Y: glöm"
: modelData.secured ? "Skyddat" : "Öppet"
valueText: page.bars(modelData.signal)
checked: modelData.inUse
busy: page.busySsid === modelData.ssid
onActivated: page.activate(modelData)
onMenuRequested: page.forget(modelData)
}
}
}
Popup {
id: passwordDialog
property string ssid
parent: Overlay.overlay
anchors.centerIn: parent
width: Math.min(parent.width - Theme.dp(120), Theme.dp(900))
modal: true
focus: true
padding: Theme.dp(40)
background: Rectangle { color: Theme.bg; radius: Theme.r(18); border.color: Theme.borderSoft }
Overlay.modal: Rectangle { color: Theme.scrim }
onOpened: { pw.text = ""; pw.forceActiveFocus() }
function submit() {
if (pw.text.length < 8) return
close()
page.connectWithPassword(ssid, pw.text)
}
contentItem: ColumnLayout {
spacing: Theme.dp(20)
Text {
text: "Lösenord för " + passwordDialog.ssid
color: Theme.text
font.family: Theme.fontFamily
font.pixelSize: Theme.dp(32)
}
TextField {
id: pw
Layout.fillWidth: true
Layout.preferredHeight: Theme.dp(64)
echoMode: showPw.checked ? TextInput.Normal : TextInput.Password
color: Theme.text
font.pixelSize: Theme.dp(28)
leftPadding: Theme.dp(16)
KeyNavigation.down: showPw
onAccepted: passwordDialog.submit()
background: Rectangle {
radius: Theme.r(8)
color: pw.activeFocus ? Theme.surfaceHi : Theme.surface
border.width: pw.activeFocus ? Theme.dp(3) : Theme.dp(1)
border.color: pw.activeFocus ? Theme.accent : Theme.border
}
}
SettingsCard {
id: showPw
property bool checked: false
Layout.fillWidth: true
title: "Visa lösenordet"
toggle: checked
onActivated: checked = !checked
KeyNavigation.up: pw
KeyNavigation.down: connectBtn
}
RowLayout {
spacing: Theme.dp(20)
TvButton {
id: connectBtn
text: "Anslut"
enabled: pw.text.length >= 8
onClicked: passwordDialog.submit()
KeyNavigation.up: showPw
KeyNavigation.right: cancelBtn
}
TvButton {
id: cancelBtn
text: "Avbryt"
onClicked: passwordDialog.close()
KeyNavigation.up: showPw
}
}
}
}
}