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