1/*
2 This file is part of the KDE project
3
4 SPDX-FileCopyrightText: 2006 Kevin Ottens <ervin@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "kcompositejob.h"
10#include "kcompositejob_p.h"
11
12KCompositeJobPrivate::KCompositeJobPrivate()
13{
14}
15
16KCompositeJobPrivate::~KCompositeJobPrivate()
17{
18}
19
20KCompositeJob::KCompositeJob(QObject *parent)
21 : KJob(*new KCompositeJobPrivate, parent)
22{
23}
24
25KCompositeJob::KCompositeJob(KCompositeJobPrivate &dd, QObject *parent)
26 : KJob(dd, parent)
27{
28}
29
30KCompositeJob::~KCompositeJob()
31{
32}
33
34bool KCompositeJob::addSubjob(KJob *job)
35{
36 Q_D(KCompositeJob);
37 if (job == nullptr || d->subjobs.contains(t: job)) {
38 return false;
39 }
40
41 job->setParent(this);
42 d->subjobs.append(t: job);
43 connect(sender: job, signal: &KJob::result, context: this, slot: &KCompositeJob::slotResult);
44
45 // Forward information from that subjob.
46 connect(sender: job, signal: &KJob::infoMessage, context: this, slot: &KCompositeJob::slotInfoMessage);
47
48 return true;
49}
50
51bool KCompositeJob::removeSubjob(KJob *job)
52{
53 Q_D(KCompositeJob);
54 // remove only Subjobs that are on the list
55 if (d->subjobs.removeAll(t: job) > 0) {
56 job->setParent(nullptr);
57 disconnect(sender: job, signal: &KJob::result, receiver: this, slot: &KCompositeJob::slotResult);
58 disconnect(sender: job, signal: &KJob::infoMessage, receiver: this, slot: &KCompositeJob::slotInfoMessage);
59 return true;
60 }
61 return false;
62}
63
64bool KCompositeJob::hasSubjobs() const
65{
66 return !d_func()->subjobs.isEmpty();
67}
68
69const QList<KJob *> &KCompositeJob::subjobs() const
70{
71 return d_func()->subjobs;
72}
73
74void KCompositeJob::clearSubjobs()
75{
76 Q_D(KCompositeJob);
77 for (KJob *job : std::as_const(t&: d->subjobs)) {
78 job->setParent(nullptr);
79 disconnect(sender: job, signal: &KJob::result, receiver: this, slot: &KCompositeJob::slotResult);
80 disconnect(sender: job, signal: &KJob::infoMessage, receiver: this, slot: &KCompositeJob::slotInfoMessage);
81 }
82 d->subjobs.clear();
83}
84
85void KCompositeJob::slotResult(KJob *job)
86{
87 // Did job have an error ?
88 if (job->error() && !error()) {
89 // Store it in the parent only if first error
90 setError(job->error());
91 setErrorText(job->errorText());
92 // Finish this job
93 emitResult();
94 }
95 // After a subjob is done, we might want to start another one.
96 // Therefore do not emitResult
97 removeSubjob(job);
98}
99
100void KCompositeJob::slotInfoMessage(KJob *job, const QString &message)
101{
102 Q_EMIT infoMessage(job, message);
103}
104
105#include "moc_kcompositejob.cpp"
106

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