Commit a8bbd238 authored by kende's avatar kende

[nemo-systemsettings] Guest user support to UserModel. Contributes to JB#50232

parent b64eea44
......@@ -42,6 +42,8 @@
#include <sys/types.h>
#include <systemd/sd-login.h>
#include <sailfishusermanagerinterface.h>
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
......
......@@ -61,6 +61,7 @@ public:
enum UserType {
User = 0,
DeviceOwner = 1,
Guest = 2,
};
Q_ENUM(UserType)
......
......@@ -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<SailfishUserManagerEntry>();
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<int>() << TransitioningRole);
createInterface();
m_dBusInterface->call(QStringLiteral("enableGuestUser"), enabled);
}
void UserModel::userAddFinished(QDBusPendingCallWatcher *call)
{
QDBusPendingReply<uint> 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();
......
......@@ -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 @@ public:
enum UserType {
User = 0,
DeviceOwner = 1,
Guest = 2,
};
Q_ENUM(UserType)
......@@ -116,6 +118,10 @@ public:
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 @@ signals:
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:
QSet<uint> m_transitioning;
QDBusInterface *m_dBusInterface;
QDBusServiceWatcher *m_dBusWatcher;
bool m_guestEnabled;
};
#endif /* USERMODEL_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment