libkcal

attachment.cpp

00001 /*
00002     This file is part of libkcal.
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 */
00021 
00022 #include "attachment.h"
00023 #include <kmdcodec.h>
00024 
00025 using namespace KCal;
00026 
00027 Attachment::Attachment( const Attachment &attachment )
00028 {
00029   mSize = attachment.mSize;
00030   mMimeType = attachment.mMimeType;
00031   mUri = attachment.mUri;
00032   mData = qstrdup( attachment.mData );
00033   mLabel = attachment.mLabel;
00034   mBinary = attachment.mBinary;
00035   mLocal = attachment.mLocal;
00036   mShowInline = attachment.mShowInline;
00037 }
00038 
00039 Attachment::Attachment( const QString &uri, const QString &mime )
00040 {
00041   mSize = 0;
00042   mMimeType = mime;
00043   mUri = uri;
00044   mData = 0;
00045   mBinary = false;
00046   mLocal = false;
00047   mShowInline = false;
00048 }
00049 
00050 Attachment::Attachment( const char *base64, const QString &mime )
00051 {
00052   mSize = 0;
00053   mMimeType = mime;
00054   mData = qstrdup( base64 );
00055   mBinary = true;
00056   mLocal = false;
00057   mShowInline = false;
00058 }
00059 
00060 Attachment::~Attachment()
00061 {
00062   delete[] mData;
00063 }
00064 
00065 bool Attachment::isUri() const
00066 {
00067   return !mBinary;
00068 }
00069 
00070 QString Attachment::uri() const
00071 {
00072   if ( !mBinary ) {
00073     return mUri;
00074   } else {
00075     return QString::null;
00076   }
00077 }
00078 
00079 void Attachment::setUri( const QString &uri )
00080 {
00081   mUri = uri;
00082   mBinary = false;
00083 }
00084 
00085 bool Attachment::isBinary() const
00086 {
00087   return mBinary;
00088 }
00089 
00090 char *Attachment::data() const
00091 {
00092   if ( mBinary ) {
00093     return mData;
00094   } else {
00095     return 0;
00096   }
00097 }
00098 
00099 QByteArray &Attachment::decodedData()
00100 {
00101   if ( mDataCache.isNull() && mData ) {
00102     QByteArray encoded;
00103     encoded.duplicate( mData, strlen( mData ) );
00104     QByteArray decoded;
00105     KCodecs::base64Decode( encoded, decoded );
00106 
00107     // base64Decode() sometimes appends a null byte when called so
00108     // if the last byte is 0 remove it (this can happen sometimes)
00109     unsigned int len = decoded.size();
00110     if ( len > 0 && decoded[len - 1] == 0 ) {
00111       decoded.truncate( len - 1 );
00112     }
00113     mDataCache = decoded;
00114   }
00115 
00116   return mDataCache;
00117 }
00118 
00119 void Attachment::setDecodedData( const QByteArray &data )
00120 {
00121   QByteArray encoded;
00122   KCodecs::base64Encode( data, encoded );
00123   setData( encoded.data() );
00124   mDataCache = data;
00125   mSize = mDataCache.size();
00126 }
00127 
00128 void Attachment::setData( const char *base64 )
00129 {
00130   delete[] mData;
00131   mData = qstrdup( base64 );
00132   mBinary = true;
00133   mDataCache = QByteArray();
00134   mSize = 0;
00135 }
00136 
00137 uint Attachment::size()
00138 {
00139   if ( isUri() ) {
00140     return 0;
00141   }
00142   if ( !mSize ) {
00143     mSize = decodedData().size();
00144   }
00145 
00146   return mSize;
00147 }
00148 
00149 QString Attachment::mimeType() const
00150 {
00151   return mMimeType;
00152 }
00153 
00154 void Attachment::setMimeType(const QString& mime)
00155 {
00156   mMimeType = mime;
00157 }
00158 
00159 bool Attachment::showInline() const
00160 {
00161   return mShowInline;
00162 }
00163 
00164 void Attachment::setShowInline( bool showinline )
00165 {
00166   mShowInline = showinline;
00167 }
00168 
00169 QString Attachment::label() const
00170 {
00171   return mLabel;
00172 }
00173 
00174 void Attachment::setLabel( const QString& label )
00175 {
00176   mLabel = label;
00177 }
00178 
00179 bool Attachment::isLocal() const
00180 {
00181   return mLocal;
00182 }
00183 
00184 void Attachment::setLocal( bool local )
00185 {
00186   mLocal = local;
00187 }
KDE Home | KDE Accessibility Home | Description of Access Keys