00001
00002
00003 #include <config.h>
00004 #include <kmimemagic.h>
00005 #include <kmimetype.h>
00006 #include <kdebug.h>
00007 #include <kmdcodec.h>
00008
00009 #include "kmmsgpart.h"
00010 #include "kmkernel.h"
00011 #include "kmmessage.h"
00012 #include "globalsettings.h"
00013
00014 #include <kasciistringtools.h>
00015 #include <kmime_charfreq.h>
00016 #include <kmime_codecs.h>
00017 #include <mimelib/enum.h>
00018 #include <mimelib/utility.h>
00019 #include <mimelib/string.h>
00020
00021 #include <kiconloader.h>
00022 #include <qtextcodec.h>
00023
00024 #include <assert.h>
00025
00026 using namespace KMime;
00027
00028
00029 KMMessagePart::KMMessagePart()
00030 : mType("text"), mSubtype("plain"), mCte("7bit"), mBodyDecodedSize(0),
00031 mParent(0), mLoadHeaders(false), mLoadPart(false)
00032 {
00033 }
00034
00035
00036 KMMessagePart::KMMessagePart( QDataStream & stream )
00037 : mParent(0), mLoadHeaders(false), mLoadPart(false)
00038 {
00039 unsigned long size;
00040 stream >> mOriginalContentTypeStr >> mName >> mContentDescription
00041 >> mContentDisposition >> mCte >> size >> mPartSpecifier;
00042
00043 KPIM::kAsciiToLower( mContentDisposition.data() );
00044 KPIM::kAsciiToUpper( mOriginalContentTypeStr.data() );
00045
00046
00047 int sep = mOriginalContentTypeStr.find('/');
00048 mType = mOriginalContentTypeStr.left(sep);
00049 mSubtype = mOriginalContentTypeStr.mid(sep+1);
00050
00051 mBodyDecodedSize = size;
00052 }
00053
00054
00055
00056 KMMessagePart::~KMMessagePart()
00057 {
00058 }
00059
00060
00061
00062 void KMMessagePart::clear()
00063 {
00064 mOriginalContentTypeStr = QCString();
00065 mType = "text";
00066 mSubtype = "plain";
00067 mCte = "7bit";
00068 mContentDescription = QCString();
00069 mContentDisposition = QCString();
00070 mBody.truncate( 0 );
00071 mAdditionalCTypeParamStr = QCString();
00072 mName = QString::null;
00073 mParameterAttribute = QCString();
00074 mParameterValue = QString::null;
00075 mCharset = QCString();
00076 mPartSpecifier = QString::null;
00077 mBodyDecodedSize = 0;
00078 mParent = 0;
00079 mLoadHeaders = false;
00080 mLoadPart = false;
00081 }
00082
00083
00084
00085 void KMMessagePart::duplicate( const KMMessagePart & msgPart )
00086 {
00087
00088 *this = msgPart;
00089
00090 mBody.detach();
00091 }
00092
00093
00094 int KMMessagePart::decodedSize(void) const
00095 {
00096 if (mBodyDecodedSize < 0)
00097 mBodyDecodedSize = bodyDecodedBinary().size();
00098 return mBodyDecodedSize;
00099 }
00100
00101
00102
00103 void KMMessagePart::setBody(const QCString &aStr)
00104 {
00105 mBody.duplicate( aStr.data(), aStr.length() );
00106
00107 int enc = cte();
00108 if (enc == DwMime::kCte7bit || enc == DwMime::kCte8bit || enc == DwMime::kCteBinary)
00109 mBodyDecodedSize = mBody.size();
00110 else
00111 mBodyDecodedSize = -1;
00112 }
00113
00114 void KMMessagePart::setBodyFromUnicode( const QString & str ) {
00115 QCString encoding = KMMsgBase::autoDetectCharset( charset(), KMMessage::preferredCharsets(), str );
00116 if ( encoding.isEmpty() )
00117 encoding = "utf-8";
00118 const QTextCodec * codec = KMMsgBase::codecForName( encoding );
00119 assert( codec );
00120 QValueList<int> dummy;
00121 setCharset( encoding );
00122 setBodyAndGuessCte( codec->fromUnicode( str ), dummy, false );
00123 }
00124
00125 const QTextCodec * KMMessagePart::codec() const {
00126 const QTextCodec * c = KMMsgBase::codecForName( charset() );
00127
00128 if ( !c ) {
00129
00130
00131 c = KMMsgBase::codecForName( GlobalSettings::self()->fallbackCharacterEncoding().latin1() );
00132 }
00133 if ( !c )
00134
00135
00136 c = kmkernel->networkCodec();
00137 assert( c );
00138 return c;
00139 }
00140
00141 QString KMMessagePart::bodyToUnicode(const QTextCodec* codec) const {
00142 if ( !codec )
00143
00144 codec = this->codec();
00145 assert( codec );
00146
00147 return codec->toUnicode( bodyDecoded() );
00148 }
00149
00150 void KMMessagePart::setCharset( const QCString & c ) {
00151 if ( type() != DwMime::kTypeText )
00152 kdWarning()
00153 << "KMMessagePart::setCharset(): trying to set a charset for a non-textual mimetype." << endl
00154 << "Fix this caller:" << endl
00155 << "====================================================================" << endl
00156 << kdBacktrace( 5 ) << endl
00157 << "====================================================================" << endl;
00158 mCharset = c;
00159 }
00160
00161
00162 void KMMessagePart::setBodyEncoded(const QCString& aStr)
00163 {
00164 mBodyDecodedSize = aStr.length();
00165
00166 switch (cte())
00167 {
00168 case DwMime::kCteQuotedPrintable:
00169 case DwMime::kCteBase64:
00170 {
00171 Codec * codec = Codec::codecForName( cteStr() );
00172 assert( codec );
00173
00174
00175 mBody.resize( codec->maxEncodedSizeFor( mBodyDecodedSize ) );
00176 QCString::ConstIterator iit = aStr.data();
00177 QCString::ConstIterator iend = aStr.data() + mBodyDecodedSize;
00178 QByteArray::Iterator oit = mBody.begin();
00179 QByteArray::ConstIterator oend = mBody.end();
00180 if ( !codec->encode( iit, iend, oit, oend ) )
00181 kdWarning(5006) << codec->name()
00182 << " codec lies about it's maxEncodedSizeFor( "
00183 << mBodyDecodedSize << " ). Result truncated!" << endl;
00184 mBody.truncate( oit - mBody.begin() );
00185 break;
00186 }
00187 default:
00188 kdWarning(5006) << "setBodyEncoded: unknown encoding '" << cteStr()
00189 << "'. Assuming binary." << endl;
00190 case DwMime::kCte7bit:
00191 case DwMime::kCte8bit:
00192 case DwMime::kCteBinary:
00193 mBody.duplicate( aStr.data(), mBodyDecodedSize );
00194 break;
00195 }
00196 }
00197
00198 void KMMessagePart::setBodyAndGuessCte(const QByteArray& aBuf,
00199 QValueList<int> & allowedCte,
00200 bool allow8Bit,
00201 bool willBeSigned )
00202 {
00203 mBodyDecodedSize = aBuf.size();
00204
00205 CharFreq cf( aBuf );
00206
00207 allowedCte = KMMessage::determineAllowedCtes( cf, allow8Bit, willBeSigned );
00208
00209 #ifndef NDEBUG
00210 DwString dwCte;
00211 DwCteEnumToStr(allowedCte[0], dwCte);
00212 kdDebug(5006) << "CharFreq returned " << cf.type() << "/"
00213 << cf.printableRatio() << " and I chose "
00214 << dwCte.c_str() << endl;
00215 #endif
00216
00217 setCte( allowedCte[0] );
00218 setBodyEncodedBinary( aBuf );
00219 }
00220
00221 void KMMessagePart::setBodyAndGuessCte(const QCString& aBuf,
00222 QValueList<int> & allowedCte,
00223 bool allow8Bit,
00224 bool willBeSigned )
00225 {
00226 mBodyDecodedSize = aBuf.length();
00227
00228 CharFreq cf( aBuf.data(), mBodyDecodedSize );
00229
00230 allowedCte = KMMessage::determineAllowedCtes( cf, allow8Bit, willBeSigned );
00231
00232 #ifndef NDEBUG
00233 DwString dwCte;
00234 DwCteEnumToStr(allowedCte[0], dwCte);
00235 kdDebug(5006) << "CharFreq returned " << cf.type() << "/"
00236 << cf.printableRatio() << " and I chose "
00237 << dwCte.c_str() << endl;
00238 #endif
00239
00240 setCte( allowedCte[0] );
00241 setBodyEncoded( aBuf );
00242 }
00243
00244
00245 void KMMessagePart::setBodyEncodedBinary(const QByteArray& aStr)
00246 {
00247 mBodyDecodedSize = aStr.size();
00248 if (aStr.isEmpty())
00249 {
00250 mBody.resize(0);
00251 return;
00252 }
00253
00254 switch (cte())
00255 {
00256 case DwMime::kCteQuotedPrintable:
00257 case DwMime::kCteBase64:
00258 {
00259 Codec * codec = Codec::codecForName( cteStr() );
00260 assert( codec );
00261
00262 mBody = codec->encode( aStr );
00263 break;
00264 }
00265 default:
00266 kdWarning(5006) << "setBodyEncodedBinary: unknown encoding '" << cteStr()
00267 << "'. Assuming binary." << endl;
00268 case DwMime::kCte7bit:
00269 case DwMime::kCte8bit:
00270 case DwMime::kCteBinary:
00271 mBody.duplicate( aStr );
00272 break;
00273 }
00274 }
00275
00276
00277
00278 QByteArray KMMessagePart::bodyDecodedBinary() const
00279 {
00280 if (mBody.isEmpty()) return QByteArray();
00281 QByteArray result;
00282
00283 switch (cte())
00284 {
00285 case DwMime::kCte7bit:
00286 case DwMime::kCte8bit:
00287 case DwMime::kCteBinary:
00288 result.duplicate(mBody);
00289 break;
00290 default:
00291 if ( const Codec * codec = Codec::codecForName( cteStr() ) )
00292
00293 result = codec->decode( mBody );
00294 else {
00295 kdWarning(5006) << "bodyDecodedBinary: unknown encoding '" << cteStr()
00296 << "'. Assuming binary." << endl;
00297 result.duplicate(mBody);
00298 }
00299 }
00300
00301 assert( mBodyDecodedSize < 0
00302 || (unsigned int)mBodyDecodedSize == result.size() );
00303 if ( mBodyDecodedSize < 0 )
00304 mBodyDecodedSize = result.size();
00305
00306 return result;
00307 }
00308
00309 QCString KMMessagePart::bodyDecoded(void) const
00310 {
00311 if (mBody.isEmpty()) return QCString("");
00312 bool decodeBinary = false;
00313 QCString result;
00314 int len;
00315
00316 switch (cte())
00317 {
00318 case DwMime::kCte7bit:
00319 case DwMime::kCte8bit:
00320 case DwMime::kCteBinary:
00321 {
00322 decodeBinary = true;
00323 break;
00324 }
00325 default:
00326 if ( const Codec * codec = Codec::codecForName( cteStr() ) ) {
00327
00328
00329 int bufSize = codec->maxDecodedSizeFor( mBody.size() ) + 1;
00330 result.resize( bufSize );
00331 QByteArray::ConstIterator iit = mBody.begin();
00332 QCString::Iterator oit = result.begin();
00333 QCString::ConstIterator oend = result.begin() + bufSize;
00334 if ( !codec->decode( iit, mBody.end(), oit, oend ) )
00335 kdWarning(5006) << codec->name()
00336 << " lies about it's maxDecodedSizeFor( "
00337 << mBody.size() << " ). Result truncated!" << endl;
00338 len = oit - result.begin();
00339 result.truncate( len );
00340 } else {
00341 kdWarning(5006) << "bodyDecoded: unknown encoding '" << cteStr()
00342 << "'. Assuming binary." << endl;
00343 decodeBinary = true;
00344 }
00345 }
00346
00347 if ( decodeBinary ) {
00348 len = mBody.size();
00349 result.resize( len+1 );
00350 memcpy(result.data(), mBody.data(), len);
00351 result[len] = 0;
00352 }
00353
00354 kdWarning( result.length() != (unsigned int)len, 5006 )
00355 << "KMMessagePart::bodyDecoded(): body is binary but used as text!" << endl;
00356
00357 result = result.replace( "\r\n", "\n" );
00358
00359 assert( mBodyDecodedSize < 0 || mBodyDecodedSize == len );
00360 if ( mBodyDecodedSize < 0 )
00361 mBodyDecodedSize = len;
00362
00363 return result;
00364 }
00365
00366
00367
00368 void KMMessagePart::magicSetType(bool aAutoDecode)
00369 {
00370 KMimeMagic::self()->setFollowLinks( true );
00371
00372 const QByteArray body = ( aAutoDecode ) ? bodyDecodedBinary() : mBody ;
00373 KMimeMagicResult * result = KMimeMagic::self()->findBufferType( body );
00374
00375 QString mimetype = result->mimeType();
00376 const int sep = mimetype.find('/');
00377 mType = mimetype.left(sep).latin1();
00378 mSubtype = mimetype.mid(sep+1).latin1();
00379 }
00380
00381
00382
00383 QString KMMessagePart::iconName() const
00384 {
00385 QCString mimeType( mType + "/" + mSubtype );
00386 KPIM::kAsciiToLower( mimeType.data() );
00387 QString fileName =
00388 KMimeType::mimeType( mimeType )->icon( QString::null, false );
00389 fileName =
00390 KGlobal::instance()->iconLoader()->iconPath( fileName, KIcon::Desktop );
00391 return fileName;
00392 }
00393
00394
00395
00396 int KMMessagePart::type() const {
00397 return DwTypeStrToEnum(DwString(mType));
00398 }
00399
00400
00401
00402 void KMMessagePart::setType(int aType)
00403 {
00404 DwString dwType;
00405 DwTypeEnumToStr(aType, dwType);
00406 mType = dwType.c_str();
00407 }
00408
00409
00410 int KMMessagePart::subtype() const {
00411 return DwSubtypeStrToEnum(DwString(mSubtype));
00412 }
00413
00414
00415
00416 void KMMessagePart::setSubtype(int aSubtype)
00417 {
00418 DwString dwSubtype;
00419 DwSubtypeEnumToStr(aSubtype, dwSubtype);
00420 mSubtype = dwSubtype.c_str();
00421 }
00422
00423
00424 QCString KMMessagePart::parameterAttribute(void) const
00425 {
00426 return mParameterAttribute;
00427 }
00428
00429
00430 QString KMMessagePart::parameterValue(void) const
00431 {
00432 return mParameterValue;
00433 }
00434
00435
00436 void KMMessagePart::setParameter(const QCString &attribute,
00437 const QString &value)
00438 {
00439 mParameterAttribute = attribute;
00440 mParameterValue = value;
00441 }
00442
00443
00444 QCString KMMessagePart::contentTransferEncodingStr(void) const
00445 {
00446 return mCte;
00447 }
00448
00449
00450
00451 int KMMessagePart::contentTransferEncoding(void) const
00452 {
00453 return DwCteStrToEnum(DwString(mCte));
00454 }
00455
00456
00457
00458 void KMMessagePart::setContentTransferEncodingStr(const QCString &aStr)
00459 {
00460 mCte = aStr;
00461 }
00462
00463
00464
00465 void KMMessagePart::setContentTransferEncoding(int aCte)
00466 {
00467 DwString dwCte;
00468 DwCteEnumToStr(aCte, dwCte);
00469 mCte = dwCte.c_str();
00470
00471 }
00472
00473
00474
00475 QString KMMessagePart::contentDescription(void) const
00476 {
00477 return KMMsgBase::decodeRFC2047String(mContentDescription);
00478 }
00479
00480
00481
00482 void KMMessagePart::setContentDescription(const QString &aStr)
00483 {
00484 QCString encoding = KMMsgBase::autoDetectCharset(charset(),
00485 KMMessage::preferredCharsets(), aStr);
00486 if (encoding.isEmpty()) encoding = "utf-8";
00487 mContentDescription = KMMsgBase::encodeRFC2047String(aStr, encoding);
00488 }
00489
00490
00491
00492 QString KMMessagePart::fileName(void) const
00493 {
00494 bool bRFC2231encoded = false;
00495
00496
00497 int startOfFilename = mContentDisposition.find("filename*=", 0, FALSE);
00498 if (startOfFilename >= 0) {
00499 bRFC2231encoded = true;
00500 startOfFilename += 10;
00501 }
00502 else {
00503 startOfFilename = mContentDisposition.find("filename=", 0, FALSE);
00504 if (startOfFilename < 0)
00505 return QString::null;
00506 startOfFilename += 9;
00507 }
00508
00509
00510 int endOfFilename;
00511 if ( '"' == mContentDisposition[startOfFilename] ) {
00512 startOfFilename++;
00513 endOfFilename = mContentDisposition.find('"', startOfFilename) - 1;
00514 }
00515 else {
00516 endOfFilename = mContentDisposition.find(';', startOfFilename) - 1;
00517 }
00518 if (endOfFilename < 0)
00519 endOfFilename = 32767;
00520
00521 const QCString str = mContentDisposition.mid(startOfFilename,
00522 endOfFilename-startOfFilename+1)
00523 .stripWhiteSpace();
00524
00525 if (bRFC2231encoded)
00526 return KMMsgBase::decodeRFC2231String(str);
00527 else
00528 return KMMsgBase::decodeRFC2047String(str);
00529 }
00530
00531
00532
00533 QCString KMMessagePart::body() const
00534 {
00535 return QCString( mBody.data(), mBody.size() + 1 );
00536 }
00537