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

akonadi

  • akonadi
collectionfilterproxymodel.cpp
1 /*
2  Copyright (c) 2007 Bruno Virlet <bruno.virlet@gmail.com>
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 #include "collectionfilterproxymodel.h"
21 
22 #include "collectionmodel.h"
23 #include "mimetypechecker.h"
24 
25 #include <kdebug.h>
26 
27 #include <QtCore/QString>
28 #include <QtCore/QStringList>
29 #include <QTimer>
30 
31 using namespace Akonadi;
32 
36 class CollectionFilterProxyModel::Private
37 {
38  public:
39  Private( CollectionFilterProxyModel *parent )
40  : mParent( parent ), mExcludeVirtualCollections( false )
41  {
42  mimeChecker.addWantedMimeType( QLatin1String( "text/uri-list" ) );
43  }
44 
45  bool collectionAccepted( const QModelIndex &index, bool checkResourceVisibility = true );
46 
47  QVector< QModelIndex > acceptedResources;
48  CollectionFilterProxyModel *mParent;
49  MimeTypeChecker mimeChecker;
50  bool mExcludeVirtualCollections;
51 };
52 
53 bool CollectionFilterProxyModel::Private::collectionAccepted( const QModelIndex &index, bool checkResourceVisibility )
54 {
55  // Retrieve supported mimetypes
56  const Collection collection = mParent->sourceModel()->data( index, CollectionModel::CollectionRole ).value<Collection>();
57 
58  if ( !collection.isValid() ) {
59  return false;
60  }
61 
62  if ( collection.isVirtual() && mExcludeVirtualCollections ) {
63  return false;
64  }
65 
66  // If this collection directly contains one valid mimetype, it is accepted
67  if ( mimeChecker.isWantedCollection( collection ) ) {
68  // The folder will be accepted, but we need to make sure the resource is visible too.
69  if ( checkResourceVisibility ) {
70 
71  // find the resource
72  QModelIndex resource = index;
73  while ( resource.parent().isValid() ) {
74  resource = resource.parent();
75  }
76 
77  // See if that resource is visible, if not, invalidate the filter.
78  if ( resource != index && !acceptedResources.contains( resource ) ) {
79  kDebug() << "We got a new collection:" << mParent->sourceModel()->data( index ).toString()
80  << "but the resource is not visible:" << mParent->sourceModel()->data( resource ).toString();
81  acceptedResources.clear();
82  // defer reset, the model might still be supplying new items at this point which crashs
83  mParent->invalidateFilter();
84  return true;
85  }
86  }
87 
88  // Keep track of all the resources that are visible.
89  if ( !index.parent().isValid() ) {
90  acceptedResources.append( index );
91  }
92 
93  return true;
94  }
95 
96  // If this collection has a child which contains valid mimetypes, it is accepted
97  QModelIndex childIndex = index.child( 0, 0 );
98  while ( childIndex.isValid() ) {
99  if ( collectionAccepted( childIndex, false /* don't check visibility of the parent, as we are checking the child now */ ) ) {
100 
101  // Keep track of all the resources that are visible.
102  if ( !index.parent().isValid() ) {
103  acceptedResources.append( index );
104  }
105 
106  return true;
107  }
108  childIndex = childIndex.sibling( childIndex.row() + 1, 0 );
109  }
110 
111  // Or else, no reason to keep this collection.
112  return false;
113 }
114 
115 
116 CollectionFilterProxyModel::CollectionFilterProxyModel( QObject *parent )
117  : QSortFilterProxyModel( parent ),
118  d( new Private( this ) )
119 {
120 }
121 
122 CollectionFilterProxyModel::~CollectionFilterProxyModel()
123 {
124  delete d;
125 }
126 
127 void CollectionFilterProxyModel::addMimeTypeFilters(const QStringList &typeList)
128 {
129  QStringList mimeTypes = d->mimeChecker.wantedMimeTypes() + typeList;
130  d->mimeChecker.setWantedMimeTypes( mimeTypes );
131  invalidateFilter();
132 }
133 
134 void CollectionFilterProxyModel::addMimeTypeFilter(const QString &type)
135 {
136  d->mimeChecker.addWantedMimeType( type );
137  invalidateFilter();
138 }
139 
140 bool CollectionFilterProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent) const
141 {
142  return d->collectionAccepted( sourceModel()->index( sourceRow, 0, sourceParent ) );
143 }
144 
145 QStringList CollectionFilterProxyModel::mimeTypeFilters() const
146 {
147  return d->mimeChecker.wantedMimeTypes();
148 }
149 
150 void CollectionFilterProxyModel::clearFilters()
151 {
152  d->mimeChecker = MimeTypeChecker();
153  invalidateFilter();
154 }
155 
156 void CollectionFilterProxyModel::setExcludeVirtualCollections( bool exclude )
157 {
158  if ( exclude != d->mExcludeVirtualCollections ) {
159  d->mExcludeVirtualCollections = exclude;
160  invalidateFilter();
161  }
162 }
163 
164 Qt::ItemFlags CollectionFilterProxyModel::flags( const QModelIndex& index ) const
165 {
166  if ( !index.isValid() ) {
167  // Don't crash
168  return 0;
169  }
170 
171  const Collection collection = sourceModel()->data( mapToSource( index ), CollectionModel::CollectionRole ).value<Collection>();
172 
173  // If this collection directly contains one valid mimetype, it is accepted
174  if ( d->mimeChecker.isWantedCollection( collection ) ) {
175  return QSortFilterProxyModel::flags( index );
176  } else {
177  return QSortFilterProxyModel::flags( index ) & ~( Qt::ItemIsSelectable );
178  }
179 }
180 
Akonadi::CollectionFilterProxyModel
A proxy model that filters collections by mime type.
Definition: collectionfilterproxymodel.h:54
Akonadi::Collection
Represents a collection of PIM items.
Definition: collection.h:75
Akonadi::MimeTypeChecker
Helper for checking MIME types of Collections and Items.
Definition: mimetypechecker.h:109
Akonadi::CollectionFilterProxyModel::addMimeTypeFilters
void addMimeTypeFilters(const QStringList &mimeTypes)
Adds a list of mime types to be shown by the filter.
Definition: collectionfilterproxymodel.cpp:127
Akonadi::CollectionFilterProxyModel::CollectionFilterProxyModel
CollectionFilterProxyModel(QObject *parent=0)
Creates a new collection proxy filter model.
Definition: collectionfilterproxymodel.cpp:116
Akonadi::CollectionFilterProxyModel::mimeTypeFilters
QStringList mimeTypeFilters() const
Returns the list of mime type filters.
Definition: collectionfilterproxymodel.cpp:145
Akonadi::CollectionFilterProxyModel::setExcludeVirtualCollections
void setExcludeVirtualCollections(bool exclude)
Sets whether we want virtual collections to be filtered or not.
Definition: collectionfilterproxymodel.cpp:156
Akonadi::CollectionModel::CollectionRole
The actual collection object.
Definition: collectionmodel.h:66
Akonadi::CollectionFilterProxyModel::addMimeTypeFilter
void addMimeTypeFilter(const QString &mimeType)
Adds a mime type to be shown by the filter.
Definition: collectionfilterproxymodel.cpp:134
Akonadi::CollectionFilterProxyModel::clearFilters
void clearFilters()
Clears all mime type filters.
Definition: collectionfilterproxymodel.cpp:150
Akonadi::Collection::parent
Id parent() const
Returns the identifier of the parent collection.
Definition: collection.cpp:129
Akonadi::Entity::isValid
bool isValid() const
Returns whether the entity is valid.
Definition: entity.cpp:97
Akonadi::CollectionFilterProxyModel::~CollectionFilterProxyModel
virtual ~CollectionFilterProxyModel()
Destroys the collection proxy filter model.
Definition: collectionfilterproxymodel.cpp:122
Akonadi::Collection::isVirtual
bool isVirtual() const
Returns whether the collection is virtual, for example a search collection.
Definition: collection.cpp:261
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Nov 26 2013 09:03:16 by doxygen 1.8.5 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.11.3 API Reference

Skip menu "kdepimlibs-4.11.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • 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