Messagelib

singlepartjob.cpp
1 /*
2  SPDX-FileCopyrightText: 2009 Constantin Berzan <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "job/singlepartjob.h"
8 
9 #include "composer/composer.h"
10 #include "contentjobbase_p.h"
11 #include "part/globalpart.h"
12 #include "utils/util.h"
13 
14 #include "messagecomposer_debug.h"
15 #include <KLocalizedString>
16 
17 #include <KMime/Content>
18 #include <KMime/Headers>
19 
20 using namespace MessageComposer;
21 
22 class MessageComposer::SinglepartJobPrivate : public ContentJobBasePrivate
23 {
24 public:
25  SinglepartJobPrivate(SinglepartJob *qq)
26  : ContentJobBasePrivate(qq)
27  {
28  }
29 
30  bool chooseCTE();
31 
32  QByteArray data;
33  KMime::Headers::ContentDescription *contentDescription = nullptr;
35  KMime::Headers::ContentID *contentID = nullptr;
36  KMime::Headers::ContentTransferEncoding *contentTransferEncoding = nullptr;
37  KMime::Headers::ContentType *contentType = nullptr;
38 
39  Q_DECLARE_PUBLIC(SinglepartJob)
40 };
41 
42 bool SinglepartJobPrivate::chooseCTE()
43 {
44  Q_Q(SinglepartJob);
45 
46  auto allowed = KMime::encodingsForData(data);
47 
48  if (!q->globalPart()->is8BitAllowed()) {
49  allowed.removeAll(KMime::Headers::CE8Bit);
50  }
51 
52 #if 0 // TODO signing
53  // In the following cases only QP and Base64 are allowed:
54  // - the buffer will be OpenPGP/MIME signed and it contains trailing
55  // whitespace (cf. RFC 3156)
56  // - a line starts with "From "
57  if ((willBeSigned && cf.hasTrailingWhitespace())
58  || cf.hasLeadingFrom()) {
59  ret.removeAll(DwMime::kCte8bit);
60  ret.removeAll(DwMime::kCte7bit);
61  }
62 #endif
63 
64  if (contentTransferEncoding) {
65  // Specific CTE set. Check that our data fits in it.
66  if (!allowed.contains(contentTransferEncoding->encoding())) {
67  q->setError(JobBase::BugError);
68  q->setErrorText(
69  i18n("%1 Content-Transfer-Encoding cannot correctly encode this message.", KMime::nameForEncoding(contentTransferEncoding->encoding())));
70  return false;
71  // TODO improve error message in case 8bit is requested but not allowed.
72  }
73  } else {
74  // No specific CTE set. Choose the best one.
75  Q_ASSERT(!allowed.isEmpty());
76  contentTransferEncoding = new KMime::Headers::ContentTransferEncoding;
77  contentTransferEncoding->setEncoding(allowed.first());
78  }
79  qCDebug(MESSAGECOMPOSER_LOG) << "Settled on encoding" << KMime::nameForEncoding(contentTransferEncoding->encoding());
80  return true;
81 }
82 
83 SinglepartJob::SinglepartJob(QObject *parent)
84  : ContentJobBase(*new SinglepartJobPrivate(this), parent)
85 {
86 }
87 
88 SinglepartJob::~SinglepartJob() = default;
89 
90 QByteArray SinglepartJob::data() const
91 {
92  Q_D(const SinglepartJob);
93  return d->data;
94 }
95 
96 void SinglepartJob::setData(const QByteArray &data)
97 {
99  d->data = data;
100 }
101 
102 KMime::Headers::ContentDescription *SinglepartJob::contentDescription()
103 {
105  if (!d->contentDescription) {
106  d->contentDescription = new KMime::Headers::ContentDescription;
107  }
108  return d->contentDescription;
109 }
110 
111 KMime::Headers::ContentDisposition *SinglepartJob::contentDisposition()
112 {
114  if (!d->contentDisposition) {
115  d->contentDisposition = new KMime::Headers::ContentDisposition;
116  }
117  return d->contentDisposition;
118 }
119 
120 KMime::Headers::ContentID *SinglepartJob::contentID()
121 {
123  if (!d->contentID) {
124  d->contentID = new KMime::Headers::ContentID;
125  }
126  return d->contentID;
127 }
128 
129 KMime::Headers::ContentTransferEncoding *SinglepartJob::contentTransferEncoding()
130 {
132  if (!d->contentTransferEncoding) {
133  d->contentTransferEncoding = new KMime::Headers::ContentTransferEncoding;
134  }
135  return d->contentTransferEncoding;
136 }
137 
138 KMime::Headers::ContentType *SinglepartJob::contentType()
139 {
141  if (!d->contentType) {
142  d->contentType = new KMime::Headers::ContentType;
143  }
144  return d->contentType;
145 }
146 
147 void SinglepartJob::process()
148 {
150  Q_ASSERT(d->resultContent == nullptr); // Not processed before.
151  d->resultContent = new KMime::Content;
152 
153  if (!d->chooseCTE()) {
154  Q_ASSERT(error());
155  emitResult();
156  return;
157  }
158 
159  // Set headers.
160  if (d->contentDescription) {
161  d->resultContent->setHeader(d->contentDescription);
162  }
163  if (d->contentDisposition) {
164  d->resultContent->setHeader(d->contentDisposition);
165  }
166  if (d->contentID) {
167  d->resultContent->setHeader(d->contentID);
168  }
169  Q_ASSERT(d->contentTransferEncoding); // chooseCTE() created it if it didn't exist.
170  {
171  d->resultContent->setHeader(d->contentTransferEncoding);
172  }
173  if (d->contentType) {
174  d->resultContent->setHeader(d->contentType);
175  }
176 
177  // Set data.
178  d->resultContent->setBody(d->data);
179 
180  emitResult();
181 }
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
The SinglepartJob class.
Definition: singlepartjob.h:31
QString i18n(const char *text, const TYPE &arg...)
void setEncoding(contentEncoding e)
contentDisposition
The ContentJobBase class.
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat May 21 2022 03:53:37 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.