From 89537e33ce47f9890951d5558f254f285896cf04 Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Thu, 30 Apr 2026 14:16:01 -0700 Subject: [PATCH] Fix duplicate download notifications. https://redmine.stoutner.com/issues/1289 --- doc/index.docbook | 2 +- src/widgets/TabWidget.cpp | 57 ++++++++++++++++++--------------------- 2 files changed, 27 insertions(+), 32 deletions(-) diff --git a/doc/index.docbook b/doc/index.docbook index 2796f5b..bcac573 100644 --- a/doc/index.docbook +++ b/doc/index.docbook @@ -1845,7 +1845,7 @@ <ulink url="https://www.stoutner.com/privacy-browser-pc-0.9/">0.9</ulink> - - 25 April 2026 + 25 April 2026 Enable JavaScript copying to the clipboard with user interaction. diff --git a/src/widgets/TabWidget.cpp b/src/widgets/TabWidget.cpp index ce8978b..f3a08fe 100644 --- a/src/widgets/TabWidget.cpp +++ b/src/widgets/TabWidget.cpp @@ -1,5 +1,5 @@ /* SPDX-License-Identifier: GPL-3.0-or-later - * SPDX-FileCopyrightText: 2022-2025 Soren Stoutner + * SPDX-FileCopyrightText: 2022-2026 Soren Stoutner * * This file is part of Privacy Browser PC . * @@ -1012,15 +1012,15 @@ void TabWidget::showSaveDialog(QWebEngineDownloadRequest *webEngineDownloadReque webEngineDownloadRequestPointer->setDownloadDirectory(absoluteSavePath); webEngineDownloadRequestPointer->setDownloadFileName(saveFileName); + // Start the download. + webEngineDownloadRequestPointer->accept(); + // Create a file download notification. KNotification *fileDownloadNotificationPointer = new KNotification(QLatin1String("FileDownload")); // Set the notification title. fileDownloadNotificationPointer->setTitle(i18nc("Download notification title", "Download")); - // Set the notification text. - fileDownloadNotificationPointer->setText(i18nc("Downloading notification text", "Downloading %1", saveFileName)); - // Get the download icon from the theme. QIcon downloadIcon = QIcon::fromTheme(QLatin1String("download"), QIcon::fromTheme(QLatin1String("document-save"))); @@ -1028,13 +1028,13 @@ void TabWidget::showSaveDialog(QWebEngineDownloadRequest *webEngineDownloadReque fileDownloadNotificationPointer->setIconName(downloadIcon.name()); // Add the cancel action. - KNotificationAction *cancelActionPointer = fileDownloadNotificationPointer->addDefaultAction(i18nc("Download notification action","Cancel")); + KNotificationAction *cancelActionPointer = fileDownloadNotificationPointer->addAction(i18nc("Download notification action","Cancel")); // Prevent the notification from being autodeleted if it is closed. Otherwise, the updates to the notification below cause a crash. fileDownloadNotificationPointer->setAutoDelete(false); // Handle clicks on the cancel action. - connect(cancelActionPointer, &KNotificationAction::activated, [webEngineDownloadRequestPointer, saveFileName] () + connect(cancelActionPointer, &KNotificationAction::activated, [webEngineDownloadRequestPointer, saveFileName, downloadIcon] () { // Cancel the download. webEngineDownloadRequestPointer->cancel(); @@ -1042,14 +1042,11 @@ void TabWidget::showSaveDialog(QWebEngineDownloadRequest *webEngineDownloadReque // Create a file download notification. KNotification *canceledDownloadNotificationPointer = new KNotification(QLatin1String("FileDownload")); - // Set the notification title. - canceledDownloadNotificationPointer->setTitle(i18nc("Download notification title", "Download")); - // Set the new text. canceledDownloadNotificationPointer->setText(i18nc("Download canceled notification", "%1 download canceled", saveFileName)); // Set the notification icon. - canceledDownloadNotificationPointer->setIconName(QLatin1String("download")); + canceledDownloadNotificationPointer->setIconName(downloadIcon.name()); // Display the notification. canceledDownloadNotificationPointer->sendEvent(); @@ -1062,24 +1059,28 @@ void TabWidget::showSaveDialog(QWebEngineDownloadRequest *webEngineDownloadReque qint64 receivedBytes = webEngineDownloadRequestPointer->receivedBytes(); qint64 totalBytes = webEngineDownloadRequestPointer->totalBytes(); - // Set the new text. Total bytes will be 0 if the download size is unknown. - if (totalBytes > 0) + // Only display the notification if it isn't at 100%. Otherwise, this notification and the download complete notification could display separately. + if (receivedBytes != totalBytes) { - // Calculate the download percentage. - int downloadPercentage = 100 * receivedBytes / totalBytes; + // Set the new text. Total bytes will be 0 if the download size is unknown. + if (totalBytes > 0) + { + // Calculate the download percentage. + int downloadPercentage = 100 * receivedBytes / totalBytes; + + // Set the file download notification text. + fileDownloadNotificationPointer->setText(i18nc("Download progress notification text", "%1% of %2 downloaded (%3 of %4 bytes)", downloadPercentage, saveFileName, + receivedBytes, totalBytes)); + } + else + { + // Set the file download notification text. + fileDownloadNotificationPointer->setText(i18nc("Download progress notification text", "%1: %2 bytes downloaded", saveFileName, receivedBytes)); + } - // Set the file download notification text. - fileDownloadNotificationPointer->setText(i18nc("Download progress notification text", "%1%% of %2 downloaded (%3 of %4 bytes)", downloadPercentage, saveFileName, - receivedBytes, totalBytes)); - } - else - { - // Set the file download notification text. - fileDownloadNotificationPointer->setText(i18nc("Download progress notification text", "%1: %2 bytes downloaded", saveFileName, receivedBytes)); + // Display the updated notification. + fileDownloadNotificationPointer->sendEvent(); } - - // Display the updated notification. - fileDownloadNotificationPointer->sendEvent(); }); // Update the notification when the download finishes. The save file name must be copied into the lambda or a crash occurs. @@ -1105,12 +1106,6 @@ void TabWidget::showSaveDialog(QWebEngineDownloadRequest *webEngineDownloadReque fileDownloadNotificationPointer->sendEvent(); } }); - - // Display the notification. - fileDownloadNotificationPointer->sendEvent(); - - // Start the download. - webEngineDownloadRequestPointer->accept(); } else // The file save path is not populated. { -- 2.53.0