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 <KMime/Content>
10 #include <KMime/Util>
11 
12 #include <QUrl>
13 
14 using namespace MessageCore;
15 
16 uint MessageCore::qHash(const MessageCore::AttachmentPart::Ptr &ptr)
17 {
18  return ::qHash(ptr.data());
19 }
20 
21 // TODO move to kmime_util?
22 static 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 
34 static 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 
52 class MessageCore::AttachmentPart::AttachmentPartPrivate
53 {
54 public:
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 
108 void AttachmentPart::setDescription(const QString &description)
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 
139 KMime::Headers::contentEncoding AttachmentPart::encoding() const
140 {
141  return d->mEncoding;
142 }
143 
144 void 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 
176 void AttachmentPart::setCompressed(bool compressed)
177 {
178  d->mCompressed = compressed;
179 }
180 
182 {
183  return d->mToEncrypt;
184 }
185 
186 void AttachmentPart::setEncrypted(bool encrypted)
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 
217 qint64 AttachmentPart::size() const
218 {
219  return d->mSize;
220 }
221 
223 {
224  return (mimeType() == QByteArrayLiteral("message/rfc822")) || (mimeType() == QByteArrayLiteral("multipart/digest"));
225 }
226 
227 void AttachmentPart::setUrl(const QUrl &url)
228 {
229  d->mUrl = url;
230 }
231 
232 QUrl AttachmentPart::url() const
233 {
234  return d->mUrl;
235 }
bool isEncrypted() const
Returns whether the attachment is encrypted.
T * data() const const
void setSigned(bool sign)
Sets whether the attachment is signed.
QString name() const
Returns the name of the attachment.
KMime::Headers::contentEncoding encoding() const
Returns the encoding that will be used for the attachment.
void setMimeType(const QByteArray &mimeType)
Sets the mimeType of the attachment.
bool isMessageOrMessageCollection() const
Returns whether the specified attachment part is an encapsulated message (message/rfc822) or a collec...
void setCompressed(bool compressed)
Sets whether the attachment is compressed.
QString description() const
Returns the description of the attachment.
KCALUTILS_EXPORT QString mimeType()
void setCharset(const QByteArray &charset)
Sets the charset that will be used for the attachment.
QByteArray data() const
Returns the payload data of the attachment.
void setInline(bool value)
Sets whether the attachment will be displayed inline the message.
void setEncrypted(bool encrypted)
Sets whether the attachment is encrypted.
void setBody(const QByteArray &body)
bool isCompressed() const
Returns whether the attachment is compressed.
virtual ~AttachmentPart()
Destroys the attachment part.
bool isEmpty() const const
void setEncoding(KMime::Headers::contentEncoding encoding)
Sets the encoding that will be used for the attachment.
bool isSigned() const
Returns whether the attachment is signed.
AttachmentPart()
Creates a new attachment part.
bool isInline() const
Returns whether the attachment will be displayed inline the message.
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
QByteArray charset() const
Returns the charset that will be used for the attachment.
void setAutoEncoding(bool enabled)
Sets whether encoding of the attachment will be determined automatically.
void setDescription(const QString &description)
Sets the description of 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.
bool isAutoEncoding() const
Returns whether encoding of the attachment will be determined automatically.
void setData(const QByteArray &data)
Sets the payload data of the attachment.
void setFileName(const QString &name)
Sets the file name of the attachment.
QByteArray mimeType() const
Returns the mime type of the attachment.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Mar 26 2023 04:08:10 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.