PageSettingsPin.qml 15.8 KB
Newer Older
1
/*
2
 * Copyright (C) 2014-2017 CZ.NIC
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * 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
        MouseArea {
123
            anchors.left: parent.left
124 125 126 127
            anchors.top: parent.top
            anchors.bottom: parent.bottom
            width: parent.width * backMouseAreaRatio
            onClicked: {
128 129 130 131 132 133 134 135
                onClicked: {
                    if (actionName != "unspecified") {
                        /* Navigate to initial action. */
                        actionName = "unspecified"
                    } else {
                        pageView.pop(StackView.Immediate)
                    }
                }
136 137
            }
        }
138 139 140 141 142 143 144 145
        Row {
            anchors.verticalCenter: parent.verticalCenter
            spacing: defaultMargin
            anchors.right: parent.right
            anchors.rightMargin: defaultMargin
            Image {
                id: acceptElement
                anchors.verticalCenter: parent.verticalCenter
Martin Straka's avatar
Martin Straka committed
146
                sourceSize.height: imgHeightHeader
147
                source: "qrc:/ui/checkbox-marked-circle.svg"
148 149 150
                MouseArea {
                    anchors.fill: parent
                    onClicked: {
151 152 153 154 155 156 157 158
                        if (actionName == "unspecified") {
                            if (currentPIN == "") {
                                settings.setInactivityInterval(0)
                                locker.setInactivityInterval(0)
                            } else {
                                settings.setInactivityInterval(lockIntervalSpinBox.val())
                                locker.setInactivityInterval(lockIntervalSpinBox.val())
                            }
159
                            pageView.pop(StackView.Immediate)
160 161 162 163 164 165 166 167 168
                        /* 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
169
                                }
170 171 172 173 174 175 176 177
                            } 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)
178
                                pageView.pop(StackView.Immediate)
179 180 181 182 183
                            }
                        /* change current pin code */
                        } else if (actionName == "change") {
                            if (pinValueField.text == currentPIN) {
                                errLineText.visible = false
184 185 186 187 188 189 190
                                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
191
                                    }
192 193 194 195
                                } else if (pinConfirmField2.text != pinConfirmField1.text) {
                                    pinConfirmField2.focus = true
                                    errLineText.text = qsTr("Error: Newly entered PIN codes are different!")
                                    errLineText.visible = true
196
                                } else {
197 198
                                    errLineText.visible = false
                                    settings.updatePinSettings(pinConfirmField1.text.toString())
199
                                    settings.saveAllSettings(accountModel)
200 201
                                    pageView.pop(StackView.Immediate)
                                }
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
                            } 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
222 223 224 225 226 227
                            }
                        }
                    }
                }
            }
        }
228 229 230 231 232 233 234 235 236 237 238 239
    } // 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
240
            Column {
241 242
                anchors.right: parent.right
                anchors.left: parent.left
243
                spacing: formItemVerticalSpacing
244
                Text {
245
                    id: topLineText
246 247
                    anchors.horizontalCenter: parent.horizontalCenter
                    color: datovkaPalette.mid
248
                    width: parent.width
249 250 251 252 253
                    text: ""
                    horizontalAlignment: Text.AlignHCenter
                    wrapMode: Text.Wrap
                }
                Button {
254
                    id: setPinButton
255
                    height: inputItemHeight
256
                    font.pointSize: defaultTextFont.font.pointSize
257 258 259 260
                    anchors.horizontalCenter: parent.horizontalCenter
                    text: qsTr("Set PIN")
                    onClicked: {
                        actionName = "new"
261
                    }
262 263
                }
                Button {
264
                    id: changePinButton
265
                    height: inputItemHeight
266 267
                    font.pointSize: defaultTextFont.font.pointSize
                    anchors.horizontalCenter: parent.horizontalCenter
268 269 270
                    text: qsTr("Change PIN")
                    onClicked: {
                        actionName = "change"
271
                    }
272 273
                }
                Button {
274
                    id: disablePinButton
275
                    height: inputItemHeight
276 277
                    font.pointSize: defaultTextFont.font.pointSize
                    anchors.horizontalCenter: parent.horizontalCenter
278 279 280
                    text: qsTr("Disable PIN")
                    onClicked: {
                        actionName = "disable"
281
                    }
282 283
                }
                TextField {
284
                    id: pinValueField
285
                    anchors.horizontalCenter: parent.horizontalCenter
286
                    height: inputItemHeight
287
                    font.pointSize: defaultTextFont.font.pointSize
288 289 290 291
                    echoMode: TextInput.Password
                    passwordMaskDelay: 500 // milliseconds
                    inputMethodHints: Qt.ImhDigitsOnly
                    placeholderText: qsTr("Current PIN code")
292
                    horizontalAlignment: TextInput.AlignHCenter
293 294 295
                    text: ""
                }
                TextField {
296
                    id: pinConfirmField1
297
                    anchors.horizontalCenter: parent.horizontalCenter
298
                    height: inputItemHeight
299
                    font.pointSize: defaultTextFont.font.pointSize
300 301 302 303
                    echoMode: TextInput.Password
                    passwordMaskDelay: 500 // milliseconds
                    inputMethodHints: Qt.ImhDigitsOnly
                    placeholderText: qsTr("New PIN code")
304
                    horizontalAlignment: TextInput.AlignHCenter
305 306 307
                    text: ""
                }
                TextField {
308
                    id: pinConfirmField2
309
                    anchors.horizontalCenter: parent.horizontalCenter
310
                    height: inputItemHeight
311
                    font.pointSize: defaultTextFont.font.pointSize
312 313 314
                    echoMode: TextInput.Password
                    passwordMaskDelay: 500 // milliseconds
                    inputMethodHints: Qt.ImhDigitsOnly
315
                    placeholderText: qsTr("Confirm new PIN code")
316
                    horizontalAlignment: TextInput.AlignHCenter
317 318 319
                    text: ""
                }
                Text {
320
                    id: errLineText
321 322 323
                    anchors.horizontalCenter: parent.horizontalCenter
                    font.bold: true
                    color: datovkaPalette.text
324
                    width: parent.width
325
                    text: ""
326 327
                    horizontalAlignment: Text.AlignHCenter
                    wrapMode: Text.Wrap
328
                }
329
                Text {
330 331 332
                    id: lockIntervalLabel
                    color: datovkaPalette.text
                    anchors.horizontalCenter: parent.horizontalCenter
333
                    width: parent.width
334 335 336
                    text: qsTr("Lock after seconds of inactivity")
                    horizontalAlignment: Text.AlignHCenter
                }
337
                SpinBoxZeroMax {
338 339 340
                    /* Holds value in seconds. */
                    id: lockIntervalSpinBox
                    anchors.horizontalCenter: parent.horizontalCenter
341
                    height: inputItemHeight
342
                    font.pointSize: defaultTextFont.font.pointSize
343
                    /* Must be a non-decreasing list ending with infinity. */
344 345
                    items: [15, 30, 60, 90, 120, 150, 180, qsTr("don't lock")]
                    dfltIdx: 0
346
                    onValueModified: {
347 348 349 350
                        /* Enable accept button. Always visible? */
                        acceptElement.visible = val() != settings.inactivityInterval()
                    }
                }
351 352 353 354 355 356
            } // Column layout
        } // Pane
        ScrollIndicator.vertical: ScrollIndicator {}
    } // Flickable
} // Item