Add a custom validator that checks if the name is still available.
parent
28454721df
commit
2a9c80fff5
|
@ -8,4 +8,4 @@ set(model_SRCS
|
||||||
)
|
)
|
||||||
|
|
||||||
add_library(model_lib STATIC ${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)
|
||||||
|
|
|
@ -178,4 +178,42 @@ namespace model
|
||||||
// TODO: warn if not
|
// 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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
#define MODEL_ACCOUNTS_H
|
#define MODEL_ACCOUNTS_H
|
||||||
|
|
||||||
#include "../account/account.h"
|
#include "../account/account.h"
|
||||||
|
#include "../validators/namevalidator.h"
|
||||||
|
|
||||||
#include <QAbstractListModel>
|
#include <QAbstractListModel>
|
||||||
#include <QByteArray>
|
#include <QByteArray>
|
||||||
|
@ -13,6 +14,7 @@
|
||||||
#include <QModelIndex>
|
#include <QModelIndex>
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
#include <QValidator>
|
||||||
#include <QVector>
|
#include <QVector>
|
||||||
|
|
||||||
namespace model
|
namespace model
|
||||||
|
@ -60,11 +62,11 @@ namespace model
|
||||||
explicit SimpleAccountListModel(accounts::AccountStorage *storage, QObject *parent = nullptr);
|
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 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 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 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||||
Q_INVOKABLE QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
Q_INVOKABLE QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||||
QHash<int, QByteArray> roleNames(void) const override;
|
QHash<int, QByteArray> roleNames(void) const override;
|
||||||
private Q_SLOTS:
|
private Q_SLOTS:
|
||||||
|
|
||||||
void added(const QString &account);
|
void added(const QString &account);
|
||||||
void removed(const QString &removed);
|
void removed(const QString &removed);
|
||||||
private:
|
private:
|
||||||
|
@ -73,8 +75,26 @@ namespace model
|
||||||
QVector<QString> m_index;
|
QVector<QString> m_index;
|
||||||
QHash<QString, accounts::Account*> m_accounts;
|
QHash<QString, accounts::Account*> 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::AccountView *);
|
||||||
|
Q_DECLARE_METATYPE(model::SimpleAccountListModel *);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue