Messagelib

attachmentpart.cpp
1 /*
2  SPDX-FileCopyrightText: 2009 Constantin Berzan <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "attachmentpart.h"
8 
9 #include "messagecore_debug.h"
10 #include <KMime/Content>
11 #include <KMime/Util>
12 
13 #include <QUrl>
14 
15 using namespace MessageCore;
16 
17 uint MessageCore::qHash(const MessageCore::AttachmentPart::Ptr &ptr)
18 {
19  return ::qHash(ptr.data());
20 }
21 
22 // TODO move to kmime_util?
23 static qint64 sizeWithEncoding(const QByteArray &data, KMime::Headers::contentEncoding encoding) // local
24 {
25  auto content = new KMime::Content;
26  content->setBody(data);
27  content->contentTransferEncoding()->setEncoding(encoding);
28 
29  const int size = content->size();
30  delete content;
31 
32  return size;
33 }
34 
35 static KMime::Headers::contentEncoding bestEncodingForTypeAndData(const QByteArray &mimeType, const QByteArray &data)
36 {
37  // Choose the best encoding for text/* and message/* attachments, but
38  // always use base64 for anything else to prevent CRLF/LF conversions
39  // etc. from corrupting the binary data
40  if (mimeType.isEmpty() || mimeType.startsWith("text/") || mimeType.startsWith("message/")) {
41  auto possibleEncodings = KMime::encodingsForData(data);
42  possibleEncodings.removeAll(KMime::Headers::CE8Bit);
43  if (!possibleEncodings.isEmpty()) {
44  return possibleEncodings.first();
45  } else {
46  return KMime::Headers::CEquPr; // TODO: maybe some other default?
47  }
48  } else {
49  return KMime::Headers::CEbase64;
50  }
51 }
52 
53 class MessageCore::AttachmentPart::AttachmentPartPrivate
54 {
55 public:
56  AttachmentPartPrivate() = default;
57 
58  QUrl mUrl;
59  QString mName;
60  QString mFileName;
61  QString mDescription;
62  QByteArray mCharset;
63  QByteArray mMimeType;
64  QByteArray mData;
65  KMime::Headers::contentEncoding mEncoding = KMime::Headers::CE7Bit;
66  qint64 mSize = -1;
67  bool mIsInline = false;
68  bool mAutoEncoding = true;
69  bool mCompressed = false;
70  bool mToEncrypt = false;
71  bool mToSign = false;
72 };
73 
75  : d(new AttachmentPartPrivate)
76 {
77 }
78 
80 {
81  delete d;
82 }
83 
85 {
86  return d->mName;
87 }
88 
90 {
91  d->mName = name;
92 }
93 
95 {
96  return d->mFileName;
97 }
98 
100 {
101  d->mFileName = name;
102 }
103 
105 {
106  return d->mDescription;
107 }
108 
110 {
111  d->mDescription = description;
112 }
113 
115 {
116  return d->mIsInline;
117 }
118 
120 {
121  d->mIsInline = inl;
122 }
123 
125 {
126  return d->mAutoEncoding;
127 }
128 
130 {
131  d->mAutoEncoding = enabled;
132 
133  if (enabled) {
134  d->mEncoding = bestEncodingForTypeAndData(d->mMimeType, d->mData);
135  }
136 
137  d->mSize = sizeWithEncoding(d->mData, d->mEncoding);
138 }
139 
140 KMime::Headers::contentEncoding AttachmentPart::encoding() const
141 {
142  return d->mEncoding;
143 }
144 
145 void AttachmentPart::setEncoding(KMime::Headers::contentEncoding encoding)
146 {
147  d->mAutoEncoding = false;
148  d->mEncoding = encoding;
149  d->mSize = sizeWithEncoding(d->mData, d->mEncoding);
150 }
151 
153 {
154  return d->mCharset;
155 }
156 
158 {
159  d->mCharset = charset;
160 }
161 
163 {
164  return d->mMimeType;
165 }
166 
168 {
169  d->mMimeType = mimeType;
170 }
171 
173 {
174  return d->mCompressed;
175 }
176 
177 void AttachmentPart::setCompressed(bool compressed)
178 {
179  d->mCompressed = compressed;
180 }
181 
183 {
184  return d->mToEncrypt;
185 }
186 
187 void AttachmentPart::setEncrypted(bool encrypted)
188 {
189  d->mToEncrypt = encrypted;
190 }
191 
193 {
194  return d->mToSign;
195 }
196 
198 {
199  d->mToSign = sign;
200 }
201 
203 {
204  return d->mData;
205 }
206 
208 {
209  d->mData = data;
210 
211  if (d->mAutoEncoding) {
212  d->mEncoding = bestEncodingForTypeAndData(d->mMimeType, d->mData);
213  }
214 
215  d->mSize = sizeWithEncoding(d->mData, d->mEncoding);
216 }
217 
218 qint64 AttachmentPart::size() const
219 {
220  return d->mSize;
221 }
222 
224 {
225  return (mimeType() == QByteArrayLiteral("message/rfc822")) || (mimeType() == QByteArrayLiteral("multipart/digest"));
226 }
227 
228 void AttachmentPart::setUrl(const QUrl &url)
229 {
230  d->mUrl = url;
231 }
232 
233 QUrl AttachmentPart::url() const
234 {
235  return d->mUrl;
236 }
void setMimeType(const QByteArray &mimeType)
Sets the mimeType of the attachment.
KMime::Headers::contentEncoding encoding() const
Returns the encoding that will be used for the attachment.
void setName(const QString &name)
Sets the name of the attachment.
QString fileName() const
Returns the file name of the attachment.
qint64 size() const
Returns the size of 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.
bool isEmpty() const const
bool startsWith(const QByteArray &ba) const const
T * data() const const
virtual ~AttachmentPart()
Destroys the attachment part.
bool isInline() const
Returns whether the attachment will be displayed inline the message.
void setEncoding(KMime::Headers::contentEncoding encoding)
Sets the encoding that will be used for the attachment.
bool isCompressed() const
Returns whether the attachment is compressed.
bool isMessageOrMessageCollection() const
Returns whether the specified attachment part is an encapsulated message (message/rfc822) or a collec...
void setCharset(const QByteArray &charset)
Sets the charset that will be used for the attachment.
QByteArray charset() const
Returns the charset that will be used for the attachment.
void setBody(const QByteArray &body)
bool isAutoEncoding() const
Returns whether encoding of the attachment will be determined automatically.
QString name() const
Returns the name of the attachment.
void setEncrypted(bool encrypted)
Sets whether the attachment is encrypted.
void setDescription(const QString &description)
Sets the description of the attachment.
void setFileName(const QString &name)
Sets the file name of the attachment.
QString description() const
Returns the description of the attachment.
bool isEncrypted() const
Returns whether the attachment is encrypted.
QByteArray data() const
Returns the payload data of the attachment.
AttachmentPart()
Creates a new attachment part.
void setData(const QByteArray &data)
Sets the payload data of the attachment.
void setSigned(bool sign)
Sets whether the attachment is signed.
void setCompressed(bool compressed)
Sets whether the attachment is compressed.
bool isSigned() const
Returns whether the attachment is signed.
void setAutoEncoding(bool enabled)
Sets whether encoding of the attachment will be determined automatically.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Nov 26 2021 23:16:41 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.