Messagelib

contentjobbase.cpp
1 /*
2  SPDX-FileCopyrightText: 2009 Constantin Berzan <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "contentjobbase.h"
8 #include "contentjobbase_p.h"
9 
10 #include "messagecomposer_debug.h"
11 
12 #include <KMime/Content>
13 
14 using namespace MessageComposer;
15 
16 void ContentJobBasePrivate::init(QObject *parent)
17 {
18  Q_Q(ContentJobBase);
19  auto parentJob = qobject_cast<ContentJobBase *>(parent);
20  if (parentJob) {
21  if (!parentJob->appendSubjob(q)) {
22  qCWarning(MESSAGECOMPOSER_LOG) << "Impossible to add subjob.";
23  }
24  }
25 }
26 
27 void ContentJobBasePrivate::doNextSubjob()
28 {
29  Q_Q(ContentJobBase);
30  if (q->hasSubjobs()) {
31  q->subjobs().first()->start();
32  } else {
33  qCDebug(MESSAGECOMPOSER_LOG) << "Calling process.";
34  q->process();
35  }
36 }
37 
38 ContentJobBase::ContentJobBase(QObject *parent)
39  : JobBase(*new ContentJobBasePrivate(this), parent)
40 {
41  Q_D(ContentJobBase);
42  d->init(parent);
43 }
44 
45 ContentJobBase::ContentJobBase(ContentJobBasePrivate &dd, QObject *parent)
46  : JobBase(dd, parent)
47 {
48  Q_D(ContentJobBase);
49  d->init(parent);
50 }
51 
52 ContentJobBase::~ContentJobBase() = default;
53 
54 void ContentJobBase::start()
55 {
56  doStart();
57 }
58 
59 KMime::Content *ContentJobBase::content() const
60 {
61  Q_D(const ContentJobBase);
62  // Q_ASSERT( !hasSubjobs() ); // Finished. // JobBase::hasSubjobs is not const :-/ TODO const_cast??
63  Q_ASSERT(d->resultContent); // process() should do something.
64  return d->resultContent;
65 }
66 
67 bool ContentJobBase::appendSubjob(ContentJobBase *job)
68 {
69  job->setParent(this);
70  return KCompositeJob::addSubjob(job);
71 }
72 
73 void ContentJobBase::setExtraContent(KMime::Content *extra)
74 {
75  Q_D(ContentJobBase);
76 
77  d->extraContent = extra;
78 }
79 
80 KMime::Content *ContentJobBase::extraContent() const
81 {
82  Q_D(const ContentJobBase);
83 
84  return d->extraContent;
85 }
86 
87 bool ContentJobBase::addSubjob(KJob *job)
88 {
89  Q_UNUSED(job)
90  qCCritical(MESSAGECOMPOSER_LOG) << "Use appendJob() instead.";
91  Q_ASSERT(false);
92  return false;
93 }
94 
95 void ContentJobBase::doStart()
96 {
97  Q_D(ContentJobBase);
98  Q_ASSERT(d->resultContent == nullptr && d->subjobContents.isEmpty()); // Not started.
99  Q_ASSERT(!error()); // Jobs emitting an error in doStart should not call ContentJobBase::doStart().
100  d->doNextSubjob();
101 }
102 
103 void ContentJobBase::slotResult(KJob *job)
104 {
105  Q_D(ContentJobBase);
106  KCompositeJob::slotResult(job); // Handles errors and removes subjob.
107  qCDebug(MESSAGECOMPOSER_LOG) << "A subjob finished." << subjobs().count() << "more to go.";
108  if (error()) {
109  return;
110  }
111 
112  Q_ASSERT(dynamic_cast<ContentJobBase *>(job));
113  auto cjob = static_cast<ContentJobBase *>(job);
114  d->subjobContents.append(cjob->content());
115  d->doNextSubjob();
116 }
virtual bool addSubjob(KJob *job)
virtual void slotResult(KJob *job)
void setParent(QObject *parent)
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
A dummy abstract class defining some errors pertaining to the Composer.
Definition: jobbase.h:24
The ContentJobBase class.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Dec 5 2021 23:04:53 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.