1 /* -*- mode: C++; c-file-style: "gnu" -*-
2 Copyright (C) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
3 Copyright (c) 2009 Andras Mantia <andras@kdab.net>
4 Copyright (c) 2010 Leo Franchi <lfranchi@kde.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "nodehelper.h"
23 #include <kmime/kmime_content.h>
24 #include <kmime/kmime_message.h>
26 KMime::Content
*MessageCore::NodeHelper::nextSibling( const KMime::Content
*node
)
31 KMime::Content
*next
= 0;
32 KMime::Content
*parent
= node
->parent();
34 const KMime::Content::List contents
= parent
->contents();
35 const int index
= contents
.indexOf( const_cast<KMime::Content
*>( node
) ) + 1;
36 if ( index
< contents
.size() ) //next on the same level
37 next
= contents
.at( index
);
43 KMime::Content
*MessageCore::NodeHelper::next( KMime::Content
*node
, bool allowChildren
)
45 if ( allowChildren
) {
46 if ( KMime::Content
*child
= firstChild( node
) ) {
51 if ( KMime::Content
*sibling
= nextSibling( node
) ) {
55 for ( KMime::Content
*parent
= node
->parent() ; parent
;
56 parent
= parent
->parent() ) {
57 if ( KMime::Content
*sibling
= nextSibling( parent
) ) {
65 KMime::Content
*MessageCore::NodeHelper::firstChild( const KMime::Content
*node
)
70 KMime::Content
*child
= 0;
71 if ( !node
->contents().isEmpty() )
72 child
= node
->contents().at( 0 );
77 bool MessageCore::NodeHelper::isAttachment( KMime::Content
*node
)
79 if ( node
->head().isEmpty() )
82 const KMime::Headers::ContentType
* const contentType
= node
? node
->contentType( false ) : 0;
84 contentType
->mediaType().toLower() == "message" &&
85 contentType
->subType().toLower() == "rfc822" ) {
86 // Messages are always attachments. Normally message attachments created from KMail have a content
87 // disposition, but some mail clients omit that.
91 if ( !node
->contentDisposition( false ) )
94 return (node
->contentDisposition()->disposition() == KMime::Headers::CDattachment
);
97 bool MessageCore::NodeHelper::isHeuristicalAttachment( KMime::Content
*node
)
99 if ( isAttachment( node
) )
102 const KMime::Headers::ContentType
* const contentType
= node
? node
->contentType( false ) : 0;
103 if ( ( contentType
&& !contentType
->name().isEmpty() ) ||
104 ( node
->contentDisposition( false ) && !node
->contentDisposition()->filename().isEmpty() ) )