PageSettingsPin.qml 15.3 KB
Newer Older
1
/*
2
 * Copyright (C) 2014-2019 CZ.NIC
3 4 5 6 7 8 9 10 11 12 13 14
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21 22 23 24
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations including
 * the two.
 */

import QtQuick 2.7
25 26
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.1
27
import cz.nic.mobileDatovka 1.0
28

29 30
Item {
    id: pageSettingsPin
31

32 33 34 35
    /* These properties must be set by caller. */
    property var pageView
    property var statusBar
    property var accountModel: null
36

37 38 39 40 41 42 43 44 45 46 47 48
    Component.onCompleted: {
        currentPIN = settings.pinValue()
        actionName = "unspecified" /* Causes onChanged() to fire. */
    }
    property string currentPIN: ""
    property string actionName: "" /* Initial value left blank. */
    onActionNameChanged : { /* Handles visibility on actionName changes. */
        if (actionName == "unspecified") {
            acceptElement.visible = false
            if (currentPIN == "") {
                topLineText.text = qsTr("Currently there is no PIN code set.")
                setPinButton.visible = true
49 50
                changePinButton.visible = false
                disablePinButton.visible = false
51 52 53 54 55 56 57 58 59 60
            } else {
                topLineText.text = qsTr("You will be asked to enter a PIN code on application start-up.")
                setPinButton.visible = false
                changePinButton.visible = true
                disablePinButton.visible = true
            }
            pinValueField.visible = false
            pinConfirmField1.visible = false
            pinConfirmField2.visible = false
            errLineText.visible = false
61

62 63
            lockIntervalLabel.visible = true
            lockIntervalSpinBox.visible = true
64

65 66 67 68 69 70 71 72 73 74 75 76 77
            lockIntervalLabel.enabled = currentPIN != ""
            lockIntervalSpinBox.enabled = currentPIN != ""
            lockIntervalSpinBox.setVal(settings.inactivityInterval())
        } else if (actionName == "new") {
            acceptElement.visible = true
            topLineText.text = qsTr("Enter a new PIN code into both text fields:")
            setPinButton.visible = false
            changePinButton.visible = false
            disablePinButton.visible = false
            pinValueField.visible = false
            pinConfirmField1.visible = true
            pinConfirmField2.visible = true
            errLineText.visible = false
78

79 80
            lockIntervalLabel.visible = false
            lockIntervalSpinBox.visible = false
81

82 83 84 85 86 87 88 89 90 91 92 93
            pinConfirmField1.focus = true
        } else if (actionName == "change") {
            acceptElement.visible = true
            topLineText.text = qsTr("In order to change the PIN code you must enter the current and a new PIN code:")
            setPinButton.visible = false
            changePinButton.visible = false
            disablePinButton.visible = false
            pinValueField.visible = true
            pinValueField.focus = true
            pinConfirmField1.visible = true
            pinConfirmField2.visible = true
            errLineText.visible = false
94

95 96
            lockIntervalLabel.visible = false
            lockIntervalSpinBox.visible = false
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
            pinValueField.focus = true
        } else if (actionName == "disable") {
            acceptElement.visible = true
            topLineText.text = qsTr("In order to change the PIN code you must enter the current and a new PIN code:")
            setPinButton.visible = false
            changePinButton.visible = false
            disablePinButton.visible = false
            pinValueField.visible = true
            pinConfirmField1.visible = false
            pinConfirmField2.visible = false
            errLineText.visible = false

            lockIntervalLabel.visible = false
            lockIntervalSpinBox.visible = false

            pinValueField.focus = true
        } else {
            /* This line should not be reached. */
            topLineText.text = qsTr("Something went wrong!")
        }
    }
    PageHeader {
        id: headerBar
        title: qsTr("PIN Settings")
122 123 124 125 126 127
        onBackClicked: {
            if (actionName != "unspecified") {
                /* Navigate to initial action. */
                actionName = "unspecified"
            } else {
                pageView.pop(StackView.Immediate)
128 129
            }
        }
130 131 132 133 134
        Row {
            anchors.verticalCenter: parent.verticalCenter
            spacing: defaultMargin
            anchors.right: parent.right
            anchors.rightMargin: defaultMargin
135
            AccessibleImageButton {
136 137
                id: acceptElement
                anchors.verticalCenter: parent.verticalCenter
Martin Straka's avatar
Martin Straka committed
138
                sourceSize.height: imgHeightHeader
139
                source: "qrc:/ui/checkbox-marked-circle.svg"
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
                accessibleName: qsTr("Accept changes")
                onClicked: {
                    if (actionName == "unspecified") {
                        if (currentPIN == "") {
                            settings.setInactivityInterval(0)
                            locker.setInactivityInterval(0)
                        } else {
                            settings.setInactivityInterval(lockIntervalSpinBox.val())
                            locker.setInactivityInterval(lockIntervalSpinBox.val())
                        }
                        pageView.pop(StackView.Immediate)
                    /* set new pin code */
                    } else if (actionName == "new") {
                        if (pinConfirmField1.text == "" || pinConfirmField2.text == "") {
                            errLineText.text = qsTr("Error: Both new PIN code fields must be filled in!")
                            errLineText.visible = true
                            if (pinConfirmField1.text == "") {
                                pinConfirmField1.focus = true
                            } else if (pinConfirmField2.text == "") {
                                pinConfirmField2.focus = true
160
                            }
161 162 163 164 165 166 167 168
                        } else if (pinConfirmField2.text != pinConfirmField1.text) {
                            pinConfirmField2.focus = true
                            errLineText.text = qsTr("Error: Newly entered PIN codes are different!")
                            errLineText.visible = true
                        } else {
                            errLineText.visible = false
                            settings.updatePinSettings(pinConfirmField1.text.toString())
                            settings.saveAllSettings(accountModel)
169
                            pageView.pop(StackView.Immediate)
170 171 172 173 174
                        }
                    /* change current pin code */
                    } else if (actionName == "change") {
                        if (pinValueField.text == currentPIN) {
                            errLineText.visible = false
175 176 177 178 179 180 181
                            if (pinConfirmField1.text == "" || pinConfirmField2.text == "") {
                                errLineText.text = qsTr("Error: Both new PIN code fields must be filled in!")
                                errLineText.visible = true
                                if (pinConfirmField1.text == "") {
                                    pinConfirmField1.focus = true
                                } else if (pinConfirmField2.text == "") {
                                    pinConfirmField2.focus = true
182
                                }
183 184 185 186 187 188 189 190
                            } else if (pinConfirmField2.text != pinConfirmField1.text) {
                                pinConfirmField2.focus = true
                                errLineText.text = qsTr("Error: Newly entered PIN codes are different!")
                                errLineText.visible = true
                            } else {
                                errLineText.visible = false
                                settings.updatePinSettings(pinConfirmField1.text.toString())
                                settings.saveAllSettings(accountModel)
191
                                pageView.pop(StackView.Immediate)
192
                            }
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
                        } else {
                            pinValueField.text = ""
                            pinValueField.focus = true
                            errLineText.text = qsTr("Error: Current PIN code is wrong!")
                            errLineText.visible = true
                        }
                    /* remove/disable current pin code */
                    } else if (actionName == "disable") {
                        if (pinValueField.text == currentPIN) {
                            errLineText.visible = false
                            settings.updatePinSettings("")
                            settings.saveAllSettings(accountModel)
                            settings.setInactivityInterval(0)
                            locker.setInactivityInterval(0)
                            pageView.pop(StackView.Immediate)
                        } else {
                            pinValueField.text = ""
                            pinValueField.focus = true
                            errLineText.text = qsTr("Error: Current PIN code is wrong!")
                            errLineText.visible = true
213 214 215 216 217
                        }
                    }
                }
            }
        }
218 219 220 221 222 223 224 225 226 227 228 229
    } // PageHeader
    Flickable {
        id: flickable
        z: 0
        anchors.top: headerBar.bottom
        anchors.right: parent.right
        anchors.left: parent.left
        anchors.bottom: parent.bottom
        contentHeight: flickContent.implicitHeight
        Pane {
            id: flickContent
            anchors.fill: parent
230
            Column {
231 232
                anchors.right: parent.right
                anchors.left: parent.left
233
                spacing: formItemVerticalSpacing
234
                AccessibleText {
235
                    id: topLineText
236 237
                    anchors.horizontalCenter: parent.horizontalCenter
                    color: datovkaPalette.mid
238
                    width: parent.width
239 240 241 242
                    text: ""
                    horizontalAlignment: Text.AlignHCenter
                    wrapMode: Text.Wrap
                }
243
                AccessibleButton {
244
                    id: setPinButton
245
                    height: inputItemHeight
246
                    font.pointSize: defaultTextFont.font.pointSize
247 248 249 250
                    anchors.horizontalCenter: parent.horizontalCenter
                    text: qsTr("Set PIN")
                    onClicked: {
                        actionName = "new"
251
                    }
252
                }
253
                AccessibleButton {
254
                    id: changePinButton
255
                    height: inputItemHeight
256 257
                    font.pointSize: defaultTextFont.font.pointSize
                    anchors.horizontalCenter: parent.horizontalCenter
258 259 260
                    text: qsTr("Change PIN")
                    onClicked: {
                        actionName = "change"
261
                    }
262
                }
263
                AccessibleButton {
264
                    id: disablePinButton
265
                    height: inputItemHeight
266 267
                    font.pointSize: defaultTextFont.font.pointSize
                    anchors.horizontalCenter: parent.horizontalCenter
268 269 270
                    text: qsTr("Disable PIN")
                    onClicked: {
                        actionName = "disable"
271
                    }
272
                }
273
                AccessibleTextField {
274
                    id: pinValueField
275
                    anchors.horizontalCenter: parent.horizontalCenter
276
                    height: inputItemHeight
277
                    font.pointSize: defaultTextFont.font.pointSize
278 279 280 281
                    echoMode: TextInput.Password
                    passwordMaskDelay: 500 // milliseconds
                    inputMethodHints: Qt.ImhDigitsOnly
                    placeholderText: qsTr("Current PIN code")
282
                    horizontalAlignment: TextInput.AlignHCenter
283 284
                    text: ""
                }
285
                AccessibleTextField {
286
                    id: pinConfirmField1
287
                    anchors.horizontalCenter: parent.horizontalCenter
288
                    height: inputItemHeight
289
                    font.pointSize: defaultTextFont.font.pointSize
290 291 292 293
                    echoMode: TextInput.Password
                    passwordMaskDelay: 500 // milliseconds
                    inputMethodHints: Qt.ImhDigitsOnly
                    placeholderText: qsTr("New PIN code")
294
                    horizontalAlignment: TextInput.AlignHCenter
295 296
                    text: ""
                }
297
                AccessibleTextField {
298
                    id: pinConfirmField2
299
                    anchors.horizontalCenter: parent.horizontalCenter
300
                    height: inputItemHeight
301
                    font.pointSize: defaultTextFont.font.pointSize
302 303 304
                    echoMode: TextInput.Password
                    passwordMaskDelay: 500 // milliseconds
                    inputMethodHints: Qt.ImhDigitsOnly
305
                    placeholderText: qsTr("Confirm new PIN code")
306
                    horizontalAlignment: TextInput.AlignHCenter
307 308
                    text: ""
                }
309
                AccessibleText {
310
                    id: errLineText
311 312 313
                    anchors.horizontalCenter: parent.horizontalCenter
                    font.bold: true
                    color: datovkaPalette.text
314
                    width: parent.width
315
                    text: ""
316 317
                    horizontalAlignment: Text.AlignHCenter
                    wrapMode: Text.Wrap
318
                }
319
                AccessibleText {
320 321 322
                    id: lockIntervalLabel
                    color: datovkaPalette.text
                    anchors.horizontalCenter: parent.horizontalCenter
323
                    width: parent.width
324 325 326
                    text: qsTr("Lock after seconds of inactivity")
                    horizontalAlignment: Text.AlignHCenter
                }
327
                AccessibleSpinBoxZeroMax {
328 329
                    /* Holds value in seconds. */
                    id: lockIntervalSpinBox
330

331
                    anchors.horizontalCenter: parent.horizontalCenter
332

333
                    /* Must be a non-decreasing list ending with infinity. */
334 335
                    items: [15, 30, 60, 90, 120, 150, 180, qsTr("don't lock")]
                    dfltIdx: 0
336 337 338

                    accessibleDescription: qsTr("Select the number of seconds after which the application is going to be locked.")

339
                    onValueModified: {
340 341 342 343
                        /* Enable accept button. Always visible? */
                        acceptElement.visible = val() != settings.inactivityInterval()
                    }
                }
344 345 346 347 348 349
            } // Column layout
        } // Pane
        ScrollIndicator.vertical: ScrollIndicator {}
    } // Flickable
} // Item