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