PageSettingsAccount.qml 16.2 KB
Newer Older
1
/*
2
 * Copyright (C) 2014-2019 CZ.NIC
3 4 5 6 7 8 9 10
 *
 * 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
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 13 14
 * 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
 *
 * 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.
 */

24
import QtQuick 2.7
25 26
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.1
27
import QtQuick.Dialogs 1.2
28
import cz.nic.mobileDatovka 1.0
29
import cz.nic.mobileDatovka.iOsHelper 1.0
30

31 32
Item {
    id: pageSettingsAccount
33

34 35 36 37 38 39
    /* These properties must be set by caller. */
    property var pageView
    property var statusBar
    property string acntName
    property string userName
    property var accountModel: null
40

41 42
    property bool isNewAccount: (userName == "")
    property string sLoginMethod: "pwd"
43
    property string oldUserName
44

45 46
    property bool iOS: false

47
    Component.onCompleted: {
48
            iOS = iOSHelper.isIos()
49 50 51 52
            if (!isNewAccount) {
                accounts.getAccountData(userName)
            }
    }
53 54 55

    FileDialogue {
        id: fileDialogue
56
        multiSelect: false
57
        onFinished: {
58 59 60 61 62 63 64 65 66 67
            var listLength = pathListModel.count
            // set last selected certificate path
            if (listLength > 0) {
                if (pathListModel.get(listLength-1).path === "") {
                    if (certPathLabelId.text === "") {
                        certPathLabelId.visible = false
                    }
                } else {
                    certPathLabelId.visible = true
                    certPathLabelId.text = pathListModel.get(listLength-1).path
68
                }
69 70 71 72
            }
        }
    }

73 74
    PageHeader {
        id: headerBar
75
        title: qsTr("New account")
76 77
        onBackClicked: {
            pageView.pop(StackView.Immediate)
78
        }
79 80 81 82 83
        Row {
            anchors.verticalCenter: parent.verticalCenter
            spacing: defaultMargin
            anchors.right: parent.right
            anchors.rightMargin: defaultMargin
84
            AccessibleImageButton {
85 86
                id: actionButton
                anchors.verticalCenter: parent.verticalCenter
Martin Straka's avatar
Martin Straka committed
87
                sourceSize.height: imgHeightHeader
88
                source: "qrc:/ui/checkbox-marked-circle.svg"
89 90 91 92 93 94 95 96 97 98 99 100 101 102
                accessibleName: qsTr("Accept changes")
                onClicked: {
                    if (isNewAccount) {
                        // Create a new account context data and add to model.
                        if (accounts.createAccount(accountModel, sLoginMethod,
                            accountNameTextField.text.toString(),
                            userNameTextField.text.toString(),
                            passwordTextField.text.toString(),
                            testAccount.checked, rememberPassword.checked,
                            useLS.checked, certPathLabelId.text.toString())) {
                                // Login to new account.
                                isds.doIsdsAction("addNewAccount", userNameTextField.text.toString())
                         }
                    } else {
103 104
                        // Change account user name.
                        if (userNameTextField.text.toString() !== oldUserName) {
Martin Straka's avatar
Martin Straka committed
105
                            // Create temporary account with a new user name.
106
                            if (accounts.prepareChangeUserName(accountModel, sLoginMethod,
107 108 109 110 111
                                  accountNameTextField.text.toString(),
                                  userNameTextField.text.toString(),
                                  passwordTextField.text.toString(),
                                  testAccount.checked, rememberPassword.checked,
                                  useLS.checked, certPathLabelId.text.toString(), oldUserName)) {
Martin Straka's avatar
Martin Straka committed
112
                                    // Login to isds with the new user name.
113 114 115 116 117
                                    isds.doIsdsAction("changeUserName", userNameTextField.text.toString())
                                }
                        } else {
                            // Update account context data.
                            if (accounts.updateAccount(accountModel, sLoginMethod,
118 119 120 121 122
                                  accountNameTextField.text.toString(),
                                  userNameTextField.text.toString(),
                                  passwordTextField.text.toString(),
                                  testAccount.checked, rememberPassword.checked,
                                  useLS.checked, certPathLabelId.text.toString())) {
123
                                    settings.saveAllSettings(accountModel)
124
                            }
125
                        }
126
                        pageView.pop(StackView.Immediate)
127 128
                    }
                }
129
                Connections {
130
                    // Connection is activated when account info was downloaded.
131 132
                    target: isds
                    onDownloadAccountInfoFinishedSig: {
133 134 135 136 137
                        settings.saveAllSettings(accountModel)
                        pageView.pop(StackView.Immediate)
                    }
                    onUnsuccessedLoginToIsdsSig: {
                        if (accounts.removeAccount(accountModel, userName, false)) {
138 139 140
                            settings.saveAllSettings(accountModel)
                        }
                    }
141 142 143 144
                    onRunGetAccountInfoSig: {
                         settings.saveAllSettings(accountModel)
                         isds.getAccountInfo(userName)
                    }
145
                    onRunChangeUserNameSig: {
Martin Straka's avatar
Martin Straka committed
146 147
                        // Login to isds with new user name has been succeeded.
                        // Get databox ID of new user name.
148
                        var dbId = isds.getAccountDbId(userName)
Martin Straka's avatar
Martin Straka committed
149
                        // Change username and rename databases.
150 151 152 153 154 155 156 157
                        if (accounts.changeAccountUserName(accountModel,
                            accountNameTextField.text.toString(),
                            userNameTextField.text.toString(),
                            useLS.checked, oldUserName, dbId)) {
                                settings.saveAllSettings(accountModel)
                                isds.getAccountInfo(userName)
                        }
                    }
158
                }
159 160
            }
        }
161 162 163 164 165 166 167 168 169 170 171 172
    } // 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
173
            Column {
174 175
                anchors.right: parent.right
                anchors.left: parent.left
176
                spacing: formItemVerticalSpacing
177
                AccessibleText {
178 179 180 181
                    id: loginMethodLabel
                    color: datovkaPalette.text
                    text: qsTr("Login method")
                }
182
                AccessibleComboBox {
183
                    id: loginMethodComboBox
184
                    width: parent.width
185
                    accessibleDescription: qsTr("Select login method")
186 187
                    model: ListModel {
                        id: loginMethodModel
188 189 190 191 192
                        /* Key values must be equivalent to constants defined in C++ code! */
                        ListElement { label: qsTr("Username + Password"); key: "pwd" } // USERNAME_PWD_ONLY
                        ListElement { label: qsTr("Certificate + Password"); key: "cert" } // USERNAME_PWD_CERT
                        ListElement { label: qsTr("Password + Security code"); key: "hotp" } // USERNAME_PWD_HOTP
                        ListElement { label: qsTr("Password + Security SMS"); key: "totp" } // USERNAME_PWD_TOTP
193
                    }
194
                    onCurrentIndexChanged: {
195
                        sLoginMethod = currentKey()
196 197 198
                        if (sLoginMethod === "cert") {
                             certificateLabel.visible = true
                             certPathButtonId.visible = true
199
                             certPathLabelId.visible = (certPathLabelId.text !== "")
200 201 202 203
                        } else {
                             certificateLabel.visible = false
                             certPathLabelId.visible = false
                             certPathButtonId.visible = false
204
                        }
205
                    }
206
                }
207
                AccessibleText {
208 209 210 211 212
                    id: certificateLabel
                    visible: false
                    color: datovkaPalette.text
                    text: qsTr("Certificate")
                }
213
                AccessibleText {
214 215 216 217 218 219
                    id: certPathLabelId
                    visible: false
                    color: datovkaPalette.text
                    width: parent.width
                    wrapMode: Text.WrapAnywhere
                    text: ""
220 221 222 223 224 225 226 227 228 229
                    Connections {
                       target: iOSHelper
                        onCertFilesSelectedSig: {
                            if (certFilePaths.length > 0) {
                                certPathLabelId.visible = true
                                certPathLabelId.text = certFilePaths[0]
                                fileDialogue.close()
                            }
                        }
                    }
230
                }
231
                AccessibleButton {
232 233 234 235 236 237
                    id: certPathButtonId
                    visible: false
                    text: qsTr("Choose file")
                    height: inputItemHeight
                    font.pointSize: defaultTextFont.font.pointSize
                    onClicked: {
238 239 240 241 242 243
                        if (iOS) {
                            iOSHelper.openDocumentPickerControllerForImport(IosImportAction.IMPORT_CERT, [])
                            fileDialogue.raise("Select certificate file", ["*.pem","*.p12","*.pfx"], true, iOSHelper.getCertFileLocation())
                        } else {
                            fileDialogue.raise("Select certificate file", ["*.pem","*.p12","*.pfx"], true, "")
                        }
244
                    }
245
                }
246
                AccessibleText {
247
                    color: datovkaPalette.text
Karel Slaný's avatar
Karel Slaný committed
248
                    text: qsTr("Account title")
249
                }
250
                AccessibleTextField {
251
                    id: accountNameTextField
252
                    width: parent.width
253 254
                    height: inputItemHeight
                    font.pointSize: defaultTextFont.font.pointSize
Karel Slaný's avatar
Karel Slaný committed
255
                    placeholderText: qsTr("Enter custom account name")
256 257 258 259
                    InputLineMenu {
                        id: accountNameMenu
                        inputTextControl: accountNameTextField
                        isPassword: false
260
                    }
261 262 263 264
                    onPressAndHold: {
                        if (settings.useExplicitClipboardOperations()) {
                            accountNameMenu.implicitWidth = computeMenuWidth(accountNameMenu)
                            accountNameMenu.open()
265
                        }
266
                    }
267
                }
268
                AccessibleText {
269 270 271
                    color: datovkaPalette.text
                    text: qsTr("Username")
                }
272
                AccessibleTextField {
273
                    id: userNameTextField
274
                    width: parent.width
275 276 277
                    height: inputItemHeight
                    font.pointSize: defaultTextFont.font.pointSize
                    inputMethodHints: Qt.ImhLowercaseOnly
278
                    placeholderText: qsTr("Enter the login name")
279 280 281 282 283 284 285 286 287
                    InputLineMenu {
                        id: userNameMenu
                        inputTextControl: userNameTextField
                        isPassword: false
                    }
                    onPressAndHold: {
                        if (settings.useExplicitClipboardOperations()) {
                            userNameMenu.implicitWidth = computeMenuWidth(userNameMenu)
                            userNameMenu.open()
288 289
                        }
                    }
290
                }
291
                AccessibleText {
292 293 294
                    color: datovkaPalette.text
                    text: qsTr("Password")
                }
295
                AccessibleTextField {
296
                    id: passwordTextField
297
                    width: parent.width
298 299 300 301
                    height: inputItemHeight
                    font.pointSize: defaultTextFont.font.pointSize
                    echoMode: TextInput.Password
                    passwordMaskDelay: 500 // milliseconds
302
                    placeholderText: qsTr("Enter the password")
303 304 305 306 307 308 309 310 311
                    InputLineMenu {
                        id: passwordMenu
                        inputTextControl: passwordTextField
                        isPassword: true
                    }
                    onPressAndHold: {
                        if (settings.useExplicitClipboardOperations()) {
                            passwordMenu.implicitWidth = computeMenuWidth(passwordMenu)
                            passwordMenu.open()
312 313 314
                        }
                    }
                }
315
                AccessibleSwitch {
316 317 318 319 320
                    id: rememberPassword
                    text: qsTr("Remember password")
                    font.pointSize: defaultTextFont.font.pointSize
                    checked: true
                }
321
                AccessibleSwitch {
322 323 324 325
                    id: testAccount
                    text: qsTr("Test account")
                    font.pointSize: defaultTextFont.font.pointSize
                }
326
                AccessibleText {
327 328
                    id: testAccountLabel
                    color: datovkaPalette.mid
329
                    width: parent.width
330 331 332
                    text: qsTr("Test accounts are used to access the ISDS testing environment.")
                    wrapMode: Text.Wrap
                }
333
                AccessibleSwitch {
334 335 336 337 338
                    id: useLS
                    font.pointSize: defaultTextFont.font.pointSize
                    text: qsTr("Use local storage (database)")
                    checked: true
                }
339
                AccessibleText {
340
                    color: datovkaPalette.mid
341
                    width: parent.width
342 343 344 345 346 347
                    text: (useLS.checked ?
                           qsTr("Messages and attachments will be locally stored. No active internet connection is needed to access locally stored data.") :
                           qsTr("Messages and attachments will be stored only temporarily in memory. These data will be lost on application exit."))
                    wrapMode: Text.Wrap
                }
                Connections {
348
                    // Connection is activated when settings of exists account is shown.
349 350 351 352 353 354 355 356 357
                    target: accounts
                    onSendAccountData: {
                        accountNameTextField.text = acntName
                        userNameTextField.text = userName
                        passwordTextField.text = password
                        rememberPassword.checked = rememberPwd
                        testAccount.checked = isTestAccount
                        useLS.checked = storeToDisk
                        certPathLabelId.text = certPath
358
                        loginMethodComboBox.selectCurrentKey(loginMethod)
359
                        oldUserName = userName
360
                        testAccount.enabled = false
361
                        headerBar.title = qsTr("Account settings")
362 363 364 365 366 367 368
                    }
                } // Connection
            } // Column layout
        } // Pane
        ScrollIndicator.vertical: ScrollIndicator {}
    } // Flickable
} // Item