Skip to content

Commit 244e6e5

Browse files
committed
fix: prevent notifications when user inactive
Added user activity check to notification system to prevent sending notifications when user is not actively using the system. The new isUserActive() method queries systemd-logind via D-Bus to determine if the current user session is active and not remote. The change ensures notifications are only shown when the user is present and actively using the system, preventing unnecessary distractions during inactive periods or remote sessions. This improves user experience by reducing notification spam when the user cannot see them. Log: Network notifications now respect user activity status Influence: 1. Test network notifications appear when user is actively using the system 2. Verify notifications are suppressed when user session is inactive 3. Test notification behavior during remote desktop sessions 4. Verify notification suppression when system is locked or user is away 5. Test different network state changes (connection/disconnection/VPN events) fix: 防止用户非活动时发送通知 在通知系统中添加用户活动状态检查,防止在用户未主动使用系统时发送通知。新 增的 isUserActive() 方法通过 D-Bus 查询 systemd-logind 来确定当前用户会 话是否活跃且非远程会话。 该变更确保通知仅在用户在场且主动使用系统时显示,避免在用户非活动期间或远 程会话时产生不必要的干扰。通过减少用户无法看到通知时的通知垃圾,提升了用 户体验。 Log: 网络通知现在会尊重用户活动状态 Influence: 1. 测试用户主动使用系统时网络通知是否正常显示 2. 验证用户会话非活动时通知是否被抑制 3. 测试远程桌面会话期间的通知行为 4. 验证系统锁定或用户离开时通知抑制功能 5. 测试不同网络状态变化(连接/断开/VPN事件)的通知行为 PMS: BUG-349057
1 parent 95539e9 commit 244e6e5

1 file changed

Lines changed: 44 additions & 1 deletion

File tree

network-service-plugin/src/session/networkstatehandler.cpp

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ void *NetworkStateHandler::nmGetDeviceActiveConnectionData(NetworkManager::Devic
635635
void NetworkStateHandler::notify(const QString &icon, const QString &summary, const QString &body)
636636
{
637637
qCDebug(DSM()) << "notify icon:" << icon << ", summary:" << summary << ", body:" << body;
638-
if (!m_notifyEnabled) {
638+
if (!m_notifyEnabled || !isUserActive()) {
639639
qCDebug(DSM()) << "notify disabled";
640640
return;
641641
}
@@ -859,6 +859,49 @@ void NetworkStateHandler::notifyVpnFailed(const QString &id, uint reason)
859859
notify(notifyIconVpnDisconnected, tr("Disconnected"), m_vpnErrorTable[reason]);
860860
}
861861

862+
bool NetworkStateHandler::isUserActive()
863+
{
864+
uid_t uid = getuid();
865+
QDBusMessage loginMsg = QDBusMessage::createMethodCall("org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", "GetUser");
866+
loginMsg << uid;
867+
QDBusPendingReply<QDBusObjectPath> userPath = QDBusConnection::systemBus().asyncCall(loginMsg);
868+
if (userPath.isError()) {
869+
qCWarning(DSM()) << "get User error:" << userPath.error();
870+
return false;
871+
}
872+
QDBusMessage userMsg = QDBusMessage::createMethodCall("org.freedesktop.login1", userPath.value().path(), "org.freedesktop.DBus.Properties", "GetAll");
873+
userMsg << "org.freedesktop.login1.User";
874+
QDBusPendingReply<QVariantMap> userInfo = QDBusConnection::systemBus().asyncCall(userMsg);
875+
if (userInfo.isError()) {
876+
qCWarning(DSM()) << "get User info error:" << userInfo.error();
877+
return false;
878+
}
879+
if (userInfo.value().value("State") != "active") {
880+
return false;
881+
}
882+
const QDBusArgument display = userInfo.value().value("Display").value<QDBusArgument>();
883+
QString id;
884+
QDBusObjectPath displayPath;
885+
display.beginStructure();
886+
display >> id;
887+
display >> displayPath;
888+
display.endStructure();
889+
if (displayPath.path().isEmpty()) {
890+
return false;
891+
}
892+
QDBusMessage sessionMsg = QDBusMessage::createMethodCall("org.freedesktop.login1", displayPath.path(), "org.freedesktop.DBus.Properties", "GetAll");
893+
sessionMsg << "org.freedesktop.login1.Session";
894+
QDBusPendingReply<QVariantMap> sessionInfo = QDBusConnection::systemBus().asyncCall(sessionMsg);
895+
if (sessionInfo.isError()) {
896+
qCWarning(DSM()) << "get Session info error:" << sessionInfo.error();
897+
return false;
898+
}
899+
if (!sessionInfo.value().value("Active").toBool() || sessionInfo.value().value("Remote").toBool()) {
900+
return false;
901+
}
902+
return true;
903+
}
904+
862905
NetworkStateHandler::ConnectionType NetworkStateHandler::getCustomConnectionType(NetworkManager::ConnectionSettings *settings)
863906
{
864907
auto t = settings->connectionType();

0 commit comments

Comments
 (0)