KCoreAddons

kjobuidelegate.cpp
1 /*
2  This file is part of the KDE libraries
3 
4  SPDX-FileCopyrightText: 2000 Stephan Kulow <[email protected]>
5  SPDX-FileCopyrightText: 2000 David Faure <[email protected]>
6  SPDX-FileCopyrightText: 2006 Kevin Ottens <[email protected]>
7 
8  SPDX-License-Identifier: LGPL-2.0-or-later
9 */
10 
11 #include "kjobuidelegate.h"
12 #include "kcoreaddons_debug.h"
13 #include "kjob.h"
14 
15 #include <QDebug>
16 
17 class KJobUiDelegatePrivate
18 {
19 public:
20  KJobUiDelegatePrivate(KJobUiDelegate *delegate)
21  : q(delegate)
22  , autoErrorHandling(false)
23  , autoWarningHandling(true)
24  {
25  }
26 
27  KJobUiDelegate *const q;
28 
29  KJob *job = nullptr;
30  bool autoErrorHandling : 1;
31  bool autoWarningHandling : 1;
32 
33  void connectJob(KJob *job);
34  void _k_result();
35 };
36 
38  : QObject()
39  , d(new KJobUiDelegatePrivate(this))
40 {
41 }
42 
44  : QObject()
45  , d(new KJobUiDelegatePrivate(this))
46 {
47  if (flags & AutoErrorHandlingEnabled) {
48  d->autoErrorHandling = true;
49  }
50  if (flags & AutoWarningHandlingEnabled) {
51  d->autoWarningHandling = true;
52  }
53 }
54 
56 
58 {
59  if (d->job != nullptr) {
60  qCWarning(KCOREADDONS_DEBUG) << "Trying to attach UI delegate:" << this << "to job" << job //
61  << "but this delegate is already attached to a different job" << d->job;
62  return false;
63  }
64 
65  d->job = job;
66  setParent(job);
67 
68  return true;
69 }
70 
72 {
73  return d->job;
74 }
75 
77 {
78  if (d->job->error() != KJob::KilledJobError) {
79  qWarning() << d->job->errorString();
80  }
81 }
82 
84 {
85  d->autoErrorHandling = enable;
86 }
87 
89 {
90  return d->autoErrorHandling;
91 }
92 
94 {
95  d->autoWarningHandling = enable;
96 }
97 
99 {
100  return d->autoWarningHandling;
101 }
102 
103 void KJobUiDelegate::slotWarning(KJob *job, const QString &plain, const QString &rich)
104 {
105  Q_UNUSED(job)
106  Q_UNUSED(plain)
107  Q_UNUSED(rich)
108 }
109 
110 void KJobUiDelegate::connectJob(KJob *job)
111 {
112  connect(job, &KJob::result, this, [this]() {
113  d->_k_result();
114  });
115  connect(job, &KJob::warning, this, &KJobUiDelegate::slotWarning);
116 }
117 
118 void KJobUiDelegatePrivate::_k_result()
119 {
120  if (job->error() && autoErrorHandling) {
121  q->showErrorMessage();
122  }
123 }
124 
125 #include "moc_kjobuidelegate.cpp"
void setAutoWarningHandlingEnabled(bool enable)
Enable or disable the automatic warning handling.
void result(KJob *job)
Emitted when the job is finished (except when killed with KJob::Quietly).
bool isAutoErrorHandlingEnabled() const
Returns whether automatic error handling is enabled or disabled.
virtual void showErrorMessage()
Display to the user the error given by this job.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void warning(KJob *job, const QString &plain, const QString &rich=QString())
Emitted to display a warning about this job.
~KJobUiDelegate() override
Destroys a KJobUiDelegate.
Definition: kjob.h:73
virtual bool setJob(KJob *job)
Attach this UI delegate to a job.
@ AutoErrorHandlingEnabled
Equivalent to setAutoErrorHandlingEnabled(true)
@ AutoWarningHandlingEnabled
Equivalent to setAutoWarningHandlingEnabled(true)
KJob * job() const
Retrieves the current job this UI delegate is attached to.
void setParent(QObject *parent)
KJobUiDelegate()
Constructs a new KJobUiDelegate.
bool isAutoWarningHandlingEnabled() const
Returns whether automatic warning handling is enabled or disabled.
void setAutoErrorHandlingEnabled(bool enable)
Enable or disable the automatic error handling.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Sep 26 2023 04:03:07 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.