• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

messageviewer

  • sources
  • kde-4.12
  • kdepim
  • messageviewer
  • viewer
mimetreemodel.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2007, 2008 Volker Krause <vkrause@kde.org>
3 
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.
8 
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.
13 
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.
18 */
19 
20 
21 
22 #include "utils/util.h"
23 #include "mimetreemodel.h"
24 
25 #include "nodehelper.h"
26 
27 #include <kmime/kmime_content.h>
28 #include <KMime/Message>
29 
30 #include <KDebug>
31 #include <KIcon>
32 #include <KLocale>
33 #include <KMimeType>
34 
35 Q_DECLARE_METATYPE(KMime::Content*)
36 Q_DECLARE_METATYPE(KMime::ContentIndex)
37 
38 using namespace MessageViewer;
39 
40 class MimeTreeModel::Private
41 {
42  public:
43  Private() :
44  root ( 0 )
45  {}
46 
47  // FIXME: this should actually be a member function of ContentIndex
48  int contentIndexUp( KMime::ContentIndex &index )
49  {
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();
55  }
56 
57  QString descriptionForContent( KMime::Content *content )
58  {
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() )
64  return name;
65  if ( content->contentDescription( false ) ) {
66  const QString desc = content->contentDescription()->asUnicodeString();
67  if ( !desc.isEmpty() )
68  return desc;
69  }
70  return i18n( "body part" );
71  }
72 
73  QString mimeTypeForContent( KMime::Content *content )
74  {
75  if ( content->contentType( false ) )
76  return QString::fromLatin1( content->contentType()->mimeType() );
77  return QString();
78  }
79 
80  QString typeForContent( KMime::Content *content )
81  {
82  if ( content->contentType( false ) )
83  {
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();
89  }
90  else
91  return QString();
92  }
93 
94  QString sizeOfContent( KMime::Content *content )
95  {
96  if ( content->body().isEmpty() )
97  return QString();
98  return KGlobal::locale()->formatByteSize( content->body().size() );
99  }
100 
101  KIcon iconForContent( KMime::Content *content )
102  {
103  if ( content->contentType( false ) )
104  {
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);
108  mimeType = MessageViewer::Util::mimetype(name);
109  }
110  if ( mimeType.isNull() || mimeType->iconName().isEmpty() )
111  return KIcon();
112  if( mimeType->name().startsWith( QLatin1String( "multipart/" ) ) )
113  return KIcon( QLatin1String("folder") );
114  return KIcon( mimeType->iconName() );
115  }
116  else
117  return KIcon();
118  }
119 
120  KMime::Content *root;
121 };
122 
123 MimeTreeModel::MimeTreeModel(QObject * parent) :
124  QAbstractItemModel( parent ),
125  d ( new Private )
126 {
127 }
128 
129 MimeTreeModel::~MimeTreeModel()
130 {
131  delete d;
132 }
133 
134 void MimeTreeModel::setRoot(KMime::Content * root)
135 {
136  d->root = root;
137  reset();
138 }
139 
140 KMime::Content* MimeTreeModel::root()
141 {
142  return d->root;
143 }
144 
145 
146 QModelIndex MimeTreeModel::index(int row, int column, const QModelIndex &parent) const
147 {
148  if ( !parent.isValid() ) {
149  if ( row != 0 )
150  return QModelIndex();
151  return createIndex( row, column, d->root );
152  }
153 
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 );
159 }
160 
161 QModelIndex MimeTreeModel::parent(const QModelIndex & index) const
162 {
163  if ( !index.isValid() )
164  return QModelIndex();
165  KMime::Content *currentContent = static_cast<KMime::Content*>( index.internalPointer() );
166  if ( !currentContent )
167  return QModelIndex();
168 
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 );
174  int row = 0;
175  if ( currentIndex.isValid() )
176  row = d->contentIndexUp( currentIndex ) - 1; // 1 based -> 0 based
177 
178  return createIndex( row, 0, parentContent );
179 }
180 
181 int MimeTreeModel::rowCount(const QModelIndex & parent) const
182 {
183  if ( !d->root )
184  return 0;
185  if ( !parent.isValid() )
186  return 1;
187  KMime::Content *parentContent = static_cast<KMime::Content*>( parent.internalPointer() );
188  if ( parentContent )
189  return parentContent->contents().count();
190  return 0;
191 }
192 
193 int MimeTreeModel::columnCount(const QModelIndex & parent) const
194 {
195  Q_UNUSED( parent );
196  return 3;
197 }
198 
199 QVariant MimeTreeModel::data(const QModelIndex & index, int role) const
200 {
201  KMime::Content *content = static_cast<KMime::Content*>( index.internalPointer() );
202  if ( !content )
203  return QVariant();
204  if ( role == Qt::ToolTipRole ) {
205  // TODO
206  //return d->root->indexForContent( content ).toString();
207  }
208  if ( role == Qt::DisplayRole ) {
209  switch( index.column() ) {
210  case 0:
211  return d->descriptionForContent( content );
212  case 1:
213  return d->typeForContent( content );
214  case 2:
215  return d->sizeOfContent( content );
216  }
217  }
218  if ( role == Qt::DecorationRole && index.column() == 0 ) {
219  return d->iconForContent( content );
220  }
221  if ( role == ContentIndexRole )
222  return QVariant::fromValue( d->root->indexForContent( content ) );
223  if ( role == ContentRole )
224  return QVariant::fromValue( content );
225  if ( role == MimeTypeRole )
226  return d->mimeTypeForContent( content );
227  if ( role == MainBodyPartRole ) {
228  KMime::Message* topLevelMsg = dynamic_cast<KMime::Message*>( d->root );
229  if ( !topLevelMsg )
230  return false;
231  return topLevelMsg->mainBodyPart() == content;
232  }
233  if ( role == AlternativeBodyPartRole ) {
234  KMime::Message* topLevelMsg = dynamic_cast<KMime::Message*>( d->root );
235  if ( !topLevelMsg )
236  return false;
237  return topLevelMsg->mainBodyPart( content->contentType()->mimeType() ) == content;
238  }
239  return QVariant();
240 }
241 
242 QVariant MimeTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
243 {
244  if ( orientation == Qt::Horizontal && role == Qt::DisplayRole ) {
245  switch ( section ) {
246  case 0:
247  return i18n( "Description" );
248  case 1:
249  return i18n( "Type" );
250  case 2:
251  return i18n( "Size" );
252  }
253  }
254  return QAbstractItemModel::headerData( section, orientation, role );
255 }
256 
257 #include "mimetreemodel.moc"
MessageViewer::MimeTreeModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: mimetreemodel.cpp:181
MessageViewer::MimeTreeModel
A model representing the mime part tree of a message.
Definition: mimetreemodel.h:34
nodehelper.h
MessageViewer::MimeTreeModel::root
KMime::Content * root()
Definition: mimetreemodel.cpp:140
MessageViewer::MimeTreeModel::~MimeTreeModel
~MimeTreeModel()
Definition: mimetreemodel.cpp:129
QObject
MessageViewer::MimeTreeModel::AlternativeBodyPartRole
Definition: mimetreemodel.h:44
MessageViewer::MimeTreeModel::parent
QModelIndex parent(const QModelIndex &index) const
Definition: mimetreemodel.cpp:161
MessageViewer::MimeTreeModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Definition: mimetreemodel.cpp:199
MessageViewer::MimeTreeModel::ContentRole
Definition: mimetreemodel.h:41
MessageViewer::MimeTreeModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: mimetreemodel.cpp:242
util.h
QAbstractItemModel
mimetreemodel.h
MessageViewer::MimeTreeModel::MimeTypeRole
Definition: mimetreemodel.h:42
MessageViewer::MimeTreeModel::setRoot
void setRoot(KMime::Content *root)
Definition: mimetreemodel.cpp:134
MessageViewer::Util::mimetype
MESSAGEVIEWER_EXPORT KMimeType::Ptr mimetype(const QString &name)
Search mimetype from filename when mimetype is empty or application/octet-stream. ...
Definition: util.cpp:568
MessageViewer::MimeTreeModel::index
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Definition: mimetreemodel.cpp:146
MessageViewer::MimeTreeModel::columnCount
int columnCount(const QModelIndex &parent=QModelIndex()) const
Definition: mimetreemodel.cpp:193
MessageViewer::MimeTreeModel::MainBodyPartRole
Definition: mimetreemodel.h:43
MessageViewer::MimeTreeModel::ContentIndexRole
Definition: mimetreemodel.h:40
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:57 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

messageviewer

Skip menu "messageviewer"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal