From 32741162d93ee1c4c3fec6d7b84884d97726916a Mon Sep 17 00:00:00 2001 From: Dominique Fuchs <32204802+DominiqueFuchs@users.noreply.github.com> Date: Sat, 11 Jan 2020 15:47:36 +0100 Subject: [PATCH] Detect newly added user and rebuild account menu automatically Signed-off-by: Dominique Fuchs <32204802+DominiqueFuchs@users.noreply.github.com> --- src/gui/tray/UserModel.cpp | 32 ++++++++++++++++++++++++-------- src/gui/tray/UserModel.h | 3 ++- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/gui/tray/UserModel.cpp b/src/gui/tray/UserModel.cpp index 86ff62f99..1a72a3eab 100644 --- a/src/gui/tray/UserModel.cpp +++ b/src/gui/tray/UserModel.cpp @@ -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() diff --git a/src/gui/tray/UserModel.h b/src/gui/tray/UserModel.h index 5a2e9cadf..ee7198641 100644 --- a/src/gui/tray/UserModel.h +++ b/src/gui/tray/UserModel.h @@ -95,8 +95,9 @@ private: UserModel(QObject *parent = 0); QList _users; int _currentUserId; + bool _init = true; - void initUserList(); + void buildUserList(); }; class ImageProvider : public QQuickImageProvider