Akonadi

entityrightsfiltermodel.cpp
1 /*
2  Copyright (c) 2007 Bruno Virlet <[email protected]>
3  Copyright (c) 2009 Stephen Kelly <[email protected]>
4 
5  This library is free software; you can redistribute it and/or modify it
6  under the terms of the GNU Library General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or (at your
8  option) any later version.
9 
10  This library is distributed in the hope that it will be useful, but WITHOUT
11  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13  License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to the
17  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  02110-1301, USA.
19 */
20 
21 #include "entityrightsfiltermodel.h"
22 #include "entitytreemodel.h"
23 
24 using namespace Akonadi;
25 
26 namespace Akonadi
27 {
28 
32 class EntityRightsFilterModelPrivate
33 {
34 public:
35  EntityRightsFilterModelPrivate(EntityRightsFilterModel *parent)
36  : q_ptr(parent)
37  , mAccessRights(Collection::AllRights)
38  {
39  }
40 
41  bool rightsMatches(const QModelIndex &index) const
42  {
43  if (mAccessRights == Collection::AllRights ||
44  mAccessRights == Collection::ReadOnly) {
45  return true;
46  }
47 
48  const Collection collection = index.data(EntityTreeModel::CollectionRole).value<Collection>();
49  if (collection.isValid()) {
50  return (mAccessRights & collection.rights());
51  } else {
52  const Item item = index.data(EntityTreeModel::ItemRole).value<Item>();
53  if (item.isValid()) {
55  return (mAccessRights & collection.rights());
56  } else {
57  return false;
58  }
59  }
60  }
61 
62  Q_DECLARE_PUBLIC(EntityRightsFilterModel)
64 
65  Collection::Rights mAccessRights;
66 };
67 
68 }
69 
71  : QSortFilterProxyModel(parent)
72  , d_ptr(new EntityRightsFilterModelPrivate(this))
73 {
74  setRecursiveFilteringEnabled(true);
75 }
76 
78 {
79  delete d_ptr;
80 }
81 
82 
83 bool EntityRightsFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
84 {
85  Q_D(const EntityRightsFilterModel);
86 
87  const QModelIndex modelIndex = sourceModel()->index(sourceRow, 0, sourceParent);
88 
89  return d->rightsMatches(modelIndex);
90 }
91 
93 {
95  d->mAccessRights = rights;
96  invalidateFilter();
97 }
98 
100 {
101  Q_D(const EntityRightsFilterModel);
102  return d->mAccessRights;
103 }
104 
106 {
107  Q_D(const EntityRightsFilterModel);
108 
109  if (d->rightsMatches(index)) {
110  return QSortFilterProxyModel::flags(index);
111  } else {
112  return QSortFilterProxyModel::flags(index) & ~(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
113  }
114 }
115 
116 QModelIndexList EntityRightsFilterModel::match(const QModelIndex &start, int role, const QVariant &value, int hits, Qt::MatchFlags flags) const
117 {
118  if (role < Qt::UserRole) {
119  return QSortFilterProxyModel::match(start, role, value, hits, flags);
120  }
121 
122  QModelIndexList list;
123  QModelIndex proxyIndex;
124  const auto matches = sourceModel()->match(mapToSource(start), role, value, hits, flags);
125  for (const auto &idx : matches) {
126  proxyIndex = mapFromSource(idx);
127  if (proxyIndex.isValid()) {
128  list.push_back(proxyIndex);
129  }
130  }
131 
132  return list;
133 }
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:137
virtual Qt::ItemFlags flags(const QModelIndex &index) const const override
typedef MatchFlags
Has all rights on this storage collection.
Definition: collection.h:102
Represents a collection of PIM items.
Definition: collection.h:76
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:93
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.
The parent collection of the entity.
Rights rights() const
Returns the rights the user has on the collection.
Definition: collection.cpp:242
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-2020 The KDE developers.
Generated on Fri Jun 5 2020 23:08:54 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.