00001
00002
00003 #include <config.h>
00004
00005 #include "globalsettings.h"
00006 #include "kmmsgbase.h"
00007
00008 #include "kmfolderindex.h"
00009 #include "kmfolder.h"
00010 #include "kmheaders.h"
00011 #include "kmmsgdict.h"
00012 #include "messageproperty.h"
00013 using KMail::MessageProperty;
00014
00015 #include <kdebug.h>
00016 #include <kglobal.h>
00017 #include <kcharsets.h>
00018 #include <kasciistringtools.h>
00019 #include <kmdcodec.h>
00020 #include <krfcdate.h>
00021
00022 #include <mimelib/mimepp.h>
00023 #include <kmime_codecs.h>
00024
00025 #include <qtextcodec.h>
00026 #include <qdeepcopy.h>
00027 #include <qregexp.h>
00028
00029 #include <ctype.h>
00030 #include <stdlib.h>
00031 #include <unistd.h>
00032
00033 #ifdef HAVE_BYTESWAP_H
00034 #include <byteswap.h>
00035 #endif
00036
00037
00038
00039
00040
00041 #ifdef bswap_16
00042 #define kmail_swap_16(x) bswap_16(x)
00043 #else
00044 #define kmail_swap_16(x) \
00045 ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
00046 #endif
00047
00048
00049 #ifdef bswap_32
00050 #define kmail_swap_32(x) bswap_32(x)
00051 #else
00052 #define kmail_swap_32(x) \
00053 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
00054 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
00055 #endif
00056
00057
00058 #ifdef bswap_64
00059 #define kmail_swap_64(x) bswap_64(x)
00060 #else
00061 #define kmail_swap_64(x) \
00062 ((((x) & 0xff00000000000000ull) >> 56) \
00063 | (((x) & 0x00ff000000000000ull) >> 40) \
00064 | (((x) & 0x0000ff0000000000ull) >> 24) \
00065 | (((x) & 0x000000ff00000000ull) >> 8) \
00066 | (((x) & 0x00000000ff000000ull) << 8) \
00067 | (((x) & 0x0000000000ff0000ull) << 24) \
00068 | (((x) & 0x000000000000ff00ull) << 40) \
00069 | (((x) & 0x00000000000000ffull) << 56))
00070 #endif
00071
00072
00073 KMMsgBase::KMMsgBase(KMFolder* aParentFolder)
00074 : mParent( aParentFolder ), mIndexOffset( 0 ),
00075 mIndexLength( 0 ), mDirty( false ), mEnableUndo( false ), mStatus( KMMsgStatusUnknown )
00076 {
00077 }
00078
00079
00080
00081 KMMsgBase::~KMMsgBase()
00082 {
00083 MessageProperty::forget( this );
00084 }
00085
00086 KMFolderIndex* KMMsgBase::storage() const
00087 {
00088
00089
00090 if( mParent )
00091 return static_cast<KMFolderIndex*>( mParent->storage() );
00092 return 0;
00093 }
00094
00095
00096 void KMMsgBase::assign(const KMMsgBase* other)
00097 {
00098 mParent = other->mParent;
00099 mDirty = other->mDirty;
00100 mIndexOffset = other->mIndexOffset;
00101 mIndexLength = other->mIndexLength;
00102 }
00103
00104
00105 KMMsgBase& KMMsgBase::operator=(const KMMsgBase& other)
00106 {
00107 assign(&other);
00108 return *this;
00109 }
00110
00111
00112
00113 KMMsgBase::KMMsgBase( const KMMsgBase& other )
00114 {
00115 assign( &other );
00116 }
00117
00118
00119 bool KMMsgBase::isMessage(void) const
00120 {
00121 return FALSE;
00122 }
00123
00124 void KMMsgBase::toggleStatus(const KMMsgStatus aStatus, int idx)
00125 {
00126 mDirty = true;
00127 KMMsgStatus oldStatus = status();
00128 if ( status() & aStatus ) {
00129 mStatus &= ~aStatus;
00130 } else {
00131 mStatus |= aStatus;
00132
00133
00134 if (aStatus == KMMsgStatusWatched)
00135 mStatus &= ~KMMsgStatusIgnored;
00136 if (aStatus == KMMsgStatusIgnored)
00137 mStatus &= ~KMMsgStatusWatched;
00138 if (aStatus == KMMsgStatusSpam)
00139 mStatus &= ~KMMsgStatusHam;
00140 if (aStatus == KMMsgStatusHam)
00141 mStatus &= ~KMMsgStatusSpam;
00142 }
00143 if (oldStatus != mStatus && storage()) {
00144 if (idx < 0)
00145 idx = storage()->find( this );
00146 storage()->msgStatusChanged( oldStatus, status(), idx );
00147 storage()->headerOfMsgChanged(this, idx);
00148 }
00149
00150 }
00151
00152
00153 void KMMsgBase::setStatus(const KMMsgStatus aStatus, int idx)
00154 {
00155 mDirty = TRUE;
00156 KMMsgStatus oldStatus = status();
00157 switch (aStatus) {
00158 case KMMsgStatusRead:
00159
00160 mStatus &= ~KMMsgStatusUnread;
00161 mStatus &= ~KMMsgStatusNew;
00162 mStatus |= KMMsgStatusRead;
00163 break;
00164
00165 case KMMsgStatusUnread:
00166
00167 mStatus &= ~KMMsgStatusOld;
00168 mStatus &= ~KMMsgStatusRead;
00169 mStatus &= ~KMMsgStatusNew;
00170 mStatus |= KMMsgStatusUnread;
00171 break;
00172
00173 case KMMsgStatusOld:
00174
00175 mStatus &= ~KMMsgStatusNew;
00176 mStatus &= ~KMMsgStatusUnread;
00177 mStatus |= KMMsgStatusOld;
00178 break;
00179
00180 case KMMsgStatusNew:
00181
00182 mStatus &= ~KMMsgStatusOld;
00183 mStatus &= ~KMMsgStatusRead;
00184 mStatus &= ~KMMsgStatusUnread;
00185 mStatus |= KMMsgStatusNew;
00186 break;
00187
00188 case KMMsgStatusDeleted:
00189 mStatus |= KMMsgStatusDeleted;
00190 break;
00191
00192 case KMMsgStatusReplied:
00193 mStatus |= KMMsgStatusReplied;
00194 break;
00195
00196 case KMMsgStatusForwarded:
00197 mStatus |= KMMsgStatusForwarded;
00198 break;
00199
00200 case KMMsgStatusQueued:
00201 mStatus |= KMMsgStatusQueued;
00202 break;
00203
00204 case KMMsgStatusTodo:
00205 mStatus |= KMMsgStatusTodo;
00206 break;
00207
00208 case KMMsgStatusSent:
00209 mStatus &= ~KMMsgStatusQueued;
00210 mStatus &= ~KMMsgStatusUnread;
00211 mStatus &= ~KMMsgStatusNew;
00212 mStatus |= KMMsgStatusSent;
00213 break;
00214
00215 case KMMsgStatusFlag:
00216 mStatus |= KMMsgStatusFlag;
00217 break;
00218
00219
00220 case KMMsgStatusWatched:
00221 mStatus &= ~KMMsgStatusIgnored;
00222 mStatus |= KMMsgStatusWatched;
00223 break;
00224
00225 case KMMsgStatusIgnored:
00226 mStatus &= ~KMMsgStatusWatched;
00227 mStatus |= KMMsgStatusIgnored;
00228 break;
00229
00230 case KMMsgStatusSpam:
00231 mStatus &= ~KMMsgStatusHam;
00232 mStatus |= KMMsgStatusSpam;
00233 break;
00234 case KMMsgStatusHam:
00235 mStatus &= ~KMMsgStatusSpam;
00236 mStatus |= KMMsgStatusHam;
00237 break;
00238 case KMMsgStatusHasAttach:
00239 mStatus &= ~KMMsgStatusHasNoAttach;
00240 mStatus |= KMMsgStatusHasAttach;
00241 break;
00242 case KMMsgStatusHasNoAttach:
00243 mStatus &= ~KMMsgStatusHasAttach;
00244 mStatus |= KMMsgStatusHasNoAttach;
00245 break;
00246 default:
00247 mStatus = aStatus;
00248 break;
00249 }
00250
00251 if ( oldStatus != mStatus && storage() ) {
00252 if (idx < 0)
00253 idx = storage()->find( this );
00254 storage()->msgStatusChanged( oldStatus, status(), idx );
00255 storage()->headerOfMsgChanged( this, idx );
00256 }
00257 }
00258
00259
00260
00261
00262 void KMMsgBase::setStatus(const char* aStatusStr, const char* aXStatusStr)
00263 {
00264
00265 if (aXStatusStr) {
00266 if (strchr(aXStatusStr, 'N')) setStatus(KMMsgStatusNew);
00267 if (strchr(aXStatusStr, 'U')) setStatus(KMMsgStatusUnread);
00268 if (strchr(aXStatusStr, 'O')) setStatus(KMMsgStatusOld);
00269 if (strchr(aXStatusStr, 'R')) setStatus(KMMsgStatusRead);
00270 if (strchr(aXStatusStr, 'D')) setStatus(KMMsgStatusDeleted);
00271 if (strchr(aXStatusStr, 'A')) setStatus(KMMsgStatusReplied);
00272 if (strchr(aXStatusStr, 'F')) setStatus(KMMsgStatusForwarded);
00273 if (strchr(aXStatusStr, 'Q')) setStatus(KMMsgStatusQueued);
00274 if (strchr(aXStatusStr, 'K')) setStatus(KMMsgStatusTodo);
00275 if (strchr(aXStatusStr, 'S')) setStatus(KMMsgStatusSent);
00276 if (strchr(aXStatusStr, 'G')) setStatus(KMMsgStatusFlag);
00277 if (strchr(aXStatusStr, 'P')) setStatus(KMMsgStatusSpam);
00278 if (strchr(aXStatusStr, 'H')) setStatus(KMMsgStatusHam);
00279 if (strchr(aXStatusStr, 'T')) setStatus(KMMsgStatusHasAttach);
00280 if (strchr(aXStatusStr, 'C')) setStatus(KMMsgStatusHasNoAttach);
00281 }
00282
00283
00284 if (aStatusStr) {
00285 if ((aStatusStr[0]== 'R' && aStatusStr[1]== 'O') ||
00286 (aStatusStr[0]== 'O' && aStatusStr[1]== 'R')) {
00287 setStatus( KMMsgStatusOld );
00288 setStatus( KMMsgStatusRead );
00289 }
00290 else if (aStatusStr[0] == 'R')
00291 setStatus(KMMsgStatusRead);
00292 else if (aStatusStr[0] == 'D')
00293 setStatus(KMMsgStatusDeleted);
00294 else
00295 setStatus(KMMsgStatusNew);
00296 }
00297 }
00298
00299
00300 void KMMsgBase::setEncryptionState( const KMMsgEncryptionState , int idx )
00301 {
00302
00303 mDirty = TRUE;
00304 if (storage())
00305 storage()->headerOfMsgChanged(this, idx);
00306 }
00307
00308 void KMMsgBase::setEncryptionStateChar( QChar status, int idx )
00309 {
00310
00311
00312 if( status.latin1() == (char)KMMsgEncryptionStateUnknown )
00313 setEncryptionState( KMMsgEncryptionStateUnknown, idx );
00314 else if( status.latin1() == (char)KMMsgNotEncrypted )
00315 setEncryptionState( KMMsgNotEncrypted, idx );
00316 else if( status.latin1() == (char)KMMsgPartiallyEncrypted )
00317 setEncryptionState( KMMsgPartiallyEncrypted, idx );
00318 else if( status.latin1() == (char)KMMsgFullyEncrypted )
00319 setEncryptionState( KMMsgFullyEncrypted, idx );
00320 else
00321 setEncryptionState( KMMsgEncryptionStateUnknown, idx );
00322 }
00323
00324
00325 void KMMsgBase::setSignatureState( const KMMsgSignatureState , int idx )
00326 {
00327
00328 mDirty = TRUE;
00329 if (storage())
00330 storage()->headerOfMsgChanged(this, idx);
00331 }
00332
00333 void KMMsgBase::setMDNSentState( KMMsgMDNSentState, int idx ) {
00334 mDirty = true;
00335 if ( storage() )
00336 storage()->headerOfMsgChanged(this, idx);
00337 }
00338
00339 void KMMsgBase::setSignatureStateChar( QChar status, int idx )
00340 {
00341
00342
00343 if( status.latin1() == (char)KMMsgSignatureStateUnknown )
00344 setSignatureState( KMMsgSignatureStateUnknown, idx );
00345 else if( status.latin1() == (char)KMMsgNotSigned )
00346 setSignatureState( KMMsgNotSigned, idx );
00347 else if( status.latin1() == (char)KMMsgPartiallySigned )
00348 setSignatureState( KMMsgPartiallySigned,idx );
00349 else if( status.latin1() == (char)KMMsgFullySigned )
00350 setSignatureState( KMMsgFullySigned, idx );
00351 else
00352 setSignatureState( KMMsgSignatureStateUnknown, idx );
00353 }
00354
00355
00356 bool KMMsgBase::isUnread(void) const
00357 {
00358 KMMsgStatus st = status();
00359 return (st & KMMsgStatusUnread && !(st & KMMsgStatusIgnored));
00360 }
00361
00362
00363 bool KMMsgBase::isNew(void) const
00364 {
00365 KMMsgStatus st = status();
00366 return (st & KMMsgStatusNew && !(st & KMMsgStatusIgnored));
00367 }
00368
00369
00370 bool KMMsgBase::isOfUnknownStatus(void) const
00371 {
00372 KMMsgStatus st = status();
00373 return (st == KMMsgStatusUnknown);
00374 }
00375
00376
00377 bool KMMsgBase::isOld(void) const
00378 {
00379 KMMsgStatus st = status();
00380 return (st & KMMsgStatusOld);
00381 }
00382
00383
00384 bool KMMsgBase::isRead(void) const
00385 {
00386 KMMsgStatus st = status();
00387 return (st & KMMsgStatusRead || st & KMMsgStatusIgnored);
00388 }
00389
00390
00391 bool KMMsgBase::isDeleted(void) const
00392 {
00393 KMMsgStatus st = status();
00394 return (st & KMMsgStatusDeleted);
00395 }
00396
00397
00398 bool KMMsgBase::isReplied(void) const
00399 {
00400 KMMsgStatus st = status();
00401 return (st & KMMsgStatusReplied);
00402 }
00403
00404
00405 bool KMMsgBase::isForwarded(void) const
00406 {
00407 KMMsgStatus st = status();
00408 return (st & KMMsgStatusForwarded);
00409 }
00410
00411
00412 bool KMMsgBase::isQueued(void) const
00413 {
00414 KMMsgStatus st = status();
00415 return (st & KMMsgStatusQueued);
00416 }
00417
00418
00419 bool KMMsgBase::isTodo(void) const
00420 {
00421 KMMsgStatus st = status();
00422 return (st & KMMsgStatusTodo);
00423 }
00424
00425
00426 bool KMMsgBase::isSent(void) const
00427 {
00428 KMMsgStatus st = status();
00429 return (st & KMMsgStatusSent);
00430 }
00431
00432
00433 bool KMMsgBase::isImportant(void) const
00434 {
00435 KMMsgStatus st = status();
00436 return (st & KMMsgStatusFlag);
00437 }
00438
00439
00440 bool KMMsgBase::isWatched(void) const
00441 {
00442 KMMsgStatus st = status();
00443 return (st & KMMsgStatusWatched);
00444 }
00445
00446
00447 bool KMMsgBase::isIgnored(void) const
00448 {
00449 KMMsgStatus st = status();
00450 return (st & KMMsgStatusIgnored);
00451 }
00452
00453
00454 bool KMMsgBase::isSpam(void) const
00455 {
00456 KMMsgStatus st = status();
00457 return (st & KMMsgStatusSpam);
00458 }
00459
00460
00461 bool KMMsgBase::isHam(void) const
00462 {
00463 KMMsgStatus st = status();
00464 return (st & KMMsgStatusHam);
00465 }
00466
00467
00468 QCString KMMsgBase::statusToStr(const KMMsgStatus status)
00469 {
00470 QCString sstr;
00471 if (status & KMMsgStatusNew) sstr += 'N';
00472 if (status & KMMsgStatusUnread) sstr += 'U';
00473 if (status & KMMsgStatusOld) sstr += 'O';
00474 if (status & KMMsgStatusRead) sstr += 'R';
00475 if (status & KMMsgStatusDeleted) sstr += 'D';
00476 if (status & KMMsgStatusReplied) sstr += 'A';
00477 if (status & KMMsgStatusForwarded) sstr += 'F';
00478 if (status & KMMsgStatusQueued) sstr += 'Q';
00479 if (status & KMMsgStatusTodo) sstr += 'K';
00480 if (status & KMMsgStatusSent) sstr += 'S';
00481 if (status & KMMsgStatusFlag) sstr += 'G';
00482 if (status & KMMsgStatusWatched) sstr += 'W';
00483 if (status & KMMsgStatusIgnored) sstr += 'I';
00484 if (status & KMMsgStatusSpam) sstr += 'P';
00485 if (status & KMMsgStatusHam) sstr += 'H';
00486 if (status & KMMsgStatusHasAttach) sstr += 'T';
00487 if (status & KMMsgStatusHasNoAttach) sstr += 'C';
00488
00489 return sstr;
00490 }
00491
00492
00493 QString KMMsgBase::statusToSortRank()
00494 {
00495 QString sstr = "bcbbbbbbbb";
00496
00497
00498 if (status() & KMMsgStatusWatched) sstr[0] = 'a';
00499 if (status() & KMMsgStatusIgnored) sstr[0] = 'c';
00500
00501
00502 if (status() & KMMsgStatusNew) sstr[1] = 'a';
00503 if (status() & KMMsgStatusUnread) sstr[1] = 'b';
00504
00505
00506
00507
00508 if (status() & KMMsgStatusDeleted) sstr[2] = 'a';
00509 if (status() & KMMsgStatusFlag) sstr[3] = 'a';
00510 if (status() & KMMsgStatusReplied) sstr[4] = 'a';
00511 if (status() & KMMsgStatusForwarded) sstr[5] = 'a';
00512 if (status() & KMMsgStatusQueued) sstr[6] = 'a';
00513 if (status() & KMMsgStatusSent) sstr[7] = 'a';
00514 if (status() & KMMsgStatusHam) sstr[8] = 'a';
00515 if (status() & KMMsgStatusSpam) sstr[8] = 'c';
00516 if (status() & KMMsgStatusTodo) sstr[9] = 'a';
00517
00518 return sstr;
00519 }
00520
00521
00522
00523 void KMMsgBase::setDate(const QCString& aDateStr)
00524 {
00525 setDate( KRFCDate::parseDate( aDateStr ) );
00526 }
00527
00528
00529
00530 QString KMMsgBase::dateStr(void) const
00531 {
00532 time_t d = date();
00533 return KMime::DateFormatter::formatDate(KMime::DateFormatter::Fancy, d);
00534 }
00535
00536
00537
00538 QString KMMsgBase::skipKeyword(const QString& aStr, QChar sepChar,
00539 bool* hasKeyword)
00540 {
00541 unsigned int i = 0, maxChars = 3;
00542 QString str = aStr;
00543
00544 while (str[0] == ' ') str.remove(0,1);
00545 if (hasKeyword) *hasKeyword=FALSE;
00546
00547 unsigned int strLength(str.length());
00548 for (i=0; i < strLength && i < maxChars; i++)
00549 {
00550 if (str[i] < 'A' || str[i] == sepChar) break;
00551 }
00552
00553 if (str[i] == sepChar)
00554 {
00555 do {
00556 i++;
00557 } while (str[i] == ' ');
00558 if (hasKeyword) *hasKeyword=TRUE;
00559 return str.mid(i);
00560 }
00561 return str;
00562 }
00563
00564
00565
00566 const QTextCodec* KMMsgBase::codecForName(const QCString& _str)
00567 {
00568 if (_str.isEmpty()) return 0;
00569 QCString codec = _str;
00570 KPIM::kAsciiToLower(codec.data());
00571 return KGlobal::charsets()->codecForName(codec);
00572 }
00573
00574
00575
00576 QCString KMMsgBase::toUsAscii(const QString& _str, bool *ok)
00577 {
00578 bool all_ok =true;
00579 QString result = _str;
00580 int len = result.length();
00581 for (int i = 0; i < len; i++)
00582 if (result.at(i).unicode() >= 128) {
00583 result.at(i) = '?';
00584 all_ok = false;
00585 }
00586 if (ok)
00587 *ok = all_ok;
00588 return result.latin1();
00589 }
00590
00591
00592
00593 QStringList KMMsgBase::supportedEncodings(bool usAscii)
00594 {
00595 QStringList encodingNames = KGlobal::charsets()->availableEncodingNames();
00596 QStringList encodings;
00597 QMap<QString,bool> mimeNames;
00598 for (QStringList::Iterator it = encodingNames.begin();
00599 it != encodingNames.end(); it++)
00600 {
00601 QTextCodec *codec = KGlobal::charsets()->codecForName(*it);
00602 QString mimeName = (codec) ? QString(codec->mimeName()).lower() : (*it);
00603 if (mimeNames.find(mimeName) == mimeNames.end())
00604 {
00605 encodings.append(KGlobal::charsets()->languageForEncoding(*it)
00606 + " ( " + mimeName + " )");
00607 mimeNames.insert(mimeName, TRUE);
00608 }
00609 }
00610 encodings.sort();
00611 if (usAscii) encodings.prepend(KGlobal::charsets()
00612 ->languageForEncoding("us-ascii") + " ( us-ascii )");
00613 return encodings;
00614 }
00615
00616 namespace {
00617
00618
00619
00620
00621 inline bool isBlank( char ch ) { return ch == ' ' || ch == '\t' ; }
00622
00623 QCString unfold( const QCString & header ) {
00624 if ( header.isEmpty() )
00625 return QCString();
00626
00627 QCString result( header.size() );
00628 char * d = result.data();
00629
00630 for ( const char * s = header.data() ; *s ; )
00631 if ( *s == '\r' ) {
00632 ++s;
00633 continue;
00634 } else if ( *s == '\n' ) {
00635 while ( isBlank( *++s ) );
00636 *d++ = ' ';
00637 } else
00638 *d++ = *s++;
00639
00640 *d++ = '\0';
00641
00642 result.truncate( d - result.data() );
00643 return result;
00644 }
00645 }
00646
00647
00648
00649 QString KMMsgBase::decodeRFC2047String(const QCString& aStr, QCString prefCharset)
00650 {
00651 if ( aStr.isEmpty() )
00652 return QString::null;
00653
00654 const QCString str = unfold( aStr );
00655
00656 if ( str.isEmpty() )
00657 return QString::null;
00658
00659 if ( str.find( "=?" ) < 0 ) {
00660 if ( !prefCharset.isEmpty() ) {
00661 if ( prefCharset == "us-ascii" ) {
00662
00663 return KMMsgBase::codecForName( "utf-8" )->toUnicode( str );
00664 } else {
00665 return KMMsgBase::codecForName( prefCharset )->toUnicode( str );
00666 }
00667 } else {
00668 return KMMsgBase::codecForName( GlobalSettings::self()->
00669 fallbackCharacterEncoding().latin1() )->toUnicode( str );
00670 }
00671 }
00672
00673 QString result;
00674 QCString LWSP_buffer;
00675 bool lastWasEncodedWord = false;
00676
00677 for ( const char * pos = str.data() ; *pos ; ++pos ) {
00678
00679
00680
00681 if ( lastWasEncodedWord && isBlank( pos[0] ) ) {
00682 LWSP_buffer += pos[0];
00683 continue;
00684 }
00685
00686 if (pos[0]!='=' || pos[1]!='?') {
00687 result += LWSP_buffer + pos[0];
00688 LWSP_buffer = 0;
00689 lastWasEncodedWord = FALSE;
00690 continue;
00691 }
00692
00693 const char * const beg = pos;
00694 {
00695
00696 QCString charset;
00697 int i = 2;
00698 pos += 2;
00699 for ( ; *pos != '?' && ( *pos==' ' || ispunct(*pos) || isalnum(*pos) );
00700 ++i, ++pos ) {
00701 charset += *pos;
00702 }
00703 if ( *pos!='?' || i<4 )
00704 goto invalid_encoded_word;
00705
00706
00707 const char encoding[2] = { pos[1], '\0' };
00708 if (pos[2]!='?' || (encoding[0]!='Q' && encoding[0]!='q' &&
00709 encoding[0]!='B' && encoding[0]!='b'))
00710 goto invalid_encoded_word;
00711 pos+=3; i+=3;
00712 const char * enc_start = pos;
00713
00714 while ( *pos && !(*pos=='?' && *(pos+1)=='=') ) {
00715 i++;
00716 pos++;
00717 }
00718 if ( !*pos )
00719 goto invalid_encoded_word;
00720
00721
00722 const KMime::Codec * c = KMime::Codec::codecForName( encoding );
00723 kdFatal( !c, 5006 ) << "No \"" << encoding << "\" codec!?" << endl;
00724
00725 QByteArray in; in.setRawData( enc_start, pos - enc_start );
00726 const QByteArray enc = c->decode( in );
00727 in.resetRawData( enc_start, pos - enc_start );
00728
00729 const QTextCodec * codec = codecForName(charset);
00730 if (!codec) codec = kmkernel->networkCodec();
00731 result += codec->toUnicode(enc);
00732 lastWasEncodedWord = true;
00733
00734 ++pos;
00735 LWSP_buffer = 0;
00736 }
00737 continue;
00738 invalid_encoded_word:
00739
00740 pos = beg;
00741 if ( !LWSP_buffer.isNull() )
00742 result += LWSP_buffer;
00743 result += "=?";
00744 lastWasEncodedWord = false;
00745 ++pos;
00746 LWSP_buffer = 0;
00747 }
00748 return result;
00749 }
00750
00751
00752
00753 static const QCString especials = "()<>@,;:\"/[]?.= \033";
00754
00755 QCString KMMsgBase::encodeRFC2047Quoted( const QCString & s, bool base64 ) {
00756 const char * codecName = base64 ? "b" : "q" ;
00757 const KMime::Codec * codec = KMime::Codec::codecForName( codecName );
00758 kdFatal( !codec, 5006 ) << "No \"" << codecName << "\" found!?" << endl;
00759 QByteArray in; in.setRawData( s.data(), s.length() );
00760 const QByteArray result = codec->encode( in );
00761 in.resetRawData( s.data(), s.length() );
00762 return QCString( result.data(), result.size() + 1 );
00763 }
00764
00765 QCString KMMsgBase::encodeRFC2047String(const QString& _str,
00766 const QCString& charset)
00767 {
00768 static const QString dontQuote = "\"()<>,@";
00769
00770 if (_str.isEmpty()) return QCString();
00771 if (charset == "us-ascii") return toUsAscii(_str);
00772
00773 QCString cset;
00774 if (charset.isEmpty())
00775 {
00776 cset = kmkernel->networkCodec()->mimeName();
00777 KPIM::kAsciiToLower(cset.data());
00778 }
00779 else cset = charset;
00780
00781 const QTextCodec *codec = codecForName(cset);
00782 if (!codec) codec = kmkernel->networkCodec();
00783
00784 unsigned int nonAscii = 0;
00785 unsigned int strLength(_str.length());
00786 for (unsigned int i = 0; i < strLength; i++)
00787 if (_str.at(i).unicode() >= 128) nonAscii++;
00788 bool useBase64 = (nonAscii * 6 > strLength);
00789
00790 unsigned int start, stop, p, pos = 0, encLength;
00791 QCString result;
00792 bool breakLine = FALSE;
00793 const unsigned int maxLen = 75 - 7 - cset.length();
00794
00795 while (pos < strLength)
00796 {
00797 start = pos; p = pos;
00798 while (p < strLength)
00799 {
00800 if (!breakLine && (_str.at(p) == ' ' || dontQuote.find(_str.at(p)) != -1))
00801 start = p + 1;
00802 if (_str.at(p).unicode() >= 128 || _str.at(p).unicode() < 32)
00803 break;
00804 p++;
00805 }
00806 if (breakLine || p < strLength)
00807 {
00808 while (dontQuote.find(_str.at(start)) != -1) start++;
00809 stop = start;
00810 while (stop < strLength && dontQuote.find(_str.at(stop)) == -1)
00811 stop++;
00812 result += _str.mid(pos, start - pos).latin1();
00813 encLength = encodeRFC2047Quoted(codec->fromUnicode(_str.
00814 mid(start, stop - start)), useBase64).length();
00815 breakLine = (encLength > maxLen);
00816 if (breakLine)
00817 {
00818 int dif = (stop - start) / 2;
00819 int step = dif;
00820 while (abs(step) > 1)
00821 {
00822 encLength = encodeRFC2047Quoted(codec->fromUnicode(_str.
00823 mid(start, dif)), useBase64).length();
00824 step = (encLength > maxLen) ? (-abs(step) / 2) : (abs(step) / 2);
00825 dif += step;
00826 }
00827 stop = start + dif;
00828 }
00829 p = stop;
00830 while (p > start && _str.at(p) != ' ') p--;
00831 if (p > start) stop = p;
00832 if (result.right(3) == "?= ") start--;
00833 if (result.right(5) == "?=\n ") {
00834 start--; result.truncate(result.length() - 1);
00835 }
00836 int lastNewLine = result.findRev("\n ");
00837 if (!result.mid(lastNewLine).stripWhiteSpace().isEmpty()
00838 && result.length() - lastNewLine + encLength + 2 > maxLen)
00839 result += "\n ";
00840 result += "=?";
00841 result += cset;
00842 result += (useBase64) ? "?b?" : "?q?";
00843 result += encodeRFC2047Quoted(codec->fromUnicode(_str.mid(start,
00844 stop - start)), useBase64);
00845 result += "?=";
00846 if (breakLine) result += "\n ";
00847 pos = stop;
00848 } else {
00849 result += _str.mid(pos).latin1();
00850 break;
00851 }
00852 }
00853 return result;
00854 }
00855
00856
00857
00858 QCString KMMsgBase::encodeRFC2231String( const QString& _str,
00859 const QCString& charset )
00860 {
00861 if ( _str.isEmpty() )
00862 return QCString();
00863
00864 QCString cset;
00865 if ( charset.isEmpty() )
00866 {
00867 cset = kmkernel->networkCodec()->mimeName();
00868 KPIM::kAsciiToLower( cset.data() );
00869 }
00870 else
00871 cset = charset;
00872 const QTextCodec *codec = codecForName( cset );
00873 QCString latin;
00874 if ( charset == "us-ascii" )
00875 latin = toUsAscii( _str );
00876 else if ( codec )
00877 latin = codec->fromUnicode( _str );
00878 else
00879 latin = _str.local8Bit();
00880
00881 char *l;
00882 for ( l = latin.data(); *l; ++l ) {
00883 if ( ( ( *l & 0xE0 ) == 0 ) || ( *l & 0x80 ) )
00884
00885 break;
00886 }
00887 if ( !*l )
00888 return latin;
00889
00890 QCString result = cset + "''";
00891 for ( l = latin.data(); *l; ++l ) {
00892 bool needsQuoting = ( *l & 0x80 );
00893 if( !needsQuoting ) {
00894 int len = especials.length();
00895 for ( int i = 0; i < len; i++ )
00896 if ( *l == especials[i] ) {
00897 needsQuoting = true;
00898 break;
00899 }
00900 }
00901 if ( needsQuoting ) {
00902 result += '%';
00903 unsigned char hexcode;
00904 hexcode = ( ( *l & 0xF0 ) >> 4 ) + 48;
00905 if ( hexcode >= 58 )
00906 hexcode += 7;
00907 result += hexcode;
00908 hexcode = ( *l & 0x0F ) + 48;
00909 if ( hexcode >= 58 )
00910 hexcode += 7;
00911 result += hexcode;
00912 } else {
00913 result += *l;
00914 }
00915 }
00916 return result;
00917 }
00918
00919
00920
00921 QString KMMsgBase::decodeRFC2231String(const QCString& _str)
00922 {
00923 int p = _str.find('\'');
00924 if (p < 0) return kmkernel->networkCodec()->toUnicode(_str);
00925
00926 QCString charset = _str.left(p);
00927
00928 QCString st = _str.mid(_str.findRev('\'') + 1);
00929 char ch, ch2;
00930 p = 0;
00931 while (p < (int)st.length())
00932 {
00933 if (st.at(p) == 37)
00934 {
00935 ch = st.at(p+1) - 48;
00936 if (ch > 16) ch -= 7;
00937 ch2 = st.at(p+2) - 48;
00938 if (ch2 > 16) ch2 -= 7;
00939 st.at(p) = ch * 16 + ch2;
00940 st.remove( p+1, 2 );
00941 }
00942 p++;
00943 }
00944 QString result;
00945 const QTextCodec * codec = codecForName( charset );
00946 if ( !codec )
00947 codec = kmkernel->networkCodec();
00948 return codec->toUnicode( st );
00949 }
00950
00951 QCString KMMsgBase::extractRFC2231HeaderField( const QCString &aStr, const QCString &field )
00952 {
00953 int n=-1;
00954 QCString str;
00955 bool found = false;
00956 while ( n<=0 || found ) {
00957 QString pattern( field );
00958 pattern += "[*]";
00959 if ( n>=0 ) {
00960 pattern += QString::number(n) + "[*]?";
00961 }
00962 pattern += "=";
00963
00964 QRegExp fnamePart( pattern, FALSE );
00965 int startPart = fnamePart.search( aStr );
00966 int endPart;
00967 found = ( startPart >= 0 );
00968 if ( found ) {
00969 startPart += fnamePart.matchedLength();
00970
00971 if ( aStr[startPart] == '"' ) {
00972 startPart++;
00973 endPart = aStr.find('"', startPart) - 1;
00974 }
00975 else {
00976 endPart = aStr.find(';', startPart) - 1;
00977 }
00978 if (endPart < 0)
00979 endPart = 32767;
00980 str += aStr.mid( startPart, endPart-startPart+1).stripWhiteSpace();
00981 }
00982 n++;
00983 }
00984 return str;
00985 }
00986
00987 QString KMMsgBase::base64EncodedMD5( const QString & s, bool utf8 ) {
00988 if (s.stripWhiteSpace().isEmpty()) return "";
00989 if ( utf8 )
00990 return base64EncodedMD5( s.stripWhiteSpace().utf8() );
00991 else
00992 return base64EncodedMD5( s.stripWhiteSpace().latin1() );
00993 }
00994
00995 QString KMMsgBase::base64EncodedMD5( const QCString & s ) {
00996 if (s.stripWhiteSpace().isEmpty()) return "";
00997 return base64EncodedMD5( s.stripWhiteSpace().data() );
00998 }
00999
01000 QString KMMsgBase::base64EncodedMD5( const char * s, int len ) {
01001 if (!s || !len) return "";
01002 static const int Base64EncodedMD5Len = 22;
01003 KMD5 md5( s, len );
01004 return md5.base64Digest().left( Base64EncodedMD5Len );
01005 }
01006
01007
01008
01009 QCString KMMsgBase::autoDetectCharset(const QCString &_encoding, const QStringList &encodingList, const QString &text)
01010 {
01011 QStringList charsets = encodingList;
01012 if (!_encoding.isEmpty())
01013 {
01014 QString currentCharset = QString::fromLatin1(_encoding);
01015 charsets.remove(currentCharset);
01016 charsets.prepend(currentCharset);
01017 }
01018
01019 QStringList::ConstIterator it = charsets.begin();
01020 for (; it != charsets.end(); ++it)
01021 {
01022 QCString encoding = (*it).latin1();
01023 if (encoding == "locale")
01024 {
01025 encoding = kmkernel->networkCodec()->mimeName();
01026 KPIM::kAsciiToLower(encoding.data());
01027 }
01028 if (text.isEmpty())
01029 return encoding;
01030 if (encoding == "us-ascii") {
01031 bool ok;
01032 (void) KMMsgBase::toUsAscii(text, &ok);
01033 if (ok)
01034 return encoding;
01035 }
01036 else
01037 {
01038 const QTextCodec *codec = KMMsgBase::codecForName(encoding);
01039 if (!codec) {
01040 kdDebug(5006) << "Auto-Charset: Something is wrong and I can not get a codec. [" << encoding << "]" << endl;
01041 } else {
01042 if (codec->canEncode(text))
01043 return encoding;
01044 }
01045 }
01046 }
01047 return 0;
01048 }
01049
01050
01051
01052 unsigned long KMMsgBase::getMsgSerNum() const
01053 {
01054 unsigned long msn = MessageProperty::serialCache( this );
01055 if (msn)
01056 return msn;
01057 if (mParent) {
01058 int index = mParent->find((KMMsgBase*)this);
01059 msn = KMMsgDict::instance()->getMsgSerNum(mParent, index);
01060 if (msn)
01061 MessageProperty::setSerialCache( this, msn );
01062 }
01063 return msn;
01064 }
01065
01066
01067
01068 KMMsgAttachmentState KMMsgBase::attachmentState() const
01069 {
01070 KMMsgStatus st = status();
01071 if (st & KMMsgStatusHasAttach)
01072 return KMMsgHasAttachment;
01073 else if (st & KMMsgStatusHasNoAttach)
01074 return KMMsgHasNoAttachment;
01075 else
01076 return KMMsgAttachmentUnknown;
01077 }
01078
01079
01080 static void swapEndian(QString &str)
01081 {
01082 uint len = str.length();
01083 str = QDeepCopy<QString>(str);
01084 QChar *unicode = const_cast<QChar*>( str.unicode() );
01085 for (uint i = 0; i < len; i++)
01086 unicode[i] = kmail_swap_16(unicode[i].unicode());
01087 }
01088
01089
01090 static int g_chunk_length = 0, g_chunk_offset=0;
01091 static uchar *g_chunk = 0;
01092
01093 namespace {
01094 template < typename T > void copy_from_stream( T & x ) {
01095 if( g_chunk_offset + int(sizeof(T)) > g_chunk_length ) {
01096 g_chunk_offset = g_chunk_length;
01097 kdDebug( 5006 ) << "This should never happen.. "
01098 << __FILE__ << ":" << __LINE__ << endl;
01099 x = 0;
01100 } else {
01101
01102
01103 memcpy( &x, g_chunk + g_chunk_offset, sizeof(T) );
01104 g_chunk_offset += sizeof(T);
01105 }
01106 }
01107 }
01108
01109
01110 QString KMMsgBase::getStringPart(MsgPartType t) const
01111 {
01112 QString ret;
01113
01114 g_chunk_offset = 0;
01115 bool using_mmap = FALSE;
01116 bool swapByteOrder = storage()->indexSwapByteOrder();
01117 if (storage()->indexStreamBasePtr()) {
01118 if (g_chunk)
01119 free(g_chunk);
01120 using_mmap = TRUE;
01121 g_chunk = storage()->indexStreamBasePtr() + mIndexOffset;
01122 g_chunk_length = mIndexLength;
01123 } else {
01124 if(!storage()->mIndexStream)
01125 return ret;
01126 if (g_chunk_length < mIndexLength)
01127 g_chunk = (uchar *)realloc(g_chunk, g_chunk_length = mIndexLength);
01128 off_t first_off=ftell(storage()->mIndexStream);
01129 fseek(storage()->mIndexStream, mIndexOffset, SEEK_SET);
01130 fread( g_chunk, mIndexLength, 1, storage()->mIndexStream);
01131 fseek(storage()->mIndexStream, first_off, SEEK_SET);
01132 }
01133
01134 MsgPartType type;
01135 Q_UINT16 l;
01136 while(g_chunk_offset < mIndexLength) {
01137 Q_UINT32 tmp;
01138 copy_from_stream(tmp);
01139 copy_from_stream(l);
01140 if (swapByteOrder)
01141 {
01142 tmp = kmail_swap_32(tmp);
01143 l = kmail_swap_16(l);
01144 }
01145 type = (MsgPartType) tmp;
01146 if(g_chunk_offset + l > mIndexLength) {
01147 kdDebug(5006) << "This should never happen.. " << __FILE__ << ":" << __LINE__ << endl;
01148 break;
01149 }
01150 if(type == t) {
01151
01152
01153 if(l)
01154 ret = QString((QChar *)(g_chunk + g_chunk_offset), l/2);
01155 break;
01156 }
01157 g_chunk_offset += l;
01158 }
01159 if(using_mmap) {
01160 g_chunk_length = 0;
01161 g_chunk = 0;
01162 }
01163
01164
01165
01166
01167
01168 #ifndef WORDS_BIGENDIAN
01169
01170 swapEndian(ret);
01171 #else
01172
01173 #endif
01174
01175 return ret;
01176 }
01177
01178
01179 off_t KMMsgBase::getLongPart(MsgPartType t) const
01180 {
01181 off_t ret = 0;
01182
01183 g_chunk_offset = 0;
01184 bool using_mmap = FALSE;
01185 int sizeOfLong = storage()->indexSizeOfLong();
01186 bool swapByteOrder = storage()->indexSwapByteOrder();
01187 if (storage()->indexStreamBasePtr()) {
01188 if (g_chunk)
01189 free(g_chunk);
01190 using_mmap = TRUE;
01191 g_chunk = storage()->indexStreamBasePtr() + mIndexOffset;
01192 g_chunk_length = mIndexLength;
01193 } else {
01194 if (!storage()->mIndexStream)
01195 return ret;
01196 assert(mIndexLength >= 0);
01197 if (g_chunk_length < mIndexLength)
01198 g_chunk = (uchar *)realloc(g_chunk, g_chunk_length = mIndexLength);
01199 off_t first_off=ftell(storage()->mIndexStream);
01200 fseek(storage()->mIndexStream, mIndexOffset, SEEK_SET);
01201 fread( g_chunk, mIndexLength, 1, storage()->mIndexStream);
01202 fseek(storage()->mIndexStream, first_off, SEEK_SET);
01203 }
01204
01205 MsgPartType type;
01206 Q_UINT16 l;
01207 while (g_chunk_offset < mIndexLength) {
01208 Q_UINT32 tmp;
01209 copy_from_stream(tmp);
01210 copy_from_stream(l);
01211 if (swapByteOrder)
01212 {
01213 tmp = kmail_swap_32(tmp);
01214 l = kmail_swap_16(l);
01215 }
01216 type = (MsgPartType) tmp;
01217
01218 if (g_chunk_offset + l > mIndexLength) {
01219 kdDebug(5006) << "This should never happen.. " << __FILE__ << ":" << __LINE__ << endl;
01220 break;
01221 }
01222 if(type == t) {
01223 assert(sizeOfLong == l);
01224 if (sizeOfLong == sizeof(ret))
01225 {
01226 copy_from_stream(ret);
01227 if (swapByteOrder)
01228 {
01229 if (sizeof(ret) == 4)
01230 ret = kmail_swap_32(ret);
01231 else
01232 ret = kmail_swap_64(ret);
01233 }
01234 }
01235 else if (sizeOfLong == 4)
01236 {
01237
01238 Q_UINT32 ret_32;
01239 copy_from_stream(ret_32);
01240 if (swapByteOrder)
01241 ret_32 = kmail_swap_32(ret_32);
01242 ret = ret_32;
01243 }
01244 else if (sizeOfLong == 8)
01245 {
01246
01247 Q_UINT32 ret_1;
01248 Q_UINT32 ret_2;
01249 copy_from_stream(ret_1);
01250 copy_from_stream(ret_2);
01251 if (!swapByteOrder)
01252 {
01253
01254 #ifndef WORDS_BIGENDIAN
01255
01256 ret = ret_1;
01257 #else
01258
01259 ret = ret_2;
01260 #endif
01261 }
01262 else
01263 {
01264
01265 #ifndef WORDS_BIGENDIAN
01266
01267 ret = ret_2;
01268 #else
01269
01270 ret = ret_1;
01271 #endif
01272
01273 ret = kmail_swap_32(ret);
01274 }
01275
01276 }
01277 break;
01278 }
01279 g_chunk_offset += l;
01280 }
01281 if(using_mmap) {
01282 g_chunk_length = 0;
01283 g_chunk = 0;
01284 }
01285 return ret;
01286 }
01287
01288 #ifndef WORDS_BIGENDIAN
01289
01290 #define memcpy_networkorder(to, from, len) swab((char *)(from), (char *)(to), len)
01291 #else
01292
01293 #define memcpy_networkorder(to, from, len) memcpy(to, from, len)
01294 #endif
01295
01296 #define STORE_DATA_LEN(type, x, len, network_order) do { \
01297 int len2 = (len > 256) ? 256 : len; \
01298 if(csize < (length + (len2 + sizeof(short) + sizeof(MsgPartType)))) \
01299 ret = (uchar *)realloc(ret, csize += len2+sizeof(short)+sizeof(MsgPartType)); \
01300 Q_UINT32 t = (Q_UINT32) type; memcpy(ret+length, &t, sizeof(t)); \
01301 Q_UINT16 l = len2; memcpy(ret+length+sizeof(t), &l, sizeof(l)); \
01302 if (network_order) \
01303 memcpy_networkorder(ret+length+sizeof(t)+sizeof(l), x, len2); \
01304 else \
01305 memcpy(ret+length+sizeof(t)+sizeof(l), x, len2); \
01306 length += len2+sizeof(t)+sizeof(l); \
01307 } while(0)
01308 #define STORE_DATA(type, x) STORE_DATA_LEN(type, &x, sizeof(x), false)
01309
01310
01311 const uchar *KMMsgBase::asIndexString(int &length) const
01312 {
01313 unsigned int csize = 256;
01314 static uchar *ret = 0;
01315 if(!ret)
01316 ret = (uchar *)malloc(csize);
01317 length = 0;
01318
01319 unsigned long tmp;
01320 QString tmp_str;
01321
01322
01323 tmp_str = msgIdMD5().stripWhiteSpace();
01324 STORE_DATA_LEN(MsgIdMD5Part, tmp_str.unicode(), tmp_str.length() * 2, true);
01325 tmp = mLegacyStatus;
01326 STORE_DATA(MsgLegacyStatusPart, tmp);
01327
01328
01329 tmp_str = fromStrip().stripWhiteSpace();
01330 STORE_DATA_LEN(MsgFromPart, tmp_str.unicode(), tmp_str.length() * 2, true);
01331 tmp_str = subject().stripWhiteSpace();
01332 STORE_DATA_LEN(MsgSubjectPart, tmp_str.unicode(), tmp_str.length() * 2, true);
01333 tmp_str = toStrip().stripWhiteSpace();
01334 STORE_DATA_LEN(MsgToPart, tmp_str.unicode(), tmp_str.length() * 2, true);
01335 tmp_str = replyToIdMD5().stripWhiteSpace();
01336 STORE_DATA_LEN(MsgReplyToIdMD5Part, tmp_str.unicode(), tmp_str.length() * 2, true);
01337 tmp_str = xmark().stripWhiteSpace();
01338 STORE_DATA_LEN(MsgXMarkPart, tmp_str.unicode(), tmp_str.length() * 2, true);
01339 tmp_str = fileName().stripWhiteSpace();
01340 STORE_DATA_LEN(MsgFilePart, tmp_str.unicode(), tmp_str.length() * 2, true);
01341 tmp = msgSize();
01342 STORE_DATA(MsgSizePart, tmp);
01343 tmp = folderOffset();
01344 STORE_DATA(MsgOffsetPart, tmp);
01345 tmp = date();
01346 STORE_DATA(MsgDatePart, tmp);
01347 tmp = (signatureState() << 16) | encryptionState();
01348 STORE_DATA(MsgCryptoStatePart, tmp);
01349 tmp = mdnSentState();
01350 STORE_DATA(MsgMDNSentPart, tmp);
01351
01352 tmp_str = replyToAuxIdMD5().stripWhiteSpace();
01353 STORE_DATA_LEN(MsgReplyToAuxIdMD5Part, tmp_str.unicode(), tmp_str.length() * 2, true);
01354
01355 tmp_str = strippedSubjectMD5().stripWhiteSpace();
01356 STORE_DATA_LEN(MsgStrippedSubjectMD5Part, tmp_str.unicode(), tmp_str.length() * 2, true);
01357
01358 tmp = status();
01359 STORE_DATA(MsgStatusPart, tmp);
01360
01361 tmp = msgSizeServer();
01362 STORE_DATA(MsgSizeServerPart, tmp);
01363 tmp = UID();
01364 STORE_DATA(MsgUIDPart, tmp);
01365
01366 return ret;
01367 }
01368 #undef STORE_DATA_LEN
01369 #undef STORE_DATA
01370
01371 bool KMMsgBase::syncIndexString() const
01372 {
01373 if(!dirty())
01374 return TRUE;
01375 int len;
01376 const uchar *buffer = asIndexString(len);
01377 if (len == mIndexLength) {
01378 Q_ASSERT(storage()->mIndexStream);
01379 fseek(storage()->mIndexStream, mIndexOffset, SEEK_SET);
01380 assert( mIndexOffset > 0 );
01381 fwrite( buffer, len, 1, storage()->mIndexStream);
01382 return TRUE;
01383 }
01384 return FALSE;
01385 }
01386
01387 static QStringList sReplySubjPrefixes, sForwardSubjPrefixes;
01388 static bool sReplaceSubjPrefix, sReplaceForwSubjPrefix;
01389
01390
01391 void KMMsgBase::readConfig()
01392 {
01393 KConfigGroup composerGroup( KMKernel::config(), "Composer" );
01394 sReplySubjPrefixes = composerGroup.readListEntry("reply-prefixes", ',');
01395 if (sReplySubjPrefixes.isEmpty())
01396 sReplySubjPrefixes << "Re\\s*:" << "Re\\[\\d+\\]:" << "Re\\d+:";
01397 sReplaceSubjPrefix = composerGroup.readBoolEntry("replace-reply-prefix", true);
01398 sForwardSubjPrefixes = composerGroup.readListEntry("forward-prefixes", ',');
01399 if (sForwardSubjPrefixes.isEmpty())
01400 sForwardSubjPrefixes << "Fwd:" << "FW:";
01401 sReplaceForwSubjPrefix = composerGroup.readBoolEntry("replace-forward-prefix", true);
01402 }
01403
01404
01405
01406 QString KMMsgBase::stripOffPrefixes( const QString& str )
01407 {
01408 return replacePrefixes( str, sReplySubjPrefixes + sForwardSubjPrefixes,
01409 true, QString::null ).stripWhiteSpace();
01410 }
01411
01412
01413
01414 QString KMMsgBase::replacePrefixes( const QString& str,
01415 const QStringList& prefixRegExps,
01416 bool replace,
01417 const QString& newPrefix )
01418 {
01419 bool recognized = false;
01420
01421
01422
01423 QString bigRegExp = QString::fromLatin1("^(?:\\s+|(?:%1))+\\s*")
01424 .arg( prefixRegExps.join(")|(?:") );
01425 QRegExp rx( bigRegExp, false );
01426 if ( !rx.isValid() ) {
01427 kdWarning(5006) << "KMMessage::replacePrefixes(): bigRegExp = \""
01428 << bigRegExp << "\"\n"
01429 << "prefix regexp is invalid!" << endl;
01430
01431 recognized = str.startsWith( newPrefix );
01432 } else {
01433 QString tmp = str;
01434 if ( rx.search( tmp ) == 0 ) {
01435 recognized = true;
01436 if ( replace )
01437 return tmp.replace( 0, rx.matchedLength(), newPrefix + ' ' );
01438 }
01439 }
01440 if ( !recognized )
01441 return newPrefix + ' ' + str;
01442 else
01443 return str;
01444 }
01445
01446
01447 QString KMMsgBase::cleanSubject() const
01448 {
01449 return cleanSubject( sReplySubjPrefixes + sForwardSubjPrefixes,
01450 true, QString::null ).stripWhiteSpace();
01451 }
01452
01453
01454 QString KMMsgBase::cleanSubject( const QStringList & prefixRegExps,
01455 bool replace,
01456 const QString & newPrefix ) const
01457 {
01458 return KMMsgBase::replacePrefixes( subject(), prefixRegExps, replace,
01459 newPrefix );
01460 }
01461
01462
01463 QString KMMsgBase::forwardSubject() const {
01464 return cleanSubject( sForwardSubjPrefixes, sReplaceForwSubjPrefix, "Fwd:" );
01465 }
01466
01467
01468 QString KMMsgBase::replySubject() const {
01469 return cleanSubject( sReplySubjPrefixes, sReplaceSubjPrefix, "Re:" );
01470 }