Skip to content

Commit

Permalink
Merge branch 'jb50772' into 'master'
Browse files Browse the repository at this point in the history
[nemo-systemsettings] Make setGuestEnabled asynchronous. Fixes JB#50772

See merge request mer-core/nemo-qml-plugin-systemsettings!151
  • Loading branch information
Tomin1 committed Aug 12, 2020
2 parents 7bb6d97 + 5613b32 commit fd255f9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/userinfo.cpp
Expand Up @@ -295,7 +295,7 @@ QString UserInfo::displayName() const
//% "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
//: Default value for guest user's name when it is not set
//% "Guest user"
return qtTrId("systemsettings-li-guest_user");
}
Expand Down
48 changes: 33 additions & 15 deletions src/usermodel.cpp
Expand Up @@ -42,6 +42,7 @@
#include <QString>
#include <functional>
#include <grp.h>
#include <pwd.h>
#include <sailfishaccesscontrol.h>
#include <sailfishusermanagerinterface.h>
#include <sys/types.h>
Expand Down Expand Up @@ -80,8 +81,10 @@ UserModel::UserModel(QObject *parent)
, m_dBusInterface(nullptr)
, m_dBusWatcher(new QDBusServiceWatcher(UserManagerService, QDBusConnection::systemBus(),
QDBusServiceWatcher::WatchForRegistration | QDBusServiceWatcher::WatchForUnregistration, this))
, m_guestEnabled(false)
, m_guestEnabled(getpwuid((uid_t)SAILFISH_USERMANAGER_GUEST_UID))
{
connect(this, &UserModel::guestEnabledChanged,
this, &UserModel::maximumCountChanged);
qDBusRegisterMetaType<SailfishUserManagerEntry>();
connect(m_dBusWatcher, &QDBusServiceWatcher::serviceRegistered,
this, &UserModel::createInterface);
Expand All @@ -98,9 +101,6 @@ 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 @@ -156,7 +156,7 @@ int UserModel::count() const
*/
int UserModel::maximumCount() const
{
return SAILFISH_USERMANAGER_MAX_USERS;
return m_guestEnabled ? SAILFISH_USERMANAGER_MAX_USERS+1 : SAILFISH_USERMANAGER_MAX_USERS;
}

QHash<int, QByteArray> UserModel::roleNames() const
Expand Down Expand Up @@ -214,6 +214,10 @@ bool UserModel::setData(const QModelIndex &index, const QVariant &value, int rol
return false;

UserInfo &user = m_users[index.row()];

if (user.type() == UserInfo::Guest)
return false;

switch (role) {
case NameRole: {
QString name = value.toString();
Expand Down Expand Up @@ -454,18 +458,16 @@ 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);
if (m_guestEnabled) {
m_transitioning.insert(SAILFISH_USERMANAGER_GUEST_UID);
auto idx = index(m_uidsToRows.value(SAILFISH_USERMANAGER_GUEST_UID), 0);
emit dataChanged(idx, idx, QVector<int>() << TransitioningRole);
}
QModelIndex idx = index(row, 0);
emit dataChanged(idx, idx, QVector<int>() << TransitioningRole);
createInterface();
m_dBusInterface->call(QStringLiteral("enableGuestUser"), enabled);
auto call = m_dBusInterface->asyncCall(QStringLiteral("enableGuestUser"), enabled);
auto *watcher = new QDBusPendingCallWatcher(call, this);
connect(watcher, &QDBusPendingCallWatcher::finished,
this, std::bind(&UserModel::enableGuestUserFinished, this, std::placeholders::_1, enabled));
}

void UserModel::userAddFinished(QDBusPendingCallWatcher *call)
Expand Down Expand Up @@ -551,6 +553,22 @@ void UserModel::removeFromGroupsFinished(QDBusPendingCallWatcher *call, uint uid
call->deleteLater();
}

void UserModel::enableGuestUserFinished(QDBusPendingCallWatcher *call, bool enabling)
{
QDBusPendingReply<void> reply = *call;
if (reply.isError()) {
auto error = reply.error();
emit setGuestEnabledFailed(enabling, getErrorType(error));
qCWarning(lcUsersLog) << ((enabling) ? "Enabling" : "Disabling") << "guest user failed:" << error;
if (!enabling) {
m_transitioning.remove(SAILFISH_USERMANAGER_GUEST_UID);
auto idx = index(m_uidsToRows.value(SAILFISH_USERMANAGER_GUEST_UID), 0);
emit dataChanged(idx, idx, QVector<int>() << TransitioningRole);
}
} // else wait for signals
call->deleteLater();
}

void UserModel::createInterface()
{
if (!m_dBusInterface) {
Expand Down
7 changes: 5 additions & 2 deletions src/usermodel.h
Expand Up @@ -51,7 +51,7 @@ class SYSTEMSETTINGS_EXPORT UserModel: public QAbstractListModel
Q_OBJECT
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(int maximumCount READ maximumCount NOTIFY maximumCountChanged)
Q_PROPERTY(bool guestEnabled READ guestEnabled WRITE setGuestEnabled NOTIFY guestEnabledChanged)

public:
Expand Down Expand Up @@ -125,14 +125,16 @@ class SYSTEMSETTINGS_EXPORT UserModel: public QAbstractListModel
signals:
void placeholderChanged();
void countChanged();
void maximumCountChanged();
void guestEnabledChanged();
void userGroupsChanged(int row);
void userAddFailed(int error);
void userModifyFailed(int row, int error);
void userRemoveFailed(int row, int error);
void setCurrentUserFailed(int row, int error);
void addGroupsFailed(int row, int error);
void removeGroupsFailed(int row, int error);
void guestEnabledChanged();
void setGuestEnabledFailed(bool enabling, int error);

private slots:
void onUserAdded(const SailfishUserManagerEntry &entry);
Expand All @@ -148,6 +150,7 @@ private slots:
void setCurrentUserFinished(QDBusPendingCallWatcher *call, uint uid);
void addToGroupsFinished(QDBusPendingCallWatcher *call, uint uid);
void removeFromGroupsFinished(QDBusPendingCallWatcher *call, uint uid);
void enableGuestUserFinished(QDBusPendingCallWatcher *call, bool enabling);

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

0 comments on commit fd255f9

Please sign in to comment.