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

messageviewer

  • sources
  • kde-4.14
  • kdepim
  • messageviewer
  • viewer
  • mimeparttree
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 "viewer/nodehelper.h"
26 
27 #include <kmime/kmime_content.h>
28 #include <KMime/Message>
29 
30 #include <KDebug>
31 #include <KIcon>
32 #include <KLocalizedString>
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  return QVariant();
208  }
209  if ( role == Qt::DisplayRole ) {
210  switch( index.column() ) {
211  case 0:
212  return d->descriptionForContent( content );
213  case 1:
214  return d->typeForContent( content );
215  case 2:
216  return d->sizeOfContent( content );
217  }
218  }
219  if ( role == Qt::DecorationRole && index.column() == 0 ) {
220  return d->iconForContent( content );
221  }
222  if ( role == ContentIndexRole )
223  return QVariant::fromValue( d->root->indexForContent( content ) );
224  if ( role == ContentRole )
225  return QVariant::fromValue( content );
226  if ( role == MimeTypeRole )
227  return d->mimeTypeForContent( content );
228  if ( role == MainBodyPartRole ) {
229  KMime::Message* topLevelMsg = dynamic_cast<KMime::Message*>( d->root );
230  if ( !topLevelMsg )
231  return false;
232  return topLevelMsg->mainBodyPart() == content;
233  }
234  if ( role == AlternativeBodyPartRole ) {
235  KMime::Message* topLevelMsg = dynamic_cast<KMime::Message*>( d->root );
236  if ( !topLevelMsg )
237  return false;
238  return topLevelMsg->mainBodyPart( content->contentType()->mimeType() ) == content;
239  }
240  return QVariant();
241 }
242 
243 QVariant MimeTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
244 {
245  if ( orientation == Qt::Horizontal && role == Qt::DisplayRole ) {
246  switch ( section ) {
247  case 0:
248  return i18n( "Description" );
249  case 1:
250  return i18n( "Type" );
251  case 2:
252  return i18n( "Size" );
253  }
254  }
255  return QAbstractItemModel::headerData( section, orientation, role );
256 }
257 
QModelIndex
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
MessageViewer::MimeTreeModel::AlternativeBodyPartRole
Definition: mimetreemodel.h:44
QAbstractItemModel::reset
void reset()
QModelIndex::isValid
bool isValid() const
MessageViewer::MimeTreeModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Definition: mimetreemodel.cpp:199
QObject
QString::toInt
int toInt(bool *ok, int base) const
QString::isEmpty
bool isEmpty() const
MessageViewer::MimeTreeModel::ContentRole
Definition: mimetreemodel.h:41
QModelIndex::internalPointer
void * internalPointer() const
MessageViewer::MimeTreeModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: mimetreemodel.cpp:243
QString
util.h
QStringList
QAbstractItemModel::headerData
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const
QAbstractItemModel::createIndex
QModelIndex createIndex(int row, int column, void *ptr) const
mimetreemodel.h
MessageViewer::MimeTreeModel::MimeTypeRole
Definition: mimetreemodel.h:42
QLatin1Char
QVariant::fromValue
QVariant fromValue(const T &value)
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:569
QLatin1String
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
QModelIndex::column
int column() const
QStringList::split
QStringList split(const QString &sep, const QString &str, bool allowEmptyEntries)
QAbstractItemModel
QString::fromLatin1
QString fromLatin1(const char *str, int size)
MessageViewer::MimeTreeModel::MainBodyPartRole
Definition: mimetreemodel.h:43
QObject::parent
QObject * parent() const
QVariant
MessageViewer::MimeTreeModel::ContentIndexRole
Definition: mimetreemodel.h:40
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:32:45 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
  • pimprint

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