Skip to content

Commit 62e60e2

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 3e2fce4 commit 62e60e2

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

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

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ const QString notifyIconMobile4gDisconnected = "notification-network-mobile-4g-d
5353
const QString notifyIconMobileUnknownConnected = "notification-network-mobile-unknown-connected";
5454
const QString notifyIconMobileUnknownDisconnected = "notification-network-mobile-unknown-disconnected";
5555

56+
constexpr auto DBUS_PROPERTIES_IFACE = "org.freedesktop.DBus.Properties";
57+
constexpr auto LOGIN1_SERVICE = "org.freedesktop.login1";
58+
5659
NetworkStateHandler::NetworkStateHandler(QObject *parent)
5760
: QObject(parent)
5861
, m_notifyEnabled(true)
@@ -62,6 +65,8 @@ NetworkStateHandler::NetworkStateHandler(QObject *parent)
6265
, m_wifiEnabled(true)
6366
, m_delayShowWifiOSD(new QTimer(this))
6467
, m_resetWifiOSDEnableTimer(new QTimer(this))
68+
, m_sessionActive(false)
69+
, m_sessionRemote(false)
6570
{
6671
m_delayShowWifiOSD->setSingleShot(true);
6772
m_resetWifiOSDEnableTimer->setSingleShot(true);
@@ -71,13 +76,18 @@ NetworkStateHandler::NetworkStateHandler(QObject *parent)
7176
connect(SettingConfig::instance(), &SettingConfig::resetWifiOSDEnableTimeoutChanged, this, &NetworkStateHandler::updateOSDTimer);
7277
QDBusConnection::systemBus().connect("org.deepin.dde.Network1", "/org/deepin/dde/Network1", "org.deepin.dde.Network1", "DeviceEnabled", this, SLOT(onDeviceEnabled(QDBusObjectPath, bool)));
7378
QDBusConnection::systemBus().connect("org.freedesktop.NetworkManager", "", "org.freedesktop.NetworkManager.VPN.Connection", "VpnStateChanged", this, SLOT(onVpnStateChanged(QDBusMessage)));
74-
QDBusConnection::systemBus().connect("org.deepin.dde.AirplaneMode1", "/org/deepin/dde/AirplaneMode1", "org.freedesktop.DBus.Properties", "PropertiesChanged", this, SLOT(onAirplaneModePropertiesChanged(QString, QVariantMap, QStringList)));
79+
QDBusConnection::systemBus().connect("org.deepin.dde.AirplaneMode1", "/org/deepin/dde/AirplaneMode1", DBUS_PROPERTIES_IFACE, "PropertiesChanged", this, SLOT(onAirplaneModePropertiesChanged(QString, QVariantMap, QStringList)));
7580
// 迁移dde-daemon/session/power1/sleep_inhibit.go ConnectHandleForSleep处理
7681
QDBusConnection::systemBus().connect("org.deepin.dde.Daemon1", "/org/deepin/dde/Daemon1", "org.deepin.dde.Daemon1", "HandleForSleep", this, SLOT(onHandleForSleep(bool)));
7782
QDBusMessage message = QDBusMessage::createMethodCall("org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "GetNameOwner");
7883
message << "org.freedesktop.NetworkManager";
7984
QDBusConnection::systemBus().callWithCallback(message, this, SLOT(init()));
8085
updateOSDTimer(SettingConfig::instance()->resetWifiOSDEnableTimeout());
86+
87+
uid_t uid = getuid();
88+
QDBusMessage userMsg = QDBusMessage::createMethodCall(LOGIN1_SERVICE, QString("/org/freedesktop/login1/user/_%1").arg(uid), DBUS_PROPERTIES_IFACE, "Get");
89+
userMsg << "org.freedesktop.login1.User" << "Display";
90+
QDBusConnection::systemBus().callWithCallback(userMsg, this, SLOT(initUserDisplay(QDBusMessage)));
8191
}
8292

8393
void NetworkStateHandler::init()
@@ -635,7 +645,7 @@ void *NetworkStateHandler::nmGetDeviceActiveConnectionData(NetworkManager::Devic
635645
void NetworkStateHandler::notify(const QString &icon, const QString &summary, const QString &body)
636646
{
637647
qCDebug(DSM()) << "notify icon:" << icon << ", summary:" << summary << ", body:" << body;
638-
if (!m_notifyEnabled) {
648+
if (!m_notifyEnabled || !m_sessionActive || m_sessionRemote) {
639649
qCDebug(DSM()) << "notify disabled";
640650
return;
641651
}
@@ -753,6 +763,50 @@ void NetworkStateHandler::updateOSDTimer(int interval)
753763
m_resetWifiOSDEnableTimer->setInterval(interval);
754764
}
755765

766+
void NetworkStateHandler::initUserDisplay(const QDBusMessage &msg)
767+
{
768+
if (msg.arguments().isEmpty()) {
769+
return;
770+
}
771+
QDBusVariant display = msg.arguments().first().value<QDBusVariant>();
772+
const QDBusArgument arg = display.variant().value<QDBusArgument>();
773+
if (arg.currentType() != QDBusArgument::StructureType) {
774+
qCWarning(DSM()) << "Invalid argument type, expected StructureType";
775+
return;
776+
}
777+
778+
QString id;
779+
QDBusObjectPath displayPath;
780+
arg.beginStructure();
781+
arg >> id >> displayPath;
782+
arg.endStructure();
783+
QString sessionPath = displayPath.path();
784+
if (sessionPath.isEmpty()) {
785+
qCWarning(DSM()) << "Empty session path";
786+
return;
787+
}
788+
QDBusMessage sessionMsg = QDBusMessage::createMethodCall(LOGIN1_SERVICE, sessionPath, DBUS_PROPERTIES_IFACE, "GetAll");
789+
sessionMsg << "org.freedesktop.login1.Session";
790+
791+
QDBusConnection::systemBus().callWithCallback(sessionMsg, this, SLOT(updateLogin1Properties(QVariantMap)));
792+
QDBusConnection::systemBus().connect(LOGIN1_SERVICE, sessionPath, DBUS_PROPERTIES_IFACE, "PropertiesChanged", this, SLOT(onLogin1PropertiesChanged(QString, QVariantMap, QStringList)));
793+
}
794+
795+
void NetworkStateHandler::onLogin1PropertiesChanged(const QString &, const QVariantMap &properties, const QStringList &)
796+
{
797+
updateLogin1Properties(properties);
798+
}
799+
800+
void NetworkStateHandler::updateLogin1Properties(const QVariantMap &properties)
801+
{
802+
if (properties.contains("Active")) {
803+
m_sessionActive = properties.value("Active").toBool();
804+
}
805+
if (properties.contains("Remote")) {
806+
m_sessionRemote = properties.value("Remote").toBool();
807+
}
808+
}
809+
756810
static QString getMobileConnectedNotifyIcon(ModemDevice::Capabilities mobileNetworkType)
757811
{
758812
switch (mobileNetworkType) {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ public Q_SLOTS:
109109
void resetWifiOSDEnable();
110110
void delayShowWifiOSD();
111111
void updateOSDTimer(int interval);
112+
void initUserDisplay(const QDBusMessage &msg);
113+
void onLogin1PropertiesChanged(const QString &, const QVariantMap &properties, const QStringList &);
114+
void updateLogin1Properties(const QVariantMap &properties);
112115

113116
protected:
114117
bool isVirtualDeviceIfc(NetworkManager::Device::Ptr dev);
@@ -142,6 +145,9 @@ public Q_SLOTS:
142145
bool m_wifiEnabled;
143146
QTimer *m_delayShowWifiOSD;
144147
QTimer *m_resetWifiOSDEnableTimer;
148+
149+
bool m_sessionActive;
150+
bool m_sessionRemote;
145151
};
146152
} // namespace sessionservice
147153
} // namespace network

0 commit comments

Comments
 (0)