Make the boss happy.
[kdepim.git] / messagecore / nodehelper.cpp
blob67ade0c1a0aa736d822e239cff970412f0e85ad2
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 )
28 if ( !node )
29 return 0;
31 KMime::Content *next = 0;
32 KMime::Content *parent = node->parent();
33 if ( 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 );
40 return next;
43 KMime::Content *MessageCore::NodeHelper::next( KMime::Content *node, bool allowChildren )
45 if ( allowChildren ) {
46 if ( KMime::Content *child = firstChild( node ) ) {
47 return child;
51 if ( KMime::Content *sibling = nextSibling( node ) ) {
52 return sibling;
55 for ( KMime::Content *parent = node->parent() ; parent ;
56 parent = parent->parent() ) {
57 if ( KMime::Content *sibling = nextSibling( parent ) ) {
58 return sibling;
62 return 0;
65 KMime::Content *MessageCore::NodeHelper::firstChild( const KMime::Content *node )
67 if ( !node )
68 return 0;
70 KMime::Content *child = 0;
71 if ( !node->contents().isEmpty() )
72 child = node->contents().at( 0 );
74 return child;
77 bool MessageCore::NodeHelper::isAttachment( KMime::Content *node )
79 if ( node->head().isEmpty() )
80 return false;
82 const KMime::Headers::ContentType* const contentType = node ? node->contentType( false ) : 0;
83 if ( contentType &&
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.
88 return true;
91 if ( !node->contentDisposition( false ) )
92 return false;
94 return (node->contentDisposition()->disposition() == KMime::Headers::CDattachment);
97 bool MessageCore::NodeHelper::isHeuristicalAttachment( KMime::Content *node )
99 if ( isAttachment( node ) )
100 return true;
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() ) )
105 return true;
107 return false;