summaryrefslogtreecommitdiff
path: root/modules/controlcenter/network/NetworkingPane.qml
blob: 55c70d2a8a77f3acf1fb028dd0e5e3bdf75d2b41 (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
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
pragma ComponentBehavior: Bound

import ".."
import "../components"
import "."
import qs.components
import qs.components.controls
import qs.components.effects
import qs.components.containers
import qs.services
import qs.config
import qs.utils
import Quickshell
import Quickshell.Widgets
import QtQuick
import QtQuick.Layouts

Item {
    id: root

    required property Session session

    anchors.fill: parent

    SplitPaneLayout {
        id: splitLayout

        anchors.fill: parent

        leftContent: Component {

                StyledFlickable {
                    id: leftFlickable

                    flickableDirection: Flickable.VerticalFlick
                    contentHeight: leftContent.height

                    StyledScrollBar.vertical: StyledScrollBar {
                        flickable: leftFlickable
                    }

                    ColumnLayout {
                        id: leftContent

                        anchors.left: parent.left
                        anchors.right: parent.right
                        spacing: Appearance.spacing.normal

                    // Network header above the collapsible sections
                    RowLayout {
                        Layout.fillWidth: true
                        spacing: Appearance.spacing.smaller

                        StyledText {
                            text: qsTr("Network")
                            font.pointSize: Appearance.font.size.large
                            font.weight: 500
                        }

                        Item {
                            Layout.fillWidth: true
                        }

                        ToggleButton {
                            toggled: Nmcli.wifiEnabled
                            icon: "wifi"
                            accent: "Tertiary"

                            onClicked: {
                                Nmcli.toggleWifi(null);
                            }
                        }

                        ToggleButton {
                            toggled: Nmcli.scanning
                            icon: "wifi_find"
                            accent: "Secondary"

                            onClicked: {
                                Nmcli.rescanWifi();
                            }
                        }

                        ToggleButton {
                            toggled: !root.session.ethernet.active && !root.session.network.active
                            icon: "settings"
                            accent: "Primary"

                            onClicked: {
                                if (root.session.ethernet.active || root.session.network.active) {
                                    root.session.ethernet.active = null;
                                    root.session.network.active = null;
                                } else {
                                    // Toggle to show settings - prefer ethernet if available, otherwise wireless
                                    if (Nmcli.ethernetDevices.length > 0) {
                                        root.session.ethernet.active = Nmcli.ethernetDevices[0];
                                    } else if (Nmcli.networks.length > 0) {
                                        root.session.network.active = Nmcli.networks[0];
                                    }
                                }
                            }
                        }
                    }

                    CollapsibleSection {
                        id: ethernetListSection

                        Layout.fillWidth: true
                        title: qsTr("Ethernet")
                        expanded: true

                        ColumnLayout {
                            Layout.fillWidth: true
                            spacing: Appearance.spacing.small

                            RowLayout {
                                Layout.fillWidth: true
                                spacing: Appearance.spacing.small

                                StyledText {
                                    text: qsTr("Devices (%1)").arg(Nmcli.ethernetDevices.length)
                                    font.pointSize: Appearance.font.size.normal
                                    font.weight: 500
                                }
                            }

                            StyledText {
                                Layout.fillWidth: true
                                text: qsTr("All available ethernet devices")
                                color: Colours.palette.m3outline
                            }

                            Repeater {
                                id: ethernetRepeater

                                Layout.fillWidth: true
                                model: Nmcli.ethernetDevices

                                delegate: StyledRect {
                                    required property var modelData

                                    Layout.fillWidth: true

                                    color: Qt.alpha(Colours.tPalette.m3surfaceContainer, root.session.ethernet.active === modelData ? Colours.tPalette.m3surfaceContainer.a : 0)
                                    radius: Appearance.rounding.normal

                                    StateLayer {
                                        function onClicked(): void {
                                            root.session.network.active = null;
                                            root.session.ethernet.active = modelData;
                                        }
                                    }

                                    RowLayout {
                                        id: rowLayout

                                        anchors.left: parent.left
                                        anchors.right: parent.right
                                        anchors.verticalCenter: parent.verticalCenter
                                        anchors.margins: Appearance.padding.normal

                                        spacing: Appearance.spacing.normal

                                        StyledRect {
                                            implicitWidth: implicitHeight
                                            implicitHeight: icon.implicitHeight + Appearance.padding.normal * 2

                                            radius: Appearance.rounding.normal
                                            color: modelData.connected ? Colours.palette.m3primaryContainer : Colours.tPalette.m3surfaceContainerHigh

                                            MaterialIcon {
                                                id: icon

                                                anchors.centerIn: parent
                                                text: "cable"
                                                font.pointSize: Appearance.font.size.large
                                                fill: modelData.connected ? 1 : 0
                                                color: modelData.connected ? Colours.palette.m3onPrimaryContainer : Colours.palette.m3onSurface
                                            }
                                        }

                                        ColumnLayout {
                                            Layout.fillWidth: true

                                            spacing: 0

                                            StyledText {
                                                Layout.fillWidth: true
                                                elide: Text.ElideRight
                                                maximumLineCount: 1

                                                text: modelData.interface || qsTr("Unknown")
                                            }

                                            StyledText {
                                                Layout.fillWidth: true
                                                text: modelData.connected ? qsTr("Connected") : qsTr("Disconnected")
                                                color: modelData.connected ? Colours.palette.m3primary : Colours.palette.m3outline
                                                font.pointSize: Appearance.font.size.small
                                                font.weight: modelData.connected ? 500 : 400
                                                elide: Text.ElideRight
                                            }
                                        }

                                        StyledRect {
                                            implicitWidth: implicitHeight
                                            implicitHeight: connectIcon.implicitHeight + Appearance.padding.smaller * 2

                                            radius: Appearance.rounding.full
                                            color: Qt.alpha(Colours.palette.m3primaryContainer, modelData.connected ? 1 : 0)

                                            StateLayer {
                                                function onClicked(): void {
                                                    if (modelData.connected && modelData.connection) {
                                                        Nmcli.disconnectEthernet(modelData.connection, () => {});
                                                    } else {
                                                        Nmcli.connectEthernet(modelData.connection || "", modelData.interface || "", () => {});
                                                    }
                                                }
                                            }

                                            MaterialIcon {
                                                id: connectIcon

                                                anchors.centerIn: parent
                                                text: modelData.connected ? "link_off" : "link"
                                                color: modelData.connected ? Colours.palette.m3onPrimaryContainer : Colours.palette.m3onSurface
                                            }
                                        }
                                    }

                                    implicitHeight: rowLayout.implicitHeight + Appearance.padding.normal * 2
                                }
                            }
                        }
                    }

                    CollapsibleSection {
                        id: wirelessListSection

                        Layout.fillWidth: true
                        title: qsTr("Wireless")
                        expanded: true

                        ColumnLayout {
                            Layout.fillWidth: true
                            spacing: Appearance.spacing.small

                            RowLayout {
                                Layout.fillWidth: true
                                spacing: Appearance.spacing.small

                                StyledText {
                                    text: qsTr("Networks (%1)").arg(Nmcli.networks.length)
                                    font.pointSize: Appearance.font.size.normal
                                    font.weight: 500
                                }

                                StyledText {
                                    visible: Nmcli.scanning
                                    text: qsTr("Scanning...")
                                    color: Colours.palette.m3primary
                                    font.pointSize: Appearance.font.size.small
                                }
                            }

                            StyledText {
                                Layout.fillWidth: true
                                text: qsTr("All available WiFi networks")
                                color: Colours.palette.m3outline
                            }

                            Repeater {
                                id: wirelessRepeater

                                Layout.fillWidth: true
                                model: ScriptModel {
                                    values: [...Nmcli.networks].sort((a, b) => {
                                        // Put active/connected network first
                                        if (a.active !== b.active)
                                            return b.active - a.active;
                                        // Then sort by signal strength
                                        return b.strength - a.strength;
                                    })
                                }

                                delegate: StyledRect {
                                    required property var modelData

                                    Layout.fillWidth: true

                                    color: Qt.alpha(Colours.tPalette.m3surfaceContainer, (modelData && root.session.network.active === modelData) ? Colours.tPalette.m3surfaceContainer.a : 0)
                                    radius: Appearance.rounding.normal

                                    StateLayer {
                                        function onClicked(): void {
                                            if (!modelData) {
                                                return;
                                            }
                                            root.session.ethernet.active = null;
                                            root.session.network.active = modelData;
                                            // Check if we need to refresh saved connections when selecting a network
                                            if (modelData.ssid) {
                                                checkSavedProfileForNetwork(modelData.ssid);
                                            }
                                        }
                                    }

                                    RowLayout {
                                        id: wirelessRowLayout

                                        anchors.left: parent.left
                                        anchors.right: parent.right
                                        anchors.verticalCenter: parent.verticalCenter
                                        anchors.margins: Appearance.padding.normal

                                        spacing: Appearance.spacing.normal

                                        StyledRect {
                                            implicitWidth: implicitHeight
                                            implicitHeight: wirelessIcon.implicitHeight + Appearance.padding.normal * 2

                                            radius: Appearance.rounding.normal
                                            color: (modelData && modelData.active) ? Colours.palette.m3primaryContainer : Colours.tPalette.m3surfaceContainerHigh

                                            MaterialIcon {
                                                id: wirelessIcon

                                                anchors.centerIn: parent
                                                text: Icons.getNetworkIcon(modelData && modelData.strength !== undefined ? modelData.strength : 0)
                                                font.pointSize: Appearance.font.size.large
                                                fill: (modelData && modelData.active) ? 1 : 0
                                                color: (modelData && modelData.active) ? Colours.palette.m3onPrimaryContainer : Colours.palette.m3onSurface
                                            }

                                            StyledRect {
                                                id: lockBadge

                                                visible: modelData && modelData.isSecure
                                                anchors.right: parent.right
                                                anchors.bottom: parent.bottom
                                                anchors.margins: -Appearance.padding.smaller / 2

                                                implicitWidth: lockIconSize + Appearance.padding.smaller
                                                implicitHeight: lockIconSize + Appearance.padding.smaller
                                                radius: Appearance.rounding.full
                                                color: Colours.palette.m3secondaryContainer

                                                readonly property real lockIconSize: lockIcon.implicitWidth

                                                Elevation {
                                                    anchors.fill: parent
                                                    radius: parent.radius
                                                    z: -1
                                                    level: 2
                                                }

                                                MaterialIcon {
                                                    id: lockIcon

                                                    anchors.centerIn: parent
                                                    text: "lock"
                                                    font.pointSize: Appearance.font.size.small
                                                    fill: 1
                                                    color: Colours.palette.m3onSurface
                                                }
                                            }
                                        }

                                        ColumnLayout {
                                            Layout.fillWidth: true

                                            spacing: 0

                                            StyledText {
                                                Layout.fillWidth: true
                                                elide: Text.ElideRight
                                                maximumLineCount: 1

                                                text: (modelData && modelData.ssid) ? modelData.ssid : qsTr("Unknown")
                                            }

                                            RowLayout {
                                                Layout.fillWidth: true
                                                spacing: Appearance.spacing.smaller

                                                StyledText {
                                                    Layout.fillWidth: true
                                                    text: {
                                                        if (!modelData) return qsTr("Open");
                                                        if (modelData.active) return qsTr("Connected");
                                                        if (modelData.isSecure && modelData.security && modelData.security.length > 0) {
                                                            return modelData.security;
                                                        }
                                                        if (modelData.isSecure) return qsTr("Secured");
                                                        return qsTr("Open");
                                                    }
                                                    color: (modelData && modelData.active) ? Colours.palette.m3primary : Colours.palette.m3outline
                                                    font.pointSize: Appearance.font.size.small
                                                    font.weight: (modelData && modelData.active) ? 500 : 400
                                                    elide: Text.ElideRight
                                                }
                                            }
                                        }

                                        StyledRect {
                                            implicitWidth: implicitHeight
                                            implicitHeight: wirelessConnectIcon.implicitHeight + Appearance.padding.smaller * 2

                                            radius: Appearance.rounding.full
                                            color: Qt.alpha(Colours.palette.m3primaryContainer, (modelData && modelData.active) ? 1 : 0)

                                            StateLayer {
                                                function onClicked(): void {
                                                    if (modelData && modelData.active) {
                                                        Nmcli.disconnectFromNetwork();
                                                    } else if (modelData) {
                                                        handleWirelessConnect(modelData);
                                                    }
                                                }
                                            }

                                            MaterialIcon {
                                                id: wirelessConnectIcon

                                                anchors.centerIn: parent
                                                text: (modelData && modelData.active) ? "link_off" : "link"
                                                color: (modelData && modelData.active) ? Colours.palette.m3onPrimaryContainer : Colours.palette.m3onSurface
                                            }
                                        }
                                    }

                                    implicitHeight: wirelessRowLayout.implicitHeight + Appearance.padding.normal * 2
                                }
                            }
                        }
                    }
                }
            }
        }

        rightContent: Component {
            Item {
                id: rightPaneItem
                
                // Right pane - networking details/settings
                property var ethernetPane: root.session.ethernet.active
                property var wirelessPane: root.session.network.active
                property var pane: ethernetPane || wirelessPane
                property string paneId: ethernetPane ? (ethernetPane.interface || "") : (wirelessPane ? (wirelessPane.ssid || wirelessPane.bssid || "") : "")
                property Component targetComponent: settings
                property Component nextComponent: settings

                function getComponentForPane() {
                    return pane ? (ethernetPane ? ethernetDetails : wirelessDetails) : settings;
                }

                Component.onCompleted: {
                    targetComponent = getComponentForPane();
                    nextComponent = targetComponent;
                }

                Connections {
                    target: root.session.ethernet
                    function onActiveChanged() {
                        nextComponent = getComponentForPane();
                        paneId = ethernetPane ? (ethernetPane.interface || "") : (wirelessPane ? (wirelessPane.ssid || wirelessPane.bssid || "") : "");
                    }
                }

                Connections {
                    target: root.session.network
                    function onActiveChanged() {
                        nextComponent = getComponentForPane();
                        paneId = ethernetPane ? (ethernetPane.interface || "") : (wirelessPane ? (wirelessPane.ssid || wirelessPane.bssid || "") : "");
                    }
                }

                Loader {
                    id: rightLoader

                    anchors.fill: parent
                    anchors.margins: Appearance.padding.large * 2

                    opacity: 1
                    scale: 1
                    transformOrigin: Item.Center
                    clip: false

                    asynchronous: true
                    sourceComponent: rightPaneItem.targetComponent

                    Connections {
                        target: rightPaneItem
                        function onPaneIdChanged() {
                            rightPaneItem.targetComponent = rightPaneItem.nextComponent;
                        }
                    }
                }

                Behavior on paneId {
                    SequentialAnimation {
                        ParallelAnimation {
                            Anim {
                                target: rightLoader
                                property: "opacity"
                                to: 0
                                easing.bezierCurve: Appearance.anim.curves.standardAccel
                            }
                            Anim {
                                target: rightLoader
                                property: "scale"
                                to: 0.8
                                easing.bezierCurve: Appearance.anim.curves.standardAccel
                            }
                        }
                        PropertyAction {
                            target: rightPaneItem
                            property: "targetComponent"
                            value: rightPaneItem.nextComponent
                        }
                        ParallelAnimation {
                            Anim {
                                target: rightLoader
                                property: "opacity"
                                to: 1
                                easing.bezierCurve: Appearance.anim.curves.standardDecel
                            }
                            Anim {
                                target: rightLoader
                                property: "scale"
                                to: 1
                                easing.bezierCurve: Appearance.anim.curves.standardDecel
                            }
                        }
                    }
                }

                Connections {
                    target: rightPaneItem
                    function onPaneIdChanged() {
                        rightPaneItem.targetComponent = rightPaneItem.nextComponent;
                    }
                }
            }
        }
    }

    Component {
        id: settings

        StyledFlickable {
            id: settingsFlickable
            flickableDirection: Flickable.VerticalFlick
            contentHeight: settingsInner.height

            StyledScrollBar.vertical: StyledScrollBar {
                flickable: settingsFlickable
            }

            NetworkSettings {
                id: settingsInner

                anchors.left: parent.left
                anchors.right: parent.right
                anchors.top: parent.top
                session: root.session
            }
        }
    }

    Component {
        id: ethernetDetails

        StyledFlickable {
            id: ethernetFlickable
            flickableDirection: Flickable.VerticalFlick
            contentHeight: ethernetDetailsInner.height

            StyledScrollBar.vertical: StyledScrollBar {
                flickable: ethernetFlickable
            }

            EthernetDetails {
                id: ethernetDetailsInner

                anchors.left: parent.left
                anchors.right: parent.right
                anchors.top: parent.top
                session: root.session
            }
        }
    }

    Component {
        id: wirelessDetails

        StyledFlickable {
            id: wirelessFlickable
            flickableDirection: Flickable.VerticalFlick
            contentHeight: wirelessDetailsInner.height

            StyledScrollBar.vertical: StyledScrollBar {
                flickable: wirelessFlickable
            }

            WirelessDetails {
                id: wirelessDetailsInner

                anchors.left: parent.left
                anchors.right: parent.right
                anchors.top: parent.top
                session: root.session
            }
        }
    }

    WirelessPasswordDialog {
        anchors.fill: parent
        session: root.session
        z: 1000
    }

    component Anim: NumberAnimation {
        duration: Appearance.anim.durations.normal / 2
        easing.type: Easing.BezierSpline
    }

    function checkSavedProfileForNetwork(ssid: string): void {
        if (ssid && ssid.length > 0) {
            Nmcli.loadSavedConnections(() => {});
        }
    }

    function handleWirelessConnect(network): void {
        if (Nmcli.active && Nmcli.active.ssid !== network.ssid) {
            Nmcli.disconnectFromNetwork();
            Qt.callLater(() => {
                connectToWirelessNetwork(network);
            });
        } else {
            connectToWirelessNetwork(network);
        }
    }

    function connectToWirelessNetwork(network): void {
        if (network.isSecure) {
            const hasSavedProfile = Nmcli.hasSavedProfile(network.ssid);

            if (hasSavedProfile) {
                Nmcli.connectToNetwork(network.ssid, "", network.bssid, null);
            } else {
                Nmcli.connectToNetworkWithPasswordCheck(
                    network.ssid,
                    network.isSecure,
                    (result) => {
                        if (result.needsPassword) {
                            if (Nmcli.pendingConnection) {
                                Nmcli.connectionCheckTimer.stop();
                                Nmcli.immediateCheckTimer.stop();
                                Nmcli.immediateCheckTimer.checkCount = 0;
                                Nmcli.pendingConnection = null;
                            }
                            root.session.network.showPasswordDialog = true;
                            root.session.network.pendingNetwork = network;
                        }
                    },
                    network.bssid
                );
            }
        } else {
            Nmcli.connectToNetwork(network.ssid, "", network.bssid, null);
        }
    }
}