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
00304 void KMMessagePart::setMessageBody( const QByteArray& aBuf )
00305 {
00306 CharFreq cf( aBuf );
00307 mBodyDecodedSize = aBuf.size();
00308
00309 int cte;
00310 switch ( cf.type() ) {
00311 case CharFreq::SevenBitText:
00312 case CharFreq::SevenBitData:
00313 cte = DwMime::kCte7bit;
00314 break;
00315 case CharFreq::EightBitText:
00316 case CharFreq::EightBitData:
00317 cte = DwMime::kCte8bit;
00318 break;
00319 default:
00320 kdWarning(5006) << "Calling " << k_funcinfo
00321 << " with something containing neither 7 nor 8 bit text!"
00322 << " Fix this caller: " << kdBacktrace() << endl;
00323 }
00324 setCte( cte );
00325 setBodyEncodedBinary( aBuf );
00326 }
00327
00328
00329 QByteArray KMMessagePart::bodyDecodedBinary() const
00330 {
00331 if (mBody.isEmpty()) return QByteArray();
00332 QByteArray result;
00333
00334 switch (cte())
00335 {
00336 case DwMime::kCte7bit:
00337 case DwMime::kCte8bit:
00338 case DwMime::kCteBinary:
00339 result.duplicate(mBody);
00340 break;
00341 default:
00342 if ( const Codec * codec = Codec::codecForName( cteStr() ) )
00343
00344 result = codec->decode( mBody );
00345 else {
00346 kdWarning(5006) << "bodyDecodedBinary: unknown encoding '" << cteStr()
00347 << "'. Assuming binary." << endl;
00348 result.duplicate(mBody);
00349 }
00350 }
00351
00352 assert( mBodyDecodedSize < 0
00353 || (unsigned int)mBodyDecodedSize == result.size() );
00354 if ( mBodyDecodedSize < 0 )
00355 mBodyDecodedSize = result.size();
00356
00357 return result;
00358 }
00359
00360 QCString KMMessagePart::bodyDecoded(void) const
00361 {
00362 if (mBody.isEmpty()) return QCString("");
00363 bool decodeBinary = false;
00364 QCString result;
00365 int len;
00366
00367 switch (cte())
00368 {
00369 case DwMime::kCte7bit:
00370 case DwMime::kCte8bit:
00371 case DwMime::kCteBinary:
00372 {
00373 decodeBinary = true;
00374 break;
00375 }
00376 default:
00377 if ( const Codec * codec = Codec::codecForName( cteStr() ) ) {
00378
00379
00380 int bufSize = codec->maxDecodedSizeFor( mBody.size() ) + 1;
00381 result.resize( bufSize );
00382 QByteArray::ConstIterator iit = mBody.begin();
00383 QCString::Iterator oit = result.begin();
00384 QCString::ConstIterator oend = result.begin() + bufSize;
00385 if ( !codec->decode( iit, mBody.end(), oit, oend ) )
00386 kdWarning(5006) << codec->name()
00387 << " lies about it's maxDecodedSizeFor( "
00388 << mBody.size() << " ). Result truncated!" << endl;
00389 len = oit - result.begin();
00390 result.truncate( len );
00391 } else {
00392 kdWarning(5006) << "bodyDecoded: unknown encoding '" << cteStr()
00393 << "'. Assuming binary." << endl;
00394 decodeBinary = true;
00395 }
00396 }
00397
00398 if ( decodeBinary ) {
00399 len = mBody.size();
00400 KMail::Util::setFromByteArray( result, mBody );
00401 }
00402
00403
00404
00405
00406
00407 result = result.replace( "\r\n", "\n" );
00408
00409 assert( mBodyDecodedSize < 0 || mBodyDecodedSize == len );
00410 if ( mBodyDecodedSize < 0 )
00411 mBodyDecodedSize = len;
00412
00413 return result;
00414 }
00415
00416
00417
00418 void KMMessagePart::magicSetType(bool aAutoDecode)
00419 {
00420 KMimeMagic::self()->setFollowLinks( true );
00421
00422 const QByteArray body = ( aAutoDecode ) ? bodyDecodedBinary() : mBody ;
00423 KMimeMagicResult * result = KMimeMagic::self()->findBufferType( body );
00424
00425 QString mimetype = result->mimeType();
00426 const int sep = mimetype.find('/');
00427 mType = mimetype.left(sep).latin1();
00428 mSubtype = mimetype.mid(sep+1).latin1();
00429 }
00430
00431
00432
00433 QString KMMessagePart::iconName() const
00434 {
00435 QCString mimeType( mType + "/" + mSubtype );
00436 KPIM::kAsciiToLower( mimeType.data() );
00437
00438 QString fileName =
00439 KMimeType::mimeType( mimeType )->icon( QString::null, false );
00440 if ( fileName.isEmpty() )
00441 {
00442 fileName = this->fileName();
00443 if ( fileName.isEmpty() ) fileName = this->name();
00444 if ( !fileName.isEmpty() )
00445 {
00446 fileName = KMimeType::findByPath( "/tmp/"+fileName, 0, true )->icon( QString::null, true );
00447 }
00448 }
00449
00450 fileName =
00451 KGlobal::instance()->iconLoader()->iconPath( fileName, KIcon::Desktop );
00452 return fileName;
00453 }
00454
00455
00456
00457 int KMMessagePart::type() const {
00458 return DwTypeStrToEnum(DwString(mType));
00459 }
00460
00461
00462
00463 void KMMessagePart::setType(int aType)
00464 {
00465 DwString dwType;
00466 DwTypeEnumToStr(aType, dwType);
00467 mType = dwType.c_str();
00468 }
00469
00470
00471 int KMMessagePart::subtype() const {
00472 return DwSubtypeStrToEnum(DwString(mSubtype));
00473 }
00474
00475
00476
00477 void KMMessagePart::setSubtype(int aSubtype)
00478 {
00479 DwString dwSubtype;
00480 DwSubtypeEnumToStr(aSubtype, dwSubtype);
00481 mSubtype = dwSubtype.c_str();
00482 }
00483
00484
00485 QCString KMMessagePart::parameterAttribute(void) const
00486 {
00487 return mParameterAttribute;
00488 }
00489
00490
00491 QString KMMessagePart::parameterValue(void) const
00492 {
00493 return mParameterValue;
00494 }
00495
00496
00497 void KMMessagePart::setParameter(const QCString &attribute,
00498 const QString &value)
00499 {
00500 mParameterAttribute = attribute;
00501 mParameterValue = value;
00502 }
00503
00504
00505 QCString KMMessagePart::contentTransferEncodingStr(void) const
00506 {
00507 return mCte;
00508 }
00509
00510
00511
00512 int KMMessagePart::contentTransferEncoding(void) const
00513 {
00514 return DwCteStrToEnum(DwString(mCte));
00515 }
00516
00517
00518
00519 void KMMessagePart::setContentTransferEncodingStr(const QCString &aStr)
00520 {
00521 mCte = aStr;
00522 }
00523
00524
00525
00526 void KMMessagePart::setContentTransferEncoding(int aCte)
00527 {
00528 DwString dwCte;
00529 DwCteEnumToStr(aCte, dwCte);
00530 mCte = dwCte.c_str();
00531
00532 }
00533
00534
00535
00536 QString KMMessagePart::contentDescription(void) const
00537 {
00538 return KMMsgBase::decodeRFC2047String(mContentDescription, charset());
00539 }
00540
00541
00542
00543 void KMMessagePart::setContentDescription(const QString &aStr)
00544 {
00545 QCString encoding = KMMsgBase::autoDetectCharset(charset(),
00546 KMMessage::preferredCharsets(), aStr);
00547 if (encoding.isEmpty()) encoding = "utf-8";
00548 mContentDescription = KMMsgBase::encodeRFC2047String(aStr, encoding);
00549 }
00550
00551
00552
00553 QString KMMessagePart::fileName(void) const
00554 {
00555 QCString str;
00556
00557
00558
00559 if ( mContentDisposition.contains( "filename*", FALSE ) ) {
00560
00561
00562 str = KMMsgBase::extractRFC2231HeaderField( mContentDisposition, "filename" );
00563 return KMMsgBase::decodeRFC2231String(str);
00564
00565 } else {
00566
00567
00568
00569 int startOfFilename = mContentDisposition.find("filename=", 0, FALSE);
00570 if (startOfFilename < 0)
00571 return QString::null;
00572 startOfFilename += 9;
00573
00574
00575 int endOfFilename;
00576 if ( '"' == mContentDisposition[startOfFilename] ) {
00577 startOfFilename++;
00578 endOfFilename = mContentDisposition.find('"', startOfFilename) - 1;
00579 }
00580 else {
00581 endOfFilename = mContentDisposition.find(';', startOfFilename) - 1;
00582 }
00583 if (endOfFilename < 0)
00584 endOfFilename = 32767;
00585
00586 const QCString str = mContentDisposition.mid(startOfFilename,
00587 endOfFilename-startOfFilename+1)
00588 .stripWhiteSpace();
00589 return KMMsgBase::decodeRFC2047String(str, charset());
00590 }
00591
00592 return QString::null;
00593 }
00594
00595 QCString KMMessagePart::body() const
00596 {
00597 return QCString( mBody.data(), mBody.size() + 1 );
00598 }
00599
00600 DwString KMMessagePart::dwBody() const
00601 {
00602 return KMail::Util::dwString( mBody );
00603 }