summaryrefslogtreecommitdiff
path: root/services/Network.qml
blob: b4827c8d1770c280f19d94ea306371e534306962 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
pragma Singleton

import Quickshell
import Quickshell.Io
import QtQuick

Singleton {
    id: root

    readonly property list<AccessPoint> networks: []
    readonly property AccessPoint active: networks.find(n => n.active) ?? null

    reloadableId: "network"

    Process {
        running: true
        command: ["nmcli", "m"]
        stdout: SplitParser {
            onRead: getNetworks.running = true
        }
    }

    Process {
        id: getNetworks
        running: true
        command: ["sh", "-c", `nmcli -g ACTIVE,SIGNAL,FREQ,SSID,BSSID d w | jq -ncR '[(inputs | split("(?<!\\\\\\\\):"; "g")) | select(.[3] | length >= 4)]'`]
        stdout: SplitParser {
            onRead: data => {
                const networks = JSON.parse(data).map(n => [n[0] === "yes", parseInt(n[1]), parseInt(n[2]), n[3], n[4].replace(/\\/g, "")]);
                const rNetworks = root.networks;

                const destroyed = rNetworks.filter(rn => !networks.find(n => n[2] === rn.frequency && n[3] === rn.ssid && n[4] === rn.bssid));
                for (const network of destroyed)
                    rNetworks.splice(rNetworks.indexOf(network), 1).forEach(n => n.destroy());

                for (const network of networks) {
                    const match = rNetworks.find(n => n.frequency === network[2] && n.ssid === network[3] && n.bssid === network[4]);
                    if (match) {
                        match.active = network[0];
                        match.strength = network[1];
                        match.frequency = network[2];
                        match.ssid = network[3];
                        match.bssid = network[4];
                    } else {
                        rNetworks.push(apComp.createObject(root, {
                            active: network[0],
                            strength: network[1],
                            frequency: network[2],
                            ssid: network[3],
                            bssid: network[4]
                        }));
                    }
                }
            }
        }
    }

    component AccessPoint: QtObject {
        required property string ssid
        required property string bssid
        required property int strength
        required property int frequency
        required property bool active
    }

    Component {
        id: apComp

        AccessPoint {}
    }
}