• Skip to content
  • Skip to link menu
KDE 4.8 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • KDE Home
  • Contact Us
 

akonadi

collectionfilterproxymodel.cpp
00001 /*
00002     Copyright (c) 2007 Bruno Virlet <bruno.virlet@gmail.com>
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 "collectionfilterproxymodel.h"
00021 
00022 #include "collectionmodel.h"
00023 #include "mimetypechecker.h"
00024 
00025 #include <kdebug.h>
00026 
00027 #include <QtCore/QString>
00028 #include <QtCore/QStringList>
00029 #include <QTimer>
00030 
00031 using namespace Akonadi;
00032 
00036 class CollectionFilterProxyModel::Private
00037 {
00038   public:
00039     Private( CollectionFilterProxyModel *parent )
00040       : mParent( parent ), mExcludeVirtualCollections( false )
00041     {
00042       mimeChecker.addWantedMimeType( QLatin1String( "text/uri-list" ) );
00043     }
00044 
00045     bool collectionAccepted( const QModelIndex &index, bool checkResourceVisibility = true );
00046 
00047     QVector< QModelIndex > acceptedResources;
00048     CollectionFilterProxyModel *mParent;
00049     MimeTypeChecker mimeChecker;
00050     bool mExcludeVirtualCollections;
00051 };
00052 
00053 bool CollectionFilterProxyModel::Private::collectionAccepted( const QModelIndex &index, bool checkResourceVisibility )
00054 {
00055   // Retrieve supported mimetypes
00056   const Collection collection = mParent->sourceModel()->data( index, CollectionModel::CollectionRole ).value<Collection>();
00057 
00058   if ( !collection.isValid() )
00059     return false;
00060 
00061   if ( collection.isVirtual() && mExcludeVirtualCollections )
00062     return false;
00063 
00064   // If this collection directly contains one valid mimetype, it is accepted
00065   if ( mimeChecker.isWantedCollection( collection ) ) {
00066     // The folder will be accepted, but we need to make sure the resource is visible too.
00067     if ( checkResourceVisibility ) {
00068 
00069       // find the resource
00070       QModelIndex resource = index;
00071       while ( resource.parent().isValid() )
00072         resource = resource.parent();
00073 
00074       // See if that resource is visible, if not, invalidate the filter.
00075       if ( resource != index && !acceptedResources.contains( resource ) ) {
00076         kDebug() << "We got a new collection:" << mParent->sourceModel()->data( index ).toString()
00077                  << "but the resource is not visible:" << mParent->sourceModel()->data( resource ).toString();
00078         acceptedResources.clear();
00079         // defer reset, the model might still be supplying new items at this point which crashs
00080         mParent->invalidateFilter();
00081         return true;
00082       }
00083     }
00084 
00085     // Keep track of all the resources that are visible.
00086     if ( !index.parent().isValid() )
00087       acceptedResources.append( index );
00088 
00089     return true;
00090   }
00091 
00092   // If this collection has a child which contains valid mimetypes, it is accepted
00093   QModelIndex childIndex = index.child( 0, 0 );
00094   while ( childIndex.isValid() ) {
00095     if ( collectionAccepted( childIndex, false /* don't check visibility of the parent, as we are checking the child now */ ) ) {
00096 
00097       // Keep track of all the resources that are visible.
00098       if ( !index.parent().isValid())
00099         acceptedResources.append( index );
00100 
00101       return true;
00102     }
00103     childIndex = childIndex.sibling( childIndex.row() + 1, 0 );
00104   }
00105 
00106   // Or else, no reason to keep this collection.
00107   return false;
00108 }
00109 
00110 
00111 CollectionFilterProxyModel::CollectionFilterProxyModel( QObject *parent )
00112   : QSortFilterProxyModel( parent ),
00113     d( new Private( this ) )
00114 {
00115 }
00116 
00117 CollectionFilterProxyModel::~CollectionFilterProxyModel()
00118 {
00119   delete d;
00120 }
00121 
00122 void CollectionFilterProxyModel::addMimeTypeFilters(const QStringList &typeList)
00123 {
00124   QStringList mimeTypes = d->mimeChecker.wantedMimeTypes() + typeList;
00125   d->mimeChecker.setWantedMimeTypes( mimeTypes );
00126   invalidateFilter();
00127 }
00128 
00129 void CollectionFilterProxyModel::addMimeTypeFilter(const QString &type)
00130 {
00131   d->mimeChecker.addWantedMimeType( type );
00132   invalidateFilter();
00133 }
00134 
00135 bool CollectionFilterProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent) const
00136 {
00137   return d->collectionAccepted( sourceModel()->index( sourceRow, 0, sourceParent ) );
00138 }
00139 
00140 QStringList CollectionFilterProxyModel::mimeTypeFilters() const
00141 {
00142   return d->mimeChecker.wantedMimeTypes();
00143 }
00144 
00145 void CollectionFilterProxyModel::clearFilters()
00146 {
00147   d->mimeChecker = MimeTypeChecker();
00148   invalidateFilter();
00149 }
00150 
00151 void CollectionFilterProxyModel::setExcludeVirtualCollections( bool exclude )
00152 {
00153   if ( exclude != d->mExcludeVirtualCollections ) {
00154     d->mExcludeVirtualCollections = exclude;
00155     invalidateFilter();
00156   }
00157 }
00158 
00159 Qt::ItemFlags CollectionFilterProxyModel::flags( const QModelIndex& index ) const
00160 {
00161   if ( !index.isValid() ) {
00162     // Don't crash
00163     return 0;
00164   }
00165   
00166   const Collection collection = sourceModel()->data( mapToSource( index ), CollectionModel::CollectionRole ).value<Collection>();
00167 
00168   // If this collection directly contains one valid mimetype, it is accepted
00169   if ( d->mimeChecker.isWantedCollection( collection ) )
00170     return QSortFilterProxyModel::flags( index );
00171   else
00172     return QSortFilterProxyModel::flags( index ) & ~( Qt::ItemIsSelectable );
00173 }
00174 
00175 #include "collectionfilterproxymodel.moc"

akonadi

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

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • 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
Generated for KDE-PIM Libraries by doxygen 1.7.6.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