Detect newly added user and rebuild account menu automatically

Signed-off-by: Dominique Fuchs <32204802+DominiqueFuchs@users.noreply.github.com>
This commit is contained in:
Dominique Fuchs 2020-01-11 15:47:36 +01:00
parent 3da982f073
commit 32741162d9
2 changed files with 26 additions and 9 deletions

View file

@ -136,17 +136,23 @@ UserModel::UserModel(QObject *parent)
{
// TODO: Remember selected user from last quit via settings file
if (AccountManager::instance()->accounts().size() > 0) {
initUserList();
buildUserList();
}
connect(AccountManager::instance(), &AccountManager::accountAdded,
this, &UserModel::buildUserList);
}
void UserModel::initUserList()
void UserModel::buildUserList()
{
for (int i = 0; i < AccountManager::instance()->accounts().size(); i++) {
auto user = AccountManager::instance()->accounts().at(i);
addUser(user);
}
_users.first().setCurrentUser(true);
if (_init) {
_users.first().setCurrentUser(true);
_init = false;
}
}
Q_INVOKABLE int UserModel::numUsers()
@ -186,12 +192,22 @@ Q_INVOKABLE bool UserModel::currentServerHasTalk()
void UserModel::addUser(AccountStatePtr &user, const bool &isCurrent)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
_users << User(user, isCurrent);
if (isCurrent) {
_currentUserId = _users.indexOf(_users.last());
bool containsUser = false;
for (int i = 0; i < _users.size(); i++) {
if (_users[i] == user) {
containsUser = true;
continue;
}
}
if (!containsUser) {
beginInsertRows(QModelIndex(), rowCount(), rowCount());
_users << User(user, isCurrent);
if (isCurrent) {
_currentUserId = _users.indexOf(_users.last());
}
endInsertRows();
}
endInsertRows();
}
int UserModel::currentUserIndex()

View file

@ -95,8 +95,9 @@ private:
UserModel(QObject *parent = 0);
QList<User> _users;
int _currentUserId;
bool _init = true;
void initUserList();
void buildUserList();
};
class ImageProvider : public QQuickImageProvider