/* SPDX-License-Identifier: GPL-3.0-or-later
- * SPDX-FileCopyrightText: 2022-2025 Soren Stoutner <soren@stoutner.com>
+ * SPDX-FileCopyrightText: 2022-2026 Soren Stoutner <soren@stoutner.com>
*
* This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc/>.
*
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")));
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();
// 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();
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.
fileDownloadNotificationPointer->sendEvent();
}
});
-
- // Display the notification.
- fileDownloadNotificationPointer->sendEvent();
-
- // Start the download.
- webEngineDownloadRequestPointer->accept();
}
else // The file save path is not populated.
{