Akonadi

agentfilterproxymodel.cpp
1 /*
2  SPDX-FileCopyrightText: 2007 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "agentfilterproxymodel.h"
8 
9 #include "agenttypemodel.h"
10 #include "agentinstancemodel.h"
11 
12 
13 
14 #include <QStringList>
15 #include <QMimeDatabase>
16 #include <QMimeType>
17 
18 using namespace Akonadi;
19 
20 // ensure the role numbers are equivalent for both source models
21 static_assert(static_cast<int>(AgentTypeModel::CapabilitiesRole) == static_cast<int>(AgentInstanceModel::CapabilitiesRole),
22  "AgentTypeModel::CapabilitiesRole does not match AgentInstanceModel::CapabilitiesRole");
23 static_assert(static_cast<int>(AgentTypeModel::MimeTypesRole) == static_cast<int>(AgentInstanceModel::MimeTypesRole),
24  "AgentTypeModel::MimeTypesRole does not match AgentInstanceModel::MimeTypesRole");
25 
29 class Q_DECL_HIDDEN AgentFilterProxyModel::Private
30 {
31 public:
34  QStringList excludeCapabilities;
35  bool filterAcceptRegExp(const QModelIndex &index, const QRegExp &filterRegExpStr);
36 };
37 
39  : QSortFilterProxyModel(parent)
40  , d(new Private)
41 {
43 }
44 
46 {
47  delete d;
48 }
49 
51 {
52  d->mimeTypes << mimeType;
54 }
55 
57 {
58  d->capabilities << capability;
60 }
61 
63 {
64  d->excludeCapabilities << capability;
66 }
67 
69 {
70  d->capabilities.clear();
71  d->mimeTypes.clear();
72  d->excludeCapabilities.clear();
74 }
75 
76 bool AgentFilterProxyModel::Private::filterAcceptRegExp(const QModelIndex &index, const QRegExp &filterRegExpStr)
77 {
78  // First see if the name matches a set regexp filter.
79  if (!filterRegExpStr.isEmpty()) {
80  return index.data(AgentTypeModel::IdentifierRole).toString().contains(filterRegExpStr)
81  || index.data().toString().contains(filterRegExpStr);
82  }
83  return true;
84 }
85 
86 bool AgentFilterProxyModel::filterAcceptsRow(int row, const QModelIndex & /*source_parent*/) const
87 {
88  const QModelIndex index = sourceModel()->index(row, 0);
89 
90  if (!d->mimeTypes.isEmpty()) {
91  QMimeDatabase mimeDb;
92  bool found = false;
94  for (const QString &mimeType : lst) {
95  if (d->mimeTypes.contains(mimeType)) {
96  found = true;
97  } else {
98  const QMimeType mt = mimeDb.mimeTypeForName(mimeType);
99  if (mt.isValid()) {
100  for (const QString &type : qAsConst(d->mimeTypes)) {
101  if (mt.inherits(type)) {
102  found = true;
103  break;
104  }
105  }
106  }
107  }
108 
109  if (found) {
110  break;
111  }
112  }
113 
114  if (!found) {
115  return false;
116  }
117  }
118 
119  if (!d->capabilities.isEmpty()) {
120  bool found = false;
122  for (const QString &capability : lst) {
123  if (d->capabilities.contains(capability)) {
124  found = true;
125  break;
126  }
127  }
128 
129  if (!found) {
130  return false;
131  }
132 
133  if (found && !d->excludeCapabilities.isEmpty()) {
135  for (const QString &capability : lst) {
136  if (d->excludeCapabilities.contains(capability)) {
137  found = false;
138  break;
139  }
140  }
141 
142  if (!found) {
143  return false;
144  }
145  }
146  }
147 
148  return d->filterAcceptRegExp(index, filterRegExp());
149 }
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const =0
bool isEmpty() const const
void addMimeTypeFilter(const QString &mimeType)
Accept agents supporting mimeType.
bool inherits(const QString &mimeTypeName) const const
void excludeCapabilities(const QString &capability)
Excludes agents with the given capability.
The identifier of the agent type.
void setDynamicSortFilter(bool enable)
AgentFilterProxyModel(QObject *parent=nullptr)
Create a new agent filter proxy model.
QMimeType mimeTypeForName(const QString &nameOrAlias) const const
QStringList mimeTypes(Types)
A list of supported capabilities.
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isValid() const const
QAbstractItemModel * sourceModel() const const
QStringList toStringList() const const
QVariant data(int role) const const
Helper integration between Akonadi and Qt.
A proxy model for filtering AgentType or AgentInstance.
KDEGAMES_EXPORT Capabilities capabilities()
~AgentFilterProxyModel() override
Destroys the agent filter proxy model.
A list of supported capabilities.
QString toString() const const
void addCapabilityFilter(const QString &capability)
Accept agents with the given capability.
void clearFilters()
Clear the filters ( mimeTypes & capabilities ).
QRegExp filterRegExp() const const
A list of supported mimetypes.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Thu Jul 9 2020 23:15:21 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.