00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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"