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

KDE's Doxygen guidelines are available online.