1/*
2 This file is part of the kcalcore library.
3
4 Copyright (c) 2002 Michael Brade <brade@kde.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21/**
22 @file
23 This file is part of the API for handling calendar data and
24 defines the Attachment class.
25
26 @brief
27 Represents information related to an attachment for a Calendar Incidence.
28
29 @author Michael Brade \<brade@kde.org\>
30*/
31
32#include "attachment.h"
33#include <QDataStream>
34
35using namespace KCalCore;
36
37/**
38 Private class that helps to provide binary compatibility between releases.
39 @internal
40*/
41//@cond PRIVATE
42class KCalCore::Attachment::Private
43{
44public:
45 Private(const QString &mime, bool binary)
46 : mSize(0),
47 mMimeType(mime),
48 mBinary(binary),
49 mLocal(false),
50 mShowInline(false)
51 {}
52 Private(const Private &other)
53 : mSize(other.mSize),
54 mMimeType(other.mMimeType),
55 mUri(other.mUri),
56 mEncodedData(other.mEncodedData),
57 mLabel(other.mLabel),
58 mBinary(other.mBinary),
59 mLocal(other.mLocal),
60 mShowInline(other.mShowInline)
61 {}
62
63 ~Private()
64 {
65 }
66
67 QByteArray mDecodedDataCache;
68 uint mSize;
69 QString mMimeType;
70 QString mUri;
71 QByteArray mEncodedData;
72 QString mLabel;
73 bool mBinary;
74 bool mLocal;
75 bool mShowInline;
76};
77//@endcond
78
79Attachment::Attachment(const Attachment &attachment)
80 : d(new Attachment::Private(*attachment.d))
81{
82}
83
84Attachment::Attachment(const QString &uri, const QString &mime)
85 : d(new Attachment::Private(mime, false))
86{
87 d->mUri = uri;
88}
89
90Attachment::Attachment(const QByteArray &base64, const QString &mime)
91 : d(new Attachment::Private(mime, true))
92{
93 d->mEncodedData = base64;
94}
95
96Attachment::~Attachment()
97{
98 delete d;
99}
100
101bool Attachment::isUri() const
102{
103 return !d->mBinary;
104}
105
106QString Attachment::uri() const
107{
108 if (!d->mBinary) {
109 return d->mUri;
110 } else {
111 return QString();
112 }
113}
114
115void Attachment::setUri(const QString &uri)
116{
117 d->mUri = uri;
118 d->mBinary = false;
119}
120
121bool Attachment::isBinary() const
122{
123 return d->mBinary;
124}
125
126QByteArray Attachment::data() const
127{
128 if (d->mBinary) {
129 return d->mEncodedData;
130 } else {
131 return QByteArray();
132 }
133}
134
135QByteArray Attachment::decodedData() const
136{
137 if (d->mDecodedDataCache.isNull()) {
138 d->mDecodedDataCache = QByteArray::fromBase64(d->mEncodedData);
139 }
140
141 return d->mDecodedDataCache;
142}
143
144void Attachment::setDecodedData(const QByteArray &data)
145{
146 setData(data.toBase64());
147 d->mDecodedDataCache = data;
148 d->mSize = d->mDecodedDataCache.size();
149}
150
151void Attachment::setData(const QByteArray &base64)
152{
153 d->mEncodedData = base64;
154 d->mBinary = true;
155 d->mDecodedDataCache = QByteArray();
156 d->mSize = 0;
157}
158
159uint Attachment::size() const
160{
161 if (isUri()) {
162 return 0;
163 }
164 if (!d->mSize) {
165 d->mSize = decodedData().size();
166 }
167
168 return d->mSize;
169}
170
171QString Attachment::mimeType() const
172{
173 return d->mMimeType;
174}
175
176void Attachment::setMimeType(const QString &mime)
177{
178 d->mMimeType = mime;
179}
180
181bool Attachment::showInline() const
182{
183 return d->mShowInline;
184}
185
186void Attachment::setShowInline(bool showinline)
187{
188 d->mShowInline = showinline;
189}
190
191QString Attachment::label() const
192{
193 return d->mLabel;
194}
195
196void Attachment::setLabel(const QString &label)
197{
198 d->mLabel = label;
199}
200
201bool Attachment::isLocal() const
202{
203 return d->mLocal;
204}
205
206void Attachment::setLocal(bool local)
207{
208 d->mLocal = local;
209}
210
211Attachment &Attachment::operator=(const Attachment &other)
212{
213 if (this != &other) {
214 d->mSize = other.d->mSize;
215 d->mMimeType = other.d->mMimeType;
216 d->mUri = other.d->mUri;
217 d->mEncodedData = other.d->mEncodedData;
218 d->mLabel = other.d->mLabel;
219 d->mBinary = other.d->mBinary;
220 d->mLocal = other.d->mLocal;
221 d->mShowInline = other.d->mShowInline;
222 }
223
224 return *this;
225}
226
227bool Attachment::operator==(const Attachment &a2) const
228{
229 return uri() == a2.uri() &&
230 d->mLabel == a2.label() &&
231 d->mLocal == a2.isLocal() &&
232 d->mBinary == a2.isBinary() &&
233 d->mShowInline == a2.showInline() &&
234 size() == a2.size() &&
235 decodedData() == a2.decodedData();
236}
237
238bool Attachment::operator!=(const Attachment &a2) const
239{
240 return !(*this == a2);
241}
242
243QDataStream& KCalCore::operator<<(QDataStream &out, const KCalCore::Attachment::Ptr &a)
244{
245 if (a)
246 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;
247 return out;
248}
249
250QDataStream& KCalCore::operator>>(QDataStream &in, const KCalCore::Attachment::Ptr &a)
251{
252 if (a)
253 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;
254 return in;
255}
256
257