Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[nemo-systemsettings] Guest user support to UserModel. Contributes to…
… JB#50232
  • Loading branch information
kende committed Jul 21, 2020
1 parent b64eea4 commit a8bbd23
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
15 changes: 14 additions & 1 deletion src/userinfo.cpp
Expand Up @@ -42,6 +42,8 @@
#include <sys/types.h>
#include <systemd/sd-login.h>

#include <sailfishusermanagerinterface.h>

namespace {

const auto UserDatabaseFile = QStringLiteral("/etc/passwd");
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/userinfo.h
Expand Up @@ -61,6 +61,7 @@ class SYSTEMSETTINGS_EXPORT UserInfo: public QObject
enum UserType {
User = 0,
DeviceOwner = 1,
Guest = 2,
};
Q_ENUM(UserType)

Expand Down
42 changes: 40 additions & 2 deletions src/usermodel.cpp
Expand Up @@ -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,
Expand All @@ -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;
}
}
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)));
}
}

Expand Down Expand Up @@ -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();
Expand Down
9 changes: 9 additions & 0 deletions src/usermodel.h
Expand Up @@ -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 {
Expand All @@ -68,6 +69,7 @@ class SYSTEMSETTINGS_EXPORT UserModel: public QAbstractListModel
enum UserType {
User = 0,
DeviceOwner = 1,
Guest = 2,
};
Q_ENUM(UserType)

Expand Down Expand Up @@ -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();
Expand All @@ -126,13 +132,15 @@ 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);
void onUserModified(uint uid, const QString &newName);
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);
Expand All @@ -152,5 +160,6 @@ private slots:
QSet<uint> m_transitioning;
QDBusInterface *m_dBusInterface;
QDBusServiceWatcher *m_dBusWatcher;
bool m_guestEnabled;
};
#endif /* USERMODEL_H */

0 comments on commit a8bbd23

Please sign in to comment.