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