Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit f83b77ee authored by Johan Ouwerkerk's avatar Johan Ouwerkerk

Add loaded/error properties to account list model

This change provides a property API on top of the low level state tracking
performed in AccountStorage.
parent b70feaf6
......@@ -82,10 +82,12 @@ namespace model
return model::millisecondsLeftForToken(m_model->epoch(), m_model->timeStep());
}
SimpleAccountListModel::SimpleAccountListModel(accounts::AccountStorage *storage, QObject *parent) : QAbstractListModel(parent), m_storage(storage), m_index(QVector<QString>())
SimpleAccountListModel::SimpleAccountListModel(accounts::AccountStorage *storage, QObject *parent) : QAbstractListModel(parent), m_storage(storage), m_has_error(false), m_index(QVector<QString>())
{
QObject::connect(storage, &accounts::AccountStorage::added, this, &SimpleAccountListModel::added);
QObject::connect(storage, &accounts::AccountStorage::removed, this, &SimpleAccountListModel::removed);
QObject::connect(storage, &accounts::AccountStorage::error, this, &SimpleAccountListModel::handleError);
QObject::connect(storage, &accounts::AccountStorage::loaded, this, &SimpleAccountListModel::loadedChanged);
beginResetModel();
for (const QString &name : m_storage->accounts()) {
......@@ -98,9 +100,37 @@ namespace model
qCDebug(logger) << "Account storage reported an account (name) but did not yield a valid account object";
}
}
m_has_error = storage->hasError();
endResetModel();
}
bool SimpleAccountListModel::error(void) const
{
return m_has_error;
}
void SimpleAccountListModel::setError(bool markAsError)
{
if (!markAsError && m_storage->hasError()) {
m_storage->clearError();
}
if (markAsError != m_has_error) {
m_has_error = markAsError;
Q_EMIT errorChanged();
}
}
void SimpleAccountListModel::handleError(void)
{
setError(true);
}
bool SimpleAccountListModel::loaded(void) const
{
return m_storage->isLoaded();
}
void SimpleAccountListModel::addTotp(const QString &account, const QString &secret, uint timeStep, int tokenLength)
{
m_storage->addTotp(account, secret, timeStep, tokenLength);
......
......@@ -53,6 +53,8 @@ namespace model
class SimpleAccountListModel: public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(bool loaded READ loaded NOTIFY loadedChanged);
Q_PROPERTY(bool error READ error WRITE setError NOTIFY errorChanged);
public:
enum NonStandardRoles {
AccountRole = Qt::ItemDataRole::UserRole
......@@ -66,12 +68,21 @@ namespace model
Q_INVOKABLE int rowCount(const QModelIndex &parent = QModelIndex()) const override;
Q_INVOKABLE QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QHash<int, QByteArray> roleNames(void) const override;
public:
bool loaded(void) const;
bool error(void) const;
void setError(bool markAsError);
Q_SIGNALS:
void loadedChanged(void);
void errorChanged(void);
private Q_SLOTS:
void added(const QString &account);
void removed(const QString &removed);
void handleError(void);
private:
accounts::AccountStorage * const m_storage;
private:
bool m_has_error;
QVector<QString> m_index;
QHash<QString, accounts::Account*> m_accounts;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment