| 1 | /* |
| 2 | This file is part of the KDE Frameworks |
| 3 | SPDX-FileCopyrightText: 2020 Kai Uwe Broulik <kde@broulik.de> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #include "knotificationjobuidelegate.h" |
| 9 | |
| 10 | #include <QString> |
| 11 | |
| 12 | #include <KJob> |
| 13 | #include <KNotification> |
| 14 | |
| 15 | class KNotificationJobUiDelegatePrivate |
| 16 | { |
| 17 | public: |
| 18 | void showNotification(KNotification::StandardEvent standardEvent, const QString &text); |
| 19 | |
| 20 | QString description; |
| 21 | }; |
| 22 | |
| 23 | void KNotificationJobUiDelegatePrivate::showNotification(KNotification::StandardEvent standardEvent, const QString &text) |
| 24 | { |
| 25 | QString title = description; |
| 26 | if (standardEvent == KNotification::Error && !title.isEmpty()) { |
| 27 | //: Job name, e.g. Copying has failed |
| 28 | title = KNotificationJobUiDelegate::tr(s: "%1 (Failed)" ).arg(a: title); |
| 29 | } |
| 30 | KNotification::event(eventId: standardEvent, title, text); |
| 31 | } |
| 32 | |
| 33 | KNotificationJobUiDelegate::KNotificationJobUiDelegate(KJobUiDelegate::Flags flags) |
| 34 | : KJobUiDelegate(flags) |
| 35 | , d(new KNotificationJobUiDelegatePrivate) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | KNotificationJobUiDelegate::~KNotificationJobUiDelegate() = default; |
| 40 | |
| 41 | bool KNotificationJobUiDelegate::setJob(KJob *job) |
| 42 | { |
| 43 | const bool ok = KJobUiDelegate::setJob(job); |
| 44 | |
| 45 | if (ok) { |
| 46 | connect(sender: job, signal: &KJob::description, context: this, slot: [this](KJob *, const QString &title, const QPair<QString, QString> &, const QPair<QString, QString> &) { |
| 47 | d->description = title; |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | return ok; |
| 52 | } |
| 53 | |
| 54 | void KNotificationJobUiDelegate::showErrorMessage() |
| 55 | { |
| 56 | if (job()->error() == KJob::KilledJobError) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | d->showNotification(standardEvent: KNotification::Error, text: job()->errorString()); |
| 61 | } |
| 62 | |
| 63 | void KNotificationJobUiDelegate::slotWarning(KJob *job, const QString &message) |
| 64 | { |
| 65 | Q_UNUSED(job); |
| 66 | |
| 67 | if (isAutoErrorHandlingEnabled()) { |
| 68 | d->showNotification(standardEvent: KNotification::Notification, text: message); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | #include "moc_knotificationjobuidelegate.cpp" |
| 73 | |