From e797c4db85a45221e3b0a410b7836dacf031659d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomi=20Lepp=C3=A4nen?= Date: Tue, 31 Mar 2020 13:58:58 +0300 Subject: [PATCH] Fix a mistake UserModel user removal handling. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There uid mapping to rows was not correcty updated when removing users. Also improve QHash usage overall and always append to the end of userAddFinished. Signed-off-by: Tomi Leppänen --- src/usermodel.cpp | 19 +++++++++++++------ src/usermodel.h | 2 +- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/usermodel.cpp b/src/usermodel.cpp index 0db1a41..2c1f09b 100644 --- a/src/usermodel.cpp +++ b/src/usermodel.cpp @@ -232,7 +232,7 @@ void UserModel::createUser() auto call = m_dBusInterface->asyncCall(QStringLiteral("addUser"), user.name()); auto *watcher = new QDBusPendingCallWatcher(call, this); connect(watcher, &QDBusPendingCallWatcher::finished, - this, std::bind(&UserModel::userAddFinished, this, std::placeholders::_1, m_users.count()-1)); + this, &UserModel::userAddFinished); } void UserModel::removeUser(int row) @@ -320,7 +320,12 @@ void UserModel::onUserRemoved(uint uid) int row = m_uidsToRows.value(uid); beginRemoveRows(QModelIndex(), row, row); m_users.remove(row); + // It is slightly costly to remove users since some row numbers may need to be updated m_uidsToRows.remove(uid); + for (auto iter = m_uidsToRows.begin(); iter != m_uidsToRows.end(); ++iter) { + if (iter.value() > row) + iter.value() -= 1; + } endRemoveRows(); } @@ -329,13 +334,13 @@ void UserModel::onCurrentUserChanged(uint uid) UserInfo *previous = getCurrentUser(); if (previous) { if (previous->updateCurrent()) { - auto idx = index(m_uidsToRows[previous->uid()], 0); + auto idx = index(m_uidsToRows.value(previous->uid()), 0); emit dataChanged(idx, idx, QVector() << CurrentRole); } delete previous; } - if (m_uidsToRows.contains(uid) && m_users[m_uidsToRows[uid]].updateCurrent()) { - auto idx = index(m_uidsToRows[uid], 0); + if (m_uidsToRows.contains(uid) && m_users[m_uidsToRows.value(uid)].updateCurrent()) { + auto idx = index(m_uidsToRows.value(uid), 0); emit dataChanged(idx, idx, QVector() << CurrentRole); } } @@ -343,12 +348,12 @@ void UserModel::onCurrentUserChanged(uint uid) void UserModel::onCurrentUserChangeFailed(uint uid) { if (m_uidsToRows.contains(uid)) { - int row = m_uidsToRows[uid]; + int row = m_uidsToRows.value(uid); emit setCurrentUserFailed(row, Failure); } } -void UserModel::userAddFinished(QDBusPendingCallWatcher *call, int row) +void UserModel::userAddFinished(QDBusPendingCallWatcher *call) { QDBusPendingReply reply = *call; if (reply.isError()) { @@ -359,6 +364,8 @@ void UserModel::userAddFinished(QDBusPendingCallWatcher *call, int row) uint uid = reply.value(); // Check that this was not just added to the list by onUserAdded if (!m_uidsToRows.contains(uid)) { + // Add to the end + int row = m_users.count()-1; beginInsertRows(QModelIndex(), row, row); m_users.insert(row, UserInfo(uid)); m_uidsToRows.insert(uid, row); diff --git a/src/usermodel.h b/src/usermodel.h index 55c1845..8e45fd0 100644 --- a/src/usermodel.h +++ b/src/usermodel.h @@ -114,7 +114,7 @@ private slots: void onCurrentUserChanged(uint uid); void onCurrentUserChangeFailed(uint uid); - void userAddFinished(QDBusPendingCallWatcher *call, int row); + void userAddFinished(QDBusPendingCallWatcher *call); void userModifyFinished(QDBusPendingCallWatcher *call, int row); void userRemoveFinished(QDBusPendingCallWatcher *call, int row); void setCurrentUserFinished(QDBusPendingCallWatcher *call, int row);