KNotifications

knotificationjobuidelegate.cpp
1 /*
2  This file is part of the KDE Frameworks
3  SPDX-FileCopyrightText: 2020 Kai Uwe Broulik <[email protected]>
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("%1 (Failed)").arg(title);
29  }
30  KNotification::event(standardEvent, title, text);
31 }
32 
34  : KJobUiDelegate()
35  , d(new KNotificationJobUiDelegatePrivate)
36 {
37 }
38 
40  : KJobUiDelegate(flags)
41  , d(new KNotificationJobUiDelegatePrivate)
42 {
43 }
44 
46 
47 bool KNotificationJobUiDelegate::setJob(KJob *job)
48 {
49  const bool ok = KJobUiDelegate::setJob(job);
50 
51  if (ok) {
52  connect(job, &KJob::description, this, [this](KJob *, const QString &title, const QPair<QString, QString> &, const QPair<QString, QString> &) {
53  d->description = title;
54  });
55  }
56 
57  return ok;
58 }
59 
61 {
62  if (job()->error() == KJob::KilledJobError) {
63  return;
64  }
65 
66  d->showNotification(KNotification::Error, job()->errorString());
67 }
68 
69 void KNotificationJobUiDelegate::slotWarning(KJob *job, const QString &plain, const QString &rich)
70 {
71  Q_UNUSED(job);
72  Q_UNUSED(rich);
73 
75  d->showNotification(KNotification::Notification, plain);
76  }
77 }
KNotificationJobUiDelegate()
Constructs a new KNotificationJobUiDelegate.
bool isAutoErrorHandlingEnabled() const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void showErrorMessage() override
Display a notification to inform the user of the error given by this job.
bool isEmpty() const const
virtual bool setJob(KJob *job)
void description(KJob *job, const QString &title, const QPair< QString, QString > &field1=QPair< QString, QString >(), const QPair< QString, QString > &field2=QPair< QString, QString >())
KJob * job() const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
~KNotificationJobUiDelegate() override
Destroys the KNotificationJobUiDelegate.
QString tr(const char *sourceText, const char *disambiguation, int n)
static KNotification * event(const QString &eventId, const QString &title, const QString &text, const QPixmap &pixmap=QPixmap(), QWidget *widget=nullptr, const NotificationFlags &flags=CloseOnTimeout, const QString &componentName=QString())
emit an event
StandardEvent
default events you can use in the event function
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon May 8 2023 03:49:15 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.