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 
KCompositeJobPrivate()12 KCompositeJobPrivate::KCompositeJobPrivate()
13 {
14 }
15 
~KCompositeJobPrivate()16 KCompositeJobPrivate::~KCompositeJobPrivate()
17 {
18 }
19 
KCompositeJob(QObject * parent)20 KCompositeJob::KCompositeJob(QObject *parent)
21     : KJob(*new KCompositeJobPrivate, parent)
22 {
23 }
24 
KCompositeJob(KCompositeJobPrivate & dd,QObject * parent)25 KCompositeJob::KCompositeJob(KCompositeJobPrivate &dd, QObject *parent)
26     : KJob(dd, parent)
27 {
28 }
29 
~KCompositeJob()30 KCompositeJob::~KCompositeJob()
31 {
32 }
33 
addSubjob(KJob * job)34 bool KCompositeJob::addSubjob(KJob *job)
35 {
36     Q_D(KCompositeJob);
37     if (job == nullptr || d->subjobs.contains(job)) {
38         return false;
39     }
40 
41     job->setParent(this);
42     d->subjobs.append(job);
43     connect(job, &KJob::result, this, &KCompositeJob::slotResult);
44 
45     // Forward information from that subjob.
46     connect(job, &KJob::infoMessage, this, &KCompositeJob::slotInfoMessage);
47 
48     return true;
49 }
50 
removeSubjob(KJob * job)51 bool KCompositeJob::removeSubjob(KJob *job)
52 {
53     Q_D(KCompositeJob);
54     // remove only Subjobs that are on the list
55     if (d->subjobs.removeAll(job) > 0) {
56         job->setParent(nullptr);
57         disconnect(job, &KJob::result, this, &KCompositeJob::slotResult);
58         disconnect(job, &KJob::infoMessage, this, &KCompositeJob::slotInfoMessage);
59         return true;
60     }
61     return false;
62 }
63 
hasSubjobs() const64 bool KCompositeJob::hasSubjobs() const
65 {
66     return !d_func()->subjobs.isEmpty();
67 }
68 
subjobs() const69 const QList<KJob *> &KCompositeJob::subjobs() const
70 {
71     return d_func()->subjobs;
72 }
73 
clearSubjobs()74 void KCompositeJob::clearSubjobs()
75 {
76     Q_D(KCompositeJob);
77     for (KJob *job : std::as_const(d->subjobs)) {
78         job->setParent(nullptr);
79         disconnect(job, &KJob::result, this, &KCompositeJob::slotResult);
80         disconnect(job, &KJob::infoMessage, this, &KCompositeJob::slotInfoMessage);
81     }
82     d->subjobs.clear();
83 }
84 
slotResult(KJob * job)85 void 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 
slotInfoMessage(KJob * job,const QString & plain,const QString & rich)100 void KCompositeJob::slotInfoMessage(KJob *job, const QString &plain, const QString &rich)
101 {
102     Q_EMIT infoMessage(job, plain, rich);
103 }
104 
105 #include "moc_kcompositejob.cpp"
106