1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
|
/* -*- c++ -*-
partNode.cpp A node in a MIME tree.
This file is part of KMail, the KDE mail client.
Copyright (c) 2002 Klar�lvdalens Datakonsult AB
KMail is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
KMail is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
In addition, as a special exception, the copyright holders give
permission to link the code of this program with any edition of
the Qt library by Trolltech AS, Norway (or with modified versions
of Qt that use the same license as Qt), and distribute linked
combinations including the two. You must obey the GNU General
Public License in all respects for all of the code used other than
Qt. If you modify this file, you may extend this exception to
your version of the file, but you are not obligated to do so. If
you do not wish to do so, delete this exception statement from
your version.
*/
#include <config.h>
#include "partNode.h"
#include <klocale.h>
#include <kdebug.h>
#include "kmmimeparttree.h"
#include <mimelib/utility.h>
#include <qregexp.h>
#include <kasciistricmp.h>
#include "util.h"
/*
===========================================================================
S T A R T O F T E M P O R A R Y M I M E C O D E
===========================================================================
N O T E : The partNode structure will most likely be replaced by KMime.
It's purpose: Speed optimization for KDE 3. (khz, 28.11.01)
===========================================================================
*/
partNode::partNode()
: mRoot( 0 ), mNext( 0 ), mChild( 0 ),
mWasProcessed( false ),
mDwPart( 0 ),
mType( DwMime::kTypeUnknown ),
mSubType( DwMime::kSubtypeUnknown ),
mEncryptionState( KMMsgNotEncrypted ),
mSignatureState( KMMsgNotSigned ),
mMsgPartOk( false ),
mEncodedOk( false ),
mDeleteDwBodyPart( false ),
mMimePartTreeItem( 0 ),
mBodyPartMemento( 0 )
{
adjustDefaultType( this );
}
partNode::partNode( DwBodyPart* dwPart, int explicitType, int explicitSubType,
bool deleteDwBodyPart )
: mRoot( 0 ), mNext( 0 ), mChild( 0 ),
mWasProcessed( false ),
mDwPart( dwPart ),
mEncryptionState( KMMsgNotEncrypted ),
mSignatureState( KMMsgNotSigned ),
mMsgPartOk( false ),
mEncodedOk( false ),
mDeleteDwBodyPart( deleteDwBodyPart ),
mMimePartTreeItem( 0 ),
mBodyPartMemento( 0 )
{
if ( explicitType != DwMime::kTypeUnknown ) {
mType = explicitType; // this happens e.g. for the Root Node
mSubType = explicitSubType; // representing the _whole_ message
} else {
// kdDebug(5006) << "\n partNode::partNode() explicitType == DwMime::kTypeUnknown\n" << endl;
if(dwPart && dwPart->hasHeaders() && dwPart->Headers().HasContentType()) {
mType = (!dwPart->Headers().ContentType().Type())?DwMime::kTypeUnknown:dwPart->Headers().ContentType().Type();
mSubType = dwPart->Headers().ContentType().Subtype();
} else {
mType = DwMime::kTypeUnknown;
mSubType = DwMime::kSubtypeUnknown;
}
}
#ifdef DEBUG
{
DwString type, subType;
DwTypeEnumToStr( mType, type );
DwSubtypeEnumToStr( mSubType, subType );
kdDebug(5006) << "\npartNode::partNode() " << type.c_str() << "/" << subType.c_str() << "\n" << endl;
}
#endif
}
partNode * partNode::fromMessage( const KMMessage * msg ) {
if ( !msg )
return 0;
int mainType = msg->type();
int mainSubType = msg->subtype();
if( (DwMime::kTypeNull == mainType)
|| (DwMime::kTypeUnknown == mainType) ){
mainType = DwMime::kTypeText;
mainSubType = DwMime::kSubtypePlain;
}
// we don't want to treat the top-level part special. mimelib does
// (Message vs. BodyPart, with common base class Entity). But we
// used DwBodyPart, not DwEntiy everywhere. *shrug*. DwStrings are
// subscrib-shared, so we just force mimelib to parse the whole mail
// as just another DwBodyPart...
DwBodyPart * mainBody = new DwBodyPart( *msg->getTopLevelPart() );
partNode * root = new partNode( mainBody, mainType, mainSubType, true );
root->buildObjectTree();
root->setFromAddress( msg->from() );
root->dump();
return root;
}
partNode::partNode( bool deleteDwBodyPart, DwBodyPart* dwPart )
: mRoot( 0 ), mNext( 0 ), mChild( 0 ),
mWasProcessed( false ),
mDwPart( dwPart ),
mEncryptionState( KMMsgNotEncrypted ),
mSignatureState( KMMsgNotSigned ),
mMsgPartOk( false ),
mEncodedOk( false ),
mDeleteDwBodyPart( deleteDwBodyPart ),
mMimePartTreeItem( 0 ),
mBodyPartMemento( 0 )
{
if ( dwPart && dwPart->hasHeaders() && dwPart->Headers().HasContentType() ) {
mType = (!dwPart->Headers().ContentType().Type())?DwMime::kTypeUnknown:dwPart->Headers().ContentType().Type();
mSubType = dwPart->Headers().ContentType().Subtype();
} else {
mType = DwMime::kTypeUnknown;
mSubType = DwMime::kSubtypeUnknown;
}
}
partNode::~partNode() {
if( mDeleteDwBodyPart )
delete mDwPart;
mDwPart = 0;
delete mChild; mChild = 0;
delete mNext; mNext = 0;
delete mBodyPartMemento; mBodyPartMemento = 0;
}
#ifndef NDEBUG
void partNode::dump( int chars ) const {
kdDebug(5006) << QString().fill( ' ', chars ) << "+ "
<< typeString() << '/' << subTypeString() << endl;
if ( mChild )
mChild->dump( chars + 1 );
if ( mNext )
mNext->dump( chars );
}
#else
void partNode::dump( int ) const {}
#endif
const QCString & partNode::encodedBody() {
if ( mEncodedOk )
return mEncodedBody;
if ( mDwPart )
mEncodedBody = KMail::Util::CString( mDwPart->Body().AsString() );
else
mEncodedBody = 0;
mEncodedOk = true;
return mEncodedBody;
}
void partNode::buildObjectTree( bool processSiblings )
{
partNode* curNode = this;
while( curNode && curNode->dwPart() ) {
//dive into multipart messages
while( DwMime::kTypeMultipart == curNode->type() ) {
partNode * newNode = new partNode( curNode->dwPart()->Body().FirstBodyPart() );
curNode->setFirstChild( newNode );
curNode = newNode;
}
// go up in the tree until reaching a node with next
// (or the last top-level node)
while( curNode
&& !( curNode->dwPart()
&& curNode->dwPart()->Next() ) ) {
curNode = curNode->mRoot;
}
// we might have to leave when all children have been processed
if( this == curNode && !processSiblings )
return;
// store next node
if( curNode && curNode->dwPart() && curNode->dwPart()->Next() ) {
partNode* nextNode = new partNode( curNode->dwPart()->Next() );
curNode->setNext( nextNode );
curNode = nextNode;
} else
curNode = 0;
}
}
QCString partNode::typeString() const {
DwString s;
DwTypeEnumToStr( type(), s );
return s.c_str();
}
QCString partNode::subTypeString() const {
DwString s;
DwSubtypeEnumToStr( subType(), s );
return s.c_str();
}
int partNode::childCount() const {
int count = 0;
for ( partNode * child = firstChild() ; child ; child = child->nextSibling() )
++ count;
return count;
}
QString partNode::contentTypeParameter( const char * name ) const {
if ( !mDwPart || !mDwPart->hasHeaders() )
return QString::null;
DwHeaders & headers = mDwPart->Headers();
if ( !headers.HasContentType() )
return QString::null;
DwString attr = name;
attr.ConvertToLowerCase();
for ( DwParameter * param = headers.ContentType().FirstParameter() ; param ; param = param->Next() ) {
DwString this_attr = param->Attribute();
this_attr.ConvertToLowerCase(); // what a braindead design!
if ( this_attr == attr )
return QString::fromLatin1( param->Value().data(), param->Value().size() );
// warning: misses rfc2231 handling!
}
return QString::null;
}
KMMsgEncryptionState partNode::overallEncryptionState() const
{
KMMsgEncryptionState myState = KMMsgEncryptionStateUnknown;
if( mEncryptionState == KMMsgNotEncrypted ) {
// NOTE: children are tested ONLY when parent is not encrypted
if( mChild )
myState = mChild->overallEncryptionState();
else
myState = KMMsgNotEncrypted;
}
else { // part is partially or fully encrypted
myState = mEncryptionState;
}
// siblings are tested always
if( mNext ) {
KMMsgEncryptionState otherState = mNext->overallEncryptionState();
switch( otherState ) {
case KMMsgEncryptionStateUnknown:
break;
case KMMsgNotEncrypted:
if( myState == KMMsgFullyEncrypted )
myState = KMMsgPartiallyEncrypted;
else if( myState != KMMsgPartiallyEncrypted )
myState = KMMsgNotEncrypted;
break;
case KMMsgPartiallyEncrypted:
myState = KMMsgPartiallyEncrypted;
break;
case KMMsgFullyEncrypted:
if( myState != KMMsgFullyEncrypted )
myState = KMMsgPartiallyEncrypted;
break;
case KMMsgEncryptionProblematic:
break;
}
}
//kdDebug(5006) << "\n\n KMMsgEncryptionState: " << myState << endl;
return myState;
}
KMMsgSignatureState partNode::overallSignatureState() const
{
KMMsgSignatureState myState = KMMsgSignatureStateUnknown;
if( mSignatureState == KMMsgNotSigned ) {
// children are tested ONLY when parent is not signed
if( mChild )
myState = mChild->overallSignatureState();
else
myState = KMMsgNotSigned;
}
else { // part is partially or fully signed
myState = mSignatureState;
}
// siblings are tested always
if( mNext ) {
KMMsgSignatureState otherState = mNext->overallSignatureState();
switch( otherState ) {
case KMMsgSignatureStateUnknown:
break;
case KMMsgNotSigned:
if( myState == KMMsgFullySigned )
myState = KMMsgPartiallySigned;
else if( myState != KMMsgPartiallySigned )
myState = KMMsgNotSigned;
break;
case KMMsgPartiallySigned:
myState = KMMsgPartiallySigned;
break;
case KMMsgFullySigned:
if( myState != KMMsgFullySigned )
myState = KMMsgPartiallySigned;
break;
case KMMsgEncryptionProblematic:
break;
}
}
//kdDebug(5006) << "\n\n KMMsgSignatureState: " << myState << endl;
return myState;
}
int partNode::nodeId() const
{
int curId = 0;
partNode* rootNode = const_cast<partNode*>( this );
while( rootNode->mRoot )
rootNode = rootNode->mRoot;
return rootNode->calcNodeIdOrFindNode( curId, this, 0, 0 );
}
partNode* partNode::findId( int id )
{
int curId = 0;
partNode* rootNode = this;
while( rootNode->mRoot )
rootNode = rootNode->mRoot;
partNode* foundNode;
rootNode->calcNodeIdOrFindNode( curId, 0, id, &foundNode );
return foundNode;
}
int partNode::calcNodeIdOrFindNode( int &curId, const partNode* findNode, int findId, partNode** foundNode )
{
// We use the same algorithm to determine the id of a node and
// to find the node when id is known.
curId++;
// check for node ?
if( findNode && this == findNode )
return curId;
// check for id ?
if( foundNode && curId == findId ) {
*foundNode = this;
return curId;
}
if( mChild )
{
int res = mChild->calcNodeIdOrFindNode( curId, findNode, findId, foundNode );
if (res != -1) return res;
}
if( mNext )
return mNext->calcNodeIdOrFindNode( curId, findNode, findId, foundNode );
if( foundNode )
*foundNode = 0;
return -1;
}
partNode* partNode::findType( int type, int subType, bool deep, bool wide )
{
#ifndef NDEBUG
DwString typeStr, subTypeStr;
DwTypeEnumToStr( mType, typeStr );
DwSubtypeEnumToStr( mSubType, subTypeStr );
kdDebug(5006) << "partNode::findType() is looking at " << typeStr.c_str()
<< "/" << subTypeStr.c_str() << endl;
#endif
if( (mType != DwMime::kTypeUnknown)
&& ( (type == DwMime::kTypeUnknown)
|| (type == mType) )
&& ( (subType == DwMime::kSubtypeUnknown)
|| (subType == mSubType) ) )
return this;
if ( mChild && deep )
return mChild->findType( type, subType, deep, wide );
if ( mNext && wide )
return mNext->findType( type, subType, deep, wide );
return 0;
}
partNode* partNode::findNodeForDwPart( DwBodyPart* part )
{
partNode* found = 0;
if( kasciistricmp( dwPart()->partId(), part->partId() ) == 0 )
return this;
if( mChild )
found = mChild->findNodeForDwPart( part );
if( mNext && !found )
found = mNext->findNodeForDwPart( part );
return found;
}
partNode* partNode::findTypeNot( int type, int subType, bool deep, bool wide )
{
if( (mType != DwMime::kTypeUnknown)
&& ( (type == DwMime::kTypeUnknown)
|| (type != mType) )
&& ( (subType == DwMime::kSubtypeUnknown)
|| (subType != mSubType) ) )
return this;
if ( mChild && deep )
return mChild->findTypeNot( type, subType, deep, wide );
if ( mNext && wide )
return mNext->findTypeNot( type, subType, deep, wide );
return 0;
}
void partNode::fillMimePartTree( KMMimePartTreeItem* parentItem,
KMMimePartTree* mimePartTree,
QString labelDescr,
QString labelCntType,
QString labelEncoding,
KIO::filesize_t size,
bool revertOrder )
{
if( parentItem || mimePartTree ) {
if( mNext )
mNext->fillMimePartTree( parentItem, mimePartTree,
QString::null, QString::null, QString::null, 0,
revertOrder );
QString cntDesc, cntType, cntEnc;
KIO::filesize_t cntSize = 0;
if( labelDescr.isEmpty() ) {
DwHeaders* headers = 0;
if( mDwPart && mDwPart->hasHeaders() )
headers = &mDwPart->Headers();
if( headers && headers->HasSubject() )
cntDesc = KMMsgBase::decodeRFC2047String( headers->Subject().AsString().c_str() );
if( headers && headers->HasContentType()) {
cntType = headers->ContentType().TypeStr().c_str();
cntType += '/';
cntType += headers->ContentType().SubtypeStr().c_str();
}
else
cntType = "text/plain";
if( cntDesc.isEmpty() )
cntDesc = msgPart().contentDescription();
if( cntDesc.isEmpty() )
cntDesc = msgPart().name().stripWhiteSpace();
if( cntDesc.isEmpty() )
cntDesc = msgPart().fileName();
if( cntDesc.isEmpty() ) {
if( mRoot && mRoot->mRoot )
cntDesc = i18n("internal part");
else
cntDesc = i18n("body part");
}
cntEnc = msgPart().contentTransferEncodingStr();
if( mDwPart )
cntSize = mDwPart->BodySize();
} else {
cntDesc = labelDescr;
cntType = labelCntType;
cntEnc = labelEncoding;
cntSize = size;
}
// remove linebreak+whitespace from folded Content-Description
cntDesc.replace( QRegExp("\\n\\s*"), " " );
kdDebug(5006) << " Inserting one item into MimePartTree" << endl;
kdDebug(5006) << " Content-Type: " << cntType << endl;
if( parentItem )
mMimePartTreeItem = new KMMimePartTreeItem( parentItem,
this,
cntDesc,
cntType,
cntEnc,
cntSize,
revertOrder );
else if( mimePartTree )
mMimePartTreeItem = new KMMimePartTreeItem( mimePartTree,
this,
cntDesc,
cntType,
cntEnc,
cntSize );
mMimePartTreeItem->setOpen( true );
if( mChild )
mChild->fillMimePartTree( mMimePartTreeItem, 0,
QString::null, QString::null, QString::null, 0,
revertOrder );
}
}
void partNode::adjustDefaultType( partNode* node )
{
// Only bodies of 'Multipart/Digest' objects have
// default type 'Message/RfC822'. All other bodies
// have default type 'Text/Plain' (khz, 5.12.2001)
if( node && DwMime::kTypeUnknown == node->type() ) {
if( node->mRoot
&& DwMime::kTypeMultipart == node->mRoot->type()
&& DwMime::kSubtypeDigest == node->mRoot->subType() ) {
node->setType( DwMime::kTypeMessage );
node->setSubType( DwMime::kSubtypeRfc822 );
}
else
{
node->setType( DwMime::kTypeText );
node->setSubType( DwMime::kSubtypePlain );
}
}
}
bool partNode::isAttachment() const
{
if( !dwPart() )
return false;
if ( !dwPart()->hasHeaders() )
return false;
DwHeaders& headers = dwPart()->Headers();
if( !headers.HasContentDisposition() )
return false;
return ( headers.ContentDisposition().DispositionType()
== DwMime::kDispTypeAttachment );
}
bool partNode::isHeuristicalAttachment() const {
if ( isAttachment() )
return true;
const KMMessagePart & p = msgPart();
return !p.fileName().isEmpty() || !p.name().isEmpty() ;
}
partNode * partNode::next( bool allowChildren ) const {
if ( allowChildren )
if ( partNode * c = firstChild() )
return c;
if ( partNode * s = nextSibling() )
return s;
for ( partNode * p = parentNode() ; p ; p = p->parentNode() )
if ( partNode * s = p->nextSibling() )
return s;
return 0;
}
bool partNode::isFirstTextPart() const {
if ( type() != DwMime::kTypeText )
return false;
const partNode * root = this;
// go up until we reach the root node of a message (of the actual message or
// of an attached message)
while ( const partNode * p = root->parentNode() ) {
if ( p->type() == DwMime::kTypeMessage )
break;
else
root = p;
}
for ( const partNode * n = root ; n ; n = n->next() )
if ( n->type() == DwMime::kTypeText )
return n == this;
kdFatal() << "partNode::isFirstTextPart(): Didn't expect to end up here..." << endl;
return false; // make comiler happy
}
bool partNode::hasContentDispositionInline() const
{
if( !dwPart() )
return false;
DwHeaders& headers = dwPart()->Headers();
if( headers.HasContentDisposition() )
return ( headers.ContentDisposition().DispositionType()
== DwMime::kDispTypeInline );
else
return false;
}
const QString& partNode::trueFromAddress() const
{
const partNode* node = this;
while( node->mFromAddress.isEmpty() && node->mRoot )
node = node->mRoot;
return node->mFromAddress;
}
|