Skip to content

Commit

Permalink
Use uid in callbacks instead of row numbers.
Browse files Browse the repository at this point in the history
Rows can changed, uids stay and we have a nice mapping that can tell
which row to use.

Signed-off-by: Tomi Leppänen <tomi.leppanen@jolla.com>
  • Loading branch information
Tomin1 committed May 22, 2020
1 parent c6e14bf commit 526317e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
35 changes: 18 additions & 17 deletions src/usermodel.cpp
Expand Up @@ -196,7 +196,7 @@ bool UserModel::setData(const QModelIndex &index, const QVariant &value, int rol
auto call = m_dBusInterface->asyncCall(QStringLiteral("modifyUser"), (uint)user.uid(), name);
auto *watcher = new QDBusPendingCallWatcher(call, this);
connect(watcher, &QDBusPendingCallWatcher::finished,
this, std::bind(&UserModel::userModifyFinished, this, std::placeholders::_1, index.row()));
this, std::bind(&UserModel::userModifyFinished, this, std::placeholders::_1, user.uid()));
}
emit dataChanged(index, index, QVector<int>() << role);
return true;
Expand Down Expand Up @@ -256,7 +256,7 @@ void UserModel::removeUser(int row)
auto call = m_dBusInterface->asyncCall(QStringLiteral("removeUser"), (uint)user.uid());
auto *watcher = new QDBusPendingCallWatcher(call, this);
connect(watcher, &QDBusPendingCallWatcher::finished,
this, std::bind(&UserModel::userRemoveFinished, this, std::placeholders::_1, row));
this, std::bind(&UserModel::userRemoveFinished, this, std::placeholders::_1, user.uid()));
}

void UserModel::setCurrentUser(int row)
Expand All @@ -272,7 +272,7 @@ void UserModel::setCurrentUser(int row)
auto call = m_dBusInterface->asyncCall(QStringLiteral("setCurrentUser"), (uint)user.uid());
auto *watcher = new QDBusPendingCallWatcher(call, this);
connect(watcher, &QDBusPendingCallWatcher::finished,
this, std::bind(&UserModel::setCurrentUserFinished, this, std::placeholders::_1, row));
this, std::bind(&UserModel::setCurrentUserFinished, this, std::placeholders::_1, user.uid()));
}

void UserModel::reset(int row)
Expand Down Expand Up @@ -315,7 +315,7 @@ void UserModel::addGroups(int row, const QStringList &groups)
auto call = m_dBusInterface->asyncCall(QStringLiteral("addToGroups"), (uint)user.uid(), groups);
auto *watcher = new QDBusPendingCallWatcher(call, this);
connect(watcher, &QDBusPendingCallWatcher::finished,
this, std::bind(&UserModel::addToGroupsFinished, this, std::placeholders::_1, row));
this, std::bind(&UserModel::addToGroupsFinished, this, std::placeholders::_1, user.uid()));
}

void UserModel::removeGroups(int row, const QStringList &groups)
Expand All @@ -331,7 +331,7 @@ void UserModel::removeGroups(int row, const QStringList &groups)
auto call = m_dBusInterface->asyncCall(QStringLiteral("removeFromGroups"), (uint)user.uid(), groups);
auto *watcher = new QDBusPendingCallWatcher(call, this);
connect(watcher, &QDBusPendingCallWatcher::finished,
this, std::bind(&UserModel::removeFromGroupsFinished, this, std::placeholders::_1, row));
this, std::bind(&UserModel::removeFromGroupsFinished, this, std::placeholders::_1, user.uid()));
}

void UserModel::onUserAdded(const SailfishUserManagerEntry &entry)
Expand Down Expand Up @@ -400,8 +400,7 @@ void UserModel::onCurrentUserChanged(uint uid)
void UserModel::onCurrentUserChangeFailed(uint uid)
{
if (m_uidsToRows.contains(uid)) {
int row = m_uidsToRows.value(uid);
emit setCurrentUserFailed(row, Failure);
emit setCurrentUserFailed(m_uidsToRows.value(uid), Failure);
}
}

Expand Down Expand Up @@ -429,10 +428,11 @@ void UserModel::userAddFinished(QDBusPendingCallWatcher *call)
call->deleteLater();
}

