diff --git a/kded/portalmonitor.h b/kded/portalmonitor.h --- a/kded/portalmonitor.h +++ b/kded/portalmonitor.h @@ -24,6 +24,7 @@ #include +#include #include class PortalMonitor : public QObject @@ -36,6 +37,9 @@ private Q_SLOTS: void connectivityChanged(NetworkManager::Connectivity connectivity); void checkConnectivity(); + +private: + KNotification *m_notification = nullptr; }; #endif // PLASMA_NM_PORTAL_MONITOR_H diff --git a/kded/portalmonitor.cpp b/kded/portalmonitor.cpp --- a/kded/portalmonitor.cpp +++ b/kded/portalmonitor.cpp @@ -40,26 +40,49 @@ PortalMonitor::~PortalMonitor() { + if (m_notification) { + m_notification->close(); + } } void PortalMonitor::connectivityChanged(NetworkManager::Connectivity connectivity) { if (connectivity == NetworkManager::Portal) { + bool updateOnly = true; NetworkManager::ActiveConnection::Ptr primaryConnection = NetworkManager::primaryConnection(); - KNotification *notification = new KNotification(QStringLiteral("CaptivePortal"), KNotification::Persistent, this); - notification->setActions(QStringList{i18n("Log in")}); - notification->setComponentName(QStringLiteral("networkmanagement")); + + if (!m_notification) { + updateOnly = false; + m_notification = new KNotification(QStringLiteral("CaptivePortal"), KNotification::Persistent, this); + m_notification->setActions(QStringList{i18n("Log in")}); + m_notification->setComponentName(QStringLiteral("networkmanagement")); + m_notification->setText(i18n("You need to log in to this network")); + + connect(m_notification, &KNotification::action1Activated, this, [this] () { + QDesktopServices::openUrl(QUrl("http://networkcheck.kde.org")); + }); + connect(m_notification, &KNotification::closed, this, [this] () { + m_notification = nullptr; + }); + } + if (primaryConnection) { - notification->setTitle(primaryConnection->id()); + m_notification->setTitle(primaryConnection->id()); } else { - notification->setTitle(i18n("Network authentication")); + m_notification->setTitle(i18n("Network authentication")); + } + + if (updateOnly) { + m_notification->update(); + } else { + m_notification->sendEvent(); + } + + } else { + if (m_notification) { + m_notification->close(); } - notification->setText(i18n("You need to log in to this network")); - notification->sendEvent(); - connect(notification, &KNotification::action1Activated, this, [notification] () { - QDesktopServices::openUrl(QUrl("http://networkcheck.kde.org")); - notification->close(); - }); + m_notification = nullptr; } }