• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

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 };
00047 
00048 AgentFilterProxyModel::AgentFilterProxyModel(QObject * parent)
00049   : QSortFilterProxyModel( parent ),
00050   d( new Private )
00051 {
00052   setDynamicSortFilter( true );
00053 }
00054 
00055 AgentFilterProxyModel::~AgentFilterProxyModel()
00056 {
00057   delete d;
00058 }
00059 
00060 void AgentFilterProxyModel::addMimeTypeFilter(const QString & mimeType)
00061 {
00062   d->mimeTypes << mimeType;
00063   invalidateFilter();
00064 }
00065 
00066 void AgentFilterProxyModel::addCapabilityFilter(const QString & capability)
00067 {
00068   d->capabilities << capability;
00069   invalidateFilter();
00070 }
00071 
00072 void AgentFilterProxyModel::clearFilters()
00073 {
00074   d->capabilities.clear();
00075   d->mimeTypes.clear();
00076   invalidateFilter();
00077 }
00078 
00079 bool AgentFilterProxyModel::filterAcceptsRow(int row, const QModelIndex&) const
00080 {
00081   QModelIndex index = sourceModel()->index( row, 0 );
00082 
00083   // First see if the name matches a set regexp filter.
00084   if ( !filterRegExp().isEmpty() && !index.data().toString().contains( filterRegExp() ) )
00085     return false;
00086 
00087   if ( !d->mimeTypes.isEmpty() ) {
00088     bool found = false;
00089     foreach ( const QString &mt, index.data( AgentTypeModel::MimeTypesRole ).toStringList() ) {
00090       if ( d->mimeTypes.contains( mt ) ) {
00091         found = true;
00092       } else {
00093         KMimeType::Ptr mimeType = KMimeType::mimeType( mt, KMimeType::ResolveAliases );
00094         if ( !mimeType.isNull() ) {
00095           foreach ( const QString &type, d->mimeTypes ) {
00096             if ( mimeType->is( type )) {
00097               found = true;
00098               break;
00099             }
00100           }
00101         }
00102       }
00103       if ( found )
00104         break;
00105     }
00106     if ( !found ) return false;
00107   }
00108 
00109   if ( !d->capabilities.isEmpty() ) {
00110     bool found = false;
00111     foreach ( const QString &cap, index.data( AgentTypeModel::CapabilitiesRole ).toStringList() ) {
00112       if ( d->capabilities.contains( cap ) ) {
00113         found = true;
00114         break;
00115       }
00116     }
00117     if ( !found ) return false;
00118   }
00119   return true;
00120 }
00121 
00122 #include "agentfilterproxymodel.moc"

akonadi

Skip menu "akonadi"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.6
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal