/ services / Network.qml
Network.qml
 1  pragma Singleton
 2  
 3  import Quickshell
 4  import Quickshell.Io
 5  import QtQuick
 6  
 7  Singleton {
 8      id: root
 9  
10      readonly property list<AccessPoint> networks: []
11      readonly property AccessPoint active: networks.find(n => n.active) ?? null
12  
13      reloadableId: "network"
14  
15      Process {
16          running: true
17          command: ["nmcli", "m"]
18          stdout: SplitParser {
19              onRead: getNetworks.running = true
20          }
21      }
22  
23      Process {
24          id: getNetworks
25          running: true
26          command: ["sh", "-c", `nmcli -g ACTIVE,SIGNAL,FREQ,SSID d w | jq -cR '[(inputs / ":") | select(.[3] | length >= 4)]'`]
27          stdout: SplitParser {
28              onRead: data => {
29                  const networks = JSON.parse(data).map(n => [n[0] === "yes", parseInt(n[1]), parseInt(n[2]), n[3]]);
30                  const rNetworks = root.networks;
31  
32                  const destroyed = rNetworks.filter(rn => !networks.find(n => n[2] === rn.frequency && n[3] === rn.ssid));
33                  for (const network of destroyed)
34                      rNetworks.splice(rNetworks.indexOf(network), 1).forEach(n => n.destroy());
35  
36                  for (const network of networks) {
37                      const match = rNetworks.find(n => n.frequency === network[2] && n.ssid === network[3]);
38                      if (match) {
39                          match.active = network[0];
40                          match.strength = network[1];
41                          match.frequency = network[2];
42                          match.ssid = network[3];
43                      } else {
44                          rNetworks.push(apComp.createObject(root, {
45                              active: network[0],
46                              strength: network[1],
47                              frequency: network[2],
48                              ssid: network[3]
49                          }));
50                      }
51                  }
52              }
53          }
54      }
55  
56      component AccessPoint: QtObject {
57          required property string ssid
58          required property int strength
59          required property int frequency
60          required property bool active
61      }
62  
63      Component {
64          id: apComp
65  
66          AccessPoint {}
67      }
68  }