attachment.cpp
Go to the documentation of this file.
00001 /* 00002 This file is part of the kcalcore library. 00003 00004 Copyright (c) 2002 Michael Brade <brade@kde.org> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301, USA. 00020 */ 00032 #include "attachment.h" 00033 00034 using namespace KCalCore; 00035 00040 //@cond PRIVATE 00041 class KCalCore::Attachment::Private 00042 { 00043 public: 00044 Private( const QString &mime, bool binary ) 00045 : mSize( 0 ), 00046 mMimeType( mime ), 00047 mBinary( binary ), 00048 mLocal( false ), 00049 mShowInline( false ) 00050 {} 00051 Private( const Private &other ) 00052 : mSize( other.mSize ), 00053 mMimeType( other.mMimeType ), 00054 mUri( other.mUri ), 00055 mEncodedData( other.mEncodedData ), 00056 mLabel( other.mLabel ), 00057 mBinary( other.mBinary ), 00058 mLocal( other.mLocal ), 00059 mShowInline( other.mShowInline ) 00060 {} 00061 00062 ~Private() 00063 { 00064 } 00065 00066 QByteArray mDecodedDataCache; 00067 uint mSize; 00068 QString mMimeType; 00069 QString mUri; 00070 QByteArray mEncodedData; 00071 QString mLabel; 00072 bool mBinary; 00073 bool mLocal; 00074 bool mShowInline; 00075 }; 00076 //@endcond 00077 00078 Attachment::Attachment( const Attachment &attachment ) 00079 : d( new Attachment::Private( *attachment.d ) ) 00080 { 00081 } 00082 00083 Attachment::Attachment( const QString &uri, const QString &mime ) 00084 : d( new Attachment::Private( mime, false ) ) 00085 { 00086 d->mUri = uri; 00087 } 00088 00089 Attachment::Attachment( const QByteArray &base64, const QString &mime ) 00090 : d( new Attachment::Private( mime, true ) ) 00091 { 00092 d->mEncodedData = base64; 00093 } 00094 00095 Attachment::~Attachment() 00096 { 00097 delete d; 00098 } 00099 00100 bool Attachment::isUri() const 00101 { 00102 return !d->mBinary; 00103 } 00104 00105 QString Attachment::uri() const 00106 { 00107 if ( !d->mBinary ) { 00108 return d->mUri; 00109 } else { 00110 return QString(); 00111 } 00112 } 00113 00114 void Attachment::setUri( const QString &uri ) 00115 { 00116 d->mUri = uri; 00117 d->mBinary = false; 00118 } 00119 00120 bool Attachment::isBinary() const 00121 { 00122 return d->mBinary; 00123 } 00124 00125 QByteArray Attachment::data() const 00126 { 00127 if ( d->mBinary ) { 00128 return d->mEncodedData; 00129 } else { 00130 return QByteArray(); 00131 } 00132 } 00133 00134 QByteArray Attachment::decodedData() const 00135 { 00136 if ( d->mDecodedDataCache.isNull() ) { 00137 d->mDecodedDataCache = QByteArray::fromBase64( d->mEncodedData ); 00138 } 00139 00140 return d->mDecodedDataCache; 00141 } 00142 00143 void Attachment::setDecodedData( const QByteArray &data ) 00144 { 00145 setData( data.toBase64() ); 00146 d->mDecodedDataCache = data; 00147 d->mSize = d->mDecodedDataCache.size(); 00148 } 00149 00150 void Attachment::setData( const QByteArray &base64 ) 00151 { 00152 d->mEncodedData = base64; 00153 d->mBinary = true; 00154 d->mDecodedDataCache = QByteArray(); 00155 d->mSize = 0; 00156 } 00157 00158 uint Attachment::size() const 00159 { 00160 if ( isUri() ) { 00161 return 0; 00162 } 00163 if ( !d->mSize ) { 00164 d->mSize = decodedData().size(); 00165 } 00166 00167 return d->mSize; 00168 } 00169 00170 QString Attachment::mimeType() const 00171 { 00172 return d->mMimeType; 00173 } 00174 00175 void Attachment::setMimeType( const QString &mime ) 00176 { 00177 d->mMimeType = mime; 00178 } 00179 00180 bool Attachment::showInline() const 00181 { 00182 return d->mShowInline; 00183 } 00184 00185 void Attachment::setShowInline( bool showinline ) 00186 { 00187 d->mShowInline = showinline; 00188 } 00189 00190 QString Attachment::label() const 00191 { 00192 return d->mLabel; 00193 } 00194 00195 void Attachment::setLabel( const QString &label ) 00196 { 00197 d->mLabel = label; 00198 } 00199 00200 bool Attachment::isLocal() const 00201 { 00202 return d->mLocal; 00203 } 00204 00205 void Attachment::setLocal( bool local ) 00206 { 00207 d->mLocal = local; 00208 } 00209 00210 Attachment &Attachment::operator=( const Attachment &other ) 00211 { 00212 if ( this != &other ) { 00213 d->mSize = other.d->mSize; 00214 d->mMimeType = other.d->mMimeType; 00215 d->mUri = other.d->mUri; 00216 d->mEncodedData = other.d->mEncodedData; 00217 d->mLabel = other.d->mLabel; 00218 d->mBinary = other.d->mBinary; 00219 d->mLocal = other.d->mLocal; 00220 d->mShowInline = other.d->mShowInline; 00221 } 00222 00223 return *this; 00224 } 00225 00226 bool Attachment::operator==( const Attachment &a2 ) const 00227 { 00228 return uri() == a2.uri() && 00229 d->mLabel == a2.label() && 00230 d->mLocal == a2.isLocal() && 00231 d->mBinary == a2.isBinary() && 00232 d->mShowInline == a2.showInline() && 00233 size() == a2.size() && 00234 decodedData() == a2.decodedData(); 00235 } 00236 00237 bool Attachment::operator!=( const Attachment &a2 ) const 00238 { 00239 return !( *this == a2 ); 00240 }