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 TQString &uri, const TQString &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 TQString &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 TQString Attachment::uri() const
00071 {
00072   if ( !mBinary ) {
00073     return mUri;
00074   } else {
00075     return TQString::null;
00076   }
00077 }
00078 
00079 void Attachment::setUri( const TQString &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 TQByteArray &Attachment::decodedData()
00100 {
00101   if ( mDataCache.isNull() && mData ) {
00102     // base64Decode() sometimes appends a null byte when called
00103     // with a TQCString so work on TQByteArray's instead
00104     TQByteArray encoded;
00105     encoded.duplicate( mData, strlen( mData ) );
00106     TQByteArray decoded;
00107     KCodecs::base64Decode( encoded, decoded );
00108     mDataCache = decoded;
00109   }
00110 
00111   return mDataCache;
00112 }
00113 
00114 void Attachment::setDecodedData( const TQByteArray &data )
00115 {
00116   TQByteArray encoded;
00117   KCodecs::base64Encode( data, encoded );
00118 
00119   encoded.resize( encoded.count() + 1 );
00120   encoded[encoded.count()-1] = '\0';
00121 
00122   setData( encoded.data() );
00123   mDataCache = data;
00124   mSize = mDataCache.size();
00125 }
00126 
00127 void Attachment::setData( const char *base64 )
00128 {
00129   delete[] mData;
00130   mData = qstrdup( base64 );
00131   mBinary = true;
00132   mDataCache = TQByteArray();
00133   mSize = 0;
00134 }
00135 
00136 uint Attachment::size()
00137 {
00138   if ( isUri() ) {
00139     return 0;
00140   }
00141   if ( !mSize ) {
00142     mSize = decodedData().size();
00143   }
00144 
00145   return mSize;
00146 }
00147 
00148 TQString Attachment::mimeType() const
00149 {
00150   return mMimeType;
00151 }
00152 
00153 void Attachment::setMimeType(const TQString& mime)
00154 {
00155   mMimeType = mime;
00156 }
00157 
00158 bool Attachment::showInline() const
00159 {
00160   return mShowInline;
00161 }
00162 
00163 void Attachment::setShowInline( bool showinline )
00164 {
00165   mShowInline = showinline;
00166 }
00167 
00168 TQString Attachment::label() const
00169 {
00170   return mLabel;
00171 }
00172 
00173 void Attachment::setLabel( const TQString& label )
00174 {
00175   mLabel = label;
00176 }
00177 
00178 bool Attachment::isLocal() const
00179 {
00180   return mLocal;
00181 }
00182 
00183 void Attachment::setLocal( bool local )
00184 {
00185   mLocal = local;
00186 }