File indexing completed on 2024-04-28 15:29:09

0001 /*
0002     This file is part of the KDE Frameworks
0003     SPDX-FileCopyrightText: 2020 Kai Uwe Broulik <kde@broulik.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "knotificationjobuidelegate.h"
0009 
0010 #include <QString>
0011 
0012 #include <KJob>
0013 #include <KNotification>
0014 
0015 class KNotificationJobUiDelegatePrivate
0016 {
0017 public:
0018     void showNotification(KNotification::StandardEvent standardEvent, const QString &text);
0019 
0020     QString description;
0021 };
0022 
0023 void KNotificationJobUiDelegatePrivate::showNotification(KNotification::StandardEvent standardEvent, const QString &text)
0024 {
0025     QString title = description;
0026     if (standardEvent == KNotification::Error && !title.isEmpty()) {
0027         //: Job name, e.g. Copying has failed
0028         title = KNotificationJobUiDelegate::tr("%1 (Failed)").arg(title);
0029     }
0030     KNotification::event(standardEvent, title, text);
0031 }
0032 
0033 KNotificationJobUiDelegate::KNotificationJobUiDelegate()
0034     : KJobUiDelegate()
0035     , d(new KNotificationJobUiDelegatePrivate)
0036 {
0037 }
0038 
0039 KNotificationJobUiDelegate::KNotificationJobUiDelegate(KJobUiDelegate::Flags flags)
0040     : KJobUiDelegate(flags)
0041     , d(new KNotificationJobUiDelegatePrivate)
0042 {
0043 }
0044 
0045 KNotificationJobUiDelegate::~KNotificationJobUiDelegate() = default;
0046 
0047 bool KNotificationJobUiDelegate::setJob(KJob *job)
0048 {
0049     const bool ok = KJobUiDelegate::setJob(job);
0050 
0051     if (ok) {
0052         connect(job, &KJob::description, this, [this](KJob *, const QString &title, const QPair<QString, QString> &, const QPair<QString, QString> &) {
0053             d->description = title;
0054         });
0055     }
0056 
0057     return ok;
0058 }
0059 
0060 void KNotificationJobUiDelegate::showErrorMessage()
0061 {
0062     if (job()->error() == KJob::KilledJobError) {
0063         return;
0064     }
0065 
0066     d->showNotification(KNotification::Error, job()->errorString());
0067 }
0068 
0069 void KNotificationJobUiDelegate::slotWarning(KJob *job, const QString &plain, const QString &rich)
0070 {
0071     Q_UNUSED(job);
0072     Q_UNUSED(rich);
0073 
0074     if (isAutoErrorHandlingEnabled()) {
0075         d->showNotification(KNotification::Notification, plain);
0076     }
0077 }
0078 
0079 #include "moc_knotificationjobuidelegate.cpp"