refactor: support validating account names without checking account availability
With this change AccountNameValidator can now be used without having a functional Accounts model. This allows the existing AddAccount page to be re-used for context without a valid Accounts model, e.g. when receiving accounts via URI from the commandline. Issues: #7, #14master
parent
9288f041c8
commit
30e66ef978
|
@ -14,6 +14,7 @@ import org.kde.kirigami 2.8 as Kirigami
|
|||
|
||||
Kirigami.FormLayout {
|
||||
id: root
|
||||
property bool validateAccountAvailability: true
|
||||
property Models.AccountListModel accounts: Keysmith.accountListModel()
|
||||
property Models.ValidatedAccountInput validatedInput
|
||||
|
||||
|
@ -21,6 +22,10 @@ Kirigami.FormLayout {
|
|||
revalidate();
|
||||
}
|
||||
|
||||
onValidateAccountAvailabilityChanged: {
|
||||
revalidate();
|
||||
}
|
||||
|
||||
property bool acceptable : accountName.acceptableInput && issuerName.acceptableInput
|
||||
|
||||
/*
|
||||
|
@ -56,6 +61,7 @@ Kirigami.FormLayout {
|
|||
id: accountNameValidator
|
||||
accounts: root.accounts
|
||||
issuer: validatedInput.issuer
|
||||
validateAvailability: validateAccountAvailability
|
||||
}
|
||||
onTextChanged: {
|
||||
if (acceptableInput) {
|
||||
|
|
|
@ -18,6 +18,7 @@ Kirigami.Page {
|
|||
signal dismissed
|
||||
property Models.AccountListModel accounts: Keysmith.accountListModel()
|
||||
property bool detailsEnabled: false
|
||||
property bool validateAccountAvailability: true
|
||||
|
||||
property bool secretAcceptable: accountSecret.acceptableInput
|
||||
property bool tokenTypeAcceptable: hotpRadio.checked || totpRadio.checked
|
||||
|
@ -41,6 +42,7 @@ Kirigami.Page {
|
|||
}
|
||||
AccountNameForm {
|
||||
id: accountName
|
||||
validateAccountAvailability: root.validateAccountAvailability
|
||||
validatedInput: root.validatedInput
|
||||
twinFormLayouts: [requiredDetails, hotpDetails, totpDetails]
|
||||
}
|
||||
|
|
|
@ -270,10 +270,23 @@ namespace model
|
|||
}
|
||||
|
||||
AccountNameValidator::AccountNameValidator(QObject *parent) :
|
||||
QValidator(parent), m_issuer(std::nullopt), m_accounts(nullptr), m_delegate(nullptr)
|
||||
QValidator(parent), m_validateAvailability(true), m_issuer(std::nullopt), m_accounts(nullptr), m_delegate(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
bool AccountNameValidator::validateAvailability(void) const
|
||||
{
|
||||
return m_validateAvailability;
|
||||
}
|
||||
|
||||
void AccountNameValidator::setValidateAvailability(bool enabled)
|
||||
{
|
||||
if (enabled != m_validateAvailability) {
|
||||
m_validateAvailability = enabled;
|
||||
Q_EMIT validateAvailabilityChanged();
|
||||
}
|
||||
}
|
||||
|
||||
QString AccountNameValidator::issuer(void) const
|
||||
{
|
||||
return m_issuer
|
||||
|
@ -294,6 +307,12 @@ namespace model
|
|||
|
||||
QValidator::State AccountNameValidator::validate(QString &input, int &pos) const
|
||||
{
|
||||
QValidator::State result = m_delegate.validate(input, pos);
|
||||
if (!m_validateAvailability) {
|
||||
qCDebug(logger) << "Not validating account availability: explicitly disabled";
|
||||
return result;
|
||||
}
|
||||
|
||||
if (!m_accounts) {
|
||||
qCDebug(logger) << "Unable to validat account name: missing accounts model object";
|
||||
return QValidator::Invalid;
|
||||
|
@ -304,9 +323,9 @@ namespace model
|
|||
return QValidator::Invalid;
|
||||
}
|
||||
|
||||
QValidator::State result = m_delegate.validate(input, pos);
|
||||
return result != QValidator::Acceptable
|
||||
|| m_accounts->isAccountStillAvailable(input, *m_issuer) ? result : QValidator::Intermediate;
|
||||
return result != QValidator::Acceptable || m_accounts->isAccountStillAvailable(input, *m_issuer)
|
||||
? result
|
||||
: QValidator::Intermediate;
|
||||
}
|
||||
|
||||
void AccountNameValidator::fixup(QString &input) const
|
||||
|
|
|
@ -116,10 +116,18 @@ namespace model
|
|||
Q_OBJECT
|
||||
Q_PROPERTY(QString issuer READ issuer WRITE setIssuer NOTIFY issuerChanged)
|
||||
Q_PROPERTY(model::SimpleAccountListModel * accounts READ accounts WRITE setAccounts NOTIFY accountsChanged);
|
||||
Q_PROPERTY(
|
||||
bool validateAvailability
|
||||
READ validateAvailability
|
||||
WRITE setValidateAvailability
|
||||
NOTIFY validateAvailabilityChanged
|
||||
);
|
||||
public:
|
||||
explicit AccountNameValidator(QObject *parent = nullptr);
|
||||
QValidator::State validate(QString &input, int &pos) const override;
|
||||
void fixup(QString &input) const override;
|
||||
bool validateAvailability(void) const;
|
||||
void setValidateAvailability(bool enabled);
|
||||
QString issuer(void) const;
|
||||
void setIssuer(const QString &issuer);
|
||||
SimpleAccountListModel * accounts(void) const;
|
||||
|
@ -127,7 +135,9 @@ namespace model
|
|||
Q_SIGNALS:
|
||||
void issuerChanged(void);
|
||||
void accountsChanged(void);
|
||||
void validateAvailabilityChanged(void);
|
||||
private:
|
||||
bool m_validateAvailability;
|
||||
std::optional<QString> m_issuer;
|
||||
SimpleAccountListModel * m_accounts;
|
||||
const validators::NameValidator m_delegate;
|
||||
|
|
Loading…
Reference in New Issue