diff --git a/kded/portalmonitor.cpp b/kded/portalmonitor.cpp index 9dc3db04..8db521bf 100644 --- a/kded/portalmonitor.cpp +++ b/kded/portalmonitor.cpp @@ -1,77 +1,100 @@ /* * Copyright 2016 Jan Grulich * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ #include "portalmonitor.h" #include #include #include #include #include #include PortalMonitor::PortalMonitor(QObject *parent) : QObject(parent) { checkConnectivity(); connect(NetworkManager::notifier(), &NetworkManager::Notifier::connectivityChanged, this, &PortalMonitor::connectivityChanged); } 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; } } void PortalMonitor::checkConnectivity() { QDBusPendingReply pendingReply = NetworkManager::checkConnectivity(); QDBusPendingCallWatcher *callWatcher = new QDBusPendingCallWatcher(pendingReply); connect(callWatcher, &QDBusPendingCallWatcher::finished, this, [this] (QDBusPendingCallWatcher *watcher) { QDBusPendingReply reply = *watcher; if (reply.isValid()) { connectivityChanged((NetworkManager::Connectivity)reply.value()); } watcher->deleteLater(); }); } diff --git a/kded/portalmonitor.h b/kded/portalmonitor.h index b3108e5c..fe6eaab6 100644 --- a/kded/portalmonitor.h +++ b/kded/portalmonitor.h @@ -1,41 +1,45 @@ /* * Copyright 2016 Jan Grulich * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ #ifndef PLASMA_NM_PORTAL_MONITOR_H #define PLASMA_NM_PORTAL_MONITOR_H #include +#include #include class PortalMonitor : public QObject { Q_OBJECT public: explicit PortalMonitor(QObject *parent); ~PortalMonitor() override; private Q_SLOTS: void connectivityChanged(NetworkManager::Connectivity connectivity); void checkConnectivity(); + +private: + KNotification *m_notification = nullptr; }; #endif // PLASMA_NM_PORTAL_MONITOR_H