27 #include <kmime/kmime_content.h>
28 #include <KMime/Message>
35 Q_DECLARE_METATYPE(KMime::Content*)
36 Q_DECLARE_METATYPE(KMime::ContentIndex)
38 using namespace MessageViewer;
48 int contentIndexUp( KMime::ContentIndex &index )
50 Q_ASSERT( index.isValid() );
51 QStringList ids = index.toString().split( QLatin1Char(
'.') );
52 const QString lastId = ids.takeLast();
53 index = KMime::ContentIndex( ids.join( QLatin1String(
".") ) );
54 return lastId.toInt();
57 QString descriptionForContent( KMime::Content *content )
59 KMime::Message *
const message =
dynamic_cast<KMime::Message*
>( content );
60 if ( message && message->subject(
false ) )
61 return message->subject()->asUnicodeString();
62 const QString name = NodeHelper::fileName( content );
63 if ( !name.isEmpty() )
65 if ( content->contentDescription(
false ) ) {
66 const QString desc = content->contentDescription()->asUnicodeString();
67 if ( !desc.isEmpty() )
70 return i18n(
"body part" );
73 QString mimeTypeForContent( KMime::Content *content )
75 if ( content->contentType(
false ) )
76 return QString::fromLatin1( content->contentType()->mimeType() );
80 QString typeForContent( KMime::Content *content )
82 if ( content->contentType(
false ) )
84 const QString contentMimeType = QString::fromLatin1(content->contentType()->mimeType());
85 KMimeType::Ptr mimeType = KMimeType::mimeType( contentMimeType );
86 if ( mimeType.isNull() )
87 return contentMimeType;
88 return mimeType->comment();
94 QString sizeOfContent( KMime::Content *content )
96 if ( content->body().isEmpty() )
98 return KGlobal::locale()->formatByteSize( content->body().size() );
101 KIcon iconForContent( KMime::Content *content )
103 if ( content->contentType(
false ) )
105 KMimeType::Ptr mimeType = KMimeType::mimeType( QString::fromLatin1( content->contentType()->mimeType() ) );
106 if ( mimeType.isNull() || mimeType->name() == QLatin1String(
"application/octet-stream") ) {
107 const QString name = descriptionForContent(content);
110 if ( mimeType.isNull() || mimeType->iconName().isEmpty() )
112 if( mimeType->name().startsWith( QLatin1String(
"multipart/" ) ) )
113 return KIcon( QLatin1String(
"folder") );
114 return KIcon( mimeType->iconName() );
120 KMime::Content *root;
123 MimeTreeModel::MimeTreeModel(
QObject * parent) :
148 if ( !parent.isValid() ) {
150 return QModelIndex();
151 return createIndex( row, column, d->root );
154 KMime::Content *parentContent =
static_cast<KMime::Content*
>( parent.internalPointer() );
155 if ( !parentContent || parentContent->contents().count() <= row || row < 0 )
156 return QModelIndex();
157 KMime::Content *content = parentContent->contents().at( row );
158 return createIndex( row, column, content );
163 if ( !index.isValid() )
164 return QModelIndex();
165 KMime::Content *currentContent =
static_cast<KMime::Content*
>( index.internalPointer() );
166 if ( !currentContent )
167 return QModelIndex();
169 KMime::ContentIndex currentIndex = d->root->indexForContent( currentContent );
170 if ( !currentIndex.isValid() )
171 return QModelIndex();
172 d->contentIndexUp( currentIndex );
173 KMime::Content *parentContent = d->root->content( currentIndex );
175 if ( currentIndex.isValid() )
176 row = d->contentIndexUp( currentIndex ) - 1;
178 return createIndex( row, 0, parentContent );
185 if ( !parent.isValid() )
187 KMime::Content *parentContent =
static_cast<KMime::Content*
>( parent.internalPointer() );
189 return parentContent->contents().count();
201 KMime::Content *content =
static_cast<KMime::Content*
>( index.internalPointer() );
204 if ( role == Qt::ToolTipRole ) {
208 if ( role == Qt::DisplayRole ) {
209 switch( index.column() ) {
211 return d->descriptionForContent( content );
213 return d->typeForContent( content );
215 return d->sizeOfContent( content );
218 if ( role == Qt::DecorationRole && index.column() == 0 ) {
219 return d->iconForContent( content );
222 return QVariant::fromValue( d->root->indexForContent( content ) );
224 return QVariant::fromValue( content );
226 return d->mimeTypeForContent( content );
228 KMime::Message* topLevelMsg =
dynamic_cast<KMime::Message*
>( d->root );
231 return topLevelMsg->mainBodyPart() == content;
234 KMime::Message* topLevelMsg =
dynamic_cast<KMime::Message*
>( d->root );
237 return topLevelMsg->mainBodyPart( content->contentType()->mimeType() ) == content;
244 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole ) {
247 return i18n(
"Description" );
249 return i18n(
"Type" );
251 return i18n(
"Size" );
254 return QAbstractItemModel::headerData( section, orientation, role );
257 #include "mimetreemodel.moc"
int rowCount(const QModelIndex &parent=QModelIndex()) const
A model representing the mime part tree of a message.
QModelIndex parent(const QModelIndex &index) const
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
void setRoot(KMime::Content *root)
MESSAGEVIEWER_EXPORT KMimeType::Ptr mimetype(const QString &name)
Search mimetype from filename when mimetype is empty or application/octet-stream. ...
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
int columnCount(const QModelIndex &parent=QModelIndex()) const