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

akonadi

  • sources
  • kde-4.12
  • kdepimlibs
  • 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 CollectionFilterProxyModel::CollectionFilterProxyModel( QObject *parent )
116  : QSortFilterProxyModel( parent ),
117  d( new Private( this ) )
118 {
119 }
120 
121 CollectionFilterProxyModel::~CollectionFilterProxyModel()
122 {
123  delete d;
124 }
125 
126 void CollectionFilterProxyModel::addMimeTypeFilters(const QStringList &typeList)
127 {
128  QStringList mimeTypes = d->mimeChecker.wantedMimeTypes() + typeList;
129  d->mimeChecker.setWantedMimeTypes( mimeTypes );
130  invalidateFilter();
131 }
132 
133 void CollectionFilterProxyModel::addMimeTypeFilter(const QString &type)
134 {
135  d->mimeChecker.addWantedMimeType( type );
136  invalidateFilter();
137 }
138 
139 bool CollectionFilterProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent) const
140 {
141  return d->collectionAccepted( sourceModel()->index( sourceRow, 0, sourceParent ) );
142 }
143 
144 QStringList CollectionFilterProxyModel::mimeTypeFilters() const
145 {
146  return d->mimeChecker.wantedMimeTypes();
147 }
148 
149 void CollectionFilterProxyModel::clearFilters()
150 {
151  d->mimeChecker = MimeTypeChecker();
152  invalidateFilter();
153 }
154 
155 void CollectionFilterProxyModel::setExcludeVirtualCollections( bool exclude )
156 {
157  if ( exclude != d->mExcludeVirtualCollections ) {
158  d->mExcludeVirtualCollections = exclude;
159  invalidateFilter();
160  }
161 }
162 
163 bool CollectionFilterProxyModel::excludeVirtualCollections() const
164 {
165  return d->mExcludeVirtualCollections;
166 }
167 
168 Qt::ItemFlags CollectionFilterProxyModel::flags( const QModelIndex& index ) const
169 {
170  if ( !index.isValid() ) {
171  // Don't crash
172  return 0;
173  }
174 
175  const Collection collection = sourceModel()->data( mapToSource( index ), CollectionModel::CollectionRole ).value<Collection>();
176 
177  // If this collection directly contains one valid mimetype, it is accepted
178  if ( d->mimeChecker.isWantedCollection( collection ) ) {
179  return QSortFilterProxyModel::flags( index );
180  } else {
181  return QSortFilterProxyModel::flags( index ) & ~( Qt::ItemIsSelectable );
182  }
183 }
184 
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:126
Akonadi::CollectionFilterProxyModel::CollectionFilterProxyModel
CollectionFilterProxyModel(QObject *parent=0)
Creates a new collection proxy filter model.
Definition: collectionfilterproxymodel.cpp:115
Akonadi::CollectionFilterProxyModel::mimeTypeFilters
QStringList mimeTypeFilters() const
Returns the list of mime type filters.
Definition: collectionfilterproxymodel.cpp:144
Akonadi::CollectionFilterProxyModel::setExcludeVirtualCollections
void setExcludeVirtualCollections(bool exclude)
Sets whether we want virtual collections to be filtered or not.
Definition: collectionfilterproxymodel.cpp:155
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:133
Akonadi::CollectionFilterProxyModel::clearFilters
void clearFilters()
Clears all mime type filters.
Definition: collectionfilterproxymodel.cpp:149
Akonadi::Collection::parent
AKONADI_DEPRECATED 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:121
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-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:26 by doxygen 1.8.7 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 API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

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