Akonadi

entityrightsfiltermodel.cpp
1 /*
2  SPDX-FileCopyrightText: 2007 Bruno Virlet <[email protected]>
3  SPDX-FileCopyrightText: 2009 Stephen Kelly <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "entityrightsfiltermodel.h"
9 
10 using namespace Akonadi;
11 
12 namespace Akonadi
13 {
14 /**
15  * @internal
16  */
17 class EntityRightsFilterModelPrivate
18 {
19 public:
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 
63 bool 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 
73 {
75  d->mAccessRights = rights;
77 }
78 
80 {
82  return d->mAccessRights;
83 }
84 
86 {
88 
89  if (d->rightsMatches(index)) {
91  } else {
93  }
94 }
95 
96 QModelIndexList 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 }
bool isValid() const
Returns whether the item is valid.
Definition: item.cpp:88
UserRole
EntityRightsFilterModel(QObject *parent=nullptr)
Creates a new entity rights filter model.
T value() const const
@ ReadOnly
Can only read items or subcollection of this collection.
Definition: collection.h:90
Q_SCRIPTABLE Q_NOREPLY void start()
Represents a collection of PIM items.
Definition: collection.h:61
A proxy model that filters entities by access rights.
void push_back(const T &value)
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const const override
QModelIndexList match(const QModelIndex &start, int role, const QVariant &value, int hits=1, Qt::MatchFlags flags=Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const override
void setRecursiveFilteringEnabled(bool recursive)
QVariant data(int role) const const
typedef ItemFlags
~EntityRightsFilterModel() override
Destroys the entity rights filter model.
virtual Qt::ItemFlags flags(const QModelIndex &index) const const override
@ CollectionRole
The collection.
@ AllRights
Has all rights on this storage collection.
Definition: collection.h:99
virtual QModelIndexList match(const QModelIndex &start, int role, const QVariant &value, int hits, Qt::MatchFlags flags) const const override
bool isValid() const const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
Collection::Rights accessRights() const
Returns the access rights that are used for filtering.
virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const const override
void setAccessRights(Collection::Rights rights)
Sets the access rights the entities shall be filtered against.
typedef MatchFlags
@ ParentCollectionRole
The parent collection of the entity.
Qt::ItemFlags flags(const QModelIndex &index) const override
Represents a PIM item stored in Akonadi storage.
Definition: item.h:104
Q_D(Todo)
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Jun 30 2022 03:51:46 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.