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 */
8 /**
9  @file
10  This file is part of the API for handling calendar data and
11  defines the Attachment class.
12 
13  @brief
14  Represents information related to an attachment for a Calendar Incidence.
15 
16  @author Michael Brade <[email protected]>
17 */
18 
19 #include "attachment.h"
20 #include <QDataStream>
21 
22 using namespace KCalendarCore;
23 
24 /**
25  Private class that helps to provide binary compatibility between releases.
26  @internal
27 */
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  }
38  Private(const Private &other) = default;
39 
40  ~Private()
41  {
42  }
43 
44  mutable uint mSize = 0;
45  mutable QByteArray mDecodedDataCache;
46  QString mMimeType;
47  QString mUri;
48  QByteArray mEncodedData;
49  QString mLabel;
50  bool mBinary = false;
51  bool mLocal = false;
52  bool mShowInline = false;
53 };
54 //@endcond
55 
57  : d(new Attachment::Private)
58 {
59 }
60 
61 Attachment::Attachment(const Attachment &attachment) = default;
62 
63 Attachment::Attachment(const QString &uri, const QString &mime)
64  : d(new Attachment::Private(mime, false))
65 {
66  d->mUri = uri;
67 }
68 
69 Attachment::Attachment(const QByteArray &base64, const QString &mime)
70  : d(new Attachment::Private(mime, true))
71 {
72  d->mEncodedData = base64;
73 }
74 
75 Attachment::~Attachment() = default;
76 
77 bool Attachment::isEmpty() const
78 {
79  return d->mMimeType.isEmpty() && d->mUri.isEmpty() && d->mEncodedData.isEmpty();
80 }
81 
82 bool Attachment::isUri() const
83 {
84  return !d->mBinary;
85 }
86 
87 QString Attachment::uri() const
88 {
89  if (!d->mBinary) {
90  return d->mUri;
91  } else {
92  return QString();
93  }
94 }
95 
96 void Attachment::setUri(const QString &uri)
97 {
98  d->mUri = uri;
99  d->mBinary = false;
100 }
101 
102 bool Attachment::isBinary() const
103 {
104  return d->mBinary;
105 }
106 
108 {
109  if (d->mBinary) {
110  return d->mEncodedData;
111  } else {
112  return QByteArray();
113  }
114 }
115 
117 {
118  if (d->mDecodedDataCache.isNull()) {
119  d->mDecodedDataCache = QByteArray::fromBase64(d->mEncodedData);
120  }
121 
122  return d->mDecodedDataCache;
123 }
124 
126 {
127  setData(data.toBase64());
128  d->mDecodedDataCache = data;
129  d->mSize = d->mDecodedDataCache.size();
130 }
131 
132 void Attachment::setData(const QByteArray &base64)
133 {
134  d->mEncodedData = base64;
135  d->mBinary = true;
136  d->mDecodedDataCache = QByteArray();
137  d->mSize = 0;
138 }
139 
140 uint Attachment::size() const
141 {
142  if (isUri()) {
143  return 0;
144  }
145  if (!d->mSize) {
146  d->mSize = decodedData().size();
147  }
148 
149  return d->mSize;
150 }
151 
152 QString Attachment::mimeType() const
153 {
154  return d->mMimeType;
155 }
156 
158 {
159  d->mMimeType = mime;
160 }
161 
162 bool Attachment::showInline() const
163 {
164  return d->mShowInline;
165 }
166 
167 void Attachment::setShowInline(bool showinline)
168 {
169  d->mShowInline = showinline;
170 }
171 
172 QString Attachment::label() const
173 {
174  return d->mLabel;
175 }
176 
177 void Attachment::setLabel(const QString &label)
178 {
179  d->mLabel = label;
180 }
181 
182 bool Attachment::isLocal() const
183 {
184  return d->mLocal;
185 }
186 
187 void Attachment::setLocal(bool local)
188 {
189  d->mLocal = local;
190 }
191 
192 Attachment &Attachment::operator=(const Attachment &other) = default;
193 
194 bool Attachment::operator==(const Attachment &a2) const
195 {
196  return uri() == a2.uri() //
197  && d->mLabel == a2.label() //
198  && d->mLocal == a2.isLocal() //
199  && d->mBinary == a2.isBinary() //
200  && d->mShowInline == a2.showInline() //
201  && size() == a2.size() //
202  && decodedData() == a2.decodedData();
203 }
204 
205 bool Attachment::operator!=(const Attachment &a2) const
206 {
207  return !(*this == a2);
208 }
209 
211 {
212  out << a.d->mSize << a.d->mMimeType << a.d->mUri << a.d->mEncodedData << a.d->mLabel << a.d->mBinary << a.d->mLocal << a.d->mShowInline;
213  return out;
214 }
215 
217 {
218  in >> a.d->mSize >> a.d->mMimeType >> a.d->mUri >> a.d->mEncodedData >> a.d->mLabel >> a.d->mBinary >> a.d->mLocal >> a.d->mShowInline;
219  return in;
220 }
221 
222 #include "moc_attachment.cpp"
void setLabel(const QString &label)
Sets the attachment label to label, which is derived from the Calendar Incidence X-LABEL parameter.
Definition: attachment.cpp:177
void setMimeType(const QString &mime)
Sets the MIME-type of the attachment to mime.
Definition: attachment.cpp:157
KCALENDARCORE_EXPORT QDataStream & operator<<(QDataStream &out, const KCalendarCore::Alarm::Ptr &)
Alarm serializer.
Definition: alarm.cpp:820
Attachment()
Constructs an empty attachment.
Definition: attachment.cpp:56
Namespace for all KCalendarCore types.
Definition: alarm.h:36
KCALENDARCORE_EXPORT QDataStream & operator>>(QDataStream &in, const KCalendarCore::Alarm::Ptr &)
Alarm deserializer.
Definition: alarm.cpp:833
QByteArray decodedData() const
Returns a QByteArray containing the decoded base64 binary data of the attachment.
Definition: attachment.cpp:116
QByteArray toBase64(QByteArray::Base64Options options) const const
QByteArray data() const
Returns a pointer to a QByteArray containing the base64 encoded binary data of the attachment.
Definition: attachment.cpp:107
void setData(const QByteArray &base64)
Sets the base64 encoded binary blob data of the attachment.
Definition: attachment.cpp:132
~Attachment()
Destroys the attachment.
QByteArray fromBase64(const QByteArray &base64, QByteArray::Base64Options options)
Represents information related to an attachment for a Calendar Incidence.
Definition: attachment.h:46
void setDecodedData(const QByteArray &data)
Sets the decoded attachment data.
Definition: attachment.cpp:125
bool operator!=(const Attachment &attachment) const
Compare this with attachment for inequality.
Definition: attachment.cpp:205
bool operator==(const Attachment &attachment) const
Compare this with attachment for equality.
Definition: attachment.cpp:194
int size() const const
void setUri(const QString &uri)
Sets the URI for this attachment to uri.
Definition: attachment.cpp:96
void setShowInline(bool showinline)
Sets the attachment "show in-line" option, which is derived from the Calendar Incidence X-CONTENT-DIS...
Definition: attachment.cpp:167
void setLocal(bool local)
Sets the attachment "local" option, which is derived from the Calendar Incidence X-KONTACT-TYPE param...
Definition: attachment.cpp:187
Attachment & operator=(const Attachment &attachment)
Assignment operator.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Thu Sep 21 2023 04:00:45 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.