Warning, /utilities/keysmith/src/contents/ui/AccountNameForm.qml is written in an unsupported language. File is not indexed.

0001 /*
0002  * SPDX-License-Identifier: GPL-3.0-or-later
0003  * SPDX-FileCopyrightText: 2020 Johan Ouwerkerk <jm.ouwerkerk@gmail.com>
0004  */
0005 
0006 import Keysmith.Application
0007 import Keysmith.Models as Models
0008 import Keysmith.Validators as Validators
0009 
0010 import QtQuick
0011 import QtQuick.Layouts
0012 import QtQuick.Controls as QQC2
0013 import org.kde.kirigami as Kirigami
0014 import org.kde.kirigamiaddons.formcard 1 as FormCard
0015 
0016 FormCard.FormCard {
0017     id: root
0018 
0019     property bool validateAccountAvailability
0020     property Models.AccountListModel accounts
0021     property Models.ValidatedAccountInput validatedInput
0022 
0023     onValidatedInputChanged: {
0024         revalidate();
0025     }
0026 
0027     onValidateAccountAvailabilityChanged: {
0028         revalidate();
0029     }
0030 
0031     property bool acceptable : accountName.acceptableInput && issuerName.acceptableInput
0032 
0033     /*
0034      * When accounts are added/removed from the model, the account name and issuer should be revalidated as well.
0035      * It may have become eligible or in-eligible depending on whether or not other accounts with the same name
0036      * for the same new issuer value now (still) exist.
0037      *
0038      * Unfortunately there seems to be nothing to explicitly trigger revalidation on the text field.
0039      * Work around is to force revalidation to happen by "editing" the value in the text field(s) directly.
0040      */
0041     data: Connections {
0042         target: accounts
0043         function onRowsInserted() {
0044             revalidate();
0045         }
0046         function onRowsRemoved() {
0047             revalidate();
0048         }
0049         function onModelReset() {
0050             revalidate();
0051         }
0052     }
0053     function revalidate() {
0054         // because of how issuer revalidation works, this also implicitly covers the account name as well
0055         issuerName.insert(issuerName.text.length, "");
0056     }
0057     function forceActiveFocus() {
0058         accountName.forceActiveFocus()
0059     }
0060 
0061     FormCard.FormTextFieldDelegate {
0062         id: accountName
0063         text: validatedInput.name
0064         label: i18nc("@label:textbox", "Account name:")
0065         validator: Validators.AccountNameValidator {
0066             id: accountNameValidator
0067             accounts: root.accounts
0068             issuer: validatedInput.issuer
0069             validateAvailability: validateAccountAvailability
0070         }
0071         onTextChanged: {
0072             if (acceptableInput) {
0073                 validatedInput.name = text;
0074             }
0075         }
0076     }
0077 
0078     FormCard.FormDelegateSeparator {}
0079 
0080     FormCard.FormTextFieldDelegate {
0081         id: issuerName
0082         text: validatedInput.issuer
0083         label: i18nc("@label:textbox", "Account issuer:")
0084         validator: Validators.AccountIssuerValidator {}
0085         /*
0086          * When the issuer changes, the account name should be revalidated as well.
0087          * It may have become eligible or in-eligible depending on whether or not other accounts with the same name
0088          * for the same new issuer value already exist.
0089          *
0090          * Unfortunately because the property binding only affects the validator, there seems to be nothing to
0091          * explicitly trigger revalidation on the text field. Work around is to force revalidation to happen by
0092          * "editing" the value in the text field directly.
0093          */
0094         onTextChanged: {
0095             /*
0096              * This signal handler may run before property bindings have been fully (re-)evaluated.
0097              * First update the account name validator to the correct new issuer value before triggering revalidation.
0098              */
0099             accountNameValidator.issuer = issuerName.text;
0100             accountName.insert(accountName.text.length, "");
0101             if (acceptableInput) {
0102                 validatedInput.issuer = text;
0103             }
0104         }
0105     }
0106 }