akonadi
entityrightsfiltermodel.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 "entityrightsfiltermodel.h" 00023 00024 #include "entitytreemodel.h" 00025 00026 #include <kdebug.h> 00027 00028 using namespace Akonadi; 00029 00030 namespace Akonadi { 00031 00035 class EntityRightsFilterModelPrivate 00036 { 00037 public: 00038 EntityRightsFilterModelPrivate( EntityRightsFilterModel *parent ) 00039 : q_ptr( parent ), mAccessRights( Collection::AllRights ) 00040 { 00041 } 00042 00043 bool rightsMatches( const QModelIndex &index ) const 00044 { 00045 if ( mAccessRights == Collection::AllRights || mAccessRights == Collection::ReadOnly ) 00046 return true; 00047 00048 const Collection collection = index.data( EntityTreeModel::CollectionRole ).value<Collection>(); 00049 if ( collection.isValid() ) { 00050 return (mAccessRights & collection.rights()); 00051 } else { 00052 const Item item = index.data( EntityTreeModel::ItemRole ).value<Item>(); 00053 if ( item.isValid() ) { 00054 const Collection collection = index.data( EntityTreeModel::ParentCollectionRole ).value<Collection>(); 00055 return (mAccessRights & collection.rights()); 00056 } else { 00057 return false; 00058 } 00059 } 00060 } 00061 00062 Q_DECLARE_PUBLIC(EntityRightsFilterModel) 00063 EntityRightsFilterModel *q_ptr; 00064 00065 Collection::Rights mAccessRights; 00066 }; 00067 00068 } 00069 00070 EntityRightsFilterModel::EntityRightsFilterModel( QObject *parent ) 00071 : KRecursiveFilterProxyModel( parent ), 00072 d_ptr( new EntityRightsFilterModelPrivate( this ) ) 00073 { 00074 } 00075 00076 EntityRightsFilterModel::~EntityRightsFilterModel() 00077 { 00078 delete d_ptr; 00079 } 00080 00081 void EntityRightsFilterModel::setAccessRights( Collection::Rights rights ) 00082 { 00083 Q_D(EntityRightsFilterModel); 00084 d->mAccessRights = rights; 00085 invalidateFilter(); 00086 } 00087 00088 Collection::Rights EntityRightsFilterModel::accessRights() const 00089 { 00090 Q_D(const EntityRightsFilterModel); 00091 return d->mAccessRights; 00092 } 00093 00094 bool EntityRightsFilterModel::acceptRow( int sourceRow, const QModelIndex &sourceParent) const 00095 { 00096 Q_D(const EntityRightsFilterModel); 00097 00098 const QModelIndex modelIndex = sourceModel()->index( sourceRow, 0, sourceParent ); 00099 00100 return d->rightsMatches( modelIndex ); 00101 } 00102 00103 Qt::ItemFlags EntityRightsFilterModel::flags( const QModelIndex &index ) const 00104 { 00105 Q_D(const EntityRightsFilterModel); 00106 00107 if ( d->rightsMatches( index ) ) 00108 return KRecursiveFilterProxyModel::flags( index ); 00109 else 00110 return KRecursiveFilterProxyModel::flags( index ) & ~(Qt::ItemIsSelectable | Qt::ItemIsEnabled); 00111 } 00112 00113 QModelIndexList EntityRightsFilterModel::match( const QModelIndex& start, int role, const QVariant& value, int hits, Qt::MatchFlags flags ) const 00114 { 00115 if ( role < Qt::UserRole ) 00116 return QSortFilterProxyModel::match( start, role, value, hits, flags ); 00117 00118 QModelIndexList list; 00119 QModelIndex proxyIndex; 00120 foreach ( const QModelIndex &idx, sourceModel()->match( mapToSource( start ), role, value, hits, flags ) ) { 00121 proxyIndex = mapFromSource( idx ); 00122 if ( proxyIndex.isValid() ) 00123 list << proxyIndex; 00124 } 00125 00126 return list; 00127 } 00128 00129 #include "entityrightsfiltermodel.moc"