2 Copyright (c) 2007, 2008 Volker Krause <vkrause@kde.org>
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 #include <config-messageviewer.h>
22 #include "mimetreemodel.h"
24 #include "nodehelper.h"
26 #include <kmime/kmime_content.h>
27 #include <KMime/Message>
34 Q_DECLARE_METATYPE(KMime::Content
*)
35 Q_DECLARE_METATYPE(KMime::ContentIndex
)
37 using namespace MessageViewer
;
39 class MimeTreeModel::Private
46 // FIXME: this should actually be a member function of ContentIndex
47 int contentIndexUp( KMime::ContentIndex
&index
)
49 Q_ASSERT( index
.isValid() );
50 QStringList ids
= index
.toString().split( QLatin1Char('.') );
51 const QString lastId
= ids
.takeLast();
52 index
= KMime::ContentIndex( ids
.join( QLatin1String(".") ) );
53 return lastId
.toInt();
56 QString
descriptionForContent( KMime::Content
*content
)
58 KMime::Message
* const message
= dynamic_cast<KMime::Message
*>( content
);
59 if ( message
&& message
->subject( false ) )
60 return message
->subject()->asUnicodeString();
61 const QString name
= NodeHelper::fileName( content
);
62 if ( !name
.isEmpty() )
64 if ( content
->contentDescription( false ) ) {
65 const QString desc
= content
->contentDescription()->asUnicodeString();
66 if ( !desc
.isEmpty() )
69 return i18n( "body part" );
72 QString
mimeTypeForContent( KMime::Content
*content
)
74 if ( content
->contentType( false ) )
75 return QString::fromLatin1( content
->contentType()->mimeType() );
79 QString
typeForContent( KMime::Content
*content
)
81 if ( content
->contentType( false ) )
83 KMimeType::Ptr mimeType
= KMimeType::mimeType( QString::fromLatin1( content
->contentType()->mimeType() ) );
84 if ( mimeType
.isNull() )
85 return QString::fromLatin1( content
->contentType()->mimeType() );
86 return mimeType
->comment();
92 QString
sizeOfContent( KMime::Content
*content
)
94 if ( content
->body().isEmpty() )
96 return KGlobal::locale()->formatByteSize( content
->body().size() );
99 KIcon
iconForContent( KMime::Content
*content
)
101 if ( content
->contentType( false ) )
103 KMimeType::Ptr mimeType
= KMimeType::mimeType( QString::fromLatin1( content
->contentType()->mimeType() ) );
104 if ( mimeType
.isNull() || mimeType
->iconName().isEmpty() )
106 if( mimeType
->name().startsWith( QLatin1String( "multipart/" ) ) )
107 return KIcon( "folder" );
108 return KIcon( mimeType
->iconName() );
114 KMime::Content
*root
;
117 MimeTreeModel::MimeTreeModel(QObject
* parent
) :
118 QAbstractItemModel( parent
),
123 MimeTreeModel::~MimeTreeModel()
128 void MimeTreeModel::setRoot(KMime::Content
* root
)
134 KMime::Content
* MimeTreeModel::root()
140 QModelIndex
MimeTreeModel::index(int row
, int column
, const QModelIndex
&parent
) const
142 if ( !parent
.isValid() ) {
144 return QModelIndex();
145 return createIndex( row
, column
, d
->root
);
148 KMime::Content
*parentContent
= static_cast<KMime::Content
*>( parent
.internalPointer() );
149 if ( !parentContent
|| parentContent
->contents().count() <= row
|| row
< 0 )
150 return QModelIndex();
151 KMime::Content
*content
= parentContent
->contents().at( row
);
152 return createIndex( row
, column
, content
);
155 QModelIndex
MimeTreeModel::parent(const QModelIndex
& index
) const
157 if ( !index
.isValid() )
158 return QModelIndex();
159 KMime::Content
*currentContent
= static_cast<KMime::Content
*>( index
.internalPointer() );
160 if ( !currentContent
)
161 return QModelIndex();
163 KMime::ContentIndex currentIndex
= d
->root
->indexForContent( currentContent
);
164 if ( !currentIndex
.isValid() )
165 return QModelIndex();
166 d
->contentIndexUp( currentIndex
);
167 KMime::Content
*parentContent
= d
->root
->content( currentIndex
);
169 if ( currentIndex
.isValid() )
170 row
= d
->contentIndexUp( currentIndex
) - 1; // 1 based -> 0 based
172 return createIndex( row
, 0, parentContent
);
175 int MimeTreeModel::rowCount(const QModelIndex
& parent
) const
179 if ( !parent
.isValid() )
181 KMime::Content
*parentContent
= static_cast<KMime::Content
*>( parent
.internalPointer() );
183 return parentContent
->contents().count();
187 int MimeTreeModel::columnCount(const QModelIndex
& parent
) const
193 QVariant
MimeTreeModel::data(const QModelIndex
& index
, int role
) const
195 KMime::Content
*content
= static_cast<KMime::Content
*>( index
.internalPointer() );
198 if ( role
== Qt::ToolTipRole
) {
200 //return d->root->indexForContent( content ).toString();
202 if ( role
== Qt::DisplayRole
) {
203 switch( index
.column() ) {
205 return d
->descriptionForContent( content
);
207 return d
->typeForContent( content
);
209 return d
->sizeOfContent( content
);
212 if ( role
== Qt::DecorationRole
&& index
.column() == 0 ) {
213 return d
->iconForContent( content
);
215 if ( role
== ContentIndexRole
)
216 return QVariant::fromValue( d
->root
->indexForContent( content
) );
217 if ( role
== ContentRole
)
218 return QVariant::fromValue( content
);
219 if ( role
== MimeTypeRole
)
220 return d
->mimeTypeForContent( content
);
221 if ( role
== MainBodyPartRole
) {
222 KMime::Message
* topLevelMsg
= dynamic_cast<KMime::Message
*>( d
->root
);
225 return topLevelMsg
->mainBodyPart() == content
;
227 if ( role
== AlternativeBodyPartRole
) {
228 KMime::Message
* topLevelMsg
= dynamic_cast<KMime::Message
*>( d
->root
);
231 return topLevelMsg
->mainBodyPart( content
->contentType()->mimeType() ) == content
;
236 QVariant
MimeTreeModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
238 if ( orientation
== Qt::Horizontal
&& role
== Qt::DisplayRole
) {
241 return i18n( "Description" );
243 return i18n( "Type" );
245 return i18n( "Size" );
248 return QAbstractItemModel::headerData( section
, orientation
, role
);
251 #include "mimetreemodel.moc"