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/kmime_content.h>
18 #include <kmime/kmime_headers.h>
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  ~SinglepartJobPrivate()
31  {
32  delete contentTransferEncoding;
33  delete contentType;
34  delete contentID;
35  delete contentDescription;
36  delete contentDisposition;
37  }
38 
39  bool chooseCTE();
40 
41  QByteArray data;
42  KMime::Headers::ContentDescription *contentDescription = nullptr;
44  KMime::Headers::ContentID *contentID = nullptr;
45  KMime::Headers::ContentTransferEncoding *contentTransferEncoding = nullptr;
46  KMime::Headers::ContentType *contentType = nullptr;
47 
48  Q_DECLARE_PUBLIC(SinglepartJob)
49 };
50 
51 bool SinglepartJobPrivate::chooseCTE()
52 {
53  Q_Q(SinglepartJob);
54 
55  auto allowed = KMime::encodingsForData(data);
56 
57  if (!q->globalPart()->is8BitAllowed()) {
58  allowed.removeAll(KMime::Headers::CE8Bit);
59  }
60 
61 #if 0 //TODO signing
62  // In the following cases only QP and Base64 are allowed:
63  // - the buffer will be OpenPGP/MIME signed and it contains trailing
64  // whitespace (cf. RFC 3156)
65  // - a line starts with "From "
66  if ((willBeSigned && cf.hasTrailingWhitespace())
67  || cf.hasLeadingFrom()) {
68  ret.removeAll(DwMime::kCte8bit);
69  ret.removeAll(DwMime::kCte7bit);
70  }
71 #endif
72 
73  if (contentTransferEncoding) {
74  // Specific CTE set. Check that our data fits in it.
75  if (!allowed.contains(contentTransferEncoding->encoding())) {
76  q->setError(JobBase::BugError);
77  q->setErrorText(i18n("%1 Content-Transfer-Encoding cannot correctly encode this message.",
78  KMime::nameForEncoding(contentTransferEncoding->encoding())));
79  return false;
80  // TODO improve error message in case 8bit is requested but not allowed.
81  }
82  } else {
83  // No specific CTE set. Choose the best one.
84  Q_ASSERT(!allowed.isEmpty());
85  contentTransferEncoding = new KMime::Headers::ContentTransferEncoding;
86  contentTransferEncoding->setEncoding(allowed.first());
87  }
88  qCDebug(MESSAGECOMPOSER_LOG) << "Settled on encoding" << KMime::nameForEncoding(contentTransferEncoding->encoding());
89  return true;
90 }
91 
92 SinglepartJob::SinglepartJob(QObject *parent)
93  : ContentJobBase(*new SinglepartJobPrivate(this), parent)
94 {
95 }
96 
97 SinglepartJob::~SinglepartJob()
98 {
99 }
100 
101 QByteArray SinglepartJob::data() const
102 {
103  Q_D(const SinglepartJob);
104  return d->data;
105 }
106 
107 void SinglepartJob::setData(const QByteArray &data)
108 {
109  Q_D(SinglepartJob);
110  d->data = data;
111 }
112 
113 KMime::Headers::ContentDescription *SinglepartJob::contentDescription()
114 {
115  Q_D(SinglepartJob);
116  if (!d->contentDescription) {
117  d->contentDescription = new KMime::Headers::ContentDescription;
118  }
119  return d->contentDescription;
120 }
121 
122 KMime::Headers::ContentDisposition *SinglepartJob::contentDisposition()
123 {
124  Q_D(SinglepartJob);
125  if (!d->contentDisposition) {
126  d->contentDisposition = new KMime::Headers::ContentDisposition;
127  }
128  return d->contentDisposition;
129 }
130 
131 KMime::Headers::ContentID *SinglepartJob::contentID()
132 {
133  Q_D(SinglepartJob);
134  if (!d->contentID) {
135  d->contentID = new KMime::Headers::ContentID;
136  }
137  return d->contentID;
138 }
139 
140 KMime::Headers::ContentTransferEncoding *SinglepartJob::contentTransferEncoding()
141 {
142  Q_D(SinglepartJob);
143  if (!d->contentTransferEncoding) {
144  d->contentTransferEncoding = new KMime::Headers::ContentTransferEncoding;
145  }
146  return d->contentTransferEncoding;
147 }
148 
149 KMime::Headers::ContentType *SinglepartJob::contentType()
150 {
151  Q_D(SinglepartJob);
152  if (!d->contentType) {
153  d->contentType = new KMime::Headers::ContentType;
154  }
155  return d->contentType;
156 }
157 
158 void SinglepartJob::process()
159 {
160  Q_D(SinglepartJob);
161  Q_ASSERT(d->resultContent == nullptr); // Not processed before.
162  d->resultContent = new KMime::Content;
163 
164  if (!d->chooseCTE()) {
165  Q_ASSERT(error());
166  emitResult();
167  return;
168  }
169 
170  // Set headers.
171  if (d->contentDescription) {
172  d->resultContent->setHeader(d->contentDescription);
173  }
174  if (d->contentDisposition) {
175  d->resultContent->setHeader(d->contentDisposition);
176  }
177  if (d->contentID) {
178  d->resultContent->setHeader(d->contentID);
179  }
180  Q_ASSERT(d->contentTransferEncoding); // chooseCTE() created it if it didn't exist.
181  {
182  d->resultContent->setHeader(d->contentTransferEncoding);
183  }
184  if (d->contentType) {
185  d->resultContent->setHeader(d->contentType);
186  }
187 
188  // Set data.
189  d->resultContent->setBody(d->data);
190 
191  emitResult();
192 }
contentDisposition
QString i18n(const char *text, const TYPE &arg...)
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
void setEncoding(contentEncoding e)
The SinglepartJob class.
Definition: singlepartjob.h:29
The ContentJobBase class.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Sep 21 2020 23:14:05 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.