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 
18 class EntityRightsFilterModelPrivate
19 {
20 public:
21  explicit EntityRightsFilterModelPrivate(EntityRightsFilterModel *parent)
22  : q_ptr(parent)
23  , mAccessRights(Collection::AllRights)
24  {
25  }
26 
27  bool rightsMatches(const QModelIndex &index) const
28  {
29  if (mAccessRights == Collection::AllRights ||
30  mAccessRights == Collection::ReadOnly) {
31  return true;
32  }
33 
34  const Collection collection = index.data(EntityTreeModel::CollectionRole).value<Collection>();
35  if (collection.isValid()) {
36  return (mAccessRights & collection.rights());
37  } else {
38  const Item item = index.data(EntityTreeModel::ItemRole).value<Item>();
39  if (item.isValid()) {
41  return (mAccessRights & collection.rights());
42  } else {
43  return false;
44  }
45  }
46  }
47 
48  Q_DECLARE_PUBLIC(EntityRightsFilterModel)
50 
51  Collection::Rights mAccessRights;
52 };
53 
54 } // namespace Akonadi
55 
57  : QSortFilterProxyModel(parent)
58  , d_ptr(new EntityRightsFilterModelPrivate(this))
59 {
60  setRecursiveFilteringEnabled(true);
61 }
62 
64 {
65  delete d_ptr;
66 }
67 
68 
69 bool EntityRightsFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
70 {
71  Q_D(const EntityRightsFilterModel);
72 
73  const QModelIndex modelIndex = sourceModel()->index(sourceRow, 0, sourceParent);
74 
75  return d->rightsMatches(modelIndex);
76 }
77 
79 {
81  d->mAccessRights = rights;
82  invalidateFilter();
83 }
84 
86 {
87  Q_D(const EntityRightsFilterModel);
88  return d->mAccessRights;
89 }
90 
92 {
93  Q_D(const EntityRightsFilterModel);
94 
95  if (d->rightsMatches(index)) {
96  return QSortFilterProxyModel::flags(index);
97  } else {
99  }
100 }
101 
102 QModelIndexList EntityRightsFilterModel::match(const QModelIndex &start, int role, const QVariant &value, int hits, Qt::MatchFlags flags) const
103 {
104  if (role < Qt::UserRole) {
105  return QSortFilterProxyModel::match(start, role, value, hits, flags);
106  }
107 
108  QModelIndexList list;
109  QModelIndex proxyIndex;
110  const auto matches = sourceModel()->match(mapToSource(start), role, value, hits, flags);
111  for (const auto &idx : matches) {
112  proxyIndex = mapFromSource(idx);
113  if (proxyIndex.isValid()) {
114  list.push_back(proxyIndex);
115  }
116  }
117 
118  return list;
119 }
void setAccessRights(Collection::Rights rights)
Sets the access rights the entities shall be filtered against.
bool isValid() const
Returns whether the collection is valid.
Definition: collection.cpp:123
virtual Qt::ItemFlags flags(const QModelIndex &index) const const override
typedef MatchFlags
Has all rights on this storage collection.
Definition: collection.h:89
Represents a collection of PIM items.
Definition: collection.h:63
Collection::Rights accessRights() const
Returns the access rights that are used for filtering.
virtual QModelIndexList match(const QModelIndex &start, int role, const QVariant &value, int hits, Qt::MatchFlags flags) const const override
T value() const const
A proxy model that filters entities by access rights.
Qt::ItemFlags flags(const QModelIndex &index) const override
Can only read items or subcollection of this collection.
Definition: collection.h:80
bool isValid() const const
QModelIndexList match(const QModelIndex &start, int role, const QVariant &value, int hits=1, Qt::MatchFlags flags=Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const override
~EntityRightsFilterModel() override
Destroys the entity rights filter model.
UserRole
The parent collection of the entity.
Rights rights() const
Returns the rights the user has on the collection.
Definition: collection.cpp:228
QVariant data(int role) const const
Helper integration between Akonadi and Qt.
typedef ItemFlags
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Jan 23 2021 07:17:48 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.