void UserModel::userModifyFinished(QDBusPendingCallWatcher *call, int row)
void UserModel::userModifyFinished(QDBusPendingCallWatcher *call, uint uid)
{
QDBusPendingReply<void> reply = *call;
if (reply.isError()) {
int row = m_uidsToRows.value(uid);
auto error = reply.error();
emit userModifyFailed(row, getErrorType(error));
qCWarning(lcUsersLog) << "Modifying user with usermanager failed:" << error;
Expand All @@ -441,50 +441,51 @@ void UserModel::userModifyFinished(QDBusPendingCallWatcher *call, int row)
call->deleteLater();
}

void UserModel::userRemoveFinished(QDBusPendingCallWatcher *call, int row)
void UserModel::userRemoveFinished(QDBusPendingCallWatcher *call, uint uid)
{
QDBusPendingReply<void> reply = *call;
if (reply.isError()) {
int row = m_uidsToRows.value(uid);
auto error = reply.error();
emit userRemoveFailed(row, getErrorType(error));
qCWarning(lcUsersLog) << "Removing user with usermanager failed:" << error;
} // else awesome! (waiting for signal to alter data)
call->deleteLater();
}

void UserModel::setCurrentUserFinished(QDBusPendingCallWatcher *call, int row)
void UserModel::setCurrentUserFinished(QDBusPendingCallWatcher *call, uint uid)
{
QDBusPendingReply<void> reply = *call;
if (reply.isError()) {
auto error = reply.error();
emit setCurrentUserFailed(row, getErrorType(error));
emit setCurrentUserFailed(m_uidsToRows.value(uid), getErrorType(error));
qCWarning(lcUsersLog) << "Switching user with usermanager failed:" << error;
} // else user switching was initiated successfully
call->deleteLater();
}

void UserModel::addToGroupsFinished(QDBusPendingCallWatcher *call, int row)
void UserModel::addToGroupsFinished(QDBusPendingCallWatcher *call, uint uid)
{
QDBusPendingReply<void> reply = *call;
if (reply.isError()) {
auto error = reply.error();
emit addGroupsFailed(row, getErrorType(error));
emit addGroupsFailed(m_uidsToRows.value(uid), getErrorType(error));
qCWarning(lcUsersLog) << "Adding user to groups failed:" << error;
} else {
emit userGroupsChanged(row);
emit userGroupsChanged(m_uidsToRows.value(uid));
}
call->deleteLater();
}

void UserModel::removeFromGroupsFinished(QDBusPendingCallWatcher *call, int row)
void UserModel::removeFromGroupsFinished(QDBusPendingCallWatcher *call, uint uid)
{
QDBusPendingReply<void> reply = *call;
if (reply.isError()) {
auto error = reply.error();
emit removeGroupsFailed(row, getErrorType(error));
emit removeGroupsFailed(m_uidsToRows.value(uid), getErrorType(error));
qCWarning(lcUsersLog) << "Adding user to groups failed:" << error;
} else {
emit userGroupsChanged(row);
emit userGroupsChanged(m_uidsToRows.value(uid));
}
call->deleteLater();
}
Expand Down
10 changes: 5 additions & 5 deletions src/usermodel.h
Expand Up @@ -127,11 +127,11 @@ private slots:
void onCurrentUserChangeFailed(uint uid);

void userAddFinished(QDBusPendingCallWatcher *call);
void userModifyFinished(QDBusPendingCallWatcher *call, int row);
void userRemoveFinished(QDBusPendingCallWatcher *call, int row);
void setCurrentUserFinished(QDBusPendingCallWatcher *call, int row);
void addToGroupsFinished(QDBusPendingCallWatcher *call, int row);
void removeFromGroupsFinished(QDBusPendingCallWatcher *call, int row);
void userModifyFinished(QDBusPendingCallWatcher *call, uint uid);
void userRemoveFinished(QDBusPendingCallWatcher *call, uint uid);
void setCurrentUserFinished(QDBusPendingCallWatcher *call, uint uid);
void addToGroupsFinished(QDBusPendingCallWatcher *call, uint uid);
void removeFromGroupsFinished(QDBusPendingCallWatcher *call, uint uid);

void createInterface();
void destroyInterface();
Expand Down

0 comments on commit 526317e

Please sign in to comment.