1/*
2 This file is part of the kcal 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
34#include <QtCore/QByteArray>
35
36using namespace KCal;
37
38/**
39 Private class that helps to provide binary compatibility between releases.
40 @internal
41*/
42//@cond PRIVATE
43class KCal::Attachment::Private
44{
45 public:
46 Private( const QString &mime, bool binary )
47 : mSize( 0 ),
48 mMimeType( mime ),
49 mData( 0 ),
50 mBinary( binary ),
51 mLocal( false ),
52 mShowInline( false )
53 {}
54 Private( const Private &other )
55 : mSize( other.mSize ),
56 mMimeType( other.mMimeType ),
57 mUri( other.mUri ),
58 mData( qstrdup( other.mData ) ),
59 mLabel( other.mLabel ),
60 mBinary( other.mBinary ),
61 mLocal( other.mLocal ),
62 mShowInline( other.mShowInline )
63 {}
64 ~Private()
65 {
66 delete[] mData;
67 }
68
69 QByteArray mDataCache;
70 uint mSize;
71 QString mMimeType;
72 QString mUri;
73 char *mData;
74 QString mLabel;
75 bool mBinary;
76 bool mLocal;
77 bool mShowInline;
78};
79//@endcond
80
81Attachment::Attachment( const Attachment &attachment )
82 : d( new Attachment::Private( *attachment.d ) )
83{
84}
85
86Attachment::Attachment( const QString &uri, const QString &mime )
87 : d( new Attachment::Private( mime, false ) )
88{
89 d->mUri = uri;
90}
91
92Attachment::Attachment( const char *base64, const QString &mime )
93 : d( new Attachment::Private( mime, true ) )
94{
95 d->mData = qstrdup( base64 );
96}
97
98Attachment::~Attachment()
99{
100 delete d;
101}
102
103bool Attachment::isUri() const
104{
105 return !d->mBinary;
106}
107
108QString Attachment::uri() const
109{
110 if ( !d->mBinary ) {
111 return d->mUri;
112 } else {
113 return QString();
114 }
115}
116
117void Attachment::setUri( const QString &uri )
118{
119 d->mUri = uri;
120 d->mBinary = false;
121}
122
123bool Attachment::isBinary() const
124{
125 return d->mBinary;
126}
127
128char *Attachment::data() const
129{
130 if ( d->mBinary ) {
131 return d->mData;
132 } else {
133 return 0;
134 }
135}
136
137QByteArray &Attachment::decodedData() const
138{
139 if ( d->mDataCache.isNull() ) {
140 d->mDataCache = QByteArray::fromBase64( d->mData );
141 }
142
143 return d->mDataCache;
144}
145
146void Attachment::setDecodedData( const QByteArray &data )
147{
148 setData( data.toBase64().constData() );
149 d->mDataCache = data;
150 d->mSize = d->mDataCache.size();
151}
152
153void Attachment::setData( const char *base64 )
154{
155 delete[] d->mData;
156 d->mData = qstrdup( base64 );
157 d->mBinary = true;
158 d->mDataCache = QByteArray();
159 d->mSize = 0;
160}
161
162uint Attachment::size() const
163{
164 if ( isUri() ) {
165 return 0;
166 }
167 if ( !d->mSize ) {
168 d->mSize = decodedData().size();
169 }
170
171 return d->mSize;
172}
173
174QString Attachment::mimeType() const
175{
176 return d->mMimeType;
177}
178
179void Attachment::setMimeType( const QString &mime )
180{
181 d->mMimeType = mime;
182}
183
184bool Attachment::showInline() const
185{
186 return d->mShowInline;
187}
188
189void Attachment::setShowInline( bool showinline )
190{
191 d->mShowInline = showinline;
192}
193
194QString Attachment::label() const
195{
196 return d->mLabel;
197}
198
199void Attachment::setLabel( const QString &label )
200{
201 d->mLabel = label;
202}
203
204bool Attachment::isLocal() const
205{
206 return d->mLocal;
207}
208
209void Attachment::setLocal( bool local )
210{
211 d->mLocal = local;
212}
213
214bool Attachment::operator==( const Attachment &a2 ) const
215{
216 return uri() == a2.uri() &&
217 d->mLabel == a2.label() &&
218 d->mLocal == a2.isLocal() &&
219 d->mBinary == a2.isBinary() &&
220 d->mShowInline == a2.showInline() &&
221 size() == a2.size() &&
222 decodedData() == a2.decodedData();
223}
224
225bool Attachment::operator!=( const Attachment &a2 ) const
226{
227 return !( *this == a2 );
228}
229