From 673f825e9073d356434dc6ed334cfaaccbd01a17 Mon Sep 17 00:00:00 2001 From: caixiangrong Date: Wed, 4 Feb 2026 14:25:51 +0800 Subject: [PATCH] fix: prevent notifications when user inactive MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../src/session/networkstatehandler.cpp | 58 ++++++++++++++++++- .../src/session/networkstatehandler.h | 6 ++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/network-service-plugin/src/session/networkstatehandler.cpp b/network-service-plugin/src/session/networkstatehandler.cpp index fcad08e6..bd7cdb0e 100644 --- a/network-service-plugin/src/session/networkstatehandler.cpp +++ b/network-service-plugin/src/session/networkstatehandler.cpp @@ -53,6 +53,9 @@ const QString notifyIconMobile4gDisconnected = "notification-network-mobile-4g-d const QString notifyIconMobileUnknownConnected = "notification-network-mobile-unknown-connected"; const QString notifyIconMobileUnknownDisconnected = "notification-network-mobile-unknown-disconnected"; +constexpr auto DBUS_PROPERTIES_IFACE = "org.freedesktop.DBus.Properties"; +constexpr auto LOGIN1_SERVICE = "org.freedesktop.login1"; + NetworkStateHandler::NetworkStateHandler(QObject *parent) : QObject(parent) , m_notifyEnabled(true) @@ -62,6 +65,8 @@ NetworkStateHandler::NetworkStateHandler(QObject *parent) , m_wifiEnabled(true) , m_delayShowWifiOSD(new QTimer(this)) , m_resetWifiOSDEnableTimer(new QTimer(this)) + , m_sessionActive(false) + , m_sessionRemote(false) { m_delayShowWifiOSD->setSingleShot(true); m_resetWifiOSDEnableTimer->setSingleShot(true); @@ -71,13 +76,18 @@ NetworkStateHandler::NetworkStateHandler(QObject *parent) connect(SettingConfig::instance(), &SettingConfig::resetWifiOSDEnableTimeoutChanged, this, &NetworkStateHandler::updateOSDTimer); QDBusConnection::systemBus().connect("org.deepin.dde.Network1", "/org/deepin/dde/Network1", "org.deepin.dde.Network1", "DeviceEnabled", this, SLOT(onDeviceEnabled(QDBusObjectPath, bool))); QDBusConnection::systemBus().connect("org.freedesktop.NetworkManager", "", "org.freedesktop.NetworkManager.VPN.Connection", "VpnStateChanged", this, SLOT(onVpnStateChanged(QDBusMessage))); - QDBusConnection::systemBus().connect("org.deepin.dde.AirplaneMode1", "/org/deepin/dde/AirplaneMode1", "org.freedesktop.DBus.Properties", "PropertiesChanged", this, SLOT(onAirplaneModePropertiesChanged(QString, QVariantMap, QStringList))); + QDBusConnection::systemBus().connect("org.deepin.dde.AirplaneMode1", "/org/deepin/dde/AirplaneMode1", DBUS_PROPERTIES_IFACE, "PropertiesChanged", this, SLOT(onAirplaneModePropertiesChanged(QString, QVariantMap, QStringList))); // 迁移dde-daemon/session/power1/sleep_inhibit.go ConnectHandleForSleep处理 QDBusConnection::systemBus().connect("org.deepin.dde.Daemon1", "/org/deepin/dde/Daemon1", "org.deepin.dde.Daemon1", "HandleForSleep", this, SLOT(onHandleForSleep(bool))); QDBusMessage message = QDBusMessage::createMethodCall("org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "GetNameOwner"); message << "org.freedesktop.NetworkManager"; QDBusConnection::systemBus().callWithCallback(message, this, SLOT(init())); updateOSDTimer(SettingConfig::instance()->resetWifiOSDEnableTimeout()); + + uid_t uid = getuid(); + QDBusMessage userMsg = QDBusMessage::createMethodCall(LOGIN1_SERVICE, QString("/org/freedesktop/login1/user/_%1").arg(uid), DBUS_PROPERTIES_IFACE, "Get"); + userMsg << "org.freedesktop.login1.User" << "Display"; + QDBusConnection::systemBus().callWithCallback(userMsg, this, SLOT(initUserDisplay(QDBusMessage))); } void NetworkStateHandler::init() @@ -635,7 +645,7 @@ void *NetworkStateHandler::nmGetDeviceActiveConnectionData(NetworkManager::Devic void NetworkStateHandler::notify(const QString &icon, const QString &summary, const QString &body) { qCDebug(DSM()) << "notify icon:" << icon << ", summary:" << summary << ", body:" << body; - if (!m_notifyEnabled) { + if (!m_notifyEnabled || !m_sessionActive || m_sessionRemote) { qCDebug(DSM()) << "notify disabled"; return; } @@ -753,6 +763,50 @@ void NetworkStateHandler::updateOSDTimer(int interval) m_resetWifiOSDEnableTimer->setInterval(interval); } +void NetworkStateHandler::initUserDisplay(const QDBusMessage &msg) +{ + if (msg.arguments().isEmpty()) { + return; + } + QDBusVariant display = msg.arguments().first().value(); + const QDBusArgument arg = display.variant().value(); + if (arg.currentType() != QDBusArgument::StructureType) { + qCWarning(DSM()) << "Invalid argument type, expected StructureType"; + return; + } + + QString id; + QDBusObjectPath displayPath; + arg.beginStructure(); + arg >> id >> displayPath; + arg.endStructure(); + QString sessionPath = displayPath.path(); + if (sessionPath.isEmpty()) { + qCWarning(DSM()) << "Empty session path"; + return; + } + QDBusMessage sessionMsg = QDBusMessage::createMethodCall(LOGIN1_SERVICE, sessionPath, DBUS_PROPERTIES_IFACE, "GetAll"); + sessionMsg << "org.freedesktop.login1.Session"; + + QDBusConnection::systemBus().callWithCallback(sessionMsg, this, SLOT(updateLogin1Properties(QVariantMap))); + QDBusConnection::systemBus().connect(LOGIN1_SERVICE, sessionPath, DBUS_PROPERTIES_IFACE, "PropertiesChanged", this, SLOT(onLogin1PropertiesChanged(QString, QVariantMap, QStringList))); +} + +void NetworkStateHandler::onLogin1PropertiesChanged(const QString &, const QVariantMap &properties, const QStringList &) +{ + updateLogin1Properties(properties); +} + +void NetworkStateHandler::updateLogin1Properties(const QVariantMap &properties) +{ + if (properties.contains("Active")) { + m_sessionActive = properties.value("Active").toBool(); + } + if (properties.contains("Remote")) { + m_sessionRemote = properties.value("Remote").toBool(); + } +} + static QString getMobileConnectedNotifyIcon(ModemDevice::Capabilities mobileNetworkType) { switch (mobileNetworkType) { diff --git a/network-service-plugin/src/session/networkstatehandler.h b/network-service-plugin/src/session/networkstatehandler.h index f6f69161..6db05386 100644 --- a/network-service-plugin/src/session/networkstatehandler.h +++ b/network-service-plugin/src/session/networkstatehandler.h @@ -109,6 +109,9 @@ public Q_SLOTS: void resetWifiOSDEnable(); void delayShowWifiOSD(); void updateOSDTimer(int interval); + void initUserDisplay(const QDBusMessage &msg); + void onLogin1PropertiesChanged(const QString &, const QVariantMap &properties, const QStringList &); + void updateLogin1Properties(const QVariantMap &properties); protected: bool isVirtualDeviceIfc(NetworkManager::Device::Ptr dev); @@ -142,6 +145,9 @@ public Q_SLOTS: bool m_wifiEnabled; QTimer *m_delayShowWifiOSD; QTimer *m_resetWifiOSDEnableTimer; + + bool m_sessionActive; + bool m_sessionRemote; }; } // namespace sessionservice } // namespace network