• Skip to content
  • Skip to link menu
KDE 4.4 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   {
00159     if (role < Qt::UserRole)
00160       return QSortFilterProxyModel::match(start, role, value, hits, flags);
00161 
00162     QModelIndexList list;
00163     QModelIndex proxyIndex;
00164     foreach(const QModelIndex idx, sourceModel()->match(mapToSource(start), role, value, hits, flags))
00165     {
00166       proxyIndex = mapFromSource(idx);
00167       if (proxyIndex.isValid())
00168         list << proxyIndex;
00169     }
00170     return list;
00171   }
00172   // We match everything in the source model because sorting will change what we should show.
00173   const int allHits = -1;
00174 
00175   QModelIndexList proxyList;
00176   QMap<int, QModelIndex> proxyMap;
00177   QModelIndexList sourceList = sourceModel()->match(mapToSource(start), role, value, allHits, flags);
00178   QModelIndexList::const_iterator it;
00179   const QModelIndexList::const_iterator begin = sourceList.constBegin();
00180   const QModelIndexList::const_iterator end = sourceList.constEnd();
00181   QModelIndex proxyIndex;
00182   for (it = begin; it != end; ++it)
00183   {
00184     proxyIndex = mapFromSource(*it);
00185 
00186     // Any filtered indexes will be invalid when mapped.
00187     if (!proxyIndex.isValid())
00188       continue;
00189 
00190     // Inserting in a QMap gives us sorting by key for free.
00191     proxyMap.insert(proxyIndex.row(), proxyIndex);
00192   }
00193 
00194   if (hits == -1)
00195     return proxyMap.values();
00196 
00197   return proxyMap.values().mid(0, hits);
00198 }
00199 
00200 int EntityMimeTypeFilterModel::columnCount(const QModelIndex &parent) const
00201 {
00202   Q_D(const EntityMimeTypeFilterModel);
00203 
00204   if (!sourceModel())
00205     return 0;
00206 
00207   QVariant var = sourceModel()->data(parent, EntityTreeModel::ColumnCountRole + (EntityTreeModel::TerminalUserRole * d->m_headerGroup));
00208   if( !var.isValid() )
00209     return 0;
00210   return var.toInt();
00211 }
00212 
00213 bool EntityMimeTypeFilterModel::hasChildren(const QModelIndex &parent) const
00214 {
00215   if (!sourceModel())
00216     return false;
00217 
00218   if ( !parent.isValid() )
00219     return sourceModel()->hasChildren(parent);
00220 
00221   Q_D(const EntityMimeTypeFilterModel);
00222   if ( EntityTreeModel::ItemListHeaders == d->m_headerGroup)
00223     return false;
00224 
00225   if ( EntityTreeModel::CollectionTreeHeaders == d->m_headerGroup )
00226   {
00227     QModelIndex childIndex = parent.child( 0, 0 );
00228     while ( childIndex.isValid() )
00229     {
00230       Collection col = childIndex.data( EntityTreeModel::CollectionRole ).value<Collection>();
00231       if (col.isValid())
00232         return true;
00233       childIndex = childIndex.sibling( childIndex.row() + 1, childIndex.column() );
00234     }
00235   }
00236   return false;
00237 }
00238 
00239 bool EntityMimeTypeFilterModel::canFetchMore( const QModelIndex &parent ) const
00240 {
00241   Q_D(const EntityMimeTypeFilterModel);
00242   if ( EntityTreeModel::CollectionTreeHeaders == d->m_headerGroup )
00243     return false;
00244   return QSortFilterProxyModel::canFetchMore(parent);
00245 }
00246 
00247 #include "entitymimetypefiltermodel.moc"
00248 

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.6.2-20100208
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