• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.4 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  if ( collection.isVirtual() && mExcludeVirtualCollections )
62  return false;
63 
64  // If this collection directly contains one valid mimetype, it is accepted
65  if ( mimeChecker.isWantedCollection( collection ) ) {
66  // The folder will be accepted, but we need to make sure the resource is visible too.
67  if ( checkResourceVisibility ) {
68 
69  // find the resource
70  QModelIndex resource = index;
71  while ( resource.parent().isValid() )
72  resource = resource.parent();
73 
74  // See if that resource is visible, if not, invalidate the filter.
75  if ( resource != index && !acceptedResources.contains( resource ) ) {
76  kDebug() << "We got a new collection:" << mParent->sourceModel()->data( index ).toString()
77  << "but the resource is not visible:" << mParent->sourceModel()->data( resource ).toString();
78  acceptedResources.clear();
79  // defer reset, the model might still be supplying new items at this point which crashs
80  mParent->invalidateFilter();
81  return true;
82  }
83  }
84 
85  // Keep track of all the resources that are visible.
86  if ( !index.parent().isValid() )
87  acceptedResources.append( index );
88 
89  return true;
90  }
91 
92  // If this collection has a child which contains valid mimetypes, it is accepted
93  QModelIndex childIndex = index.child( 0, 0 );
94  while ( childIndex.isValid() ) {
95  if ( collectionAccepted( childIndex, false /* don't check visibility of the parent, as we are checking the child now */ ) ) {
96 
97  // Keep track of all the resources that are visible.
98  if ( !index.parent().isValid())
99  acceptedResources.append( index );
100 
101  return true;
102  }
103  childIndex = childIndex.sibling( childIndex.row() + 1, 0 );
104  }
105 
106  // Or else, no reason to keep this collection.
107  return false;
108 }
109 
110 
111 CollectionFilterProxyModel::CollectionFilterProxyModel( QObject *parent )
112  : QSortFilterProxyModel( parent ),
113  d( new Private( this ) )
114 {
115 }
116 
117 CollectionFilterProxyModel::~CollectionFilterProxyModel()
118 {
119  delete d;
120 }
121 
122 void CollectionFilterProxyModel::addMimeTypeFilters(const QStringList &typeList)
123 {
124  QStringList mimeTypes = d->mimeChecker.wantedMimeTypes() + typeList;
125  d->mimeChecker.setWantedMimeTypes( mimeTypes );
126  invalidateFilter();
127 }
128 
129 void CollectionFilterProxyModel::addMimeTypeFilter(const QString &type)
130 {
131  d->mimeChecker.addWantedMimeType( type );
132  invalidateFilter();
133 }
134 
135 bool CollectionFilterProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent) const
136 {
137  return d->collectionAccepted( sourceModel()->index( sourceRow, 0, sourceParent ) );
138 }
139 
140 QStringList CollectionFilterProxyModel::mimeTypeFilters() const
141 {
142  return d->mimeChecker.wantedMimeTypes();
143 }
144 
145 void CollectionFilterProxyModel::clearFilters()
146 {
147  d->mimeChecker = MimeTypeChecker();
148  invalidateFilter();
149 }
150 
151 void CollectionFilterProxyModel::setExcludeVirtualCollections( bool exclude )
152 {
153  if ( exclude != d->mExcludeVirtualCollections ) {
154  d->mExcludeVirtualCollections = exclude;
155  invalidateFilter();
156  }
157 }
158 
159 Qt::ItemFlags CollectionFilterProxyModel::flags( const QModelIndex& index ) const
160 {
161  if ( !index.isValid() ) {
162  // Don't crash
163  return 0;
164  }
165 
166  const Collection collection = sourceModel()->data( mapToSource( index ), CollectionModel::CollectionRole ).value<Collection>();
167 
168  // If this collection directly contains one valid mimetype, it is accepted
169  if ( d->mimeChecker.isWantedCollection( collection ) )
170  return QSortFilterProxyModel::flags( index );
171  else
172  return QSortFilterProxyModel::flags( index ) & ~( Qt::ItemIsSelectable );
173 }
174 
175 #include "collectionfilterproxymodel.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Dec 10 2012 13:48:08 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