File indexing completed on 2024-06-23 05:18:31

0001 /*
0002   SPDX-FileCopyrightText: 2009 Constantin Berzan <exit3219@gmail.com>
0003 
0004   SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "contentjobbase.h"
0008 #include "contentjobbase_p.h"
0009 
0010 #include "messagecomposer_debug.h"
0011 
0012 #include <KMime/Content>
0013 
0014 using namespace MessageComposer;
0015 
0016 void ContentJobBasePrivate::init(QObject *parent)
0017 {
0018     Q_Q(ContentJobBase);
0019     auto parentJob = qobject_cast<ContentJobBase *>(parent);
0020     if (parentJob) {
0021         if (!parentJob->appendSubjob(q)) {
0022             qCWarning(MESSAGECOMPOSER_LOG) << "Impossible to add subjob.";
0023         }
0024     }
0025 }
0026 
0027 void ContentJobBasePrivate::doNextSubjob()
0028 {
0029     Q_Q(ContentJobBase);
0030     if (q->hasSubjobs()) {
0031         q->subjobs().first()->start();
0032     } else {
0033         qCDebug(MESSAGECOMPOSER_LOG) << "Calling process.";
0034         q->process();
0035     }
0036 }
0037 
0038 ContentJobBase::ContentJobBase(QObject *parent)
0039     : JobBase(*new ContentJobBasePrivate(this), parent)
0040 {
0041     Q_D(ContentJobBase);
0042     d->init(parent);
0043 }
0044 
0045 ContentJobBase::ContentJobBase(ContentJobBasePrivate &dd, QObject *parent)
0046     : JobBase(dd, parent)
0047 {
0048     Q_D(ContentJobBase);
0049     d->init(parent);
0050 }
0051 
0052 ContentJobBase::~ContentJobBase() = default;
0053 
0054 void ContentJobBase::start()
0055 {
0056     doStart();
0057 }
0058 
0059 KMime::Content *ContentJobBase::content() const
0060 {
0061     Q_D(const ContentJobBase);
0062     // Q_ASSERT( !hasSubjobs() ); // Finished. // JobBase::hasSubjobs is not const :-/ TODO const_cast??
0063     Q_ASSERT(d->resultContent); // process() should do something.
0064     return d->resultContent;
0065 }
0066 
0067 bool ContentJobBase::appendSubjob(ContentJobBase *job)
0068 {
0069     job->setParent(this);
0070     return KCompositeJob::addSubjob(job);
0071 }
0072 
0073 void ContentJobBase::setExtraContent(KMime::Content *extra)
0074 {
0075     Q_D(ContentJobBase);
0076 
0077     d->extraContent = extra;
0078 }
0079 
0080 KMime::Content *ContentJobBase::extraContent() const
0081 {
0082     Q_D(const ContentJobBase);
0083 
0084     return d->extraContent;
0085 }
0086 
0087 bool ContentJobBase::addSubjob(KJob *job)
0088 {
0089     Q_UNUSED(job)
0090     qCCritical(MESSAGECOMPOSER_LOG) << "Use appendJob() instead.";
0091     Q_ASSERT(false);
0092     return false;
0093 }
0094 
0095 void ContentJobBase::doStart()
0096 {
0097     Q_D(ContentJobBase);
0098     Q_ASSERT(d->resultContent == nullptr && d->subjobContents.isEmpty()); // Not started.
0099     Q_ASSERT(!error()); // Jobs emitting an error in doStart should not call ContentJobBase::doStart().
0100     d->doNextSubjob();
0101 }
0102 
0103 void ContentJobBase::slotResult(KJob *job)
0104 {
0105     Q_D(ContentJobBase);
0106     KCompositeJob::slotResult(job); // Handles errors and removes subjob.
0107     qCDebug(MESSAGECOMPOSER_LOG) << "A subjob finished." << subjobs().count() << "more to go.";
0108     if (error()) {
0109         return;
0110     }
0111 
0112     Q_ASSERT(dynamic_cast<ContentJobBase *>(job));
0113     auto cjob = static_cast<ContentJobBase *>(job);
0114     d->subjobContents.append(cjob->content());
0115     d->doNextSubjob();
0116 }
0117 
0118 #include "moc_contentjobbase.cpp"