Messagelib

attachmentpart.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 "attachmentpart.h"
8
9#include <KMime/Content>
10#include <KMime/Util>
11
12#include <QUrl>
13
14using namespace MessageCore;
15
16// TODO move to kmime_util?
17static qint64 sizeWithEncoding(const QByteArray &data, KMime::Headers::contentEncoding encoding) // local
18{
19 KMime::Content content;
20 content.setBody(data);
21 content.contentTransferEncoding()->setEncoding(encoding);
22
23 return content.size();
24}
25
26static KMime::Headers::contentEncoding bestEncodingForTypeAndData(const QByteArray &mimeType, const QByteArray &data)
27{
28 // Choose the best encoding for text/* and message/* attachments, but
29 // always use base64 for anything else to prevent CRLF/LF conversions
30 // etc. from corrupting the binary data
31 if (mimeType.isEmpty() || mimeType.startsWith("text/") || mimeType.startsWith("message/")) {
32 auto possibleEncodings = KMime::encodingsForData(data);
33 possibleEncodings.removeAll(KMime::Headers::CE8Bit);
34 if (!possibleEncodings.isEmpty()) {
35 return possibleEncodings.first();
36 } else {
37 return KMime::Headers::CEquPr; // TODO: maybe some other default?
38 }
39 } else {
40 return KMime::Headers::CEbase64;
41 }
42}
43
44class MessageCore::AttachmentPart::AttachmentPartPrivate
45{
46public:
47 AttachmentPartPrivate() = default;
48
49 QUrl mUrl;
50 QString mName;
51 QString mFileName;
52 QString mDescription;
53 QByteArray mCharset;
54 QByteArray mMimeType;
55 QByteArray mData;
56 KMime::Headers::contentEncoding mEncoding = KMime::Headers::CE7Bit;
57 qint64 mSize = -1;
58 bool mIsInline = false;
59 bool mAutoEncoding = true;
60 bool mCompressed = false;
61 bool mToEncrypt = false;
62 bool mToSign = false;
63};
64
66 : d(new AttachmentPartPrivate)
67{
68}
69
71{
72 delete d;
73}
74
76{
77 return d->mName;
78}
79
81{
82 d->mName = name;
83}
84
86{
87 return d->mFileName;
88}
89
91{
92 d->mFileName = name;
93}
94
96{
97 return d->mDescription;
98}
99
101{
102 d->mDescription = description;
103}
104
106{
107 return d->mIsInline;
108}
109
111{
112 d->mIsInline = inl;
113}
114
116{
117 return d->mAutoEncoding;
118}
119
121{
122 d->mAutoEncoding = enabled;
123
124 if (enabled) {
125 d->mEncoding = bestEncodingForTypeAndData(d->mMimeType, d->mData);
126 }
127
128 d->mSize = sizeWithEncoding(d->mData, d->mEncoding);
129}
130
131KMime::Headers::contentEncoding AttachmentPart::encoding() const
132{
133 return d->mEncoding;
134}
135
136void AttachmentPart::setEncoding(KMime::Headers::contentEncoding encoding)
137{
138 d->mAutoEncoding = false;
139 d->mEncoding = encoding;
140 d->mSize = sizeWithEncoding(d->mData, d->mEncoding);
141}
142
144{
145 return d->mCharset;
146}
147
149{
150 d->mCharset = charset;
151}
152
154{
155 return d->mMimeType;
156}
157
159{
160 d->mMimeType = mimeType;
161}
162
164{
165 return d->mCompressed;
166}
167
168void AttachmentPart::setCompressed(bool compressed)
169{
170 d->mCompressed = compressed;
171}
172
174{
175 return d->mToEncrypt;
176}
177
179{
180 d->mToEncrypt = encrypted;
181}
182
184{
185 return d->mToSign;
186}
187
189{
190 d->mToSign = sign;
191}
192
194{
195 return d->mData;
196}
197
199{
200 d->mData = data;
201
202 if (d->mAutoEncoding) {
203 d->mEncoding = bestEncodingForTypeAndData(d->mMimeType, d->mData);
204 }
205
206 d->mSize = sizeWithEncoding(d->mData, d->mEncoding);
207}
208
210{
211 return d->mSize;
212}
213
215{
216 return (mimeType() == QByteArrayLiteral("message/rfc822")) || (mimeType() == QByteArrayLiteral("multipart/digest"));
217}
218
219void AttachmentPart::setUrl(const QUrl &url)
220{
221 d->mUrl = url;
222}
223
224QUrl AttachmentPart::url() const
225{
226 return d->mUrl;
227}
qsizetype size() const
const Headers::ContentTransferEncoding * contentTransferEncoding() const
void setBody(const QByteArray &body)
void setEncoding(contentEncoding e)
void setEncrypted(bool encrypted)
Sets whether the attachment is encrypted.
void setMimeType(const QByteArray &mimeType)
Sets the mimeType of the attachment.
void setSigned(bool sign)
Sets whether the attachment is signed.
AttachmentPart()
Creates a new attachment part.
bool isEncrypted() const
Returns whether the attachment is encrypted.
bool isSigned() const
Returns whether the attachment is signed.
KMime::Headers::contentEncoding encoding() const
Returns the encoding that will be used for the attachment.
QByteArray mimeType() const
Returns the mime type of the attachment.
void setInline(bool value)
Sets whether the attachment will be displayed inline the message.
void setName(const QString &name)
Sets the name of the attachment.
bool isAutoEncoding() const
Returns whether encoding of the attachment will be determined automatically.
QString name() const
Returns the name of the attachment.
void setDescription(const QString &description)
Sets the description of the attachment.
QByteArray charset() const
Returns the charset that will be used for the attachment.
QString fileName() const
Returns the file name of the attachment.
void setAutoEncoding(bool enabled)
Sets whether encoding of the attachment will be determined automatically.
QString description() const
Returns the description of the attachment.
bool isInline() const
Returns whether the attachment will be displayed inline the message.
void setData(const QByteArray &data)
Sets the payload data of the attachment.
qint64 size() const
Returns the size of the attachment.
void setFileName(const QString &name)
Sets the file name of the attachment.
void setCharset(const QByteArray &charset)
Sets the charset that will be used for the attachment.
bool isMessageOrMessageCollection() const
Returns whether the specified attachment part is an encapsulated message (message/rfc822) or a collec...
~AttachmentPart()
Destroys the attachment part.
QByteArray data() const
Returns the payload data of the attachment.
void setCompressed(bool compressed)
Sets whether the attachment is compressed.
bool isCompressed() const
Returns whether the attachment is compressed.
void setEncoding(KMime::Headers::contentEncoding encoding)
Sets the encoding that will be used for the attachment.
KCALUTILS_EXPORT QString mimeType()
bool isEmpty() const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:33:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.