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

akonadi

entitymimetypefiltermodel.cpp
00001 /*
00002     Copyright (c) 2007 Bruno Virlet <bruno.virlet@gmail.com>
00003     Copyright (c) 2009 Stephen Kelly <steveire@gmail.com>
00004 
00005 
00006     This library is free software; you can redistribute it and/or modify it
00007     under the terms of the GNU Library General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or (at your
00009     option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful, but WITHOUT
00012     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00014     License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to the
00018     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00019     02110-1301, USA.
00020 */
00021 
00022 #include "entitymimetypefiltermodel.h"
00023 
00024 #include "entitytreemodel.h"
00025 #include "mimetypechecker.h"
00026 
00027 #include <kdebug.h>
00028 #include <kmimetype.h>
00029 
00030 #include <QtCore/QString>
00031 #include <QtCore/QStringList>
00032 
00033 using namespace Akonadi;
00034 
00035 namespace Akonadi {
00039 class EntityMimeTypeFilterModelPrivate
00040 {
00041   public:
00042     EntityMimeTypeFilterModelPrivate( EntityMimeTypeFilterModel *parent )
00043       : q_ptr( parent ),
00044         m_headerGroup( EntityTreeModel::EntityTreeHeaders )
00045     {
00046     }
00047 
00048     Q_DECLARE_PUBLIC(EntityMimeTypeFilterModel)
00049     EntityMimeTypeFilterModel *q_ptr;
00050 
00051     QStringList includedMimeTypes;
00052     QStringList excludedMimeTypes;
00053 
00054     QPersistentModelIndex m_rootIndex;
00055 
00056     EntityTreeModel::HeaderGroup m_headerGroup;
00057 };
00058 
00059 }
00060 
00061 EntityMimeTypeFilterModel::EntityMimeTypeFilterModel( QObject *parent )
00062   : QSortFilterProxyModel( parent ),
00063     d_ptr( new EntityMimeTypeFilterModelPrivate( this ) )
00064 {
00065 }
00066 
00067 EntityMimeTypeFilterModel::~EntityMimeTypeFilterModel()
00068 {
00069   delete d_ptr;
00070 }
00071 
00072 void EntityMimeTypeFilterModel::addMimeTypeInclusionFilters(const QStringList &typeList)
00073 {
00074   Q_D(EntityMimeTypeFilterModel);
00075   d->includedMimeTypes << typeList;
00076   invalidateFilter();
00077 }
00078 
00079 void EntityMimeTypeFilterModel::addMimeTypeExclusionFilters(const QStringList &typeList)
00080 {
00081   Q_D(EntityMimeTypeFilterModel);
00082   d->excludedMimeTypes << typeList;
00083   invalidateFilter();
00084 }
00085 
00086 void EntityMimeTypeFilterModel::addMimeTypeInclusionFilter(const QString &type)
00087 {
00088   Q_D(EntityMimeTypeFilterModel);
00089   d->includedMimeTypes << type;
00090   invalidateFilter();
00091 }
00092 
00093 void EntityMimeTypeFilterModel::addMimeTypeExclusionFilter(const QString &type)
00094 {
00095   Q_D(EntityMimeTypeFilterModel);
00096   d->excludedMimeTypes << type;
00097   invalidateFilter();
00098 }
00099 
00100 bool EntityMimeTypeFilterModel::filterAcceptsColumn( int sourceColumn, const QModelIndex &sourceParent ) const
00101 {
00102     if (sourceColumn >= columnCount(mapFromSource(sourceParent)))
00103         return false;
00104     return QSortFilterProxyModel::filterAcceptsColumn( sourceColumn, sourceParent );
00105 }
00106 
00107 bool EntityMimeTypeFilterModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent) const
00108 {
00109   Q_D(const EntityMimeTypeFilterModel);
00110   const QModelIndex idx = sourceModel()->index(sourceRow, 0, sourceParent);
00111 
00112   const Akonadi::Item item = idx.data( EntityTreeModel::ItemRole ).value<Akonadi::Item>();
00113 
00114   if ( item.isValid() && !item.hasPayload() ) {
00115     kDebug() << "Item " << item.id() << " doesn't have payload";
00116     return false;
00117   }
00118 
00119   const QString rowMimetype = idx.data( EntityTreeModel::MimeTypeRole ).toString();
00120 
00121   if ( d->excludedMimeTypes.contains( rowMimetype ) )
00122     return false;
00123   if ( d->includedMimeTypes.isEmpty() || d->includedMimeTypes.contains( rowMimetype ) )
00124     return true;
00125 
00126   return false;
00127 }
00128 
00129 QStringList EntityMimeTypeFilterModel::mimeTypeInclusionFilters() const
00130 {
00131   Q_D(const EntityMimeTypeFilterModel);
00132   return d->includedMimeTypes;
00133 }
00134 
00135 QStringList EntityMimeTypeFilterModel::mimeTypeExclusionFilters() const
00136 {
00137   Q_D(const EntityMimeTypeFilterModel);
00138   return d->excludedMimeTypes;
00139 }
00140 
00141 void EntityMimeTypeFilterModel::clearFilters()
00142 {
00143   Q_D(EntityMimeTypeFilterModel);
00144   d->includedMimeTypes.clear();
00145   d->excludedMimeTypes.clear();
00146   invalidateFilter();
00147 }
00148 
00149 void EntityMimeTypeFilterModel::setHeaderGroup(EntityTreeModel::HeaderGroup headerGroup)
00150 {
00151   Q_D(EntityMimeTypeFilterModel);
00152   d->m_headerGroup = headerGroup;
00153 }
00154 
00155 QVariant EntityMimeTypeFilterModel::headerData(int section, Qt::Orientation orientation, int role ) const
00156 {
00157   if (!sourceModel())
00158     return QVariant();
00159 
00160   Q_D(const EntityMimeTypeFilterModel);
00161   role += (EntityTreeModel::TerminalUserRole * d->m_headerGroup);
00162   return sourceModel()->headerData(section, orientation, role);
00163 }
00164 
00165 QModelIndexList EntityMimeTypeFilterModel::match( const QModelIndex& start, int role, const QVariant& value, int hits, Qt::MatchFlags flags ) const
00166 {
00167   if ( !sourceModel() )
00168     return QModelIndexList();
00169 
00170   if ( EntityTreeModel::AmazingCompletionRole != role ) {
00171     if ( role < Qt::UserRole )
00172       return QSortFilterProxyModel::match( start, role, value, hits, flags );
00173 
00174     QModelIndexList list;
00175     QModelIndex proxyIndex;
00176     foreach ( const QModelIndex &idx, sourceModel()->match( mapToSource( start ), role, value, hits, flags ) ) {
00177       proxyIndex = mapFromSource(idx);
00178       if (proxyIndex.isValid())
00179         list << proxyIndex;
00180     }
00181 
00182     return list;
00183   }
00184   // We match everything in the source model because sorting will change what we should show.
00185   const int allHits = -1;
00186 
00187   QModelIndexList proxyList;
00188   QMap<int, QModelIndex> proxyMap;
00189   const QModelIndexList sourceList = sourceModel()->match( mapToSource( start ), role, value, allHits, flags );
00190   QModelIndexList::const_iterator it;
00191   const QModelIndexList::const_iterator begin = sourceList.constBegin();
00192   const QModelIndexList::const_iterator end = sourceList.constEnd();
00193   QModelIndex proxyIndex;
00194   for ( it = begin; it != end; ++it ) {
00195     proxyIndex = mapFromSource( *it );
00196 
00197     // Any filtered indexes will be invalid when mapped.
00198     if ( !proxyIndex.isValid() )
00199       continue;
00200 
00201     // Inserting in a QMap gives us sorting by key for free.
00202     proxyMap.insert( proxyIndex.row(), proxyIndex );
00203   }
00204 
00205   if ( hits == -1 )
00206     return proxyMap.values();
00207 
00208   return proxyMap.values().mid( 0, hits );
00209 }
00210 
00211 int EntityMimeTypeFilterModel::columnCount(const QModelIndex &parent) const
00212 {
00213   Q_D(const EntityMimeTypeFilterModel);
00214 
00215   if (!sourceModel())
00216     return 0;
00217 
00218   const QVariant value = sourceModel()->data(mapToSource(parent), EntityTreeModel::ColumnCountRole + (EntityTreeModel::TerminalUserRole * d->m_headerGroup));
00219   if ( !value.isValid() )
00220     return 0;
00221 
00222   return value.toInt();
00223 }
00224 
00225 bool EntityMimeTypeFilterModel::hasChildren(const QModelIndex &parent) const
00226 {
00227   if (!sourceModel())
00228     return false;
00229 
00230   // QSortFilterProxyModel implementation is buggy in that it emits rowsAboutToBeInserted etc
00231   // only after the source model has emitted rowsInserted, instead of emitting it when the
00232   // source model emits rowsAboutToBeInserted. That means that the source and the proxy are out
00233   // of sync around the time of insertions, so we can't use the optimization below.
00234   return rowCount(parent) > 0;
00235 #if 0
00236 
00237   if ( !parent.isValid() )
00238     return sourceModel()->hasChildren(parent);
00239 
00240   Q_D(const EntityMimeTypeFilterModel);
00241   if ( EntityTreeModel::ItemListHeaders == d->m_headerGroup)
00242     return false;
00243 
00244   if ( EntityTreeModel::CollectionTreeHeaders == d->m_headerGroup )
00245   {
00246     QModelIndex childIndex = parent.child( 0, 0 );
00247     while ( childIndex.isValid() )
00248     {
00249       Collection col = childIndex.data( EntityTreeModel::CollectionRole ).value<Collection>();
00250       if (col.isValid())
00251         return true;
00252       childIndex = childIndex.sibling( childIndex.row() + 1, childIndex.column() );
00253     }
00254   }
00255   return false;
00256 #endif
00257 }
00258 
00259 bool EntityMimeTypeFilterModel::canFetchMore( const QModelIndex &parent ) const
00260 {
00261   Q_D(const EntityMimeTypeFilterModel);
00262   if ( EntityTreeModel::CollectionTreeHeaders == d->m_headerGroup )
00263     return false;
00264   return QSortFilterProxyModel::canFetchMore(parent);
00265 }
00266 
00267 #include "entitymimetypefiltermodel.moc"
00268 
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Apr 30 2012 21:49:15 by doxygen 1.8.0 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.8.3 API Reference

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