• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.4 API Reference
  • KDE Home
  • Contact Us
 

akonadi

  • akonadi
entitymimetypefiltermodel.cpp
1 /*
2  Copyright (c) 2007 Bruno Virlet <bruno.virlet@gmail.com>
3  Copyright (c) 2009 Stephen Kelly <steveire@gmail.com>
4 
5 
6  This library is free software; you can redistribute it and/or modify it
7  under the terms of the GNU Library General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or (at your
9  option) any later version.
10 
11  This library is distributed in the hope that it will be useful, but WITHOUT
12  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14  License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301, USA.
20 */
21 
22 #include "entitymimetypefiltermodel.h"
23 
24 #include "entitytreemodel.h"
25 #include "mimetypechecker.h"
26 
27 #include <kdebug.h>
28 #include <kmimetype.h>
29 
30 #include <QtCore/QString>
31 #include <QtCore/QStringList>
32 
33 using namespace Akonadi;
34 
35 namespace Akonadi {
39 class EntityMimeTypeFilterModelPrivate
40 {
41  public:
42  EntityMimeTypeFilterModelPrivate( EntityMimeTypeFilterModel *parent )
43  : q_ptr( parent ),
44  m_headerGroup( EntityTreeModel::EntityTreeHeaders )
45  {
46  }
47 
48  Q_DECLARE_PUBLIC(EntityMimeTypeFilterModel)
49  EntityMimeTypeFilterModel *q_ptr;
50 
51  QStringList includedMimeTypes;
52  QStringList excludedMimeTypes;
53 
54  QPersistentModelIndex m_rootIndex;
55 
56  EntityTreeModel::HeaderGroup m_headerGroup;
57 };
58 
59 }
60 
61 EntityMimeTypeFilterModel::EntityMimeTypeFilterModel( QObject *parent )
62  : QSortFilterProxyModel( parent ),
63  d_ptr( new EntityMimeTypeFilterModelPrivate( this ) )
64 {
65 }
66 
67 EntityMimeTypeFilterModel::~EntityMimeTypeFilterModel()
68 {
69  delete d_ptr;
70 }
71 
72 void EntityMimeTypeFilterModel::addMimeTypeInclusionFilters(const QStringList &typeList)
73 {
74  Q_D(EntityMimeTypeFilterModel);
75  d->includedMimeTypes << typeList;
76  invalidateFilter();
77 }
78 
79 void EntityMimeTypeFilterModel::addMimeTypeExclusionFilters(const QStringList &typeList)
80 {
81  Q_D(EntityMimeTypeFilterModel);
82  d->excludedMimeTypes << typeList;
83  invalidateFilter();
84 }
85 
86 void EntityMimeTypeFilterModel::addMimeTypeInclusionFilter(const QString &type)
87 {
88  Q_D(EntityMimeTypeFilterModel);
89  d->includedMimeTypes << type;
90  invalidateFilter();
91 }
92 
93 void EntityMimeTypeFilterModel::addMimeTypeExclusionFilter(const QString &type)
94 {
95  Q_D(EntityMimeTypeFilterModel);
96  d->excludedMimeTypes << type;
97  invalidateFilter();
98 }
99 
100 bool EntityMimeTypeFilterModel::filterAcceptsColumn( int sourceColumn, const QModelIndex &sourceParent ) const
101 {
102  if (sourceColumn >= columnCount(mapFromSource(sourceParent)))
103  return false;
104  return QSortFilterProxyModel::filterAcceptsColumn( sourceColumn, sourceParent );
105 }
106 
107 bool EntityMimeTypeFilterModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent) const
108 {
109  Q_D(const EntityMimeTypeFilterModel);
110  const QModelIndex idx = sourceModel()->index(sourceRow, 0, sourceParent);
111 
112  const Akonadi::Item item = idx.data( EntityTreeModel::ItemRole ).value<Akonadi::Item>();
113 
114  if ( item.isValid() && !item.hasPayload() ) {
115  kDebug() << "Item " << item.id() << " doesn't have payload";
116  return false;
117  }
118 
119  const QString rowMimetype = idx.data( EntityTreeModel::MimeTypeRole ).toString();
120 
121  if ( d->excludedMimeTypes.contains( rowMimetype ) )
122  return false;
123  if ( d->includedMimeTypes.isEmpty() || d->includedMimeTypes.contains( rowMimetype ) )
124  return true;
125 
126  return false;
127 }
128 
129 QStringList EntityMimeTypeFilterModel::mimeTypeInclusionFilters() const
130 {
131  Q_D(const EntityMimeTypeFilterModel);
132  return d->includedMimeTypes;
133 }
134 
135 QStringList EntityMimeTypeFilterModel::mimeTypeExclusionFilters() const
136 {
137  Q_D(const EntityMimeTypeFilterModel);
138  return d->excludedMimeTypes;
139 }
140 
141 void EntityMimeTypeFilterModel::clearFilters()
142 {
143  Q_D(EntityMimeTypeFilterModel);
144  d->includedMimeTypes.clear();
145  d->excludedMimeTypes.clear();
146  invalidateFilter();
147 }
148 
149 void EntityMimeTypeFilterModel::setHeaderGroup(EntityTreeModel::HeaderGroup headerGroup)
150 {
151  Q_D(EntityMimeTypeFilterModel);
152  d->m_headerGroup = headerGroup;
153 }
154 
155 QVariant EntityMimeTypeFilterModel::headerData(int section, Qt::Orientation orientation, int role ) const
156 {
157  if (!sourceModel())
158  return QVariant();
159 
160  Q_D(const EntityMimeTypeFilterModel);
161  role += (EntityTreeModel::TerminalUserRole * d->m_headerGroup);
162  return sourceModel()->headerData(section, orientation, role);
163 }
164 
165 QModelIndexList EntityMimeTypeFilterModel::match( const QModelIndex& start, int role, const QVariant& value, int hits, Qt::MatchFlags flags ) const
166 {
167  if ( !sourceModel() )
168  return QModelIndexList();
169 
170  if ( EntityTreeModel::AmazingCompletionRole != role ) {
171  if ( role < Qt::UserRole )
172  return QSortFilterProxyModel::match( start, role, value, hits, flags );
173 
174  QModelIndexList list;
175  QModelIndex proxyIndex;
176  foreach ( const QModelIndex &idx, sourceModel()->match( mapToSource( start ), role, value, hits, flags ) ) {
177  proxyIndex = mapFromSource(idx);
178  if (proxyIndex.isValid())
179  list << proxyIndex;
180  }
181 
182  return list;
183  }
184  // We match everything in the source model because sorting will change what we should show.
185  const int allHits = -1;
186 
187  QModelIndexList proxyList;
188  QMap<int, QModelIndex> proxyMap;
189  const QModelIndexList sourceList = sourceModel()->match( mapToSource( start ), role, value, allHits, flags );
190  QModelIndexList::const_iterator it;
191  const QModelIndexList::const_iterator begin = sourceList.constBegin();
192  const QModelIndexList::const_iterator end = sourceList.constEnd();
193  QModelIndex proxyIndex;
194  for ( it = begin; it != end; ++it ) {
195  proxyIndex = mapFromSource( *it );
196 
197  // Any filtered indexes will be invalid when mapped.
198  if ( !proxyIndex.isValid() )
199  continue;
200 
201  // Inserting in a QMap gives us sorting by key for free.
202  proxyMap.insert( proxyIndex.row(), proxyIndex );
203  }
204 
205  if ( hits == -1 )
206  return proxyMap.values();
207 
208  return proxyMap.values().mid( 0, hits );
209 }
210 
211 int EntityMimeTypeFilterModel::columnCount(const QModelIndex &parent) const
212 {
213  Q_D(const EntityMimeTypeFilterModel);
214 
215  if (!sourceModel())
216  return 0;
217 
218  const QVariant value = sourceModel()->data(mapToSource(parent), EntityTreeModel::ColumnCountRole + (EntityTreeModel::TerminalUserRole * d->m_headerGroup));
219  if ( !value.isValid() )
220  return 0;
221 
222  return value.toInt();
223 }
224 
225 bool EntityMimeTypeFilterModel::hasChildren(const QModelIndex &parent) const
226 {
227  if (!sourceModel())
228  return false;
229 
230  // QSortFilterProxyModel implementation is buggy in that it emits rowsAboutToBeInserted etc
231  // only after the source model has emitted rowsInserted, instead of emitting it when the
232  // source model emits rowsAboutToBeInserted. That means that the source and the proxy are out
233  // of sync around the time of insertions, so we can't use the optimization below.
234  return rowCount(parent) > 0;
235 #if 0
236 
237  if ( !parent.isValid() )
238  return sourceModel()->hasChildren(parent);
239 
240  Q_D(const EntityMimeTypeFilterModel);
241  if ( EntityTreeModel::ItemListHeaders == d->m_headerGroup)
242  return false;
243 
244  if ( EntityTreeModel::CollectionTreeHeaders == d->m_headerGroup )
245  {
246  QModelIndex childIndex = parent.child( 0, 0 );
247  while ( childIndex.isValid() )
248  {
249  Collection col = childIndex.data( EntityTreeModel::CollectionRole ).value<Collection>();
250  if (col.isValid())
251  return true;
252  childIndex = childIndex.sibling( childIndex.row() + 1, childIndex.column() );
253  }
254  }
255  return false;
256 #endif
257 }
258 
259 bool EntityMimeTypeFilterModel::canFetchMore( const QModelIndex &parent ) const
260 {
261  Q_D(const EntityMimeTypeFilterModel);
262  if ( EntityTreeModel::CollectionTreeHeaders == d->m_headerGroup )
263  return false;
264  return QSortFilterProxyModel::canFetchMore(parent);
265 }
266 
267 #include "entitymimetypefiltermodel.moc"
268 
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Tue Dec 4 2012 14:36:04 by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

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

kdepimlibs-4.9.4 API Reference

Skip menu "kdepimlibs-4.9.4 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
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