diff options
Diffstat (limited to 'desktop/modules/network/NetworkModule.qml')
| -rw-r--r-- | desktop/modules/network/NetworkModule.qml | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/desktop/modules/network/NetworkModule.qml b/desktop/modules/network/NetworkModule.qml new file mode 100644 index 0000000..d14853f --- /dev/null +++ b/desktop/modules/network/NetworkModule.qml @@ -0,0 +1,110 @@ +// Copyright (C) 2026 Danilo M. <danix@danix.xyz> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +import Quickshell +import Quickshell.Networking +import Quickshell.Io +import QtQuick +import "../.." + +// Always active: the backends push and there is no poll to gate, so the +// state the tile shows is live from launch. Referencing Networking here is +// also what instantiates the singleton at shell start. +Module { + id: mod + + name: "network" + label: "Network" + alwaysActive: true + + // .values on the device ObjectModel; empty until the backend is ready, + // roughly two seconds in. Bindings repaint when it arrives. + readonly property var devices: Networking.devices ? Networking.devices.values : [] + readonly property var wiredDevices: devices.filter(d => d.type === DeviceType.Wired) + readonly property var wifiDevice: devices.find(d => d.type === DeviceType.Wifi) ?? null + + readonly property bool wiredUp: wiredDevices.some(d => d.connected) + readonly property bool wifiUp: wifiDevice ? wifiDevice.connected : false + readonly property bool wifiOn: Networking.wifiEnabled + readonly property var wifiNetworks: wifiDevice && wifiDevice.networks ? wifiDevice.networks.values : [] + + readonly property string wifiSsid: { + const n = wifiNetworks.find(x => x.connected); + return n ? n.name : ""; + } + readonly property string wiredName: { + const d = wiredDevices.find(x => x.connected); + return d ? d.name : ""; + } + + // Connected first, then known, then strongest. Kept here so both the + // tile and the page read the same order. + readonly property var wifiNetworksSorted: { + const arr = wifiNetworks.slice(); + arr.sort((a, b) => + ((b.connected ? 1 : 0) - (a.connected ? 1 : 0)) || + ((b.known ? 1 : 0) - (a.known ? 1 : 0)) || + ((b.signalStrength ?? 0) - (a.signalStrength ?? 0))); + return arr; + } + + // The icon follows the active link, live, with no contract change: the + // drawer reads this property like any other. + icon: mod.wiredUp ? "\uf796" : (mod.wifiUp ? "\uf1eb" : "\uf05e") + + // The network a connect is pending on, so connectionFailed can be caught. + property var pendingNetwork: null + property string error: "" + + function connect(net) { + mod.pendingNetwork = net; + mod.error = ""; + net.connect(); + } + + function connectWithPsk(net, psk) { + mod.pendingNetwork = net; + mod.error = ""; + net.connectWithPsk(psk); + } + + function disconnectNetwork(net) { if (net) net.disconnect(); } + function forget(net) { if (net) net.forget(); } + + // A failure after the drawer closed would otherwise go unseen, so it also + // raises a notification. Same cached-Process shape as VmModule. + function notify(title, body) { + notifyProc.command = ["notify-send", "--app-name=network", + "--urgency=critical", "--icon=error", title, body]; + notifyProc.running = false; + notifyProc.running = true; + } + + property Process notifyProc: Process {} + + property Connections conn: Connections { + target: mod.pendingNetwork + function onConnectionFailed(reason) { + mod.error = "Connection failed: " + reason; + mod.notify("Network", mod.error); + mod.pendingNetwork = null; + } + } + + tileContent: Component { NetworkTile { net: mod } } + + page: Component { + Page { + title: "Network" + NetworkPage { width: parent.width; net: mod } + } + } +} |
