summaryrefslogtreecommitdiff
path: root/gui/components/Locations.qml
blob: 30a34e5961ce7e1f62287444728e6c19238ca24a (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
import QtQuick 2.15
import QtQuick.Controls 2.2
import QtQuick.Layouts 1.14
import QtGraphicalEffects 1.0

import "../themes/themes.js" as Theme


/* TODO
 [ ] corner case: manual override, not full list yet
     [x] persist bridges
     [ ] persist manual selection
     [ ] display the location we know
 [ ] corner case: user selects bridges with manual selection
     (I think the backend should discard any manual selection when selecting bridges...
      unless the current selection provides the bridge, in which case we can maintain it)
 */
ThemedPage {

    id: locationPage
    title: qsTr("Select Location")

    // TODO add ScrollIndicator
    // https://doc.qt.io/qt-5.12//qml-qtquick-controls2-scrollindicator.html

    //: this is in the radio button for the auto selection
    property var autoSelectionLabel: qsTr("Automatically use best connection")
    //: Location Selection: label for radio buttons that selects manually
    property var manualSelectionLabel: qsTr("Manually select")
    //: A little display to signal that the clicked gateway is being switched to
    property var switchingLocationLabel: qsTr("Switching gateways...")
    //: Subtitle to explain that only bridge locations are shown in the selector
    property var onlyBridgesWarning: qsTr("Only locations with bridges")

    property bool switching: false

    ButtonGroup {
        id: locsel
    }

    Rectangle {
        id: autoBox
        width: root.width * 0.90
        height: 90
        radius: 10
        color: "white"

        anchors {
            horizontalCenter: parent.horizontalCenter
            top: parent.top
            margins: 10
        }

        Rectangle {
            anchors {
                fill: parent
                margins: 10
            }
            Rectangle {
                id: recommendedHeader
                height: 20
                Label {
                    id: recommendedLabel
                    //: Location Selection: label for radio button that selects automatically
                    text: qsTr("Recommended")
                    font.weight: Font.Bold
                    font.bold: true
                }
                Image {
                    id: lightning 
                    smooth: true
                    width: 16
                    source: "../resources/lightning.svg"
                    fillMode: Image.PreserveAspectFit
                    verticalAlignment: Image.AlignVCenter
                    anchors {
                        left: recommendedLabel.right
                        top: parent.top
                        leftMargin: 5
                        topMargin: 2
                        //verticalCenterOffset: 3
                    }
                    ColorOverlay{
                        anchors.fill: lightning
                        source: lightning
                        color: "black"
                        antialiasing: true
                    }
                }
            }
            WrappedRadioButton {
                id: autoRadioButton
                text: getAutoLabel()
                ButtonGroup.group: locsel
                checked: false
                anchors {
                    top: recommendedHeader.bottom
                    leftMargin: -5
                }
                HoverHandler {
                    cursorShape: Qt.PointingHandCursor
                }
                onClicked: {
                    root.selectedGateway = "auto"
                    console.debug("Selected gateway: auto")
                    backend.useAutomaticGateway()
                }
            }
        }
    }

    Rectangle {
        id: manualBox
        visible: root.locationsModel.length > 0
        width: root.width * 0.90
        radius: 10
        color: Theme.fgColor
        height: getManualBoxHeight()

        anchors {
            horizontalCenter: parent.horizontalCenter
            top: autoBox.bottom
            margins: 10
        }

        ScrollView {
            id: frame
            clip: true
            anchors.fill: parent
            ScrollBar.vertical.policy: ScrollBar.AlwaysOff

            Flickable {
                id: flickable
                contentHeight: getManualBoxHeight()
                width: parent.width

                ScrollIndicator.vertical: ScrollIndicator {
                    size: 5
                    contentItem: Rectangle {
                        implicitWidth: 5
                        implicitHeight: 100
                        color: "grey"
                    }
                }

                Rectangle {
                    anchors {
                        fill: parent
                        margins: 10
                    }
                    Label {
                        id: manualLabel
                        text: manualSelectionLabel
                        font.bold: true
                    }
                    Label {
                        id: bridgeWarning
                        text: onlyBridgesWarning
                        color: "gray"
                        visible: isBridgeSelected()
                        wrapMode: Text.Wrap
                        font.pixelSize: Theme.fontSize - 3
                        anchors {
                            topMargin: 5
                            top: manualLabel.bottom
                        }
                    }

                    ColumnLayout {
                        id: gatewayListColumn
                        width: parent.width
                        spacing: 1
                        anchors {
                            topMargin: 10
                            top: getManualAnchor()
                        }

                        Repeater {
                            id: gwManualSelectorList
                            width: parent.width
                            model: root.locationsModel

                            RowLayout {
                                width: parent.width
                                WrappedRadioButton {
                                    text: getLocationLabel(modelData)
                                    location: modelData
                                    ButtonGroup.group: locsel
                                    checked: false
                                    enabled: locationPage.switching ? false : true
                                    HoverHandler {
                                        cursorShape: Qt.PointingHandCursor
                                    }
                                    onClicked: {
                                        if (ctx.status == "on") {
                                            locationPage.switching = true
                                        }
                                        root.selectedGateway = location
                                        backend.useLocation(location)
                                    }
                                }
                                Item {
                                    Layout.fillWidth: true
                                }
                                Image {
                                    height: 30
                                    width: 30
                                    smooth: true
                                    visible: isBridgeSelected()
                                    fillMode: Image.PreserveAspectFit
                                    source: "../resources/bridge.svg"
                                    Layout.alignment: Qt.AlignRight
                                    Layout.rightMargin: 10
                                }
                                SignalIcon {
                                    quality: getSignalFor(modelData)
                                    Layout.alignment: Qt.AlignRight
                                    Layout.rightMargin: 20
                                }
                            }
                        }
                    }
                }
            } //flickable
        } // scrollview
    } // manualbox

    StateGroup {
        states: [
            State {
                when: locationPage.switching && ctx.status != "on"
                PropertyChanges {
                    target: manualLabel
                    text: switchingLocationLabel
                }
            },
            State {
                when: ctx != undefined && ctx.status == "on"
                PropertyChanges {
                    target: manualLabel
                    text: manualSelectionLabel
                }
                StateChangeScript {
                    script: {
                        locationPage.switching = false
                    }
                }
            }
        ]
    }

    function getAutoLabel() {
        let l = autoSelectionLabel
        /* There's been some discussion about whether to include this.
         An argument is that it is not 100% sure that we're going to connect
         to this "recommended" gateway. However, it's fair to tell the user what's likely
         to be the recomended location, to make a better choice. ALso, we can
         implement a warning if finally connecting to a different location.
         That said, all is made worse by the fact that menshen will not return
         the "right" location if we're connecting  from the vpn, a proxy etc... For that we need to modify menshen to accept a location parameter.
         Disabling the hint for now, but some agreement needs to be done on android + desktop about this behavior.
        if (ctx && ctx.locations && ctx.bestLocation) {
            let best = ctx.locationLabels[ctx.bestLocation]
            let label = best[0] + ", " + best[1]
            l += " (" + label + ")"
        }
        */
        return l
    }

    function getLocationLabel(location) {
        if (!ctx) {
            return ""
        }
        let l = ctx.locationLabels[location]
        return l[0] + ", " + l[1]
    }

    function getManualBoxHeight() {
        let h = Math.min(
            root.locationsModel.length * 35,
            root.appHeight - autoBox.height - 100
        )
        if (bridgeWarning.visible) {
            h += bridgeWarning.height
        }
        return h + 50
    }

    function getSignalFor(location) {
        // this is an ad-hoc solution for the no-menshen, riseup case.
        // when menshen is deployed we'll want to tweak the values for each bucket.
        let load = ctx.locations[location]
        switch (true) {
        case (load > 0.5):
            return "good"
        case (load > 0.25):
            return "medium"
        default:
            return "low"
        }
    }

    function getLocationColor() {
        if (ctx && ctx.status == "on") {
            return "black"
        } else {
            // TODO darker gray
            return "gray"
        }
    }

    function isBridgeSelected() {
        if (ctx && ctx.transport == "obfs4") {
            return true
        } else {
            return false
        }
    }

    function getManualAnchor() {
        if (isBridgeSelected()) {
            return bridgeWarning.bottom
        } else {
            return manualLabel.bottom
        }
    }

    Component.onCompleted: {
        if (root.selectedGateway == "auto") {
            autoRadioButton.checked = true
        } else {
            let match = false
            for (var i = 1; i < locsel.buttons.length; i++) {
                let b = locsel.buttons[i]
                if (b.location == root.selectedGateway) {
                    match = true
                    b.checked = true
                }
            }
        }
    }
}