• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • 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 )
00041     {
00042       mimeChecker.addWantedMimeType( QLatin1String( "text/uri-list" ) );
00043     }
00044 
00045     bool collectionAccepted( const QModelIndex &index, bool checkResourceVisibility = true );
00046 
00047     void slotReset()
00048     {
00049       mParent->reset();
00050     }
00051 
00052     QList< QModelIndex > acceptedResources;
00053     CollectionFilterProxyModel *mParent;
00054     MimeTypeChecker mimeChecker;
00055 };
00056 
00057 bool CollectionFilterProxyModel::Private::collectionAccepted( const QModelIndex &index, bool checkResourceVisibility )
00058 {
00059   // Retrieve supported mimetypes
00060   const Collection collection = mParent->sourceModel()->data( index, CollectionModel::CollectionRole ).value<Collection>();
00061 
00062   // If this collection directly contains one valid mimetype, it is accepted
00063   if ( mimeChecker.isWantedCollection( collection ) ) {
00064     // The folder will be accepted, but we need to make sure the resource is visible too.
00065     if ( checkResourceVisibility ) {
00066 
00067       // find the resource
00068       QModelIndex resource = index;
00069       while ( resource.parent().isValid() )
00070         resource = resource.parent();
00071 
00072       // See if that resource is visible, if not, reset the model.
00073       if ( resource != index && !acceptedResources.contains( resource ) ) {
00074         kDebug() << "We got a new collection:" << mParent->sourceModel()->data( index ).toString()
00075                  << "but the resource is not visible:" << mParent->sourceModel()->data( resource ).toString();
00076         acceptedResources.clear();
00077         // defer reset, the model might still be supplying new items at this point which crashs
00078         QTimer::singleShot( 0, mParent, SLOT(slotReset()) );
00079         return true;
00080       }
00081     }
00082 
00083     // Keep track of all the resources that are visible.
00084     if ( !index.parent().isValid() )
00085       acceptedResources.append( index );
00086 
00087     return true;
00088   }
00089 
00090   // If this collection has a child which contains valid mimetypes, it is accepted
00091   QModelIndex childIndex = index.child( 0, 0 );
00092   while ( childIndex.isValid() ) {
00093     if ( collectionAccepted( childIndex, false /* don't check visibility of the parent, as we are checking the child now */ ) ) {
00094 
00095       // Keep track of all the resources that are visible.
00096       if ( !index.parent().isValid())
00097         acceptedResources.append( index );
00098 
00099       return true;
00100     }
00101     childIndex = childIndex.sibling( childIndex.row() + 1, 0 );
00102   }
00103 
00104   // Or else, no reason to keep this collection.
00105   return false;
00106 }
00107 
00108 
00109 CollectionFilterProxyModel::CollectionFilterProxyModel( QObject *parent )
00110   : QSortFilterProxyModel( parent ),
00111     d( new Private( this ) )
00112 {
00113   setSupportedDragActions( Qt::CopyAction | Qt::MoveAction );
00114 }
00115 
00116 CollectionFilterProxyModel::~CollectionFilterProxyModel()
00117 {
00118   delete d;
00119 }
00120 
00121 void CollectionFilterProxyModel::addMimeTypeFilters(const QStringList &typeList)
00122 {
00123   QStringList mimeTypes = d->mimeChecker.wantedMimeTypes() + typeList;
00124   d->mimeChecker.setWantedMimeTypes( mimeTypes );
00125   invalidateFilter();
00126 }
00127 
00128 void CollectionFilterProxyModel::addMimeTypeFilter(const QString &type)
00129 {
00130   d->mimeChecker.addWantedMimeType( type );
00131   invalidateFilter();
00132 }
00133 
00134 bool CollectionFilterProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent) const
00135 {
00136   return d->collectionAccepted( sourceModel()->index( sourceRow, 0, sourceParent ) );
00137 }
00138 
00139 QStringList CollectionFilterProxyModel::mimeTypeFilters() const
00140 {
00141   return d->mimeChecker.wantedMimeTypes();
00142 }
00143 
00144 void CollectionFilterProxyModel::clearFilters()
00145 {
00146   d->mimeChecker = MimeTypeChecker();
00147   invalidateFilter();
00148 }
00149 
00150 #include "collectionfilterproxymodel.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
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.9
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