Akonadi

entityrightsfiltermodel.cpp
1/*
2 SPDX-FileCopyrightText: 2007 Bruno Virlet <bruno.virlet@gmail.com>
3 SPDX-FileCopyrightText: 2009 Stephen Kelly <steveire@gmail.com>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "entityrightsfiltermodel.h"
9
10using namespace Akonadi;
11
12namespace Akonadi
13{
14/**
15 * @internal
16 */
17class EntityRightsFilterModelPrivate
18{
19public:
20 explicit EntityRightsFilterModelPrivate(EntityRightsFilterModel *parent)
21 : q_ptr(parent)
22 , mAccessRights(Collection::AllRights)
23 {
24 }
25
26 bool rightsMatches(const QModelIndex &index) const
27 {
28 if (mAccessRights == Collection::AllRights || mAccessRights == Collection::ReadOnly) {
29 return true;
30 }
31
32 const auto collection = index.data(EntityTreeModel::CollectionRole).value<Collection>();
33 if (collection.isValid()) {
34 return (mAccessRights & collection.rights());
35 } else {
36 const Item item = index.data(EntityTreeModel::ItemRole).value<Item>();
37 if (item.isValid()) {
38 const auto collection = index.data(EntityTreeModel::ParentCollectionRole).value<Collection>();
39 return (mAccessRights & collection.rights());
40 } else {
41 return false;
42 }
43 }
44 }
45
46 Q_DECLARE_PUBLIC(EntityRightsFilterModel)
48
49 Collection::Rights mAccessRights;
50};
51
52} // namespace Akonadi
53
55 : QSortFilterProxyModel(parent)
56 , d_ptr(new EntityRightsFilterModelPrivate(this))
57{
59}
60
62
63bool EntityRightsFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
64{
66
67 const QModelIndex modelIndex = sourceModel()->index(sourceRow, 0, sourceParent);
68
69 return d->rightsMatches(modelIndex);
70}
71
78
80{
82 return d->mAccessRights;
83}
84
86{
88
89 if (d->rightsMatches(index)) {
91 } else {
93 }
94}
95
96QModelIndexList EntityRightsFilterModel::match(const QModelIndex &start, int role, const QVariant &value, int hits, Qt::MatchFlags flags) const
97{
98 if (role < Qt::UserRole) {
99 return QSortFilterProxyModel::match(start, role, value, hits, flags);
100 }
101
102 QModelIndexList list;
103 QModelIndex proxyIndex;
104 const auto matches = sourceModel()->match(mapToSource(start), role, value, hits, flags);
105 for (const auto &idx : matches) {
106 proxyIndex = mapFromSource(idx);
107 if (proxyIndex.isValid()) {
108 list.push_back(proxyIndex);
109 }
110 }
111
112 return list;
113}
114
115#include "moc_entityrightsfiltermodel.cpp"
Represents a collection of PIM items.
Definition collection.h:62
@ ReadOnly
Can only read items or subcollection of this collection.
Definition collection.h:90
@ AllRights
Has all rights on this storage collection.
Definition collection.h:99
A proxy model that filters entities by access rights.
void setAccessRights(Collection::Rights rights)
Sets the access rights the entities shall be filtered against.
~EntityRightsFilterModel() override
Destroys the entity rights filter model.
Qt::ItemFlags flags(const QModelIndex &index) const override
EntityRightsFilterModel(QObject *parent=nullptr)
Creates a new entity rights filter model.
QModelIndexList match(const QModelIndex &start, int role, const QVariant &value, int hits=1, Qt::MatchFlags flags=Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const override
Collection::Rights accessRights() const
Returns the access rights that are used for filtering.
@ ParentCollectionRole
The parent collection of the entity.
@ CollectionRole
The collection.
Q_SCRIPTABLE Q_NOREPLY void start()
Helper integration between Akonadi and Qt.
void push_back(parameter_type value)
QVariant data(int role) const const
bool isValid() const const
virtual Qt::ItemFlags flags(const QModelIndex &index) const const override
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const const override
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const const override
virtual QModelIndexList match(const QModelIndex &start, int role, const QVariant &value, int hits, Qt::MatchFlags flags) const const override
void setRecursiveFilteringEnabled(bool recursive)
UserRole
typedef ItemFlags
typedef MatchFlags
T value() const const
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:29 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.