-
-
Notifications
You must be signed in to change notification settings - Fork 445
Expand file tree
/
Copy pathNetworkService.qml
More file actions
1186 lines (1068 loc) · 37 KB
/
NetworkService.qml
File metadata and controls
1186 lines (1068 loc) · 37 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
pragma Singleton
import QtQuick
import Quickshell
import Quickshell.Io
import Quickshell.Networking
import qs.Commons
import qs.Services.System
import qs.Services.UI
Singleton {
id: root
// Shared core (read-only) properties
readonly property bool wifiAvailable: _wifiAvailable
readonly property bool ethernetAvailable: _ethernetAvailable
readonly property bool internetConnectivity: _internetConnectivity
readonly property string networkConnectivity: _networkConnectivity
// Supported Wi-Fi security types
readonly property var supportedSecurityTypes: [
{
key: "open",
name: I18n.tr("wifi.panel.security-open")
},
{
key: "wep",
name: I18n.tr("wifi.panel.security-wep")
},
{
key: "wpa-psk",
name: I18n.tr("wifi.panel.security-wpa")
},
{
key: "wpa2-psk",
name: I18n.tr("wifi.panel.security-wpa23")
},
{
key: "sae",
name: I18n.tr("wifi.panel.security-wpa3")
},
{
key: "wpa-eap",
name: I18n.tr("wifi.panel.security-wpa-ent")
},
{
key: "wpa2-eap",
name: I18n.tr("wifi.panel.security-wpa2-ent")
},
{
key: "wpa3-eap",
name: I18n.tr("wifi.panel.security-wpa3-ent")
}
]
// Core properties
property bool _wifiAvailable: false
property bool _ethernetAvailable: false
property string _networkConnectivity: "unknown"
property bool _internetConnectivity: false
property string lastError: ""
property int activeDetailsTtlMs: 10000
// Ethernet properties
property var ethernetInterfaces: ([])
property var activeEthernetDetails: ({})
property bool ethernetConnected: false
property string activeEthernetIf: ""
property bool ethernetDetailsLoading: false
property double activeEthernetDetailsTimestamp: 0
// Wi-Fi properties
readonly property bool wifiEnabled: Networking.wifiEnabled
property var networks: ({})
property var activeWifiDetails: ({})
property bool wifiConnected: false
property string activeWifiIf: ""
property bool wifiDetailsLoading: false
property double activeWifiDetailsTimestamp: 0
property bool wifiInit: false
// Wi-Fi adapter/connection properties
property bool connecting: false
property string connectingTo: ""
property string disconnectingFrom: ""
property string forgettingNetwork: ""
property bool scanPending: false
property bool scanningActive: false
property var existingProfiles: ({})
// Airplane mode status
property bool airplaneModeEnabled: false
property bool airplaneModeToggled: false
Connections {
target: root
function onWifiEnabledChanged() {
if (!root.wifiInit) {
return;
}
wifiDebounce.restart();
}
}
// Handle system resume to refresh network status and trigger a scan
Connections {
target: Time
function onResumed() {
Logger.i("Network", "System resumed - refreshing state");
deviceStatusProcess.running = true;
connectivityCheckProcess.running = true;
scan();
}
}
// Start initial checks when nmcli becomes available
Connections {
target: ProgramCheckerService
function onNmcliAvailableChanged() {
if (ProgramCheckerService.nmcliAvailable) {
deviceStatusProcess.running = true;
connectivityCheckProcess.running = true;
}
}
}
Component.onCompleted: {
Logger.i("Network", "Service started");
wifiInitTimer.running = true;
// Ensure initial detection if nmcli is already available at startup
if (ProgramCheckerService.nmcliAvailable) {
deviceStatusProcess.running = true;
connectivityCheckProcess.running = true;
}
}
// Prevent an initial "Wi-Fi enabled" toast and trigger initial scan
Timer {
id: wifiInitTimer
interval: 500
onTriggered: {
root.wifiInit = true;
if (root.wifiEnabled) {
scan();
}
if (!root.wifiEnabled && BluetoothService.blocked) {
root.airplaneModeEnabled = true;
}
}
}
// Debounce to prevent multiple toast notifications from transient states
Timer {
id: wifiDebounce
interval: 300
onTriggered: {
if (!ProgramCheckerService.nmcliAvailable) {
return;
}
if (root.airplaneModeToggled) {
root.airplaneModeToggled = false;
if (root.wifiEnabled) {
scan();
} else {
root.networks = ({});
}
return;
}
var isAirplaneModeActive = !root.wifiEnabled && BluetoothService.blocked;
// Extra check for Airplane Mode if Bluetooth has been blocked before Wi-Fi
if (isAirplaneModeActive && !root.airplaneModeEnabled) {
root.airplaneModeEnabled = true;
ToastService.showNotice(I18n.tr("toast.airplane-mode.title"), I18n.tr("common.enabled"), "plane");
Logger.i("AirplaneMode", "Enabled");
root.networks = ({});
return;
}
// Extra check for Airplane Mode if Wi-Fi has been unblocked before Bluetooth
if (!isAirplaneModeActive && root.airplaneModeEnabled) {
root.airplaneModeEnabled = false;
ToastService.showNotice(I18n.tr("toast.airplane-mode.title"), I18n.tr("common.disabled"), "plane-off");
Logger.i("AirplaneMode", "Disabled");
scan();
return;
}
if (root.wifiEnabled) {
ToastService.showNotice(I18n.tr("common.wifi"), I18n.tr("common.enabled"), "wifi");
scan();
} else {
ToastService.showNotice(I18n.tr("common.wifi"), I18n.tr("common.disabled"), "wifi-off");
root.networks = ({});
}
}
}
// Internet connectivity check timer
Timer {
id: connectivityCheckTimer
interval: 15000
running: ProgramCheckerService.nmcliAvailable && (root.ethernetConnected || root.wifiConnected)
repeat: true
onTriggered: connectivityCheckProcess.running = true
}
// Delayed scan timer
Timer {
id: delayedScanTimer
interval: 7000
onTriggered: scan()
}
// Timer to restart the monitor process if it exits (e.g. after sleep or NM restart)
Timer {
id: monitorRestartTimer
interval: 2000
repeat: false
onTriggered: networkMonitorProcess.running = true
}
// Core functions
function setWifiEnabled(enabled) {
if (!ProgramCheckerService.nmcliAvailable) {
return;
}
Logger.i("Wi-Fi", "SetWifiEnabled", enabled);
Networking.wifiEnabled = enabled;
}
function setAirplaneMode(state) {
if (state) {
Quickshell.execDetached(["rfkill", "block", "all"]);
} else {
Quickshell.execDetached(["rfkill", "unblock", "all"]);
}
}
function scan() {
if (!ProgramCheckerService.nmcliAvailable || !root.wifiEnabled) {
return;
}
lastError = "";
// If scanning in progress, mark as pending to trigger another scan when current when finished.
if (profileCheckProcess.running || scanProcess.running) {
root.scanPending = true;
return;
}
// Get existing profiles first, then scan
profileCheckProcess.running = true;
root.scanningActive = true;
Logger.d("Network", "Scanning Wi-Fi networks...");
}
function connect(ssid, password = "", isHidden = false, securityKey = "", identity = "", enterpriseConfig = {}) {
if (!ProgramCheckerService.nmcliAvailable || connecting) {
return;
}
const isSaved = (networks[ssid] && networks[ssid].existing);
const isEnt = securityKey ? isEnterprise(securityKey) : isEnterprise(networks[ssid] ? networks[ssid].security : "");
connecting = true;
connectingTo = ssid;
lastError = "";
connectProcess.ssid = ssid;
connectProcess.password = password;
connectProcess.isHidden = isHidden;
if (isSaved) {
connectProcess.mode = "saved";
} else if (isEnt || securityKey === "wep" || (securityKey && securityKey !== "open" && securityKey !== "wpa-psk" && securityKey !== "wpa2-psk")) {
connectProcess.mode = "manual";
connectProcess.securityKey = securityKey || (networks[ssid] ? networks[ssid].security : "wpa-psk");
connectProcess.identity = identity;
connectProcess.eap = enterpriseConfig.eap || "peap";
connectProcess.phase2 = enterpriseConfig.phase2 || "mschapv2";
connectProcess.anonIdentity = enterpriseConfig.anonIdentity || "";
connectProcess.caCert = enterpriseConfig.caCert || "";
} else {
connectProcess.mode = "new";
}
connectProcess.running = true;
}
function disconnect(ssid) {
if (!ProgramCheckerService.nmcliAvailable) {
return;
}
disconnectingFrom = ssid;
disconnectProcess.ssid = ssid;
disconnectProcess.running = true;
}
function forget(ssid) {
if (!ProgramCheckerService.nmcliAvailable) {
return;
}
forgettingNetwork = ssid;
// Remove from system
forgetProcess.ssid = ssid;
forgetProcess.running = true;
}
// Refresh details for the currently active Wi‑Fi link
function refreshActiveWifiDetails() {
const now = Date.now();
if (wifiDetailsLoading || (activeWifiIf && wifiConnected && activeWifiDetails && (now - activeWifiDetailsTimestamp) < activeDetailsTtlMs)) {
return;
}
if (wifiConnected && activeWifiIf) {
wifiDetailsLoading = true;
deviceStatusProcess.running = true;
}
}
// Refresh details for the currently active Ethernet link
function refreshActiveEthernetDetails() {
const now = Date.now();
if (ethernetDetailsLoading || activeEthernetIf && activeEthernetDetails && (now - activeEthernetDetailsTimestamp) < activeDetailsTtlMs) {
return;
}
if (ethernetConnected && activeEthernetIf) {
ethernetDetailsLoading = true;
deviceStatusProcess.running = true;
}
}
// Helper function to immediately update network status
function updateNetworkStatus(ssid, connected) {
let nets = networks;
// Update all networks connected status
for (let key in nets) {
if (nets[key].connected && key !== ssid) {
nets[key].connected = false;
}
}
// Update the target network if it exists
if (nets[ssid]) {
nets[ssid].connected = connected;
nets[ssid].existing = true;
} else if (connected) {
// Create a temporary entry if network doesn't exist yet
nets[ssid] = {
"ssid": ssid,
"security": "--",
"signal": 100,
"connected": true,
"existing": true
};
}
// Trigger property change notification
networks = ({});
networks = nets;
}
// Helper functions
function getSignalInfo(signal, isConnected) {
let icon = "";
if (isConnected) {
if (root._networkConnectivity === "limited") {
icon = "wifi-exclamation";
} else if (root._networkConnectivity === "portal" || root._networkConnectivity === "unknown") {
icon = "wifi-question";
}
}
const label = signal >= 80 ? I18n.tr("wifi.signal.excellent") : signal >= 60 ? I18n.tr("wifi.signal.good") : signal >= 35 ? I18n.tr("wifi.signal.fair") : signal >= 15 ? I18n.tr("wifi.signal.poor") : I18n.tr("wifi.signal.weak");
if (!icon) {
icon = signal >= 80 ? "wifi" : signal >= 60 ? "wifi-3" : signal >= 35 ? "wifi-2" : signal >= 15 ? "wifi-1" : "wifi-0";
}
return {
icon,
label
};
}
function isSecured(security) {
return security && security !== "--" && security.trim() !== "";
}
function isEnterprise(security) {
if (!security) {
return false;
}
const s = security.toUpperCase();
return s.indexOf("802.1X") !== -1 || s.indexOf("EAP") !== -1 || s.indexOf("ENTERPRISE") !== -1;
}
function parseIpDetails(text) {
const details = {
connectionName: "",
ipv4: "",
gateway4: "",
dns4: [],
ipv6: [],
gateway6: [],
dns6: [],
hwAddr: "",
speed: ""
};
const addUnique = (arr, val) => {
if (val && arr.indexOf(val) === -1) {
arr.push(val);
}
};
const handlers = {
"GENERAL.CONNECTION": v => {
details.connectionName = v;
},
"GENERAL.HWADDR": v => {
details.hwAddr = v;
},
"CAPABILITIES.SPEED": v => {
if (v && v !== "unknown") {
details.speed = v;
}
},
"IP4.ADDRESS": v => {
details.ipv4 = v.split("/")[0];
},
"IP4.GATEWAY": v => {
details.gateway4 = v;
},
"IP6.ADDRESS": v => {
addUnique(details.ipv6, v.split("/")[0]);
},
"IP6.GATEWAY": v => {
addUnique(details.gateway6, v);
},
"IP4.DNS": v => {
addUnique(details.dns4, v);
},
"IP6.DNS": v => {
addUnique(details.dns6, v);
}
};
const lines = text.split("\n");
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
if (!line) {
continue;
}
const idx = line.indexOf(":");
if (idx === -1) {
continue;
}
const key = line.substring(0, idx).replace(/\[\d+\]$/, "");
const val = line.substring(idx + 1).trim();
if (handlers[key]) {
handlers[key](val);
}
}
return details;
}
// Functions used in /Modules/Panels/ControlCenter/Widgets/Network.qml & /Modules/Bar/Widgets/Network.qml
function getStatusText(showSpeed = false) {
// This variable can be tied to a toggle
if (root.connecting) {
return root.connectingTo ? I18n.tr("common.connecting") + " " + root.connectingTo : I18n.tr("common.connecting");
}
if (NetworkService.airplaneModeEnabled) {
return I18n.tr("toast.airplane-mode.title");
}
if (!root.wifiEnabled) {
return "";
}
// Ethernet
if (root.ethernetConnected) {
const eth = root.activeEthernetDetails;
const name = eth.connectionName || (root.ethernetInterfaces.length > 0 ? root.ethernetInterfaces[0].connectionName : "") || "";
const speed = eth.speed || "";
return (name + (showSpeed && speed ? " - " + speed : ""));
}
// Wi-Fi
if (root.wifiConnected) {
const wl = root.activeWifiDetails;
const speed = wl.rateShort || wl.rate || "";
const connectedNet = Object.values(root.networks).find(net => net.connected);
const name = connectedNet ? connectedNet.ssid : (wl.connectionName || "");
return (name + (showSpeed && speed ? " - " + speed : ""));
}
return "";
}
function getIcon(forceEthernet = false) {
if (NetworkService.airplaneModeEnabled && !forceEthernet) {
return "plane";
}
// 1. Ethernet Priority: Show Ethernet icon if connected OR if specifically requested (Panel)
if (root.ethernetConnected || forceEthernet) {
switch (root._networkConnectivity) {
case "limited":
return "ethernet-exclamation";
case "portal":
case "unknown":
return "ethernet-question";
case "full":
return "ethernet";
default:
return "ethernet-off";
}
}
// 2. Wi-Fi Fallback
if (root.wifiAvailable || !forceEthernet) {
const networkCount = Object.values(root.networks).length;
if (!root.wifiEnabled) {
return "wifi-off";
}
if (root.wifiConnected) {
let s = (root.activeWifiDetails && root.activeWifiDetails.signal !== undefined && root.activeWifiDetails.signal !== "") ? root.activeWifiDetails.signal : 0;
return root.getSignalInfo(s, true).icon;
}
if (root.connecting || networkCount > 0) {
return "wifi-question";
}
}
return (root.ethernetAvailable || root.ethernetConnected) ? "ethernet-off" : root.wifiAvailable ? "wifi-0" : "wifi-off";
}
// Processes
// Discover connected interface[s] and fetch details [1]
Process {
id: deviceStatusProcess
running: false
command: ["sh", "-c", "nmcli -t -f GENERAL.DEVICE,GENERAL.TYPE,GENERAL.STATE,GENERAL.CONNECTION,GENERAL.HWADDR,IP4.ADDRESS,IP4.GATEWAY,IP4.DNS,IP6.ADDRESS,IP6.GATEWAY,IP6.DNS,CAPABILITIES.SPEED device show; echo \"------\"; nmcli -t -f IN-USE,SIGNAL,RATE,CHAN,FREQ,BANDWIDTH device wifi list"]
environment: ({
"LC_ALL": "C"
})
stdout: StdioCollector {
onStreamFinished: {
const outputParts = text.split("------");
const deviceText = outputParts[0];
const wifiText = outputParts[1] || "";
let lines = deviceText.split("\n");
let deviceBlocks = [];
let currentBlock = [];
for (let i = 0; i < lines.length; i++) {
let line = lines[i].trim();
if (!line) {
continue;
}
if (line.startsWith("GENERAL.DEVICE:")) {
if (currentBlock.length > 0) {
deviceBlocks.push(currentBlock);
}
currentBlock = [line];
} else if (currentBlock.length > 0) {
currentBlock.push(line);
}
}
if (currentBlock.length > 0) {
deviceBlocks.push(currentBlock);
}
let activeEthIf = "";
let activeWifiIf = "";
let wifiAvailable = false;
let ethernetAvailable = false;
let ethList = [];
let newActiveWifiDetails = ({});
let newActiveEthernetDetails = ({});
for (let b = 0; b < deviceBlocks.length; b++) {
let block = deviceBlocks[b];
let blockText = block.join("\n");
let details = root.parseIpDetails(blockText);
let name = "";
let type = "";
let stateStr = "";
for (let l = 0; l < block.length; l++) {
let line = block[l];
if (line.startsWith("GENERAL.DEVICE:")) {
name = line.substring(15).trim();
} else if (line.startsWith("GENERAL.TYPE:")) {
type = line.substring(13).trim();
} else if (line.startsWith("GENERAL.STATE:")) {
stateStr = line.substring(14).trim();
}
}
if (stateStr.indexOf("(unmanaged)") !== -1) {
continue;
}
let isConnected = stateStr.indexOf("(connected)") !== -1;
if (type === "ethernet") {
ethernetAvailable = true;
let stateName = stateStr.split(" ")[1] ? stateStr.split(" ")[1].replace(/[()]/g, "") : stateStr;
ethList.push({
ifname: name,
state: stateName,
connected: isConnected,
connectionName: details.connectionName
});
if (isConnected && !activeEthIf) {
activeEthIf = name;
newActiveEthernetDetails = details;
newActiveEthernetDetails.ifname = name;
}
} else if (type === "wifi") {
wifiAvailable = true;
if (isConnected && !activeWifiIf) {
activeWifiIf = name;
newActiveWifiDetails = details;
newActiveWifiDetails.ifname = name;
}
}
}
// Parse Wi-Fi details if active
if (activeWifiIf && wifiText) {
let rate = "";
let freq = "";
let channel = "";
let width = "";
let signal = "";
const wifiLines = wifiText.split("\n");
for (let i = 0; i < wifiLines.length; i++) {
const line = wifiLines[i].trim();
if (line.startsWith("*")) {
const parts = line.split(":");
if (parts.length >= 6) {
signal = parts[1];
rate = parts[2];
channel = parts[3];
freq = parts[4].replace(" MHz", "");
width = parts[5];
}
break;
}
}
let band = "";
if (freq) {
const f = +freq;
if (f) {
switch (true) {
case (f >= 5925 && f < 7125):
band = "6 GHz";
break;
case (f >= 5150 && f < 5925):
band = "5 GHz";
break;
case (f >= 2400 && f < 2500):
band = "2.4 GHz";
break;
default:
band = `${f} MHz`;
}
}
}
let rateShort = "";
if (rate) {
var rparts = rate.trim().split(" ");
var compact = [];
for (var i = 0; i < rparts.length; i++) {
if (rparts[i]) {
compact.push(rparts[i]);
}
}
var unitIdx = -1;
for (var j = 0; j < compact.length; j++) {
var token = compact[j].toLowerCase();
if (token === "mbit/s" || token === "mb/s" || token === "mbits/s") {
unitIdx = j;
break;
}
}
if (unitIdx > 0) {
var num = compact[unitIdx - 1];
var parsed = parseFloat(num);
if (!isNaN(parsed)) {
rateShort = parsed + " Mbit/s";
}
}
if (!rateShort) {
rateShort = compact.slice(0, 2).join(" ");
}
}
let enhancedBand = band;
if (channel && width && width !== "0 MHz") {
enhancedBand = `${band} / ${channel} (${width})`;
} else if (channel) {
enhancedBand = `${band} / ${channel}`;
}
if (newActiveWifiDetails.speed) {
newActiveWifiDetails.rate = newActiveWifiDetails.speed.replace(/Mb\/s/i, "Mbit/s");
newActiveWifiDetails.rateShort = newActiveWifiDetails.rate;
} else {
newActiveWifiDetails.rate = rate;
newActiveWifiDetails.rateShort = rateShort;
}
newActiveWifiDetails.band = enhancedBand;
newActiveWifiDetails.channel = channel;
newActiveWifiDetails.width = width;
newActiveWifiDetails.signal = signal;
}
root._wifiAvailable = wifiAvailable;
root._ethernetAvailable = ethernetAvailable;
root.ethernetConnected = (activeEthIf !== "");
root.wifiConnected = (activeWifiIf !== "");
Logger.d("Network", "Device sync: wifiAvailable: " + wifiAvailable + ", ethAvailable: " + ethernetAvailable + ", wifiConnected: " + root.wifiConnected + " (" + activeWifiIf + "), ethConnected: " + root.ethernetConnected + " (" + activeEthIf + ")");
ethList.sort((a, b) => (a.connected !== b.connected) ? (a.connected ? -1 : 1) : a.ifname.localeCompare(b.ifname));
root.ethernetInterfaces = ethList;
root.activeEthernetIf = activeEthIf;
root.activeEthernetDetails = newActiveEthernetDetails;
root.activeEthernetDetailsTimestamp = Date.now();
root.ethernetDetailsLoading = false;
root.activeWifiIf = activeWifiIf;
root.activeWifiDetails = newActiveWifiDetails;
root.activeWifiDetailsTimestamp = Date.now();
root.wifiDetailsLoading = false;
}
}
stderr: StdioCollector {
onStreamFinished: {
if (text && text.trim()) {
Logger.w("Network", "nmcli device show stderr:", text.trim());
}
root.ethernetDetailsLoading = false;
root.wifiDetailsLoading = false;
}
}
}
// Process to check the internet connectivity of the connected network
Process {
id: connectivityCheckProcess
running: false
command: ["nmcli", "networking", "connectivity", "check"]
stdout: StdioCollector {
onStreamFinished: {
const r = text.trim();
if (!r) {
return;
}
root._networkConnectivity = (r === "none") ? "unknown" : r;
root._internetConnectivity = (r === "full");
}
}
stderr: StdioCollector {
onStreamFinished: {
if (text.trim()) {
Logger.w("Network", "Connectivity check error: " + text);
}
}
}
}
// Helper process to get existing profiles
Process {
id: profileCheckProcess
running: false
command: ["nmcli", "-t", "-f", "NAME", "connection", "show"]
stdout: StdioCollector {
onStreamFinished: {
var profiles = {};
var lines = text.split("\n");
for (var i = 0; i < lines.length; i++) {
var l = lines[i];
if (l && l.trim()) {
profiles[l.trim()] = true;
}
}
root.existingProfiles = profiles;
scanProcess.running = true;
}
}
stderr: StdioCollector {
onStreamFinished: {
if (text && text.trim()) {
Logger.w("Network", "Profile check stderr:", text.trim());
if (root.scanningActive) {
if (root.scanPending) {
root.scanPending = false;
delayedScanTimer.interval = 3000;
} else {
delayedScanTimer.interval = 5000;
}
delayedScanTimer.restart();
}
}
}
}
}
// Scan for Wi-Fi networks
Process {
id: scanProcess
running: false
command: ["nmcli", "-t", "-f", "SSID,SECURITY,SIGNAL,IN-USE", "device", "wifi", "list", "--rescan", "yes"]
stdout: StdioCollector {
onStreamFinished: {
const lines = text.trim().split("\n");
const networksMap = {};
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
if (!line) {
continue;
}
// Parse SSID:SECURITY:SIGNAL:IN-USE
const parts = line.split(":");
if (parts.length < 4) {
continue;
}
const inUse = parts[parts.length - 1];
const signal = parseInt(parts[parts.length - 2]) || 0;
let security = parts[parts.length - 3];
if (security) {
security = security.replace("WPA2 WPA3", "WPA2/WPA3").replace("WPA1 WPA2", "WPA1/WPA2");
}
const ssid = parts.slice(0, parts.length - 3).join(":");
if (ssid) {
const isConnected = (inUse === "*");
if (!networksMap[ssid]) {
networksMap[ssid] = {
"ssid": ssid,
"security": security || "--",
"signal": signal,
"connected": isConnected,
"existing": !!root.existingProfiles[ssid]
};
} else {
if (isConnected) {
networksMap[ssid].connected = true;
networksMap[ssid].signal = signal;
connectivityCheckProcess.running = true;
} else if (!networksMap[ssid].connected && signal > networksMap[ssid].signal) {
networksMap[ssid].signal = signal;
}
}
}
}
// Logging & Diffing
const oldSSIDs = Object.keys(root.networks);
const newSSIDs = Object.keys(networksMap);
const newNetworks = newSSIDs.filter(s => oldSSIDs.indexOf(s) === -1);
const lostNetworks = oldSSIDs.filter(s => newSSIDs.indexOf(s) === -1);
// Always update networks, this makes more reflective of state/signal.
root.networks = networksMap;
if (newNetworks.length > 0 || lostNetworks.length > 0) {
if (newNetworks.length > 0) {
Logger.d("Network", "New Wi-Fi network appeared:", newNetworks.join(", "));
}
if (lostNetworks.length > 0) {
Logger.d("Network", "Wi-Fi network disappeared:", lostNetworks.join(", "));
}
Logger.d("Network", "Total Wi-Fi networks:", Object.keys(networksMap).length);
}
if (Object.values(networksMap).some(n => n.connected)) {
root.refreshActiveWifiDetails();
}
if (root.scanPending) {
root.scanPending = false;
delayedScanTimer.interval = 100;
delayedScanTimer.restart();
}
root.scanningActive = false;
}
}
stderr: StdioCollector {
onStreamFinished: {
if (text.trim()) {
Logger.w("Network", "Scan error: " + text);
// Even on error, if a scan was pending, try again
if (root.scanPending) {
root.scanPending = false;
delayedScanTimer.interval = 3000;
} else if (root.scanningActive) {
delayedScanTimer.interval = 10000;
}
delayedScanTimer.restart();
}
root.scanningActive = false;
}
}
}
// Connect to Wi-Fi network
Process {
id: connectProcess
property string mode: "new" // "saved", "new", or "manual"
property string ssid: ""
property string password: ""
property bool isHidden: false
// Manual properties
property string securityKey: ""
property string identity: ""
property string eap: "peap"
property string phase2: "mschapv2"
property string anonIdentity: ""
property string caCert: ""
running: false
command: {
if (mode === "saved") {
return ["nmcli", "-t", "connection", "up", "id", ssid];
} else if (mode === "manual") {
const nmArgs = ["connection", "add", "type", "wifi", "con-name", ssid, "ssid", ssid, "--", "802-11-wireless.hidden", isHidden ? "yes" : "no"];
if (securityKey === "wpa-psk" || securityKey === "wpa2-psk") {
nmArgs.push("wifi-sec.key-mgmt", "wpa-psk", "wifi-sec.psk", password);
} else if (securityKey === "sae") {
nmArgs.push("wifi-sec.key-mgmt", "sae", "wifi-sec.psk", password);
} else if (securityKey === "wep") {
nmArgs.push("wifi-sec.key-mgmt", "none", "wifi-sec.wep-key0", password);
} else if (securityKey && securityKey.indexOf("-eap") !== -1) {
nmArgs.push("wifi-sec.key-mgmt", "wpa-eap", "802-1x.eap", eap, "802-1x.phase2-auth", phase2, "802-1x.identity", identity, "802-1x.password", password);
if (anonIdentity) {
nmArgs.push("802-1x.anonymous-identity", anonIdentity);
}
if (caCert) {
nmArgs.push("802-1x.ca-cert", caCert);
}
}
const script = `
SSID="$1"
shift
# Find existing profile by Name and Type
UUID=$(nmcli -t -f NAME,UUID,TYPE connection show | awk -F: -v target="$SSID" '$1 == target && $3 == "802-11-wireless" { print $2; exit }')
if [ -n "$UUID" ]; then
echo "Using existing profile: $UUID"
nmcli connection delete uuid "$UUID" 2>/dev/null || true
else
echo "Creating new profile for $SSID"
fi
nmcli "$@"
nmcli connection up id "$SSID"
`;
return ["sh", "-c", script, "--", ssid].concat(nmArgs);
} else {
var cmd = ["nmcli", "-t", "device", "wifi", "connect", ssid];
if (isHidden) {
cmd.push("hidden", "yes");
}
if (password) {
cmd.push("password", password);
}
if (root.activeWifiIf) {
cmd.push("ifname", root.activeWifiIf);
}
return cmd;
}
}
environment: ({
"LC_ALL": "C"
})
stdout: StdioCollector {
onStreamFinished: {
const output = text.trim();
if (!output || (output.indexOf("successfully activated") === -1 && output.indexOf("Connection successfully") === -1)) {
return;
}
root.wifiConnected = true;
root.updateNetworkStatus(connectProcess.ssid, true);
root.refreshActiveWifiDetails(); // This needs wifiConnected true.