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

import ".."
import "."
import qs.components
import qs.components.controls
import qs.components.containers
import qs.services
import qs.config
import QtQuick
import QtQuick.Layouts

ColumnLayout {
    id: root

    required property Session session

    spacing: Appearance.spacing.small

    RowLayout {
        spacing: Appearance.spacing.smaller

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

        Item {
            Layout.fillWidth: true
        }

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

            onClicked: {
                Network.toggleWifi();
            }
        }

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

            onClicked: {
                Network.rescanWifi();
            }
        }

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

            onClicked: {
                if (root.session.network.active)
                    root.session.network.active = null;
                else {
                    root.session.network.active = view.model.get(0)?.modelData ?? null;
                }
            }
        }
    }

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

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

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

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

    StyledListView {
        id: view

        Layout.fillWidth: true
        Layout.fillHeight: true

        model: Network.networks

        spacing: Appearance.spacing.small / 2
        clip: true

        StyledScrollBar.vertical: StyledScrollBar {
            flickable: view
        }

        delegate: StyledRect {
            required property var modelData

            anchors.left: parent.left
            anchors.right: parent.right

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

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

            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.active ? Colours.palette.m3primaryContainer : Colours.tPalette.m3surfaceContainerHigh

                    MaterialIcon {
                        id: icon

                        anchors.centerIn: parent
                        text: modelData.isSecure ? "lock" : "wifi"
                        font.pointSize: Appearance.font.size.large
                        fill: modelData.active ? 1 : 0
                        color: modelData.active ? Colours.palette.m3onPrimaryContainer : Colours.palette.m3onSurface
                    }
                }

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

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

                StyledText {
                    text: modelData.active ? qsTr("Connected") : (modelData.isSecure ? qsTr("Secured") : qsTr("Open"))
                    color: modelData.active ? Colours.palette.m3primary : Colours.palette.m3outline
                    font.pointSize: Appearance.font.size.small
                    font.weight: modelData.active ? 500 : 400
                }

                StyledText {
                    text: qsTr("%1%").arg(modelData.strength)
                    color: Colours.palette.m3outline
                    font.pointSize: Appearance.font.size.small
                }

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

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

                    StateLayer {
                        function onClicked(): void {
                            if (modelData.active) {
                                Network.disconnectFromNetwork();
                            } else {
                                handleConnect(modelData);
                            }
                        }
                    }

                    MaterialIcon {
                        id: connectIcon

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

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

    function checkSavedProfileForNetwork(ssid: string): void {
        // Refresh saved connections list to ensure it's up to date
        // This ensures accurate profile detection when selecting networks
        if (ssid && ssid.length > 0) {
            // Always refresh to ensure we have the latest saved connections
            // This is important when a network is selected from the list
            Network.listConnectionsProc.running = true;
        }
    }

    function handleConnect(network): void {
        // If already connected to a different network, disconnect first
        if (Network.active && Network.active.ssid !== network.ssid) {
            Network.disconnectFromNetwork();
            Qt.callLater(() => {
                connectToNetwork(network);
            });
        } else {
            connectToNetwork(network);
        }
    }

    function connectToNetwork(network): void {
        if (network.isSecure) {
            // Check if we have a saved connection profile for this network (by SSID)
            const hasSavedProfile = Network.hasSavedProfile(network.ssid);
            
            if (hasSavedProfile) {
                // Try connecting with saved password - don't show dialog if it fails
                // The saved password should work, but if connection fails for other reasons,
                // we'll let the user try manually later
                Network.connectToNetwork(network.ssid, "", network.bssid, null);
            } else {
                // No saved profile, try connecting without password first
                Network.connectToNetworkWithPasswordCheck(
                    network.ssid,
                    network.isSecure,
                    () => {
                        // Callback: connection failed, show password dialog
                        root.session.network.showPasswordDialog = true;
                        root.session.network.pendingNetwork = network;
                    },
                    network.bssid
                );
            }
        } else {
            Network.connectToNetwork(network.ssid, "", network.bssid, null);
        }
    }
}