From d2fa5fd0f5b1972bd372510cc14509e85b972b23 Mon Sep 17 00:00:00 2001 From: Kirill Burtsev Date: Wed, 20 Mar 2019 18:04:50 +0100 Subject: Notification API cleanup Task-number: QTBUG-74543 Change-Id: Ice5a0dbfc3485c8b7e6fa900ef427a9aed871d42 Reviewed-by: Allan Sandfeld Jensen --- .../api/qwebenginenotificationpresenter.cpp | 38 ++++++++++++---------- .../api/qwebenginenotificationpresenter_p.h | 10 ++++-- src/webenginewidgets/api/qwebengineprofile.cpp | 10 +++--- src/webenginewidgets/api/qwebengineprofile.h | 3 +- src/webenginewidgets/api/qwebengineprofile_p.h | 2 +- 5 files changed, 37 insertions(+), 26 deletions(-) (limited to 'src/webenginewidgets/api') diff --git a/src/webenginewidgets/api/qwebenginenotificationpresenter.cpp b/src/webenginewidgets/api/qwebenginenotificationpresenter.cpp index da97c4662..667605c37 100644 --- a/src/webenginewidgets/api/qwebenginenotificationpresenter.cpp +++ b/src/webenginewidgets/api/qwebenginenotificationpresenter.cpp @@ -39,6 +39,7 @@ #include "qwebenginenotificationpresenter_p.h" +#include #include QT_BEGIN_NAMESPACE @@ -55,50 +56,53 @@ DefaultNotificationPresenter::~DefaultNotificationPresenter() { } -void DefaultNotificationPresenter::show(const QWebEngineNotification ¬ification) +void DefaultNotificationPresenter::show(std::unique_ptr notification) { - if (m_activeNotification.isValid()) { - m_activeNotification.close(); - m_activeNotification.disconnect(this); + Q_ASSERT(notification); + if (m_activeNotification) { + m_activeNotification->close(); + m_activeNotification->disconnect(this); } - m_activeNotification = notification; + m_activeNotification = std::move(notification); #ifndef QT_NO_SYSTEMTRAYICON - if (m_activeNotification.isValid() && m_systemTrayIcon) { + if (m_activeNotification && m_systemTrayIcon) { + m_systemTrayIcon->setIcon(qApp->windowIcon()); m_systemTrayIcon->show(); - QIcon icon = notification.icon(); - if (!icon.isNull()) - m_systemTrayIcon->showMessage(notification.title(), notification.message(), icon); + QImage notificationIconImage = m_activeNotification->icon(); + m_notificationIcon = QIcon(QPixmap::fromImage(std::move(notificationIconImage), Qt::NoFormatConversion)); + if (!m_notificationIcon.isNull()) + m_systemTrayIcon->showMessage(m_activeNotification->title(), m_activeNotification->message(), m_notificationIcon); else - m_systemTrayIcon->showMessage(notification.title(), notification.message()); - notification.show(); - connect(&m_activeNotification, &QWebEngineNotification::closed, this, &DefaultNotificationPresenter::closeNotification); + m_systemTrayIcon->showMessage(m_activeNotification->title(), m_activeNotification->message()); + m_activeNotification->show(); + connect(m_activeNotification.get(), &QWebEngineNotification::closed, this, &DefaultNotificationPresenter::closeNotification); } #endif } void DefaultNotificationPresenter::messageClicked() { - if (m_activeNotification.isValid()) - m_activeNotification.click(); + if (m_activeNotification) + m_activeNotification->click(); } void DefaultNotificationPresenter::closeNotification() { #ifndef QT_NO_SYSTEMTRAYICON const QWebEngineNotification *canceled = static_cast(QObject::sender()); - if (m_systemTrayIcon && canceled->matches(m_activeNotification)) + if (m_systemTrayIcon && canceled->matches(m_activeNotification.get())) m_systemTrayIcon->hide(); #endif } -void defaultNotificationPresenter(const QWebEngineNotification ¬ification) +void defaultNotificationPresenter(std::unique_ptr notification) { static DefaultNotificationPresenter *presenter = nullptr; if (!presenter) presenter = new DefaultNotificationPresenter(); - presenter->show(notification); + presenter->show(std::move(notification)); } diff --git a/src/webenginewidgets/api/qwebenginenotificationpresenter_p.h b/src/webenginewidgets/api/qwebenginenotificationpresenter_p.h index a66dbc1b2..49d774806 100644 --- a/src/webenginewidgets/api/qwebenginenotificationpresenter_p.h +++ b/src/webenginewidgets/api/qwebenginenotificationpresenter_p.h @@ -54,6 +54,9 @@ #include #include +#include + +#include QT_BEGIN_NAMESPACE @@ -65,7 +68,7 @@ public: DefaultNotificationPresenter(QObject *parent = nullptr); virtual ~DefaultNotificationPresenter(); - void show(const QWebEngineNotification ¬ification); + void show(std::unique_ptr notification); private Q_SLOTS: void messageClicked(); @@ -73,10 +76,11 @@ private Q_SLOTS: private: QSystemTrayIcon *m_systemTrayIcon; - QWebEngineNotification m_activeNotification; + QIcon m_notificationIcon; + std::unique_ptr m_activeNotification; }; -void defaultNotificationPresenter(const QWebEngineNotification ¬ification); +void defaultNotificationPresenter(std::unique_ptr notification); QT_END_NAMESPACE diff --git a/src/webenginewidgets/api/qwebengineprofile.cpp b/src/webenginewidgets/api/qwebengineprofile.cpp index d69ddb343..47b3f4363 100644 --- a/src/webenginewidgets/api/qwebengineprofile.cpp +++ b/src/webenginewidgets/api/qwebengineprofile.cpp @@ -141,10 +141,12 @@ using QtWebEngineCore::ProfileAdapter; Both session and persistent cookies are saved to and restored from disk. */ -void QWebEngineProfilePrivate::showNotification(QSharedPointer ¬ification) +void QWebEngineProfilePrivate::showNotification(QSharedPointer &controller) { - if (m_notificationPresenter) - m_notificationPresenter(QWebEngineNotification(notification)); + if (m_notificationPresenter) { + std::unique_ptr notification(new QWebEngineNotification(controller)); + m_notificationPresenter(std::move(notification)); + } } /*! @@ -668,7 +670,7 @@ QWebEngineScriptCollection *QWebEngineProfile::scripts() const \since 5.13 \sa QWebEngineNotification */ -void QWebEngineProfile::setNotificationPresenter(std::function notificationPresenter) +void QWebEngineProfile::setNotificationPresenter(std::function)> notificationPresenter) { Q_D(QWebEngineProfile); d->m_notificationPresenter = std::move(notificationPresenter); diff --git a/src/webenginewidgets/api/qwebengineprofile.h b/src/webenginewidgets/api/qwebengineprofile.h index e3ddb594a..794ba6279 100644 --- a/src/webenginewidgets/api/qwebengineprofile.h +++ b/src/webenginewidgets/api/qwebengineprofile.h @@ -47,6 +47,7 @@ #include #include +#include QT_BEGIN_NAMESPACE @@ -141,7 +142,7 @@ public: QString downloadPath() const; void setDownloadPath(const QString &path); - void setNotificationPresenter(std::function notificationPresenter); + void setNotificationPresenter(std::function)> notificationPresenter); QWebEngineClientCertificateStore *clientCertificateStore(); diff --git a/src/webenginewidgets/api/qwebengineprofile_p.h b/src/webenginewidgets/api/qwebengineprofile_p.h index 91c43cf0a..64e9500b0 100644 --- a/src/webenginewidgets/api/qwebengineprofile_p.h +++ b/src/webenginewidgets/api/qwebengineprofile_p.h @@ -100,7 +100,7 @@ private: QPointer m_profileAdapter; QScopedPointer m_scriptCollection; QMap > m_ongoingDownloads; - std::function m_notificationPresenter; + std::function)> m_notificationPresenter; }; QT_END_NAMESPACE -- cgit v1.2.3