~ci-train-bot/ubuntu-settings-components/ubuntu-settings-components-ubuntu-yakkety-1895

« back to all changes in this revision

Viewing changes to plugins/Ubuntu/Settings/Fingerprint/Fingerprints.qml

  • Committer: Bileto Bot
  • Date: 2016-06-20 14:01:03 UTC
  • mfrom: (103.6.69 fingerprint)
  • Revision ID: ci-train-bot@canonical.com-20160620140103-q57lulbze89e6yyd
jonas-drange
* Add components for Fingerprint management.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2016 Canonical Ltd.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU Lesser General Public License as published by
 
6
 * the Free Software Foundation; version 3.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by Jonas G. Drange <jonas.drange@canonical.com>
 
17
 */
 
18
 
 
19
import QtQuick 2.4
 
20
import QtQuick.Layouts 1.1
 
21
import Biometryd 0.0
 
22
import GSettings 1.0
 
23
import Ubuntu.Components 1.3
 
24
import Ubuntu.Components.Popups 1.3
 
25
import Ubuntu.Settings.Fingerprint 0.1
 
26
 
 
27
Page {
 
28
    id: root
 
29
    objectName: "fingerprintPage"
 
30
 
 
31
    header: PageHeader {
 
32
        title: i18n.dtr("ubuntu-settings-components", "Fingerprint ID")
 
33
        flickable: content
 
34
    }
 
35
 
 
36
    property var _ts: Biometryd.defaultDevice.templateStore
 
37
    property var _enrollmentOperation: null
 
38
    property var _clearanceOperation: null
 
39
    property var _removalOperation: null
 
40
    property var _listOperation: null
 
41
    property var _setupPage: null
 
42
    property var _fpInstancePage: null
 
43
    property var _settings: sysSettings
 
44
 
 
45
    property Dialog _diag: null
 
46
    property bool passcodeSet: false
 
47
 
 
48
    function enroll () {
 
49
        _enrollmentOperation = _ts.enroll(user);
 
50
        _enrollmentOperation.start(enrollmentObserver);
 
51
    }
 
52
 
 
53
    function cancel () {
 
54
        if (_enrollmentOperation)
 
55
            _enrollmentOperation.cancel();
 
56
    }
 
57
 
 
58
    function clear() {
 
59
        _clearanceOperation = _ts.clear(user);
 
60
        _clearanceOperation.start(clearanceObserver);
 
61
    }
 
62
 
 
63
    function removeTemplate(templateId) {
 
64
        var names = sysSettings.fingerprintNames;
 
65
        delete names[templateId];
 
66
        sysSettings.fingerprintNames = names;
 
67
    }
 
68
 
 
69
    function renameTemplate(templateId, newName) {
 
70
        var names = sysSettings.fingerprintNames;
 
71
        names[templateId] = newName;
 
72
        sysSettings.fingerprintNames = names;
 
73
    }
 
74
 
 
75
    function addTemplate(templateId, name) {
 
76
        var names = sysSettings.fingerprintNames;
 
77
        names[templateId] = name;
 
78
        sysSettings.fingerprintNames = names;
 
79
    }
 
80
 
 
81
    function createTemplateName() {
 
82
        var map = sysSettings.fingerprintNames;
 
83
        var currentNames = [];
 
84
        var newName;
 
85
        for (var k in map) {
 
86
            if (map.hasOwnProperty(k))
 
87
                currentNames.push(map[k]);
 
88
        }
 
89
 
 
90
        var i = 0;
 
91
        do {
 
92
            newName = i18n.dtr("ubuntu-settings-components",
 
93
                               "Finger %1").arg(++i); // Start at 1
 
94
        } while (currentNames.indexOf(newName) >= 0);
 
95
        return newName;
 
96
    }
 
97
 
 
98
    // Assign names to unnamed fingerprints. This exist because we can't
 
99
    // guarantee that all fingerprints get names (i.e. enrollment can complete
 
100
    // after System Settings closes).
 
101
    function assignNames(templateIds) {
 
102
        var names = sysSettings.fingerprintNames;
 
103
        for (var i = 0; i < templateIds.length; i++) {
 
104
            if ( !(templateIds[i] in names) || !names[templateIds[i]])
 
105
                names[templateIds[i]] = createTemplateName();
 
106
                sysSettings.fingerprintNames = names;
 
107
        }
 
108
    }
 
109
 
 
110
    signal requestPasscode()
 
111
 
 
112
    Component.onCompleted: {
 
113
        // Start a list operation immediately.
 
114
        if (Biometryd.available) {
 
115
            _listOperation = _ts.list(user);
 
116
            _listOperation.start(listObserver);
 
117
        }
 
118
    }
 
119
 
 
120
    Component.onDestruction: {
 
121
        if (_enrollmentOperation)
 
122
            _enrollmentOperation.cancel();
 
123
 
 
124
        if (_clearanceOperation)
 
125
            _clearanceOperation.cancel();
 
126
 
 
127
        if (_removalOperation)
 
128
            _removalOperation.cancel();
 
129
 
 
130
        if (_listOperation)
 
131
            _listOperation.cancel();
 
132
    }
 
133
 
 
134
    states: [
 
135
        State {
 
136
            name: "noPasscode"
 
137
            PropertyChanges {
 
138
                target: setupPasscode
 
139
                visible: true
 
140
            }
 
141
            PropertyChanges {
 
142
                target: setupFingerprint
 
143
                enabled: false
 
144
            }
 
145
            PropertyChanges {
 
146
                target: fingerprintsList
 
147
                enabled: false
 
148
            }
 
149
            when: !passcodeSet
 
150
        },
 
151
        State {
 
152
            name: "noScanner"
 
153
            PropertyChanges {
 
154
                target: setupFingerprint
 
155
                enabled: false
 
156
            }
 
157
            PropertyChanges {
 
158
                target: fingerprintsList
 
159
                visible: false
 
160
            }
 
161
            when: !Biometryd.available
 
162
        }
 
163
    ]
 
164
 
 
165
    Flickable {
 
166
        id: content
 
167
        anchors.fill: parent
 
168
        boundsBehavior: (contentHeight > root.height) ?
 
169
                            Flickable.DragAndOvershootBounds :
 
170
                            Flickable.StopAtBounds
 
171
        contentHeight: contentItem.childrenRect.height
 
172
 
 
173
        Column {
 
174
            anchors { left: parent.left; right: parent.right }
 
175
 
 
176
            Column {
 
177
                id: setupPasscode
 
178
                anchors { left: parent.left; right: parent.right }
 
179
                visible: false
 
180
 
 
181
                ListItem {
 
182
                    height: setPcodeLayout.height + (divider.visible ? divider.height : 0)
 
183
                    ListItemLayout {
 
184
                        id: setPcodeLayout
 
185
                        title.text: i18n.dtr(
 
186
                            "ubuntu-settings-components",
 
187
                            "Passcode required"
 
188
                        )
 
189
                        subtitle.text: i18n.dtr(
 
190
                            "ubuntu-settings-components",
 
191
                            "You must set a passcode to use fingerprint ID"
 
192
                        )
 
193
                        Button {
 
194
                            objectName: "fingerprintSetPasscodeButton"
 
195
                            onClicked: root.requestPasscode()
 
196
                            text: i18n.dtr(
 
197
                                "ubuntu-settings-components",
 
198
                                "Set Passcode…"
 
199
                            )
 
200
                        }
 
201
                    }
 
202
                }
 
203
            }
 
204
 
 
205
            Column {
 
206
                id: fingerprints
 
207
                anchors { left: parent.left; right: parent.right }
 
208
 
 
209
                Repeater {
 
210
                    id: fingerprintsList
 
211
                    property bool enabled: true
 
212
                    objectName: "fingerprintsList"
 
213
                    model: {
 
214
                        var map = sysSettings.fingerprintNames;
 
215
                        var m = [];
 
216
                        for (var k in map) {
 
217
                            if (map.hasOwnProperty(k)) {
 
218
                                m.push({
 
219
                                    templateId: k,
 
220
                                    name: map[k]
 
221
                                });
 
222
                            }
 
223
                        }
 
224
                        m.sort(function (a, b) {
 
225
                            if (a.name < b.name) return -1;
 
226
                            if (a.name > b.name) return 1;
 
227
                            if (a.name == b.name) return 0;
 
228
                        });
 
229
                        return m;
 
230
                    }
 
231
 
 
232
                    ListItem {
 
233
                        height: fpLayout.height + (divider.visible ? divider.height : 0)
 
234
                        onClicked: _fpInstancePage = pageStack.push(
 
235
                                Qt.resolvedUrl("Fingerprint.qml"), {
 
236
                                name: modelData.name,
 
237
                                templateId: modelData.templateId
 
238
                            }
 
239
                        )
 
240
                        enabled: fingerprintsList.enabled
 
241
 
 
242
                        ListItemLayout {
 
243
                            id: fpLayout
 
244
                            objectName: "fingerprintInstance-" + index
 
245
                            title.text: modelData.name
 
246
 
 
247
                            ProgressionSlot {}
 
248
                        }
 
249
                    }
 
250
                }
 
251
            }
 
252
 
 
253
            Column {
 
254
                id: setupFingerprint
 
255
 
 
256
                anchors { left: parent.left; right: parent.right }
 
257
                objectName: "fingerprintSetupEntry"
 
258
                property bool enabled: true
 
259
                spacing: units.gu(2)
 
260
 
 
261
                ListItem {
 
262
                    height: addFpLayout.height + (divider.visible ? divider.height : 0)
 
263
                    onClicked: {
 
264
                        _setupPage = pageStack.push(Qt.resolvedUrl("Setup.qml"));
 
265
                        root.enroll();
 
266
                    }
 
267
                    enabled: parent.enabled
 
268
 
 
269
                    ListItemLayout {
 
270
                        id: addFpLayout
 
271
                        objectName: "fingerprintAddListItemLayout"
 
272
                        title.text: i18n.dtr(
 
273
                            "ubuntu-settings-components",
 
274
                            "Add fingerprint"
 
275
                        )
 
276
 
 
277
                        ProgressionSlot {}
 
278
                    }
 
279
                }
 
280
 
 
281
                Button {
 
282
                    anchors.horizontalCenter: parent.horizontalCenter
 
283
                    enabled: fingerprintsList.model.length > 0
 
284
                    objectName: "fingerprintRemoveAllButton"
 
285
                    onClicked: _diag = PopupUtils.open(removeAllAlert)
 
286
                    text: i18n.dtr("ubuntu-settings-components",
 
287
                                   "Remove All…")
 
288
                }
 
289
            }
 
290
        }
 
291
    }
 
292
 
 
293
    Component {
 
294
        id: removeAllAlert
 
295
 
 
296
        Dialog {
 
297
            id: removeAllAlertDialog
 
298
 
 
299
            objectName: "fingerprintRemoveAllDialog"
 
300
            text: i18n.dtr(
 
301
                "ubuntu-settings-components",
 
302
                "Are you sure you want to forget all stored fingerprints?"
 
303
            )
 
304
 
 
305
            RowLayout {
 
306
                anchors { left: parent.left; right: parent.right }
 
307
                spacing: units.gu(2)
 
308
 
 
309
                Button {
 
310
                    onClicked: PopupUtils.close(removeAllAlertDialog)
 
311
                    text: i18n.dtr("ubuntu-settings-components", "Cancel")
 
312
                    Layout.fillWidth: true
 
313
                }
 
314
 
 
315
                Button {
 
316
                    objectName: "fingerprintRemoveAllConfirmationButton"
 
317
                    onClicked: root.clear()
 
318
                    text: i18n.dtr("ubuntu-settings-components", "Remove")
 
319
                    Layout.fillWidth: true
 
320
                }
 
321
            }
 
322
        }
 
323
    }
 
324
 
 
325
    Component {
 
326
        id: fingerprintReaderBroken
 
327
 
 
328
        Dialog {
 
329
            id: fingerprintReaderBrokenDialog
 
330
            objectName: "fingerprintReaderBrokenDialog"
 
331
            text: i18n.dtr("ubuntu-settings-components",
 
332
                           "Sorry, the reader doesn’t seem to be working.")
 
333
 
 
334
            Button {
 
335
                objectName: "fingerprintReaderBrokenDialogOK"
 
336
                onClicked: PopupUtils.close(fingerprintReaderBrokenDialog)
 
337
                text: i18n.dtr("ubuntu-settings-components", "OK")
 
338
            }
 
339
        }
 
340
    }
 
341
 
 
342
    Connections {
 
343
        target: _setupPage
 
344
        onEnroll: root.enroll()
 
345
        onCancel: root.cancel()
 
346
    }
 
347
 
 
348
    Connections {
 
349
        target: _fpInstancePage
 
350
 
 
351
        onRequestDeletion: {
 
352
            _removalOperation = _ts.remove(user, templateId);
 
353
            _removalOperation.start(removalObserver);
 
354
        }
 
355
        onRequestRename: renameTemplate(templateId, name)
 
356
    }
 
357
 
 
358
    Observer {
 
359
        id: enrollmentObserver
 
360
        objectName: "enrollmentObserver"
 
361
        onFailed: {
 
362
            if (_setupPage)
 
363
                _setupPage.enrollmentFailed(reason);
 
364
            _enrollmentOperation = null;
 
365
            console.error("Enrollment failed", reason);
 
366
        }
 
367
        onCanceled: _enrollmentOperation = null
 
368
        onProgressed: _setupPage.enrollmentProgressed(percent, details)
 
369
        onSucceeded: {
 
370
            if (!(result in sysSettings.fingerprintNames))
 
371
                root.addTemplate(result, root.createTemplateName());
 
372
            if (_setupPage)
 
373
                _setupPage.enrollmentCompleted();
 
374
            _enrollmentOperation = null;
 
375
        }
 
376
    }
 
377
 
 
378
    Observer {
 
379
        id: clearanceObserver
 
380
        objectName: "clearanceObserver"
 
381
        onFailed: {
 
382
            _clearanceOperation = null;
 
383
            if (_diag) PopupUtils.close(_diag);
 
384
            _diag = PopupUtils.open(fingerprintReaderBroken);
 
385
            console.error("Biometry clearance failed:", reason);
 
386
        }
 
387
        onCanceled: _clearanceOperation = null
 
388
        onSucceeded: {
 
389
            _clearanceOperation = null;
 
390
            if (_diag) PopupUtils.close(_diag);
 
391
            sysSettings.fingerprintNames = {};
 
392
        }
 
393
    }
 
394
 
 
395
    Observer {
 
396
        id: removalObserver
 
397
        objectName: "removalObserver"
 
398
        onFailed: {
 
399
            _removalOperation = null;
 
400
            if (_fpInstancePage)
 
401
                _fpInstancePage.deletionFailed()
 
402
            console.error("Biometryd template deletion failed:", reason);
 
403
        }
 
404
        onCanceled: _removalOperation = null
 
405
        onSucceeded: {
 
406
            _removalOperation = null;
 
407
            if (pageStack.currentPage === _fpInstancePage)
 
408
                pageStack.pop();
 
409
            root.removeTemplate(result);
 
410
        }
 
411
    }
 
412
 
 
413
    Observer {
 
414
        id: listObserver
 
415
        objectName: "listObserver"
 
416
        onFailed: {
 
417
            _listOperation = null;
 
418
            if (_diag) PopupUtils.close(_diag);
 
419
            _diag = PopupUtils.open(fingerprintReaderBroken);
 
420
            console.error("Biometryd list failed:", reason);
 
421
        }
 
422
        onCanceled: _listOperation = null
 
423
        onSucceeded: {
 
424
            _listOperation = null;
 
425
            root.assignNames(result);
 
426
        }
 
427
    }
 
428
 
 
429
    User {
 
430
        id: user
 
431
        uid: UbuntuSettingsFingerprint.uid
 
432
    }
 
433
 
 
434
    GSettings {
 
435
        id: sysSettings
 
436
        objectName: "systemSettings"
 
437
        schema.id: "com.ubuntu.touch.system"
 
438
    }
 
439
}