File indexing completed on 2024-05-19 04:39:57

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 "kcompoundjob.h"
0010 #include "kcompoundjob_p.h"
0011 
0012 using namespace KDevCoreAddons;
0013 
0014 KCompoundJobPrivate::KCompoundJobPrivate() = default;
0015 KCompoundJobPrivate::~KCompoundJobPrivate() = default;
0016 
0017 void KCompoundJobPrivate::disconnectSubjob(KJob *job)
0018 {
0019     Q_Q(KCompoundJob);
0020     job->setParent(nullptr);
0021     QObject::disconnect(job, &KJob::finished, q, &KCompoundJob::subjobFinished);
0022     QObject::disconnect(job, &KJob::infoMessage, q, &KCompoundJob::subjobInfoMessage);
0023 }
0024 
0025 KCompoundJob::KCompoundJob(QObject *parent)
0026     : KCompoundJob(*new KCompoundJobPrivate, parent)
0027 {
0028 }
0029 
0030 KCompoundJob::KCompoundJob(KCompoundJobPrivate &dd, QObject *parent)
0031     : KJob(parent)
0032     , d_ptr(&dd)
0033 {
0034     d_ptr->q_ptr = this;
0035 }
0036 
0037 KCompoundJob::~KCompoundJob() = default;
0038 
0039 bool KCompoundJob::addSubjob(KJob *job)
0040 {
0041     Q_D(KCompoundJob);
0042     if (job == nullptr || d->m_subjobs.contains(job)) {
0043         return false;
0044     }
0045 
0046     job->setParent(this);
0047     d->m_subjobs.append(job);
0048     connect(job, &KJob::finished, this, &KCompoundJob::subjobFinished);
0049 
0050     // Forward information from that subjob.
0051     connect(job, &KJob::infoMessage, this, &KCompoundJob::subjobInfoMessage);
0052 
0053     return true;
0054 }
0055 
0056 bool KCompoundJob::removeSubjob(KJob *job)
0057 {
0058     Q_D(KCompoundJob);
0059     // remove only Subjobs that are on the list
0060     if (d->m_subjobs.removeAll(job) > 0) {
0061         d->disconnectSubjob(job);
0062         return true;
0063     }
0064     return false;
0065 }
0066 
0067 bool KCompoundJob::hasSubjobs() const
0068 {
0069     return !d_func()->m_subjobs.isEmpty();
0070 }
0071 
0072 const QList<KJob *> &KCompoundJob::subjobs() const
0073 {
0074     return d_func()->m_subjobs;
0075 }
0076 
0077 void KCompoundJob::clearSubjobs()
0078 {
0079     Q_D(KCompoundJob);
0080     for (KJob *job : std::as_const(d->m_subjobs)) {
0081         d->disconnectSubjob(job);
0082     }
0083     d->m_subjobs.clear();
0084 }
0085 
0086 void KCompoundJob::subjobFinished(KJob *job)
0087 {
0088     // Did job have an error ?
0089     if (job->error() && !isFinished() && !error()) {
0090         // Store it in the parent only if first error
0091         setError(job->error());
0092         setErrorText(job->errorText());
0093         // Finish this job
0094         emitResult();
0095     }
0096     // After a subjob is done, we might want to start another one.
0097     // Therefore do not emitResult
0098     removeSubjob(job);
0099 }
0100 
0101 void KCompoundJob::subjobInfoMessage(KJob *job, const QString &plain, const QString &rich)
0102 {
0103     Q_EMIT infoMessage(job, plain, rich);
0104 }
0105 
0106 #include "moc_kcompoundjob.cpp"