Modernise AccountModel C++ code a bit

master
Johan Ouwerkerk 2019-09-25 14:19:07 +02:00
parent 8b03fe5123
commit 29e9348097
2 changed files with 29 additions and 27 deletions

View File

@ -28,11 +28,12 @@ AccountModel::AccountModel(QObject *parent) :
QAbstractListModel(parent) QAbstractListModel(parent)
{ {
QSettings settings("org.kde.otpclient", "otpclient"); QSettings settings("org.kde.otpclient", "otpclient");
const QStringList entries = settings.childGroups();
// qDebug() << "loading settings file:" << settings.fileName(); // qDebug() << "loading settings file:" << settings.fileName();
foreach(const QString & group, settings.childGroups()) { for(const QString &group : entries) {
// qDebug() << "found group" << group << QUuid(group).toString(); // qDebug() << "found group" << group << QUuid(group).toString();
QUuid id = QUuid(group); QUuid id(group);
settings.beginGroup(group); settings.beginGroup(group);
Account *account = new Account(id, this); Account *account = new Account(id, this);
@ -43,15 +44,8 @@ AccountModel::AccountModel(QObject *parent) :
account->setTimeStep(settings.value("timeStep").toInt()); account->setTimeStep(settings.value("timeStep").toInt());
account->setPinLength(settings.value("pinLength").toInt()); account->setPinLength(settings.value("pinLength").toInt());
connect(account, SIGNAL(nameChanged()), SLOT(accountChanged()));
connect(account, SIGNAL(typeChanged()), SLOT(accountChanged()));
connect(account, SIGNAL(secretChanged()), SLOT(accountChanged()));
connect(account, SIGNAL(counterChanged()), SLOT(accountChanged()));
connect(account, SIGNAL(timeStepChanged()), SLOT(accountChanged()));
connect(account, SIGNAL(pinLengthChanged()), SLOT(accountChanged()));
connect(account, SIGNAL(otpChanged()), SLOT(accountChanged()));
m_accounts.append(account); m_accounts.append(account);
wireAccount(account);
settings.endGroup(); settings.endGroup();
} }
} }
@ -62,6 +56,17 @@ int AccountModel::rowCount(const QModelIndex &parent) const
return m_accounts.count(); return m_accounts.count();
} }
void AccountModel::wireAccount(const Account *account)
{
const auto h = &AccountModel::accountChanged;
QObject::connect(account, &Account::nameChanged, this, h);
QObject::connect(account, &Account::typeChanged, this, h);
QObject::connect(account, &Account::secretChanged, this, h);
QObject::connect(account, &Account::counterChanged, this, h);
QObject::connect(account, &Account::pinLengthChanged, this, h);
QObject::connect(account, &Account::otpChanged, this, h);
}
QVariant AccountModel::data(const QModelIndex &index, int role) const QVariant AccountModel::data(const QModelIndex &index, int role) const
{ {
switch (role) { switch (role) {
@ -89,7 +94,7 @@ Account *AccountModel::get(int index) const
if (index > -1 && m_accounts.count() > index) { if (index > -1 && m_accounts.count() > index) {
return m_accounts.at(index); return m_accounts.at(index);
} }
return 0; return nullptr;
} }
Account *AccountModel::createAccount() Account *AccountModel::createAccount()
@ -97,13 +102,8 @@ Account *AccountModel::createAccount()
Account *account = new Account(QUuid::createUuid(), this); Account *account = new Account(QUuid::createUuid(), this);
beginInsertRows(QModelIndex(), m_accounts.count(), m_accounts.count()); beginInsertRows(QModelIndex(), m_accounts.count(), m_accounts.count());
m_accounts.append(account); m_accounts.append(account);
connect(account, SIGNAL(nameChanged()), SLOT(accountChanged()));
connect(account, SIGNAL(typeChanged()), SLOT(accountChanged()));
connect(account, SIGNAL(secretChanged()), SLOT(accountChanged()));
connect(account, SIGNAL(counterChanged()), SLOT(accountChanged()));
connect(account, SIGNAL(pinLengthChanged()), SLOT(accountChanged()));
connect(account, SIGNAL(otpChanged()), SLOT(accountChanged()));
wireAccount(account);
storeAccount(account); storeAccount(account);
endInsertRows(); endInsertRows();
@ -151,13 +151,13 @@ QHash<int, QByteArray> AccountModel::roleNames() const
void AccountModel::generateNext(int account) void AccountModel::generateNext(int account)
{ {
m_accounts.at(account)->next(); m_accounts.at(account)->next();
emit dataChanged(index(account), index(account), QVector<int>() << RoleCounter << RoleOtp); Q_EMIT dataChanged(index(account), index(account), QVector<int>() << RoleCounter << RoleOtp);
} }
void AccountModel::refresh() void AccountModel::refresh()
{ {
emit beginResetModel(); Q_EMIT beginResetModel();
emit endResetModel(); Q_EMIT endResetModel();
} }
void AccountModel::accountChanged() void AccountModel::accountChanged()
@ -167,10 +167,10 @@ void AccountModel::accountChanged()
// qDebug() << "account changed"; // qDebug() << "account changed";
int accountIndex = m_accounts.indexOf(account); int accountIndex = m_accounts.indexOf(account);
emit dataChanged(index(accountIndex), index(accountIndex)); Q_EMIT dataChanged(index(accountIndex), index(accountIndex));
} }
void AccountModel::storeAccount(Account *account) void AccountModel::storeAccount(const Account *account)
{ {
QSettings settings("org.kde.otpclient", "otpclient"); QSettings settings("org.kde.otpclient", "otpclient");
settings.beginGroup(account->id().toString()); settings.beginGroup(account->id().toString());

View File

@ -37,7 +37,7 @@ public:
RoleOtp RoleOtp
}; };
explicit AccountModel(QObject *parent = 0); explicit AccountModel(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent) const override; int rowCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex &index, int role) const override; QVariant data(const QModelIndex &index, int role) const override;
@ -48,14 +48,16 @@ public:
Q_INVOKABLE void deleteAccount(int index); Q_INVOKABLE void deleteAccount(int index);
Q_INVOKABLE void deleteAccount(Account *account); Q_INVOKABLE void deleteAccount(Account *account);
public Q_SLOTS:
public slots:
void generateNext(int account); void generateNext(int account);
void refresh(); void refresh();
private slots: private Q_SLOTS:
void accountChanged(); void accountChanged();
void storeAccount(Account *account); void storeAccount(const Account *account);
private:
void wireAccount(const Account *account);
private: private:
QList<Account*> m_accounts; QList<Account*> m_accounts;