File indexing completed on 2024-04-28 03:56:21

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(KJobUiDelegate::Flags flags)
0034     : KJobUiDelegate(flags)
0035     , d(new KNotificationJobUiDelegatePrivate)
0036 {
0037 }
0038 
0039 KNotificationJobUiDelegate::~KNotificationJobUiDelegate() = default;
0040 
0041 bool KNotificationJobUiDelegate::setJob(KJob *job)
0042 {
0043     const bool ok = KJobUiDelegate::setJob(job);
0044 
0045     if (ok) {
0046         connect(job, &KJob::description, this, [this](KJob *, const QString &title, const QPair<QString, QString> &, const QPair<QString, QString> &) {
0047             d->description = title;
0048         });
0049     }
0050 
0051     return ok;
0052 }
0053 
0054 void KNotificationJobUiDelegate::showErrorMessage()
0055 {
0056     if (job()->error() == KJob::KilledJobError) {
0057         return;
0058     }
0059 
0060     d->showNotification(KNotification::Error, job()->errorString());
0061 }
0062 
0063 void KNotificationJobUiDelegate::slotWarning(KJob *job, const QString &message)
0064 {
0065     Q_UNUSED(job);
0066 
0067     if (isAutoErrorHandlingEnabled()) {
0068         d->showNotification(KNotification::Notification, message);
0069     }
0070 }
0071 
0072 #include "moc_knotificationjobuidelegate.cpp"