Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[nemo-systemsettings] Add setCurrentUser to UserModel. Contributes to…
… JB#47825

Allows to logout and login to another user session using usermanager.

Signed-off-by: Tomi Leppänen <tomi.leppanen@jolla.com>
  • Loading branch information
Tomin1 committed Mar 31, 2020
1 parent eab99e0 commit b138822
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 11 deletions.
2 changes: 1 addition & 1 deletion rpm/nemo-qml-plugin-systemsettings.spec
Expand Up @@ -12,7 +12,7 @@ Requires: connman
Requires: mce >= 1.83.0
Requires: libsailfishkeyprovider >= 0.0.14
Requires: connman-qt5 >= 1.2.21
Requires: user-managerd
Requires: user-managerd >= 0.3.0
Requires(post): coreutils
BuildRequires: pkgconfig(Qt5Qml)
BuildRequires: pkgconfig(Qt5SystemInfo)
Expand Down
68 changes: 61 additions & 7 deletions src/usermodel.cpp
Expand Up @@ -232,6 +232,22 @@ void UserModel::removeUser(int row)
this, std::bind(&UserModel::userRemoveFinished, this, std::placeholders::_1, row));
}

void UserModel::setCurrentUser(int row)
{
if (row < 0 || row >= m_users.count())
return;

auto user = m_users.at(row);
if (!user.isValid())
return;

createInterface();
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));
}

void UserModel::reset(int row)
{
if (row < 0 || row >= m_users.count())
Expand All @@ -247,7 +263,7 @@ UserInfo * UserModel::getCurrentUser() const
return new UserInfo();
}

void UserModel::userAdded(const SailfishUserManagerEntry &entry)
void UserModel::onUserAdded(const SailfishUserManagerEntry &entry)
{
if (m_uidsToRows.contains(entry.uid))
return;
Expand All @@ -263,7 +279,7 @@ void UserModel::userAdded(const SailfishUserManagerEntry &entry)
}
}

void UserModel::userModified(uint uid, const QString &newName)
void UserModel::onUserModified(uint uid, const QString &newName)
{
if (!m_uidsToRows.contains(uid))
return;
Expand All @@ -277,7 +293,7 @@ void UserModel::userModified(uint uid, const QString &newName)
}
}

void UserModel::userRemoved(uint uid)
void UserModel::onUserRemoved(uint uid)
{
if (!m_uidsToRows.contains(uid))
return;
Expand All @@ -289,6 +305,30 @@ void UserModel::userRemoved(uint uid)
endRemoveRows();
}

void UserModel::onCurrentUserChanged(uint uid)
{
UserInfo *previous = getCurrentUser();
if (previous) {
if (previous->updateCurrent()) {
auto idx = index(m_uidsToRows[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);
emit dataChanged(idx, idx, QVector<int>() << CurrentRole);
}
}

void UserModel::onCurrentUserChangeFailed(uint uid)
{
if (m_uidsToRows.contains(uid)) {
int row = m_uidsToRows[uid];
emit setCurrentUserFailed(row, m_users[row].name());
}
}

void UserModel::userAddFinished(QDBusPendingCallWatcher *call, int row)
{
QDBusPendingReply<uint> reply = *call;
Expand All @@ -297,7 +337,7 @@ void UserModel::userAddFinished(QDBusPendingCallWatcher *call, int row)
qWarning() << "Adding user with usermanager failed:" << reply.error();
} else {
uint uid = reply.value();
// Check that this was not just added to the list by userAdded
// Check that this was not just added to the list by onUserAdded
if (!m_uidsToRows.contains(uid)) {
beginInsertRows(QModelIndex(), row, row);
m_users.insert(row, UserInfo(uid));
Expand Down Expand Up @@ -331,18 +371,32 @@ void UserModel::userRemoveFinished(QDBusPendingCallWatcher *call, int row)
call->deleteLater();
}

void UserModel::setCurrentUserFinished(QDBusPendingCallWatcher *call, int row)
{
QDBusPendingReply<void> reply = *call;
if (reply.isError()) {
emit setCurrentUserFailed(row, m_users.at(row).name());
qWarning() << "Switching user with usermanager failed:" << reply.error();
} // else user switching was initiated successfully
call->deleteLater();
}

void UserModel::createInterface()
{
if (!m_dBusInterface) {
qCDebug(lcUsersLog) << "Creating interface to user-managerd";
m_dBusInterface = new QDBusInterface(UserManagerService, UserManagerPath, UserManagerInterface,
QDBusConnection::systemBus(), this);
connect(m_dBusInterface, SIGNAL(userAdded(const SailfishUserManagerEntry &)),
this, SLOT(userAdded(const SailfishUserManagerEntry &)), Qt::QueuedConnection);
this, SLOT(onUserAdded(const SailfishUserManagerEntry &)), Qt::QueuedConnection);
connect(m_dBusInterface, SIGNAL(userModified(uint, const QString &)),
this, SLOT(userModified(uint, const QString &)));
this, SLOT(onUserModified(uint, const QString &)));
connect(m_dBusInterface, SIGNAL(userRemoved(uint)),
this, SLOT(userRemoved(uint)));
this, SLOT(onUserRemoved(uint)));
connect(m_dBusInterface, SIGNAL(currentUserChanged(uint)),
this, SLOT(onCurrentUserChanged(uint)));
connect(m_dBusInterface, SIGNAL(currentUserChangeFailed(uint)),
this, SLOT(onCurrentUserChangeFailed(uint)));
}
}

Expand Down
13 changes: 10 additions & 3 deletions src/usermodel.h
Expand Up @@ -81,21 +81,28 @@ class SYSTEMSETTINGS_EXPORT UserModel: public QAbstractListModel
Q_INVOKABLE void createUser();
Q_INVOKABLE void removeUser(int row);
Q_INVOKABLE void reset(int row);
Q_INVOKABLE void setCurrentUser(int row);
Q_INVOKABLE UserInfo * getCurrentUser() const;

signals:
void placeholderChanged();
void userAddFailed();
void userModifyFailed(int row, const QString &name);
void userRemoveFailed(int row, const QString &name);
void setCurrentUserFailed(int row, const QString &name);

private slots:
void userAdded(const SailfishUserManagerEntry &entry);
void userModified(uint uid, const QString &newName);
void userRemoved(uint uid);
void onUserAdded(const SailfishUserManagerEntry &entry);
void onUserModified(uint uid, const QString &newName);
void onUserRemoved(uint uid);
void onCurrentUserChanged(uint uid);
void onCurrentUserChangeFailed(uint uid);

void userAddFinished(QDBusPendingCallWatcher *call, int row);
void userModifyFinished(QDBusPendingCallWatcher *call, int row);
void userRemoveFinished(QDBusPendingCallWatcher *call, int row);
void setCurrentUserFinished(QDBusPendingCallWatcher *call, int row);

void createInterface();
void destroyInterface();

Expand Down

0 comments on commit b138822

Please sign in to comment.