File indexing completed on 2024-12-29 05:06:01

0001 /*
0002  * SPDX-FileCopyrightText: 2021 Devin Lin <devin@kde.org>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 .import org.kde.notificationmanager as NotificationManager
0008 .import QtQml as QtQml
0009 
0010 function determineNotificationHeadingText(notificationItem) {
0011     if (notificationItem.notificationType === NotificationManager.Notifications.JobType) {
0012         if (notificationItem.jobState === NotificationManager.Notifications.JobStateSuspended) {
0013             if (notificationItem.summary) {
0014                 return i18nc("Job name, e.g. Copying is paused", "%1 (Paused)", notificationItem.summary);
0015             }
0016         } else if (notificationItem.jobState === NotificationManager.Notifications.JobStateStopped) {
0017             if (notificationItem.jobError) {
0018                 if (notificationItem.summary) {
0019                     return i18nc("Job name, e.g. Copying has failed", "%1 (Failed)", notificationItem.summary);
0020                 } else {
0021                     return i18n("Job Failed");
0022                 }
0023             } else if (notificationItem.summary) {
0024                 return i18ndc("plasma_applet_org.kde.plasma.notifications", "Job name, e.g. Copying has finished", "%1 (Finished)", notificationItem.summary);
0025             }
0026             return i18nd("plasma_applet_org.kde.plasma.notifications", "Job Finished");
0027         }
0028     }
0029     // some apps use their app name as summary, avoid showing the same text twice
0030     // try very hard to match the two
0031     if (notificationItem.summary && notificationItem.summary.toLocaleLowerCase().trim() !== notificationItem.applicationName.toLocaleLowerCase().trim()) {
0032         return notificationItem.summary;
0033     }
0034     return "";
0035 }
0036 
0037 function generateNotificationHeaderAgoText(time, jobState) {
0038     if (!time || isNaN(time.getTime()) || jobState === NotificationManager.Notifications.JobStateRunning) {
0039         return "";
0040     }
0041 
0042     const deltaMinutes = Math.floor((Date.now() - time.getTime()) / 1000 / 60);
0043     if (deltaMinutes < 1) {
0044         return "";
0045     }
0046 
0047     // Received less than an hour ago, show relative minutes
0048     if (deltaMinutes < 60) {
0049         return i18nc("Notification was added minutes ago, keep short", "%1m ago", deltaMinutes);
0050     }
0051     // Received less than a day ago, show time, 22 hours so the time isn't as ambiguous between today and yesterday
0052     if (deltaMinutes < 60 * 22) {
0053         return Qt.formatTime(time, Qt.locale().timeFormat(QtQml.Locale.ShortFormat).replace(/.ss?/i, ""));
0054     }
0055 
0056     // Otherwise show relative date (Yesterday, "Last Sunday", or just date if too far in the past)
0057     return KCoreAddons.Format.formatRelativeDate(time, QtQml.Locale.ShortFormat);
0058 }
0059 
0060 function generateNotificationHeaderRemainingText(notificationType, jobState, jobDetails) {
0061     if (notificationType !== NotificationManager.Notifications.JobType || jobState !== NotificationManager.Notifications.JobStateRunning) {
0062         return "";
0063     }
0064 
0065     const details = jobDetails;
0066     if (!details || !details.speed) {
0067         return "";
0068     }
0069 
0070     var remaining = details.totalBytes - details.processedBytes;
0071     if (remaining <= 0) {
0072         return "";
0073     }
0074 
0075     var eta = remaining / details.speed;
0076     if (eta < 0.5) { // Avoid showing "0 seconds remaining"
0077         return "";
0078     }
0079 
0080     if (eta < 60) { // 1 minute
0081         return i18nc("seconds remaining, keep short", "%1 s remaining", Math.round(eta));
0082     }
0083     if (eta < 60 * 60) {// 1 hour
0084         return i18nc("minutes remaining, keep short", "%1m remaining", Math.round(eta / 60));
0085     }
0086     if (eta < 60 * 60 * 5) { // 5 hours max, if it takes even longer there's no real point in showing that
0087         return i18nc("hours remaining, keep short", "%1h remaining", Math.round(eta / 60 / 60));
0088     }
0089 
0090     return "";
0091 }