diff --git a/src/userinfo.cpp b/src/userinfo.cpp index d2823f4..3b46337 100644 --- a/src/userinfo.cpp +++ b/src/userinfo.cpp @@ -68,10 +68,10 @@ UserInfoPrivate::UserInfoPrivate(struct passwd *pwd) : m_uid(pwd->pw_uid) , m_username(QString::fromUtf8(pwd->pw_name)) , m_name(nameFromGecos(pwd->pw_gecos)) - // require_active == false -> both online and active are logged in. + // require_active == true -> only active user is logged in. // Specifying seat should make sure that remote users are not // counted as they don't have seats. - , m_loggedIn(sd_uid_is_on_seat(m_uid, 0, "seat0") > 0) + , m_loggedIn(sd_uid_is_on_seat(m_uid, 1, "seat0") > 0) { } @@ -113,9 +113,7 @@ void UserInfoPrivate::set(struct passwd *pwd) */ UserInfo::UserInfo() { - if (!UserInfoPrivate::s_current.isNull()) { - d_ptr = QSharedPointer(UserInfoPrivate::s_current); - } + d_ptr = UserInfoPrivate::s_current.toStrongRef(); if (d_ptr.isNull()) { uid_t uid = InvalidId; struct passwd *pwd; @@ -251,10 +249,27 @@ bool UserInfo::current() const return d->m_loggedIn; } +bool UserInfo::updateCurrent() +{ + Q_D(UserInfo); + bool previous = d->m_loggedIn; + d->m_loggedIn = sd_uid_is_on_seat(d->m_uid, 1, "seat0") > 0; + if (d->m_loggedIn != previous) { + if (d->m_loggedIn) + UserInfoPrivate::s_current = d_ptr; + else if (UserInfoPrivate::s_current == d_ptr) + UserInfoPrivate::s_current.clear(); + emit d_ptr->currentChanged(); + return true; + } + return false; +} + void UserInfo::reset() { Q_D(UserInfo); d->set((isValid()) ? getpwuid(d->m_uid) : nullptr); + updateCurrent(); } UserInfo &UserInfo::operator=(const UserInfo &other) @@ -286,4 +301,5 @@ void UserInfo::connectSignals() connect(d_ptr.data(), &UserInfoPrivate::usernameChanged, this, &UserInfo::usernameChanged); connect(d_ptr.data(), &UserInfoPrivate::nameChanged, this, &UserInfo::nameChanged); connect(d_ptr.data(), &UserInfoPrivate::uidChanged, this, &UserInfo::uidChanged); + connect(d_ptr.data(), &UserInfoPrivate::currentChanged, this, &UserInfo::currentChanged); } diff --git a/src/userinfo.h b/src/userinfo.h index f25a771..e06a055 100644 --- a/src/userinfo.h +++ b/src/userinfo.h @@ -50,7 +50,7 @@ class SYSTEMSETTINGS_EXPORT UserInfo: public QObject Q_PROPERTY(QString name READ name NOTIFY nameChanged) Q_PROPERTY(UserType type READ type CONSTANT) Q_PROPERTY(int uid READ uid NOTIFY uidChanged) - Q_PROPERTY(bool current READ current CONSTANT) + Q_PROPERTY(bool current READ current NOTIFY currentChanged) friend class UserModel; @@ -85,6 +85,7 @@ class SYSTEMSETTINGS_EXPORT UserInfo: public QObject void usernameChanged(); void nameChanged(); void uidChanged(); + void currentChanged(); private: explicit UserInfo(int uid); @@ -92,6 +93,7 @@ class SYSTEMSETTINGS_EXPORT UserInfo: public QObject void setUsername(QString username); void setName(QString name); + bool updateCurrent(); void connectSignals(); diff --git a/src/userinfo_p.h b/src/userinfo_p.h index 22cc168..c0ac063 100644 --- a/src/userinfo_p.h +++ b/src/userinfo_p.h @@ -59,6 +59,7 @@ class UserInfoPrivate : public QObject void usernameChanged(); void nameChanged(); void uidChanged(); + void currentChanged(); }; #endif /* USERINFOPRIVATE_H */