From a8bbd238f8ccf1681e3b42fa186f32d3a93788cd Mon Sep 17 00:00:00 2001 From: kende Date: Tue, 21 Jul 2020 06:52:17 +0000 Subject: [PATCH] [nemo-systemsettings] Guest user support to UserModel. Contributes to JB#50232 --- src/userinfo.cpp | 15 ++++++++++++++- src/userinfo.h | 1 + src/usermodel.cpp | 42 ++++++++++++++++++++++++++++++++++++++++-- src/usermodel.h | 9 +++++++++ 4 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/userinfo.cpp b/src/userinfo.cpp index 2b5e9c8..5d23522 100644 --- a/src/userinfo.cpp +++ b/src/userinfo.cpp @@ -42,6 +42,8 @@ #include #include +#include + namespace { const auto UserDatabaseFile = QStringLiteral("/etc/passwd"); @@ -292,6 +294,10 @@ QString UserInfo::displayName() const //: Default value for device owner's name when it is not set //% "Device owner" return qtTrId("systemsettings-li-device_owner"); + } else if (d->m_uid == SAILFISH_USERMANAGER_GUEST_UID) { + //: Default value for device owner's name when it is not set + //% "Guest user" + return qtTrId("systemsettings-li-guest_user"); } return d->m_username; } @@ -337,7 +343,14 @@ UserInfo::UserType UserInfo::type() const // Device lock considers user with id 100000 as device owner. // Some other places consider the user belonging to sailfish-system // as device owner. We have to pick one here. - return (d->m_uid == DeviceOwnerId) ? DeviceOwner : User; + switch (d->m_uid) { + case DeviceOwnerId: + return DeviceOwner; + case SAILFISH_USERMANAGER_GUEST_UID: + return Guest; + default: + return User; + } } int UserInfo::uid() const diff --git a/src/userinfo.h b/src/userinfo.h index b0d019f..c4ea79f 100644 --- a/src/userinfo.h +++ b/src/userinfo.h @@ -61,6 +61,7 @@ class SYSTEMSETTINGS_EXPORT UserInfo: public QObject enum UserType { User = 0, DeviceOwner = 1, + Guest = 2, }; Q_ENUM(UserType) diff --git a/src/usermodel.cpp b/src/usermodel.cpp index 943d292..49b188e 100644 --- a/src/usermodel.cpp +++ b/src/usermodel.cpp @@ -80,6 +80,7 @@ UserModel::UserModel(QObject *parent) , m_dBusInterface(nullptr) , m_dBusWatcher(new QDBusServiceWatcher(UserManagerService, QDBusConnection::systemBus(), QDBusServiceWatcher::WatchForRegistration | QDBusServiceWatcher::WatchForUnregistration, this)) + , m_guestEnabled(false) { qDBusRegisterMetaType(); connect(m_dBusWatcher, &QDBusServiceWatcher::serviceRegistered, @@ -97,6 +98,9 @@ UserModel::UserModel(QObject *parent) if (user.isValid()) { // Skip invalid users here m_users.append(user); m_uidsToRows.insert(user.uid(), m_users.count()-1); + + if (!m_guestEnabled && user.uid() == SAILFISH_USERMANAGER_GUEST_UID) + m_guestEnabled = true; } } } @@ -432,6 +436,38 @@ void UserModel::onCurrentUserChangeFailed(uint uid) } } +void UserModel::onGuestUserEnabled(bool enabled) +{ + if (enabled != m_guestEnabled) { + m_guestEnabled = enabled; + emit guestEnabledChanged(); + } +} + +bool UserModel::guestEnabled() const +{ + return m_guestEnabled; +} + +void UserModel::setGuestEnabled(bool enabled) +{ + if (enabled == m_guestEnabled) + return; + + m_transitioning.insert(SAILFISH_USERMANAGER_GUEST_UID); + + int row; + if (enabled) { + row = placeholder() ? m_users.count() - 1 : m_users.count(); + } else { + row = m_uidsToRows.value(SAILFISH_USERMANAGER_GUEST_UID); + } + QModelIndex idx = index(row, 0); + emit dataChanged(idx, idx, QVector() << TransitioningRole); + createInterface(); + m_dBusInterface->call(QStringLiteral("enableGuestUser"), enabled); +} + void UserModel::userAddFinished(QDBusPendingCallWatcher *call) { QDBusPendingReply reply = *call; @@ -531,6 +567,8 @@ void UserModel::createInterface() this, SLOT(onCurrentUserChanged(uint))); connect(m_dBusInterface, SIGNAL(currentUserChangeFailed(uint)), this, SLOT(onCurrentUserChangeFailed(uint))); + connect(m_dBusInterface, SIGNAL(guestUserEnabled(bool)), + this, SLOT(onGuestUserEnabled(bool))); } } @@ -559,11 +597,11 @@ void UserModel::add(UserInfo &user) m_transitioning.remove(m_users[row+1].uid()); endInsertRows(); } else { - // Find the last position that's not placeholder and insert there - int row = placeholder() ? m_users.count()-1 : m_users.count(); + int row = placeholder() ? m_users.count() - 1 : m_users.count(); beginInsertRows(QModelIndex(), row, row); m_users.insert(row, user); m_uidsToRows.insert(user.uid(), row); + m_transitioning.remove(user.uid()); endInsertRows(); } emit countChanged(); diff --git a/src/usermodel.h b/src/usermodel.h index cf776fa..f3f60bc 100644 --- a/src/usermodel.h +++ b/src/usermodel.h @@ -52,6 +52,7 @@ class SYSTEMSETTINGS_EXPORT UserModel: public QAbstractListModel Q_PROPERTY(bool placeholder READ placeholder WRITE setPlaceholder NOTIFY placeholderChanged) Q_PROPERTY(int count READ count NOTIFY countChanged) Q_PROPERTY(int maximumCount READ maximumCount CONSTANT) + Q_PROPERTY(bool guestEnabled READ guestEnabled WRITE setGuestEnabled NOTIFY guestEnabledChanged) public: enum Roles { @@ -68,6 +69,7 @@ class SYSTEMSETTINGS_EXPORT UserModel: public QAbstractListModel enum UserType { User = 0, DeviceOwner = 1, + Guest = 2, }; Q_ENUM(UserType) @@ -116,6 +118,10 @@ class SYSTEMSETTINGS_EXPORT UserModel: public QAbstractListModel Q_INVOKABLE void addGroups(int row, const QStringList &groups); Q_INVOKABLE void removeGroups(int row, const QStringList &groups); + // Guest methods + bool guestEnabled() const; + Q_INVOKABLE void setGuestEnabled(bool enabled); + signals: void placeholderChanged(); void countChanged(); @@ -126,6 +132,7 @@ class SYSTEMSETTINGS_EXPORT UserModel: public QAbstractListModel void setCurrentUserFailed(int row, int error); void addGroupsFailed(int row, int error); void removeGroupsFailed(int row, int error); + void guestEnabledChanged(); private slots: void onUserAdded(const SailfishUserManagerEntry &entry); @@ -133,6 +140,7 @@ private slots: void onUserRemoved(uint uid); void onCurrentUserChanged(uint uid); void onCurrentUserChangeFailed(uint uid); + void onGuestUserEnabled(bool enabled); void userAddFinished(QDBusPendingCallWatcher *call); void userModifyFinished(QDBusPendingCallWatcher *call, uint uid); @@ -152,5 +160,6 @@ private slots: QSet m_transitioning; QDBusInterface *m_dBusInterface; QDBusServiceWatcher *m_dBusWatcher; + bool m_guestEnabled; }; #endif /* USERMODEL_H */