1/*
2 This file is part of the KDE libraries
3
4 SPDX-FileCopyrightText: 2000 Stephan Kulow <coolo@kde.org>
5 SPDX-FileCopyrightText: 2000 David Faure <faure@kde.org>
6 SPDX-FileCopyrightText: 2006 Kevin Ottens <ervin@kde.org>
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
17class KJobUiDelegatePrivate
18{
19public:
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
37KJobUiDelegate::KJobUiDelegate(Flags flags)
38 : QObject()
39 , d(new KJobUiDelegatePrivate(this))
40{
41 if (flags & AutoErrorHandlingEnabled) {
42 d->autoErrorHandling = true;
43 }
44 if (flags & AutoWarningHandlingEnabled) {
45 d->autoWarningHandling = true;
46 }
47}
48
49KJobUiDelegate::~KJobUiDelegate() = default;
50
51bool KJobUiDelegate::setJob(KJob *job)
52{
53 if (d->job != nullptr) {
54 qCWarning(KCOREADDONS_DEBUG) << "Trying to attach UI delegate:" << this << "to job" << job //
55 << "but this delegate is already attached to a different job" << d->job;
56 return false;
57 }
58
59 d->job = job;
60 setParent(job);
61
62 return true;
63}
64
65KJob *KJobUiDelegate::job() const
66{
67 return d->job;
68}
69
70void KJobUiDelegate::showErrorMessage()
71{
72 if (d->job->error() != KJob::KilledJobError) {
73 qWarning() << d->job->errorString();
74 }
75}
76
77void KJobUiDelegate::setAutoErrorHandlingEnabled(bool enable)
78{
79 d->autoErrorHandling = enable;
80}
81
82bool KJobUiDelegate::isAutoErrorHandlingEnabled() const
83{
84 return d->autoErrorHandling;
85}
86
87void KJobUiDelegate::setAutoWarningHandlingEnabled(bool enable)
88{
89 d->autoWarningHandling = enable;
90}
91
92bool KJobUiDelegate::isAutoWarningHandlingEnabled() const
93{
94 return d->autoWarningHandling;
95}
96
97void KJobUiDelegate::slotWarning(KJob *job, const QString &message)
98{
99 Q_UNUSED(job)
100 Q_UNUSED(message)
101}
102
103void KJobUiDelegate::connectJob(KJob *job)
104{
105 connect(sender: job, signal: &KJob::result, context: this, slot: [this]() {
106 d->_k_result();
107 });
108 connect(sender: job, signal: &KJob::warning, context: this, slot: &KJobUiDelegate::slotWarning);
109}
110
111void KJobUiDelegatePrivate::_k_result()
112{
113 if (job->error() && autoErrorHandling) {
114 q->showErrorMessage();
115 }
116}
117
118#include "moc_kjobuidelegate.cpp"
119

source code of kcoreaddons/src/lib/jobs/kjobuidelegate.cpp