File indexing completed on 2024-05-19 03:56:21

0001 /*
0002     This file is part of the KDE project
0003 
0004     SPDX-FileCopyrightText: 2006 Kevin Ottens <ervin@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "kcompositejob.h"
0010 #include "kcompositejob_p.h"
0011 
0012 KCompositeJobPrivate::KCompositeJobPrivate()
0013 {
0014 }
0015 
0016 KCompositeJobPrivate::~KCompositeJobPrivate()
0017 {
0018 }
0019 
0020 KCompositeJob::KCompositeJob(QObject *parent)
0021     : KJob(*new KCompositeJobPrivate, parent)
0022 {
0023 }
0024 
0025 KCompositeJob::KCompositeJob(KCompositeJobPrivate &dd, QObject *parent)
0026     : KJob(dd, parent)
0027 {
0028 }
0029 
0030 KCompositeJob::~KCompositeJob()
0031 {
0032 }
0033 
0034 bool KCompositeJob::addSubjob(KJob *job)
0035 {
0036     Q_D(KCompositeJob);
0037     if (job == nullptr || d->subjobs.contains(job)) {
0038         return false;
0039     }
0040 
0041     job->setParent(this);
0042     d->subjobs.append(job);
0043     connect(job, &KJob::result, this, &KCompositeJob::slotResult);
0044 
0045     // Forward information from that subjob.
0046     connect(job, &KJob::infoMessage, this, &KCompositeJob::slotInfoMessage);
0047 
0048     return true;
0049 }
0050 
0051 bool KCompositeJob::removeSubjob(KJob *job)
0052 {
0053     Q_D(KCompositeJob);
0054     // remove only Subjobs that are on the list
0055     if (d->subjobs.removeAll(job) > 0) {
0056         job->setParent(nullptr);
0057         disconnect(job, &KJob::result, this, &KCompositeJob::slotResult);
0058         disconnect(job, &KJob::infoMessage, this, &KCompositeJob::slotInfoMessage);
0059         return true;
0060     }
0061     return false;
0062 }
0063 
0064 bool KCompositeJob::hasSubjobs() const
0065 {
0066     return !d_func()->subjobs.isEmpty();
0067 }
0068 
0069 const QList<KJob *> &KCompositeJob::subjobs() const
0070 {
0071     return d_func()->subjobs;
0072 }
0073 
0074 void KCompositeJob::clearSubjobs()
0075 {
0076     Q_D(KCompositeJob);
0077     for (KJob *job : std::as_const(d->subjobs)) {
0078         job->setParent(nullptr);
0079         disconnect(job, &KJob::result, this, &KCompositeJob::slotResult);
0080         disconnect(job, &KJob::infoMessage, this, &KCompositeJob::slotInfoMessage);
0081     }
0082     d->subjobs.clear();
0083 }
0084 
0085 void KCompositeJob::slotResult(KJob *job)
0086 {
0087     // Did job have an error ?
0088     if (job->error() && !error()) {
0089         // Store it in the parent only if first error
0090         setError(job->error());
0091         setErrorText(job->errorText());
0092         // Finish this job
0093         emitResult();
0094     }
0095     // After a subjob is done, we might want to start another one.
0096     // Therefore do not emitResult
0097     removeSubjob(job);
0098 }
0099 
0100 void KCompositeJob::slotInfoMessage(KJob *job, const QString &message)
0101 {
0102     Q_EMIT infoMessage(job, message);
0103 }
0104 
0105 #include "moc_kcompositejob.cpp"