From b00007e07d621363aa030df49f135b3943da7a6b Mon Sep 17 00:00:00 2001 From: alex-z Date: Mon, 24 Oct 2022 16:36:53 +0300 Subject: [PATCH 1/2] Fix incorrect current user index when removing and adding an account. Signed-off-by: alex-z --- src/gui/tray/usermodel.cpp | 55 +++++++++++++++++++++++++++----------- 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 995e2907bc358..c7d71ec3bd3df 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("

Do you really want to remove the connection to the account %1?

" "

Note: This will not delete any files.

") - .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 UserModel::userStatusConnector(int id) diff --git a/src/gui/tray/usermodel.h b/src/gui/tray/usermodel.h index 62ad1eb2f0607..3ebc8d6237b00 100644 --- a/src/gui/tray/usermodel.h +++ b/src/gui/tray/usermodel.h @@ -213,7 +213,7 @@ public slots: static UserModel *_instance; UserModel(QObject *parent = nullptr); QList _users; - int _currentUserId = 0; + int _currentUserId = -1; bool _init = true; void buildUserList(); From 6cb988b66788e11cef439269959a06bf1c1278f0 Mon Sep 17 00:00:00 2001 From: allexzander Date: Tue, 25 Oct 2022 16:49:56 +0300 Subject: [PATCH 2/2] Fix accounts avatar issue when it becomes completely white due to non-changing account id member with changing account index in the list. Signed-off-by: allexzander --- src/gui/tray/usermodel.cpp | 67 ++++++++++++++++++++------------------ src/gui/tray/usermodel.h | 2 +- 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/src/gui/tray/usermodel.cpp b/src/gui/tray/usermodel.cpp index c7d71ec3bd3df..3cee8809d5080 100644 --- a/src/gui/tray/usermodel.cpp +++ b/src/gui/tray/usermodel.cpp @@ -915,12 +915,17 @@ bool UserModel::isUserConnected(const int id) return _users[id]->isConnected(); } -QImage UserModel::avatarById(const int id) +QImage UserModel::avatarById(const int id) const { - if (id < 0 || id >= _users.size()) + const auto foundUserByIdIter = std::find_if(std::cbegin(_users), std::cend(_users), [&id](const OCC::User* const user) { + return user->account()->id() == QString::number(id); + }); + + if (foundUserByIdIter == std::cend(_users)) { return {}; + } - return _users[id]->avatar(); + return (*foundUserByIdIter)->avatar(); } QString UserModel::currentUserServer() @@ -1018,32 +1023,31 @@ void UserModel::openCurrentAccountServer() void UserModel::setCurrentUserId(const int id) { - 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); + Q_ASSERT(id < _users.size()); + + if (id < 0 || id >= _users.size()) { + if (id < 0 && _currentUserId != id) { + _currentUserId = id; emit currentUserChanged(); } return; } - _currentUserId = id; - if (_users.isEmpty()) { - emit currentUserChanged(); - return; - } - - if (id >= 0 && id < _users.size()) { - for (auto &user : _users) { + const auto isCurrentUserChanged = !_users[id]->isCurrentUser(); + if (isCurrentUserChanged) { + for (const auto user : _users) { user->setCurrentUser(false); } _users[id]->setCurrentUser(true); } - emit currentUserChanged(); + if (_currentUserId == id && isCurrentUserChanged) { + // order has changed, index remained the same + emit currentUserChanged(); + } else if (_currentUserId != id) { + _currentUserId = id; + emit currentUserChanged(); + } } void UserModel::login(const int id) @@ -1069,11 +1073,12 @@ void UserModel::removeAccount(const int id) } QMessageBox messageBox(QMessageBox::Question, - tr("Confirm Account Removal"), - tr("

Do you really want to remove the connection to the account %1?

" - "

Note: This will not delete any files.

") - .arg(_users[id]->name()), QMessageBox::NoButton); - QPushButton *yesButton = messageBox.addButton(tr("Remove connection"), QMessageBox::YesRole); + tr("Confirm Account Removal"), + tr("

Do you really want to remove the connection to the account %1?

" + "

Note: This will not delete any files.

") + .arg(_users[id]->name()), + QMessageBox::NoButton); + const auto * const yesButton = messageBox.addButton(tr("Remove connection"), QMessageBox::YesRole); messageBox.addButton(tr("Cancel"), QMessageBox::NoRole); messageBox.exec(); @@ -1088,14 +1093,12 @@ void UserModel::removeAccount(const int id) _users.removeAt(id); endRemoveRows(); - if (_users.isEmpty()) { - setCurrentUserId(-1); - } else if (_users.size() == 1) { - setCurrentUserId(0); - } else { - if (currentUserId() != id && currentUserId() < id) { - return; - } + if (_users.size() <= 1) { + setCurrentUserId(_users.size() - 1); + } else if (currentUserId() > id) { + // an account was removed from the in-between 0 and the current one, the index of the current one needs a decrement + setCurrentUserId(currentUserId() - 1); + } else if (currentUserId() == id) { setCurrentUserId(id < _users.size() ? id : id - 1); } } diff --git a/src/gui/tray/usermodel.h b/src/gui/tray/usermodel.h index 3ebc8d6237b00..08e23f0b361bf 100644 --- a/src/gui/tray/usermodel.h +++ b/src/gui/tray/usermodel.h @@ -160,7 +160,7 @@ class UserModel : public QAbstractListModel [[nodiscard]] int rowCount(const QModelIndex &parent = QModelIndex()) const override; [[nodiscard]] QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; - QImage avatarById(const int id); + [[nodiscard]] QImage avatarById(const int id) const; [[nodiscard]] User *currentUser() const;