• Skip to content
  • Skip to link menu
KDE 4.5 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • 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::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent) const
00101 {
00102   Q_D(const EntityMimeTypeFilterModel);
00103 
00104   const QModelIndex idx = sourceModel()->index(sourceRow, 0, sourceParent);
00105 
00106   const QString rowMimetype = idx.data( EntityTreeModel::MimeTypeRole ).toString();
00107 
00108   if ( d->excludedMimeTypes.contains( rowMimetype ) )
00109     return false;
00110   if ( d->includedMimeTypes.isEmpty() || d->includedMimeTypes.contains( rowMimetype ) )
00111     return true;
00112 
00113   return false;
00114 }
00115 
00116 QStringList EntityMimeTypeFilterModel::mimeTypeInclusionFilters() const
00117 {
00118   Q_D(const EntityMimeTypeFilterModel);
00119   return d->includedMimeTypes;
00120 }
00121 
00122 QStringList EntityMimeTypeFilterModel::mimeTypeExclusionFilters() const
00123 {
00124   Q_D(const EntityMimeTypeFilterModel);
00125   return d->excludedMimeTypes;
00126 }
00127 
00128 void EntityMimeTypeFilterModel::clearFilters()
00129 {
00130   Q_D(EntityMimeTypeFilterModel);
00131   d->includedMimeTypes.clear();
00132   d->excludedMimeTypes.clear();
00133   invalidateFilter();
00134 }
00135 
00136 void EntityMimeTypeFilterModel::setHeaderGroup(EntityTreeModel::HeaderGroup headerGroup)
00137 {
00138   Q_D(EntityMimeTypeFilterModel);
00139   d->m_headerGroup = headerGroup;
00140 }
00141 
00142 QVariant EntityMimeTypeFilterModel::headerData(int section, Qt::Orientation orientation, int role ) const
00143 {
00144   if (!sourceModel())
00145     return QVariant();
00146 
00147   Q_D(const EntityMimeTypeFilterModel);
00148   role += (EntityTreeModel::TerminalUserRole * d->m_headerGroup);
00149   return sourceModel()->headerData(section, orientation, role);
00150 }
00151 
00152 QModelIndexList EntityMimeTypeFilterModel::match( const QModelIndex& start, int role, const QVariant& value, int hits, Qt::MatchFlags flags ) const
00153 {
00154   if ( !sourceModel() )
00155     return QModelIndexList();
00156 
00157   if ( EntityTreeModel::AmazingCompletionRole != role ) {
00158     if ( role < Qt::UserRole )
00159       return QSortFilterProxyModel::match( start, role, value, hits, flags );
00160 
00161     QModelIndexList list;
00162     QModelIndex proxyIndex;
00163     foreach ( const QModelIndex &idx, sourceModel()->match( mapToSource( start ), role, value, hits, flags ) ) {
00164       proxyIndex = mapFromSource(idx);
00165       if (proxyIndex.isValid())
00166         list << proxyIndex;
00167     }
00168 
00169     return list;
00170   }
00171   // We match everything in the source model because sorting will change what we should show.
00172   const int allHits = -1;
00173 
00174   QModelIndexList proxyList;
00175   QMap<int, QModelIndex> proxyMap;
00176   const QModelIndexList sourceList = sourceModel()->match( mapToSource( start ), role, value, allHits, flags );
00177   QModelIndexList::const_iterator it;
00178   const QModelIndexList::const_iterator begin = sourceList.constBegin();
00179   const QModelIndexList::const_iterator end = sourceList.constEnd();
00180   QModelIndex proxyIndex;
00181   for ( it = begin; it != end; ++it ) {
00182     proxyIndex = mapFromSource( *it );
00183 
00184     // Any filtered indexes will be invalid when mapped.
00185     if ( !proxyIndex.isValid() )
00186       continue;
00187 
00188     // Inserting in a QMap gives us sorting by key for free.
00189     proxyMap.insert( proxyIndex.row(), proxyIndex );
00190   }
00191 
00192   if ( hits == -1 )
00193     return proxyMap.values();
00194 
00195   return proxyMap.values().mid( 0, hits );
00196 }
00197 
00198 int EntityMimeTypeFilterModel::columnCount(const QModelIndex &parent) const
00199 {
00200   Q_D(const EntityMimeTypeFilterModel);
00201 
00202   if (!sourceModel())
00203     return 0;
00204 
00205   const QVariant value = sourceModel()->data(mapToSource(parent), EntityTreeModel::ColumnCountRole + (EntityTreeModel::TerminalUserRole * d->m_headerGroup));
00206   if ( !value.isValid() )
00207     return 0;
00208 
00209   return value.toInt();
00210 }
00211 
00212 bool EntityMimeTypeFilterModel::hasChildren(const QModelIndex &parent) const
00213 {
00214   if (!sourceModel())
00215     return false;
00216 
00217   // QSortFilterProxyModel implementation is buggy in that it emits rowsAboutToBeInserted etc
00218   // only after the source model has emitted rowsInserted, instead of emitting it when the
00219   // source model emits rowsAboutToBeInserted. That means that the source and the proxy are out
00220   // of sync around the time of insertions, so we can't use the optimization below.
00221   return rowCount(parent) > 0;
00222 #if 0
00223 
00224   if ( !parent.isValid() )
00225     return sourceModel()->hasChildren(parent);
00226 
00227   Q_D(const EntityMimeTypeFilterModel);
00228   if ( EntityTreeModel::ItemListHeaders == d->m_headerGroup)
00229     return false;
00230 
00231   if ( EntityTreeModel::CollectionTreeHeaders == d->m_headerGroup )
00232   {
00233     QModelIndex childIndex = parent.child( 0, 0 );
00234     while ( childIndex.isValid() )
00235     {
00236       Collection col = childIndex.data( EntityTreeModel::CollectionRole ).value<Collection>();
00237       if (col.isValid())
00238         return true;
00239       childIndex = childIndex.sibling( childIndex.row() + 1, childIndex.column() );
00240     }
00241   }
00242   return false;
00243 #endif
00244 }
00245 
00246 bool EntityMimeTypeFilterModel::canFetchMore( const QModelIndex &parent ) const
00247 {
00248   Q_D(const EntityMimeTypeFilterModel);
00249   if ( EntityTreeModel::CollectionTreeHeaders == d->m_headerGroup )
00250     return false;
00251   return QSortFilterProxyModel::canFetchMore(parent);
00252 }
00253 
00254 #include "entitymimetypefiltermodel.moc"
00255 

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
  •   contact
  •   kmime
  • kabc
  • kblog
  • kcal
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.7.1
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