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
16size_t qHash(const MessageCore::AttachmentPart::Ptr &ptr, size_t seed)
17{
18 return ::qHash(ptr.data(), seed);
19}
20
21// TODO move to kmime_util?
22static qint64 sizeWithEncoding(const QByteArray &data, KMime::Headers::contentEncoding encoding) // local
23{
24 auto content = new KMime::Content;
25 content->setBody(data);
26 content->contentTransferEncoding()->setEncoding(encoding);
27
28 const int size = content->size();
29 delete content;
30
31 return size;
32}
33
34static KMime::Headers::contentEncoding bestEncodingForTypeAndData(const QByteArray &mimeType, const QByteArray &data)
35{
36 // Choose the best encoding for text/* and message/* attachments, but
37 // always use base64 for anything else to prevent CRLF/LF conversions
38 // etc. from corrupting the binary data
39 if (mimeType.isEmpty() || mimeType.startsWith("text/") || mimeType.startsWith("message/")) {
40 auto possibleEncodings = KMime::encodingsForData(data);
41 possibleEncodings.removeAll(KMime::Headers::CE8Bit);
42 if (!possibleEncodings.isEmpty()) {
43 return possibleEncodings.first();
44 } else {
45 return KMime::Headers::CEquPr; // TODO: maybe some other default?
46 }
47 } else {
48 return KMime::Headers::CEbase64;
49 }
50}
51
52class MessageCore::AttachmentPart::AttachmentPartPrivate
53{
54public:
55 AttachmentPartPrivate() = default;
56
57 QUrl mUrl;
58 QString mName;
59 QString mFileName;
60 QString mDescription;
61 QByteArray mCharset;
62 QByteArray mMimeType;
63 QByteArray mData;
64 KMime::Headers::contentEncoding mEncoding = KMime::Headers::CE7Bit;
65 qint64 mSize = -1;
66 bool mIsInline = false;
67 bool mAutoEncoding = true;
68 bool mCompressed = false;
69 bool mToEncrypt = false;
70 bool mToSign = false;
71};
72
74 : d(new AttachmentPartPrivate)
75{
76}
77
79{
80 delete d;
81}
82
84{
85 return d->mName;
86}
87
89{
90 d->mName = name;
91}
92
94{
95 return d->mFileName;
96}
97
99{
100 d->mFileName = name;
101}
102
104{
105 return d->mDescription;
106}
107
109{
110 d->mDescription = description;
111}
112
114{
115 return d->mIsInline;
116}
117
119{
120 d->mIsInline = inl;
121}
122
124{
125 return d->mAutoEncoding;
126}
127
129{
130 d->mAutoEncoding = enabled;
131
132 if (enabled) {
133 d->mEncoding = bestEncodingForTypeAndData(d->mMimeType, d->mData);
134 }
135
136 d->mSize = sizeWithEncoding(d->mData, d->mEncoding);
137}
138
139KMime::Headers::contentEncoding AttachmentPart::encoding() const
140{
141 return d->mEncoding;
142}
143
144void AttachmentPart::setEncoding(KMime::Headers::contentEncoding encoding)
145{
146 d->mAutoEncoding = false;
147 d->mEncoding = encoding;
148 d->mSize = sizeWithEncoding(d->mData, d->mEncoding);
149}
150
152{
153 return d->mCharset;
154}
155
157{
158 d->mCharset = charset;
159}
160
162{
163 return d->mMimeType;
164}
165
167{
168 d->mMimeType = mimeType;
169}
170
172{
173 return d->mCompressed;
174}
175
176void AttachmentPart::setCompressed(bool compressed)
177{
178 d->mCompressed = compressed;
179}
180
182{
183 return d->mToEncrypt;
184}
185
187{
188 d->mToEncrypt = encrypted;
189}
190
192{
193 return d->mToSign;
194}
195
197{
198 d->mToSign = sign;
199}
200
202{
203 return d->mData;
204}
205
207{
208 d->mData = data;
209
210 if (d->mAutoEncoding) {
211 d->mEncoding = bestEncodingForTypeAndData(d->mMimeType, d->mData);
212 }
213
214 d->mSize = sizeWithEncoding(d->mData, d->mEncoding);
215}
216
218{
219 return d->mSize;
220}
221
223{
224 return (mimeType() == QByteArrayLiteral("message/rfc822")) || (mimeType() == QByteArrayLiteral("multipart/digest"));
225}
226
227void AttachmentPart::setUrl(const QUrl &url)
228{
229 d->mUrl = url;
230}
231
232QUrl AttachmentPart::url() const
233{
234 return d->mUrl;
235}
void setBody(const QByteArray &body)
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...
virtual ~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()
KTEXTEDITOR_EXPORT size_t qHash(KTextEditor::Cursor cursor, size_t seed=0) noexcept
T * data() const const
bool isEmpty() const const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:37:31 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.