~jonas-drange/ubuntu-system-settings/hotspots-change-test-backend

« back to all changes in this revision

Viewing changes to plugins/cellular/HotspotSetup.qml

  • Committer: jonas-drange
  • Date: 2015-07-22 14:16:13 UTC
  • mfrom: (1312.3.74 tmp)
  • Revision ID: jonas.drange@canonical.com-20150722141613-4hz7rolyt8eei8ht
  Pete Woods 2015-07-16 Manage hotspots via connectity API

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
import SystemSettings 1.0
22
22
import Ubuntu.Components 0.1
23
23
import Ubuntu.Components.ListItems 0.1 as ListItem
 
24
import Ubuntu.Connectivity 1.0
24
25
import Ubuntu.SystemSettings.Cellular 1.0
25
 
 
26
 
ItemPage {
27
 
 
 
26
import Ubuntu.Components.Popups 0.1
 
27
 
 
28
Component {
28
29
    id: hotspotSetup
29
 
    title: i18n.tr("Change hotspot setup")
30
 
 
31
 
    property var hotspotManager: null
32
 
 
33
 
 
34
 
    Column {
35
 
 
36
 
        anchors.fill: parent
37
 
 
38
 
        ListItem.Standard {
39
 
            id: ssidLabel
40
 
            text: i18n.tr("Hotspot name")
41
 
        }
42
 
 
43
 
        TextField {
44
 
            id: ssidField
45
 
            text: hotspotManager.getHotspotName()
46
 
            anchors.left: parent.left
47
 
            anchors.right: parent.right
48
 
            anchors.leftMargin: units.gu(2)
49
 
            anchors.rightMargin: units.gu(2)
50
 
        }
51
 
 
52
 
        ListItem.Standard {
53
 
            id: passwordLabel
54
 
            text: i18n.tr("Key (must be 8 characters or longer)")
55
 
        }
56
 
 
57
 
        TextField {
58
 
            id: passwordField
59
 
            text: hotspotManager.getHotspotPassword()
60
 
            echoMode: passwordVisibleSwitch.checked ? TextInput.Normal : TextInput.Password
61
 
            anchors.left: parent.left
62
 
            anchors.right: parent.right
63
 
            anchors.leftMargin: units.gu(2)
64
 
            anchors.rightMargin: units.gu(2)
65
 
        }
66
 
 
67
 
        ListItem.Standard {
68
 
            text: i18n.tr("Show key")
69
 
            id: passwordVisible
70
 
            control: Switch {
71
 
                id: passwordVisibleSwitch
 
30
 
 
31
    Dialog {
 
32
        id: hotspotSetupDialog
 
33
 
 
34
        /* Connectivity.hotspotStored changes as soon as the user has added a
 
35
        hotspot, and we use this value when we choose between e.g. "Change" and
 
36
        "Setup". We'd like the narrative to be consistent, so we stick with
 
37
        what the stored value was at component completion.
 
38
        */
 
39
        property bool stored: false
 
40
        Component.onCompleted: stored = Connectivity.hotspotStored;
 
41
 
 
42
        objectName: "hotspotSetup"
 
43
        anchorToKeyboard: true
 
44
 
 
45
        function settingsValid() {
 
46
            return ssidField.text != "" && passwordField.length >= 8;
 
47
        }
 
48
 
 
49
        title: stored ?
 
50
            i18n.tr("Change hotspot setup") : i18n.tr("Setup hotspot")
 
51
        text: feedback.enabled ? feedback.text : "";
 
52
 
 
53
        Common {
 
54
            id: common
 
55
        }
 
56
 
 
57
        states: [
 
58
            State {
 
59
                name: "STARTING"
 
60
                PropertyChanges {
 
61
                    target: workingIndicator
 
62
                    running: true
 
63
                }
 
64
                PropertyChanges {
 
65
                    target: ssidLabel
 
66
                    opacity: 0.5
 
67
                }
 
68
                PropertyChanges {
 
69
                    target: ssidField
 
70
                    enabled: false
 
71
                }
 
72
                PropertyChanges {
 
73
                    target: passwordLabel
 
74
                    opacity: 0.5
 
75
                }
 
76
                PropertyChanges {
 
77
                    target: passwordField
 
78
                    enabled: false
 
79
                }
 
80
                PropertyChanges {
 
81
                    target: feedback
 
82
                    enabled: true
 
83
                }
 
84
                PropertyChanges {
 
85
                    target: confirmButton
 
86
                    enabled: false
 
87
                }
 
88
            },
 
89
 
 
90
            State {
 
91
                name: "FAILED"
 
92
                PropertyChanges {
 
93
                    target: feedback
 
94
                    enabled: true
 
95
                }
 
96
                PropertyChanges {
 
97
                    target: ssidField
 
98
                    errorHighlight: true
 
99
                }
 
100
                StateChangeScript {
 
101
                    script: ssidField.forceActiveFocus()
 
102
                }
 
103
            },
 
104
 
 
105
            State {
 
106
                name: "SUCCEEDED"
 
107
                PropertyChanges {
 
108
                    target: successIcon
 
109
                    visible: true
 
110
                }
 
111
                PropertyChanges {
 
112
                    target: successIndicator
 
113
                    running: true
 
114
                }
 
115
                PropertyChanges {
 
116
                    target: ssidLabel
 
117
                    opacity: 0.5
 
118
                }
 
119
                PropertyChanges {
 
120
                    target: ssidField
 
121
                    enabled: false
 
122
                }
 
123
                PropertyChanges {
 
124
                    target: passwordLabel
 
125
                    opacity: 0.5
 
126
                }
 
127
                PropertyChanges {
 
128
                    target: passwordField
 
129
                    enabled: false
 
130
                }
 
131
                PropertyChanges {
 
132
                    target: confirmButton
 
133
                    enabled: false
 
134
                }
72
135
            }
73
 
        }
 
136
        ]
74
137
 
75
 
        RowLayout {
 
138
        Column {
76
139
            anchors {
77
140
                left: parent.left
78
141
                right: parent.right
79
 
                margins: units.gu(2)
80
 
            }
81
 
            
82
 
        Button {
83
 
            id: cancelButton
84
 
            Layout.fillWidth: true
85
 
            text: i18n.tr("Cancel")
86
 
            onClicked: {
87
 
                pageStack.pop()
88
 
            }
89
 
        }
90
 
 
91
 
        Button {
92
 
            id: connectButton
93
 
            Layout.fillWidth: true
94
 
            text: i18n.tr("Change")
95
 
            enabled: ssidField.text != "" && passwordField.length >= 8
96
 
            onClicked: {
97
 
                hotspotManager.setupHotspot(ssidField.text, passwordField.text)
98
 
                pageStack.pop()
99
 
            }
100
 
        }
 
142
            }
 
143
            spacing: units.gu(1)
 
144
 
 
145
            Label {
 
146
                property bool enabled: false
 
147
                id: feedback
 
148
                horizontalAlignment: Text.AlignHCenter
 
149
                height: contentHeight
 
150
                wrapMode: Text.WordWrap
 
151
                visible: false
 
152
            }
 
153
 
 
154
            Label {
 
155
                id: ssidLabel
 
156
                text: i18n.tr("Hotspot name")
 
157
                fontSize: "medium"
 
158
                font.bold: true
 
159
                color: Theme.palette.selected.backgroundText
 
160
                elide: Text.ElideRight
 
161
            }
 
162
 
 
163
            TextField {
 
164
                id: ssidField
 
165
                objectName: "ssidField"
 
166
                text: Connectivity.hotspotSsid
 
167
                inputMethodHints: Qt.ImhNoAutoUppercase | Qt.ImhNoPredictiveText
 
168
                Component.onCompleted: forceActiveFocus()
 
169
                width: parent.width
 
170
            }
 
171
 
 
172
            Label {
 
173
                id: passwordLabel
 
174
                text: i18n.tr("Key (must be 8 characters or longer)")
 
175
                fontSize: "medium"
 
176
                font.bold: true
 
177
                color: Theme.palette.selected.backgroundText
 
178
                wrapMode: Text.WordWrap
 
179
                width: parent.width
 
180
            }
 
181
 
 
182
            TextField {
 
183
                id: passwordField
 
184
                objectName: "passwordField"
 
185
                text: Connectivity.hotspotPassword
 
186
                echoMode: passwordVisibleSwitch.checked ?
 
187
                    TextInput.Normal : TextInput.Password
 
188
                inputMethodHints: Qt.ImhNoAutoUppercase | Qt.ImhNoPredictiveText
 
189
                width: parent.width
 
190
            }
 
191
 
 
192
            ListItem.Standard {
 
193
                text: i18n.tr("Show key")
 
194
                id: passwordVisible
 
195
                onClicked: passwordVisibleSwitch.trigger()
 
196
                control: Switch {
 
197
                    id: passwordVisibleSwitch
 
198
                    activeFocusOnPress: false
 
199
                }
 
200
            }
 
201
 
 
202
            Row {
 
203
 
 
204
                anchors {
 
205
                    left: parent.left
 
206
                    right: parent.right
 
207
                }
 
208
                width: parent.width
 
209
                spacing: units.gu(2)
 
210
 
 
211
                Button {
 
212
                    id: cancelButton
 
213
                    width: (parent.width / 2) - units.gu(1)
 
214
                    text: i18n.tr("Cancel")
 
215
                    activeFocusOnPress: false
 
216
                    onClicked: PopupUtils.close(hotspotSetupDialog)
 
217
                }
 
218
 
 
219
                Button {
 
220
                    id: confirmButton
 
221
                    objectName: "confirmButton"
 
222
                    width: (parent.width / 2) - units.gu(1)
 
223
                    text: hotspotSetupDialog.stored ? i18n.tr("Change") :
 
224
                        i18n.tr("Enable")
 
225
                    enabled: settingsValid()
 
226
                    activeFocusOnPress: false
 
227
                    onClicked: {
 
228
                        if (hotspotSetupDialog.stored) {
 
229
                            changeAction.trigger()
 
230
                        } else {
 
231
                            enableAction.trigger();
 
232
                        }
 
233
                    }
 
234
 
 
235
                    Icon {
 
236
                        id: successIcon
 
237
                        anchors.centerIn: parent
 
238
                        height: parent.height - units.gu(1.5)
 
239
                        width: parent.height - units.gu(1.5)
 
240
                        name: "tick"
 
241
                        color: "green"
 
242
                        visible: false
 
243
                    }
 
244
 
 
245
                    ActivityIndicator {
 
246
                        id: workingIndicator
 
247
                        anchors.centerIn: parent
 
248
                        running: false
 
249
                        visible: running
 
250
                        height: parent.height - units.gu(1.5)
 
251
                    }
 
252
                }
 
253
            }
 
254
        }
 
255
 
 
256
        Action {
 
257
            id: enableAction
 
258
            enabled: settingsValid()
 
259
            onTriggered: {
 
260
                hotspotSetupDialog.state = "STARTING";
 
261
 
 
262
                function hotspotEnabledHandler (enabled) {
 
263
                    if (enabled) {
 
264
                        hotspotSetupDialog.state = "SUCCEEDED";
 
265
                        Connectivity.hotspotEnabledUpdated.disconnect(
 
266
                            hotspotEnabledHandler);
 
267
                    }
 
268
                }
 
269
 
 
270
                Connectivity.hotspotSsid = ssidField.text;
 
271
                Connectivity.hotspotPassword = passwordField.text;
 
272
                Connectivity.hotspotEnabledUpdated.connect(hotspotEnabledHandler);
 
273
                Connectivity.hotspotEnabled = true;
 
274
            }
 
275
        }
 
276
 
 
277
        Action {
 
278
            id: changeAction
 
279
            enabled: settingsValid()
 
280
            onTriggered: {
 
281
 
 
282
                function hotspotEnabledHandler (enabled) {
 
283
                    if (enabled) {
 
284
                        hotspotSetupDialog.state = "SUCCEEDED";
 
285
                        Connectivity.hotspotEnabledUpdated.disconnect(
 
286
                            hotspotEnabledHandler);
 
287
                    }
 
288
                }
 
289
 
 
290
                function hotspotDisabledHandler (enabled) {
 
291
                    if (!enabled) {
 
292
                        Connectivity.hotspotEnabledUpdated.connect(hotspotEnabledHandler);
 
293
                        Connectivity.hotspotEnabled = true;
 
294
                        Connectivity.hotspotEnabledUpdated.disconnect(
 
295
                            hotspotDisabledHandler);
 
296
                    }
 
297
                }
 
298
 
 
299
                Connectivity.hotspotSsid = ssidField.text;
 
300
                Connectivity.hotspotPassword = passwordField.text;
 
301
 
 
302
                if (Connectivity.hotspotEnabled) {
 
303
                    hotspotSetupDialog.state = "STARTING";
 
304
                    Connectivity.hotspotEnabledUpdated.connect(
 
305
                        hotspotDisabledHandler);
 
306
                    Connectivity.hotspotEnabled = false;
 
307
                } else {
 
308
                    PopupUtils.close(hotspotSetupDialog);
 
309
                }
 
310
            }
 
311
        }
 
312
 
 
313
        /* Timer that shows a tick in the connect button once we have
 
314
        successfully changed/started a hotspot. */
 
315
        Timer {
 
316
            id: successIndicator
 
317
            interval: 2000
 
318
            running: false
 
319
            repeat: false
 
320
            onTriggered: PopupUtils.close(hotspotSetupDialog)
 
321
        }
 
322
 
 
323
        Connections {
 
324
            target: Connectivity
 
325
 
 
326
            onReportError: {
 
327
                if (hotspotSetupDialog.state === "STARTING") {
 
328
                    hotspotSetupDialog.state = "FAILED";
 
329
                    feedback.text = common.reasonToString(reason);
 
330
                }
 
331
            }
101
332
        }
102
333
    }
103
334
}