diff options
author | alex-z <blackslayer4@gmail.com> | 2022-10-24 16:36:53 +0300 |
---|---|---|
committer | allexzander <allexzander@users.noreply.github.com> | 2022-11-03 12:14:44 +0300 |
commit | b00007e07d621363aa030df49f135b3943da7a6b (patch) | |
tree | ffd2c31c4f2d615444ed95db0085046a163b91cd | |
parent | ca15f308e6d4e7e2e5aa069e8f76f97b83fef88d (diff) |
Fix incorrect current user index when removing and adding an account.
Signed-off-by: alex-z <blackslayer4@gmail.com>
-rw-r--r-- | src/gui/tray/usermodel.cpp | 55 | ||||
-rw-r--r-- | src/gui/tray/usermodel.h | 2 |
2 files changed, 41 insertions, 16 deletions
diff --git a/src/gui/tray/usermodel.cpp b/src/gui/tray/usermodel.cpp index 995e2907b..c7d71ec3b 100644 --- a/src/gui/tray/usermodel.cpp +++ b/src/gui/tray/usermodel.cpp @@ -966,8 +966,8 @@ void UserModel::addUser(AccountStatePtr &user, const bool &isCurrent) }); _users << u; - if (isCurrent) { - _currentUserId = _users.indexOf(_users.last()); + if (isCurrent || _currentUserId < 0) { + setCurrentUserId(_users.size() - 1); } endInsertRows(); @@ -1018,12 +1018,31 @@ void UserModel::openCurrentAccountServer() void UserModel::setCurrentUserId(const int id) { - if (_currentUserId == id || _currentUserId < 0 || _currentUserId >= _users.size()) + if (_currentUserId == id) { + // order has changed, index remained the same + if (id >= 0 && id < _users.size() && !_users[id]->isCurrentUser()) { + for (auto &user : _users) { + user->setCurrentUser(false); + } + _users[id]->setCurrentUser(true); + emit currentUserChanged(); + } return; - - _users[_currentUserId]->setCurrentUser(false); - _users[id]->setCurrentUser(true); + } _currentUserId = id; + + if (_users.isEmpty()) { + emit currentUserChanged(); + return; + } + + if (id >= 0 && id < _users.size()) { + for (auto &user : _users) { + user->setCurrentUser(false); + } + _users[id]->setCurrentUser(true); + } + emit currentUserChanged(); } @@ -1045,17 +1064,16 @@ void UserModel::logout(const int id) void UserModel::removeAccount(const int id) { - if (id < 0 || id >= _users.size()) + if (id < 0 || id >= _users.size()) { return; + } QMessageBox messageBox(QMessageBox::Question, tr("Confirm Account Removal"), tr("<p>Do you really want to remove the connection to the account <i>%1</i>?</p>" "<p><b>Note:</b> This will <b>not</b> delete any files.</p>") - .arg(_users[id]->name()), - QMessageBox::NoButton); - QPushButton *yesButton = - messageBox.addButton(tr("Remove connection"), QMessageBox::YesRole); + .arg(_users[id]->name()), QMessageBox::NoButton); + QPushButton *yesButton = messageBox.addButton(tr("Remove connection"), QMessageBox::YesRole); messageBox.addButton(tr("Cancel"), QMessageBox::NoRole); messageBox.exec(); @@ -1063,16 +1081,23 @@ void UserModel::removeAccount(const int id) return; } - if (_users[id]->isCurrentUser() && _users.count() > 1) { - id == 0 ? setCurrentUserId(1) : setCurrentUserId(0); - } - _users[id]->logout(); _users[id]->removeAccount(); beginRemoveRows(QModelIndex(), id, id); _users.removeAt(id); endRemoveRows(); + + if (_users.isEmpty()) { + setCurrentUserId(-1); + } else if (_users.size() == 1) { + setCurrentUserId(0); + } else { + if (currentUserId() != id && currentUserId() < id) { + return; + } + setCurrentUserId(id < _users.size() ? id : id - 1); + } } std::shared_ptr<OCC::UserStatusConnector> UserModel::userStatusConnector(int id) diff --git a/src/gui/tray/usermodel.h b/src/gui/tray/usermodel.h index 62ad1eb2f..3ebc8d623 100644 --- a/src/gui/tray/usermodel.h +++ b/src/gui/tray/usermodel.h @@ -213,7 +213,7 @@ private: static UserModel *_instance; UserModel(QObject *parent = nullptr); QList<User*> _users; - int _currentUserId = 0; + int _currentUserId = -1; bool _init = true; void buildUserList(); |