From 2a9c80fff56c2e5c8b8640f9f0d0dd14acfbba73 Mon Sep 17 00:00:00 2001 From: Johan Ouwerkerk Date: Wed, 25 Mar 2020 20:13:20 +0100 Subject: [PATCH] Add a custom validator that checks if the name is still available. --- src/model/CMakeLists.txt | 2 +- src/model/accounts.cpp | 38 ++++++++++++++++++++++++++++++++++++++ src/model/accounts.h | 22 +++++++++++++++++++++- 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/src/model/CMakeLists.txt b/src/model/CMakeLists.txt index a5f25c6..e9d1e40 100644 --- a/src/model/CMakeLists.txt +++ b/src/model/CMakeLists.txt @@ -8,4 +8,4 @@ set(model_SRCS ) add_library(model_lib STATIC ${model_SRCS}) -target_link_libraries(model_lib Qt5::Core Qt5::Qml Qt5::Gui account_lib) +target_link_libraries(model_lib Qt5::Core Qt5::Qml Qt5::Gui account_lib validator_lib) diff --git a/src/model/accounts.cpp b/src/model/accounts.cpp index 43651a3..0ea2dd1 100644 --- a/src/model/accounts.cpp +++ b/src/model/accounts.cpp @@ -178,4 +178,42 @@ namespace model // TODO: warn if not } + bool SimpleAccountListModel::isNameStillAvailable(const QString &account) const + { + return m_storage && m_storage->isNameStillAvailable(account); + } + + AccountNameValidator::AccountNameValidator(QObject *parent) : QValidator(parent), m_accounts(nullptr), m_delegate(nullptr) + { + } + + QValidator::State AccountNameValidator::validate(QString &input, int &pos) const + { + if (!m_accounts) { + // TODO warn about this + return QValidator::Invalid; + } + + QValidator::State result = m_delegate.validate(input, pos); + return result != QValidator::Acceptable || m_accounts->isNameStillAvailable(input) ? result : QValidator::Intermediate; + } + + void AccountNameValidator::fixup(QString &input) const + { + m_delegate.fixup(input); + } + + SimpleAccountListModel * AccountNameValidator::accounts(void) const + { + return m_accounts; + } + + void AccountNameValidator::setAccounts(SimpleAccountListModel *accounts) + { + if (accounts) { + m_accounts = accounts; + Q_EMIT accountsChanged(); + } + // TODO warn if not + } } diff --git a/src/model/accounts.h b/src/model/accounts.h index e3d9253..78109c2 100644 --- a/src/model/accounts.h +++ b/src/model/accounts.h @@ -6,6 +6,7 @@ #define MODEL_ACCOUNTS_H #include "../account/account.h" +#include "../validators/namevalidator.h" #include #include @@ -13,6 +14,7 @@ #include #include #include +#include #include namespace model @@ -60,11 +62,11 @@ namespace model explicit SimpleAccountListModel(accounts::AccountStorage *storage, QObject *parent = nullptr); Q_INVOKABLE void addTotp(const QString &account, const QString &secret, uint timeStep, int tokenLength); Q_INVOKABLE void addHotp(const QString &account, const QString &secret, quint64 counter, int tokenLength); + Q_INVOKABLE bool isNameStillAvailable(const QString &account) const; Q_INVOKABLE int rowCount(const QModelIndex &parent = QModelIndex()) const override; Q_INVOKABLE QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; QHash roleNames(void) const override; private Q_SLOTS: - void added(const QString &account); void removed(const QString &removed); private: @@ -73,8 +75,26 @@ namespace model QVector m_index; QHash m_accounts; }; + + class AccountNameValidator: public QValidator + { + Q_OBJECT + Q_PROPERTY(model::SimpleAccountListModel * accounts READ accounts WRITE setAccounts NOTIFY accountsChanged); + public: + explicit AccountNameValidator(QObject *parent = nullptr); + QValidator::State validate(QString &input, int &pos) const override; + void fixup(QString &input) const override; + SimpleAccountListModel * accounts(void) const; + void setAccounts(SimpleAccountListModel *accounts); + Q_SIGNALS: + void accountsChanged(void); + private: + SimpleAccountListModel * m_accounts; + validators::NameValidator m_delegate; + }; } Q_DECLARE_METATYPE(model::AccountView *); +Q_DECLARE_METATYPE(model::SimpleAccountListModel *); #endif