akonadi
agentfilterproxymodel.cpp
00001 /* 00002 Copyright (c) 2007 Volker Krause <vkrause@kde.org> 00003 00004 This library is free software; you can redistribute it and/or modify it 00005 under the terms of the GNU Library General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or (at your 00007 option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, but WITHOUT 00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00011 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00012 License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to the 00016 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00017 02110-1301, USA. 00018 */ 00019 00020 #include "agentfilterproxymodel.h" 00021 00022 #include "agenttypemodel.h" 00023 #include "agentinstancemodel.h" 00024 00025 #include <kdebug.h> 00026 #include <kmimetype.h> 00027 00028 #include <QtCore/QStringList> 00029 00030 #include <boost/static_assert.hpp> 00031 00032 using namespace Akonadi; 00033 00034 // ensure the role numbers are equivalent for both source models 00035 BOOST_STATIC_ASSERT( (int)AgentTypeModel::CapabilitiesRole == (int)AgentInstanceModel::CapabilitiesRole ); 00036 BOOST_STATIC_ASSERT( (int)AgentTypeModel::MimeTypesRole == (int)AgentInstanceModel::MimeTypesRole ); 00037 00041 class AgentFilterProxyModel::Private 00042 { 00043 public: 00044 QStringList mimeTypes; 00045 QStringList capabilities; 00046 QStringList excludeCapabilities; 00047 }; 00048 00049 AgentFilterProxyModel::AgentFilterProxyModel( QObject *parent ) 00050 : QSortFilterProxyModel( parent ), 00051 d( new Private ) 00052 { 00053 setDynamicSortFilter( true ); 00054 } 00055 00056 AgentFilterProxyModel::~AgentFilterProxyModel() 00057 { 00058 delete d; 00059 } 00060 00061 void AgentFilterProxyModel::addMimeTypeFilter( const QString &mimeType ) 00062 { 00063 d->mimeTypes << mimeType; 00064 invalidateFilter(); 00065 } 00066 00067 void AgentFilterProxyModel::addCapabilityFilter( const QString &capability ) 00068 { 00069 d->capabilities << capability; 00070 invalidateFilter(); 00071 } 00072 00073 00074 void AgentFilterProxyModel::excludeCapabilities( const QString &capability ) 00075 { 00076 d->excludeCapabilities << capability; 00077 invalidateFilter(); 00078 } 00079 00080 void AgentFilterProxyModel::clearFilters() 00081 { 00082 d->capabilities.clear(); 00083 d->mimeTypes.clear(); 00084 d->excludeCapabilities.clear(); 00085 invalidateFilter(); 00086 } 00087 00088 bool AgentFilterProxyModel::filterAcceptsRow( int row, const QModelIndex& ) const 00089 { 00090 const QModelIndex index = sourceModel()->index( row, 0 ); 00091 00092 // First see if the name matches a set regexp filter. 00093 if ( !filterRegExp().isEmpty() && !index.data().toString().contains( filterRegExp() ) ) 00094 return false; 00095 00096 if ( !d->mimeTypes.isEmpty() ) { 00097 bool found = false; 00098 foreach ( const QString &mimeType, index.data( AgentTypeModel::MimeTypesRole ).toStringList() ) { 00099 if ( d->mimeTypes.contains( mimeType ) ) { 00100 found = true; 00101 } else { 00102 KMimeType::Ptr mimeTypePtr = KMimeType::mimeType( mimeType, KMimeType::ResolveAliases ); 00103 if ( !mimeTypePtr.isNull() ) { 00104 foreach ( const QString &type, d->mimeTypes ) { 00105 if ( mimeTypePtr->is( type )) { 00106 found = true; 00107 break; 00108 } 00109 } 00110 } 00111 } 00112 00113 if ( found ) 00114 break; 00115 } 00116 00117 if ( !found ) 00118 return false; 00119 } 00120 00121 if ( !d->capabilities.isEmpty() ) { 00122 bool found = false; 00123 foreach ( const QString &capability, index.data( AgentTypeModel::CapabilitiesRole ).toStringList() ) { 00124 if ( d->capabilities.contains( capability ) ) { 00125 found = true; 00126 break; 00127 } 00128 } 00129 00130 if ( !found ) 00131 return false; 00132 00133 if ( found && !d->excludeCapabilities.isEmpty() ) { 00134 foreach ( const QString &capability, index.data( AgentTypeModel::CapabilitiesRole ).toStringList() ) { 00135 if ( d->excludeCapabilities.contains( capability ) ) { 00136 found = false; 00137 break; 00138 } 00139 } 00140 00141 if ( !found ) 00142 return false; 00143 } 00144 } 00145 00146 return true; 00147 } 00148 00149 #include "agentfilterproxymodel.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 14 2012 04:52:51 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 14 2012 04:52:51 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.