Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix a mistake UserModel user removal handling.
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 <tomi.leppanen@jolla.com>
  • Loading branch information
Tomin1 committed Mar 31, 2020
1 parent 437de5d commit e797c4d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
19 changes: 13 additions & 6 deletions src/usermodel.cpp
Expand Up @@ -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)
Expand Down Expand Up @@ -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();
}

Expand All @@ -329,26 +334,26 @@ 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<int>() << 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<int>() << CurrentRole);
}
}

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<uint> reply = *call;
if (reply.isError()) {
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/usermodel.h
Expand Up @@ -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);
Expand Down

0 comments on commit e797c4d

Please sign in to comment.