kilobyte is kB not kb
[kdepim.git] / messageviewer / mimetreemodel.cpp
blob99c608acd8dc1350da11a63e8750d1261f148f22
1 /*
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
17 02110-1301, USA.
20 #include <config-messageviewer.h>
22 #include "mimetreemodel.h"
24 #include "nodehelper.h"
26 #include <kmime/kmime_content.h>
27 #include <KMime/Message>
29 #include <KDebug>
30 #include <KIcon>
31 #include <KLocale>
32 #include <KMimeType>
34 Q_DECLARE_METATYPE(KMime::Content*)
35 Q_DECLARE_METATYPE(KMime::ContentIndex)
37 using namespace MessageViewer;
39 class MimeTreeModel::Private
41 public:
42 Private() :
43 root ( 0 )
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() )
63 return name;
64 if ( content->contentDescription( false ) ) {
65 const QString desc = content->contentDescription()->asUnicodeString();
66 if ( !desc.isEmpty() )
67 return desc;
69 return i18n( "body part" );
72 QString mimeTypeForContent( KMime::Content *content )
74 if ( content->contentType( false ) )
75 return QString::fromLatin1( content->contentType()->mimeType() );
76 return QString();
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();
88 else
89 return QString();
92 QString sizeOfContent( KMime::Content *content )
94 if ( content->body().isEmpty() )
95 return QString();
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() )
105 return KIcon();
106 if( mimeType->name().startsWith( QLatin1String( "multipart/" ) ) )
107 return KIcon( "folder" );
108 return KIcon( mimeType->iconName() );
110 else
111 return KIcon();
114 KMime::Content *root;
117 MimeTreeModel::MimeTreeModel(QObject * parent) :
118 QAbstractItemModel( parent ),
119 d ( new Private )
123 MimeTreeModel::~MimeTreeModel()
125 delete d;
128 void MimeTreeModel::setRoot(KMime::Content * root)
130 d->root = root;
131 reset();
134 KMime::Content* MimeTreeModel::root()
136 return d->root;
140 QModelIndex MimeTreeModel::index(int row, int column, const QModelIndex &parent) const
142 if ( !parent.isValid() ) {
143 if ( row != 0 )
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 );
168 int row = 0;
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
177 if ( !d->root )
178 return 0;
179 if ( !parent.isValid() )
180 return 1;
181 KMime::Content *parentContent = static_cast<KMime::Content*>( parent.internalPointer() );
182 if ( parentContent )
183 return parentContent->contents().count();
184 return 0;
187 int MimeTreeModel::columnCount(const QModelIndex & parent) const
189 Q_UNUSED( parent );
190 return 3;
193 QVariant MimeTreeModel::data(const QModelIndex & index, int role) const
195 KMime::Content *content = static_cast<KMime::Content*>( index.internalPointer() );
196 if ( !content )
197 return QVariant();
198 if ( role == Qt::ToolTipRole ) {
199 // TODO
200 //return d->root->indexForContent( content ).toString();
202 if ( role == Qt::DisplayRole ) {
203 switch( index.column() ) {
204 case 0:
205 return d->descriptionForContent( content );
206 case 1:
207 return d->typeForContent( content );
208 case 2:
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 );
223 if ( !topLevelMsg )
224 return false;
225 return topLevelMsg->mainBodyPart() == content;
227 if ( role == AlternativeBodyPartRole ) {
228 KMime::Message* topLevelMsg = dynamic_cast<KMime::Message*>( d->root );
229 if ( !topLevelMsg )
230 return false;
231 return topLevelMsg->mainBodyPart( content->contentType()->mimeType() ) == content;
233 return QVariant();
236 QVariant MimeTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
238 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole ) {
239 switch ( section ) {
240 case 0:
241 return i18n( "Description" );
242 case 1:
243 return i18n( "Type" );
244 case 2:
245 return i18n( "Size" );
248 return QAbstractItemModel::headerData( section, orientation, role );
251 #include "mimetreemodel.moc"