kmail

kmmsgpart.cpp

00001 // kmmsgpart.cpp
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   // set the type
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   // copy the data of msgPart
00089   *this = msgPart;
00090   // detach the explicitely shared QByteArray
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; // Can't know the decoded size
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; // Can't know the decoded size
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; // Can't know the decoded size
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 /* no 8bit */ );
00146 }
00147 
00148 const QTextCodec * KMMessagePart::codec() const {
00149   const QTextCodec * c = KMMsgBase::codecForName( charset() );
00150 
00151   if ( !c ) {
00152     // Ok, no override and nothing in the message, let's use the fallback
00153     // the user configured
00154     c = KMMsgBase::codecForName( GlobalSettings::self()->fallbackCharacterEncoding().latin1() );
00155   }
00156   if ( !c )
00157     // no charset means us-ascii (RFC 2045), so using local encoding should
00158     // be okay
00159     c = kmkernel->networkCodec();
00160   assert( c );
00161   return c;
00162 }
00163 
00164 QString KMMessagePart::bodyToUnicode(const QTextCodec* codec) const {
00165   if ( !codec )
00166     // No codec was given, so try the charset in the mail
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; // same as aStr.length() but faster - assuming no embedded nuls
00188   switch (cte())
00189   {
00190   case DwMime::kCteQuotedPrintable:
00191   case DwMime::kCteBase64:
00192     {
00193       Codec * codec = Codec::codecForName( cteStr() );
00194       assert( codec );
00195       // we can't use the convenience function here, since aStr is not
00196       // a QByteArray...:
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     // fall through
00213   case DwMime::kCte7bit:
00214   case DwMime::kCte8bit:
00215   case DwMime::kCteBinary:
00216     // This is slow and memory hungry - consider using setBodyEncodedBinary instead!
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 ); // save to pass null arrays...
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] ); // choose best fitting
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; // same as aStr.length() but faster - assuming no embedded nuls
00251 
00252   CharFreq cf( aBuf.data(), mBodyDecodedSize ); // save to pass null strings
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] ); // choose best fitting
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       // Nice: We can use the convenience function :-)
00286       mBody = codec->encode( aStr );
00287       break;
00288     }
00289   default:
00290     kdWarning(5006) << "setBodyEncodedBinary: unknown encoding '" << cteStr()
00291             << "'. Assuming binary." << endl;
00292     // fall through
00293   case DwMime::kCte7bit:
00294   case DwMime::kCte8bit:
00295   case DwMime::kCteBinary:
00296     //mBody.duplicate( aStr );
00297     mBody = aStr;
00298     // Caller has to detach before it modifies aStr!
00299     break;
00300   }
00301 }
00302 
00303 
00304 //-----------------------------------------------------------------------------
00305 QByteArray KMMessagePart::bodyDecodedBinary() const
00306 {
00307   if (mBody.isEmpty()) return QByteArray();
00308   QByteArray result;
00309 
00310   switch (cte())
00311   {
00312     case DwMime::kCte7bit:
00313     case DwMime::kCte8bit:
00314     case DwMime::kCteBinary:
00315       result.duplicate(mBody);
00316       break;
00317     default:
00318       if ( const Codec * codec = Codec::codecForName( cteStr() ) )
00319         // Nice: we can use the convenience function :-)
00320         result = codec->decode( mBody );
00321       else {
00322         kdWarning(5006) << "bodyDecodedBinary: unknown encoding '" << cteStr()
00323                         << "'. Assuming binary." << endl;
00324     result.duplicate(mBody);
00325       }
00326   }
00327 
00328   assert( mBodyDecodedSize < 0
00329       || (unsigned int)mBodyDecodedSize == result.size() );
00330   if ( mBodyDecodedSize < 0 )
00331     mBodyDecodedSize = result.size(); // cache the decoded size.
00332 
00333   return result;
00334 }
00335 
00336 QCString KMMessagePart::bodyDecoded(void) const
00337 {
00338   if (mBody.isEmpty()) return QCString("");
00339   bool decodeBinary = false;
00340   QCString result;
00341   int len;
00342 
00343   switch (cte())
00344   {
00345     case DwMime::kCte7bit:
00346     case DwMime::kCte8bit:
00347     case DwMime::kCteBinary:
00348     {
00349       decodeBinary = true;
00350       break;
00351     }
00352     default:
00353       if ( const Codec * codec = Codec::codecForName( cteStr() ) ) {
00354         // We can't use the codec convenience functions, since we must
00355         // return a QCString, not a QByteArray:
00356         int bufSize = codec->maxDecodedSizeFor( mBody.size() ) + 1; // trailing NUL
00357         result.resize( bufSize );
00358         QByteArray::ConstIterator iit = mBody.begin();
00359         QCString::Iterator oit = result.begin();
00360         QCString::ConstIterator oend = result.begin() + bufSize;
00361         if ( !codec->decode( iit, mBody.end(), oit, oend ) )
00362           kdWarning(5006) << codec->name()
00363                           << " lies about it's maxDecodedSizeFor( "
00364                           << mBody.size() << " ). Result truncated!" << endl;
00365         len = oit - result.begin();
00366         result.truncate( len ); // adds trailing NUL
00367       } else {
00368         kdWarning(5006) << "bodyDecoded: unknown encoding '" << cteStr()
00369                         << "'. Assuming binary." << endl;
00370         decodeBinary = true;
00371       }
00372   }
00373 
00374   if ( decodeBinary ) {
00375     len = mBody.size();
00376     KMail::Util::setFromByteArray( result, mBody );
00377   }
00378 
00379   // Calls length -> slow
00380   //kdWarning( result.length() != (unsigned int)len, 5006 )
00381   //  << "KMMessagePart::bodyDecoded(): body is binary but used as text!" << endl;
00382 
00383   result = result.replace( "\r\n", "\n" ); // CRLF -> LF conversion
00384 
00385   assert( mBodyDecodedSize < 0 || mBodyDecodedSize == len );
00386   if ( mBodyDecodedSize < 0 )
00387     mBodyDecodedSize = len; // cache decoded size
00388 
00389   return result;
00390 }
00391 
00392 
00393 //-----------------------------------------------------------------------------
00394 void KMMessagePart::magicSetType(bool aAutoDecode)
00395 {
00396   KMimeMagic::self()->setFollowLinks( true ); // is it necessary ?
00397 
00398   const QByteArray body = ( aAutoDecode ) ? bodyDecodedBinary() : mBody ;
00399   KMimeMagicResult * result = KMimeMagic::self()->findBufferType( body );
00400 
00401   QString mimetype = result->mimeType();
00402   const int sep = mimetype.find('/');
00403   mType = mimetype.left(sep).latin1();
00404   mSubtype = mimetype.mid(sep+1).latin1();
00405 }
00406 
00407 
00408 //-----------------------------------------------------------------------------
00409 QString KMMessagePart::iconName() const
00410 {
00411   QCString mimeType( mType + "/" + mSubtype );
00412   KPIM::kAsciiToLower( mimeType.data() );
00413 
00414   QString fileName =
00415     KMimeType::mimeType( mimeType )->icon( QString::null, false );
00416   if ( fileName.isEmpty() )
00417   {
00418     fileName = this->fileName();
00419     if ( fileName.isEmpty() ) fileName = this->name();
00420     if ( !fileName.isEmpty() )
00421     {
00422       fileName = KMimeType::findByPath( "/tmp/"+fileName, 0, true )->icon( QString::null, true );
00423     }
00424   }
00425 
00426   fileName =
00427     KGlobal::instance()->iconLoader()->iconPath( fileName, KIcon::Desktop );
00428   return fileName;
00429 }
00430 
00431 
00432 //-----------------------------------------------------------------------------
00433 int KMMessagePart::type() const {
00434   return DwTypeStrToEnum(DwString(mType));
00435 }
00436 
00437 
00438 //-----------------------------------------------------------------------------
00439 void KMMessagePart::setType(int aType)
00440 {
00441   DwString dwType;
00442   DwTypeEnumToStr(aType, dwType);
00443   mType = dwType.c_str();
00444 }
00445 
00446 //-----------------------------------------------------------------------------
00447 int KMMessagePart::subtype() const {
00448   return DwSubtypeStrToEnum(DwString(mSubtype));
00449 }
00450 
00451 
00452 //-----------------------------------------------------------------------------
00453 void KMMessagePart::setSubtype(int aSubtype)
00454 {
00455   DwString dwSubtype;
00456   DwSubtypeEnumToStr(aSubtype, dwSubtype);
00457   mSubtype = dwSubtype.c_str();
00458 }
00459 
00460 //-----------------------------------------------------------------------------
00461 QCString KMMessagePart::parameterAttribute(void) const
00462 {
00463   return mParameterAttribute;
00464 }
00465 
00466 //-----------------------------------------------------------------------------
00467 QString KMMessagePart::parameterValue(void) const
00468 {
00469   return mParameterValue;
00470 }
00471 
00472 //-----------------------------------------------------------------------------
00473 void KMMessagePart::setParameter(const QCString &attribute,
00474                                  const QString &value)
00475 {
00476   mParameterAttribute = attribute;
00477   mParameterValue = value;
00478 }
00479 
00480 //-----------------------------------------------------------------------------
00481 QCString KMMessagePart::contentTransferEncodingStr(void) const
00482 {
00483   return mCte;
00484 }
00485 
00486 
00487 //-----------------------------------------------------------------------------
00488 int KMMessagePart::contentTransferEncoding(void) const
00489 {
00490   return DwCteStrToEnum(DwString(mCte));
00491 }
00492 
00493 
00494 //-----------------------------------------------------------------------------
00495 void KMMessagePart::setContentTransferEncodingStr(const QCString &aStr)
00496 {
00497     mCte = aStr;
00498 }
00499 
00500 
00501 //-----------------------------------------------------------------------------
00502 void KMMessagePart::setContentTransferEncoding(int aCte)
00503 {
00504   DwString dwCte;
00505   DwCteEnumToStr(aCte, dwCte);
00506   mCte = dwCte.c_str();
00507 
00508 }
00509 
00510 
00511 //-----------------------------------------------------------------------------
00512 QString KMMessagePart::contentDescription(void) const
00513 {
00514   return KMMsgBase::decodeRFC2047String(mContentDescription, charset());
00515 }
00516 
00517 
00518 //-----------------------------------------------------------------------------
00519 void KMMessagePart::setContentDescription(const QString &aStr)
00520 {
00521   QCString encoding = KMMsgBase::autoDetectCharset(charset(),
00522     KMMessage::preferredCharsets(), aStr);
00523   if (encoding.isEmpty()) encoding = "utf-8";
00524   mContentDescription = KMMsgBase::encodeRFC2047String(aStr, encoding);
00525 }
00526 
00527 
00528 //-----------------------------------------------------------------------------
00529 QString KMMessagePart::fileName(void) const
00530 {
00531   QCString str;
00532   
00533   // Allow for multiple filname*0, filename*1, ... params (defined by RFC 2231) 
00534   // in the Content-Disposision
00535   if ( mContentDisposition.contains( "filename*", FALSE ) ) {
00536   
00537     // It's RFC 2231 encoded, so extract the file name with the 2231 method
00538     str = KMMsgBase::extractRFC2231HeaderField( mContentDisposition, "filename" );
00539     return KMMsgBase::decodeRFC2231String(str);
00540   
00541   } else {
00542     
00543     // Standard RFC 2047-encoded
00544     // search the start of the filename
00545     int startOfFilename = mContentDisposition.find("filename=", 0, FALSE);
00546     if (startOfFilename < 0)
00547       return QString::null;
00548     startOfFilename += 9;
00549 
00550     // search the end of the filename
00551     int endOfFilename;
00552     if ( '"' == mContentDisposition[startOfFilename] ) {
00553       startOfFilename++; // the double quote isn't part of the filename
00554       endOfFilename = mContentDisposition.find('"', startOfFilename) - 1;
00555     }
00556     else {
00557       endOfFilename = mContentDisposition.find(';', startOfFilename) - 1;
00558     }
00559     if (endOfFilename < 0)
00560       endOfFilename = 32767;
00561 
00562     const QCString str = mContentDisposition.mid(startOfFilename,
00563                                 endOfFilename-startOfFilename+1)
00564                            .stripWhiteSpace();
00565     return KMMsgBase::decodeRFC2047String(str, charset());
00566   }
00567   
00568   return QString::null;
00569 }
00570 
00571 QCString KMMessagePart::body() const
00572 {
00573   return QCString( mBody.data(), mBody.size() + 1 ); // space for trailing NUL
00574 }
00575 
00576 DwString KMMessagePart::dwBody() const
00577 {
00578   return KMail::Util::dwString( mBody );
00579 }
KDE Home | KDE Accessibility Home | Description of Access Keys