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

akonadi

  • akonadi
agentfilterproxymodel.cpp
1 /*
2  Copyright (c) 2007 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 #include "agentfilterproxymodel.h"
21 
22 #include "agenttypemodel.h"
23 #include "agentinstancemodel.h"
24 
25 #include <kdebug.h>
26 #include <kmimetype.h>
27 
28 #include <QtCore/QStringList>
29 
30 #include <boost/static_assert.hpp>
31 
32 using namespace Akonadi;
33 
34 // ensure the role numbers are equivalent for both source models
35 BOOST_STATIC_ASSERT( (int)AgentTypeModel::CapabilitiesRole == (int)AgentInstanceModel::CapabilitiesRole );
36 BOOST_STATIC_ASSERT( (int)AgentTypeModel::MimeTypesRole == (int)AgentInstanceModel::MimeTypesRole );
37 
41 class AgentFilterProxyModel::Private
42 {
43  public:
44  QStringList mimeTypes;
45  QStringList capabilities;
46  QStringList excludeCapabilities;
47 };
48 
49 AgentFilterProxyModel::AgentFilterProxyModel( QObject *parent )
50  : QSortFilterProxyModel( parent ),
51  d( new Private )
52 {
53  setDynamicSortFilter( true );
54 }
55 
56 AgentFilterProxyModel::~AgentFilterProxyModel()
57 {
58  delete d;
59 }
60 
61 void AgentFilterProxyModel::addMimeTypeFilter( const QString &mimeType )
62 {
63  d->mimeTypes << mimeType;
64  invalidateFilter();
65 }
66 
67 void AgentFilterProxyModel::addCapabilityFilter( const QString &capability )
68 {
69  d->capabilities << capability;
70  invalidateFilter();
71 }
72 
73 
74 void AgentFilterProxyModel::excludeCapabilities( const QString &capability )
75 {
76  d->excludeCapabilities << capability;
77  invalidateFilter();
78 }
79 
80 void AgentFilterProxyModel::clearFilters()
81 {
82  d->capabilities.clear();
83  d->mimeTypes.clear();
84  d->excludeCapabilities.clear();
85  invalidateFilter();
86 }
87 
88 bool AgentFilterProxyModel::filterAcceptsRow( int row, const QModelIndex& ) const
89 {
90  const QModelIndex index = sourceModel()->index( row, 0 );
91 
92  // First see if the name matches a set regexp filter.
93  if ( !filterRegExp().isEmpty() && !index.data().toString().contains( filterRegExp() ) )
94  return false;
95 
96  if ( !d->mimeTypes.isEmpty() ) {
97  bool found = false;
98  foreach ( const QString &mimeType, index.data( AgentTypeModel::MimeTypesRole ).toStringList() ) {
99  if ( d->mimeTypes.contains( mimeType ) ) {
100  found = true;
101  } else {
102  KMimeType::Ptr mimeTypePtr = KMimeType::mimeType( mimeType, KMimeType::ResolveAliases );
103  if ( !mimeTypePtr.isNull() ) {
104  foreach ( const QString &type, d->mimeTypes ) {
105  if ( mimeTypePtr->is( type )) {
106  found = true;
107  break;
108  }
109  }
110  }
111  }
112 
113  if ( found )
114  break;
115  }
116 
117  if ( !found )
118  return false;
119  }
120 
121  if ( !d->capabilities.isEmpty() ) {
122  bool found = false;
123  foreach ( const QString &capability, index.data( AgentTypeModel::CapabilitiesRole ).toStringList() ) {
124  if ( d->capabilities.contains( capability ) ) {
125  found = true;
126  break;
127  }
128  }
129 
130  if ( !found )
131  return false;
132 
133  if ( found && !d->excludeCapabilities.isEmpty() ) {
134  foreach ( const QString &capability, index.data( AgentTypeModel::CapabilitiesRole ).toStringList() ) {
135  if ( d->excludeCapabilities.contains( capability ) ) {
136  found = false;
137  break;
138  }
139  }
140 
141  if ( !found )
142  return false;
143  }
144  }
145 
146  return true;
147 }
148 
149 #include "agentfilterproxymodel.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Wed Nov 28 2012 21:51:21 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.3 API Reference

Skip menu "kdepimlibs-4.9.3 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