Messagelib

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

KDE's Doxygen guidelines are available online.