KCalendarCore

attachment.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the kcalcore library.
3 
4  SPDX-FileCopyrightText: 2002 Michael Brade <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
19 #include "attachment.h"
20 #include <QDataStream>
21 
22 using namespace KCalendarCore;
23 
28 //@cond PRIVATE
29 class Q_DECL_HIDDEN KCalendarCore::Attachment::Private : public QSharedData
30 {
31 public:
32  Private() = default;
33  Private(const QString &mime, bool binary)
34  : mMimeType(mime),
35  mBinary(binary)
36  {}
37  Private(const Private &other) = default;
38 
39  ~Private()
40  {
41  }
42 
43  mutable uint mSize = 0;
44  mutable QByteArray mDecodedDataCache;
45  QString mMimeType;
46  QString mUri;
47  QByteArray mEncodedData;
48  QString mLabel;
49  bool mBinary = false;
50  bool mLocal = false;
51  bool mShowInline = false;
52 };
53 //@endcond
54 
56  : d(new Attachment::Private)
57 {
58 }
59 
60 Attachment::Attachment(const Attachment &attachment) = default;
61 
63  : d(new Attachment::Private(mime, false))
64 {
65  d->mUri = uri;
66 }
67 
68 Attachment::Attachment(const QByteArray &base64, const QString &mime)
69  : d(new Attachment::Private(mime, true))
70 {
71  d->mEncodedData = base64;
72 }
73 
74 Attachment::~Attachment() = default;
75 
76 bool Attachment::isEmpty() const
77 {
78  return d->mMimeType.isEmpty() && d->mUri.isEmpty() && d->mEncodedData.isEmpty();
79 }
80 
81 bool Attachment::isUri() const
82 {
83  return !d->mBinary;
84 }
85 
87 {
88  if (!d->mBinary) {
89  return d->mUri;
90  } else {
91  return QString();
92  }
93 }
94 
96 {
97  d->mUri = uri;
98  d->mBinary = false;
99 }
100 
101 bool Attachment::isBinary() const
102 {
103  return d->mBinary;
104 }
105 
107 {
108  if (d->mBinary) {
109  return d->mEncodedData;
110  } else {
111  return QByteArray();
112  }
113 }
114 
116 {
117  if (d->mDecodedDataCache.isNull()) {
118  d->mDecodedDataCache = QByteArray::fromBase64(d->mEncodedData);
119  }
120 
121  return d->mDecodedDataCache;
122 }
123 
125 {
126  setData(data.toBase64());
127  d->mDecodedDataCache = data;
128  d->mSize = d->mDecodedDataCache.size();
129 }
130 
131 void Attachment::setData(const QByteArray &base64)
132 {
133  d->mEncodedData = base64;
134  d->mBinary = true;
135  d->mDecodedDataCache = QByteArray();
136  d->mSize = 0;
137 }
138 
139 uint Attachment::size() const
140 {
141  if (isUri()) {
142  return 0;
143  }
144  if (!d->mSize) {
145  d->mSize = decodedData().size();
146  }
147 
148  return d->mSize;
149 }
150 
152 {
153  return d->mMimeType;
154 }
155 
157 {
158  d->mMimeType = mime;
159 }
160 
161 bool Attachment::showInline() const
162 {
163  return d->mShowInline;
164 }
165 
166 void Attachment::setShowInline(bool showinline)
167 {
168  d->mShowInline = showinline;
169 }
170 
172 {
173  return d->mLabel;
174 }
175 
177 {
178  d->mLabel = label;
179 }
180 
181 bool Attachment::isLocal() const
182 {
183  return d->mLocal;
184 }
185 
186 void Attachment::setLocal(bool local)
187 {
188  d->mLocal = local;
189 }
190 
191 Attachment &Attachment::operator=(const Attachment &other) = default;
192 
193 bool Attachment::operator==(const Attachment &a2) const
194 {
195  return uri() == a2.uri() &&
196  d->mLabel == a2.label() &&
197  d->mLocal == a2.isLocal() &&
198  d->mBinary == a2.isBinary() &&
199  d->mShowInline == a2.showInline() &&
200  size() == a2.size() &&
201  decodedData() == a2.decodedData();
202 }
203 
204 bool Attachment::operator!=(const Attachment &a2) const
205 {
206  return !(*this == a2);
207 }
208 
210 {
211  out << a.d->mSize
212  << a.d->mMimeType
213  << a.d->mUri
214  << a.d->mEncodedData
215  << a.d->mLabel
216  << a.d->mBinary
217  << a.d->mLocal
218  << a.d->mShowInline;
219  return out;
220 }
221 
223 {
224  in >> a.d->mSize
225  >> a.d->mMimeType
226  >> a.d->mUri
227  >> a.d->mEncodedData
228  >> a.d->mLabel
229  >> a.d->mBinary
230  >> a.d->mLocal
231  >> a.d->mShowInline;
232  return in;
233 }
KCALENDARCORE_EXPORT QDataStream & operator<<(QDataStream &out, const KCalendarCore::Alarm::Ptr &)
Alarm serializer.
Definition: alarm.cpp:825
bool showInline() const
Returns the attachment "show in-line" flag.
bool isBinary() const
Returns true if the attachment has a binary blob; false otherwise.
void setDecodedData(const QByteArray &data)
Sets the decoded attachment data.
Definition: attachment.cpp:124
~Attachment()
Destroys the attachment.
Attachment()
Constructs an empty attachment.
Definition: attachment.cpp:55
KCALENDARCORE_EXPORT QDataStream & operator>>(QDataStream &in, const KCalendarCore::Alarm::Ptr &)
Alarm deserializer.
Definition: alarm.cpp:849
QByteArray data() const
Returns a pointer to a QByteArray containing the base64 encoded binary data of the attachment...
Definition: attachment.cpp:106
void setData(const QByteArray &base64)
Sets the base64 encoded binary blob data of the attachment.
Definition: attachment.cpp:131
bool isLocal() const
Returns the attachment "local" flag.
QByteArray decodedData() const
Returns a QByteArray containing the decoded base64 binary data of the attachment. ...
Definition: attachment.cpp:115
QString label() const
Returns the attachment label string.
QString uri() const
Returns the URI of the attachment.
Represents information related to an attachment for a Calendar Incidence.
Definition: attachment.h:47
void setLocal(bool local)
Sets the attachment "local" option, which is derived from the Calendar Incidence X-KONTACT-TYPE param...
Definition: attachment.cpp:186
This file is part of the API for handling calendar data and defines the Attachment class...
bool operator!=(const Attachment &attachment) const
Compare this with attachment for inequality.
Definition: attachment.cpp:204
QByteArray fromBase64(const QByteArray &base64, QByteArray::Base64Options options)
uint size() const
Returns the size of the attachment, in bytes.
bool isUri() const
Returns true if the attachment has a URI; false otherwise.
bool operator==(const Attachment &attachment) const
Compare this with attachment for equality.
Definition: attachment.cpp:193
Attachment & operator=(const Attachment &attachment)
Assignment operator.
bool isEmpty() const
Returns whether this is an empty or default constructed object.
int size() const const
QString mimeType() const
Returns the MIME-type of the attachment.
void setUri(const QString &uri)
Sets the URI for this attachment to uri.
Definition: attachment.cpp:95
void setMimeType(const QString &mime)
Sets the MIME-type of the attachment to mime.
Definition: attachment.cpp:156
QByteArray toBase64(QByteArray::Base64Options options) const const
void setShowInline(bool showinline)
Sets the attachment "show in-line" option, which is derived from the Calendar Incidence X-CONTENT-DIS...
Definition: attachment.cpp:166
Namespace for all KCalendarCore types.
Definition: alarm.h:36
void setLabel(const QString &label)
Sets the attachment label to label, which is derived from the Calendar Incidence X-LABEL parameter...
Definition: attachment.cpp:176
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue Aug 11 2020 22:52:48 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.