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 "agentinstancemodel.h"
10 #include "agenttypemodel.h"
11 
12 #include <QMimeDatabase>
13 #include <QMimeType>
14 #include <QStringList>
15 
16 using namespace Akonadi;
17 
18 // ensure the role numbers are equivalent for both source models
19 static_assert(static_cast<int>(AgentTypeModel::CapabilitiesRole) == static_cast<int>(AgentInstanceModel::CapabilitiesRole),
20  "AgentTypeModel::CapabilitiesRole does not match AgentInstanceModel::CapabilitiesRole");
21 static_assert(static_cast<int>(AgentTypeModel::MimeTypesRole) == static_cast<int>(AgentInstanceModel::MimeTypesRole),
22  "AgentTypeModel::MimeTypesRole does not match AgentInstanceModel::MimeTypesRole");
23 
24 /**
25  * @internal
26  */
27 class Akonadi::AgentFilterProxyModelPrivate
28 {
29 public:
32  QStringList excludeCapabilities;
33  bool filterAcceptRegExp(const QModelIndex &index, const QRegularExpression &filterRegExpStr);
34 };
35 
37  : QSortFilterProxyModel(parent)
38  , d(new AgentFilterProxyModelPrivate)
39 {
41 }
42 
44 
46 {
47  d->mimeTypes << mimeType;
49 }
50 
52 {
53  d->capabilities << capability;
55 }
56 
58 {
59  d->excludeCapabilities << capability;
61 }
62 
64 {
65  d->capabilities.clear();
66  d->mimeTypes.clear();
67  d->excludeCapabilities.clear();
69 }
70 
71 bool AgentFilterProxyModelPrivate::filterAcceptRegExp(const QModelIndex &index, const QRegularExpression &filterRegExpStr)
72 {
73  // First see if the name matches a set regexp filter.
74  if (!filterRegExpStr.pattern().isEmpty()) {
75  return index.data(AgentTypeModel::IdentifierRole).toString().contains(filterRegExpStr) || index.data().toString().contains(filterRegExpStr);
76  }
77  return true;
78 }
79 
80 bool AgentFilterProxyModel::filterAcceptsRow(int row, const QModelIndex & /*source_parent*/) const
81 {
82  const QModelIndex index = sourceModel()->index(row, 0);
83 
84  if (!d->mimeTypes.isEmpty()) {
85  QMimeDatabase mimeDb;
86  bool found = false;
88  for (const QString &mimeType : lst) {
89  if (d->mimeTypes.contains(mimeType)) {
90  found = true;
91  } else {
92  const QMimeType mt = mimeDb.mimeTypeForName(mimeType);
93  if (mt.isValid()) {
94  for (const QString &type : std::as_const(d->mimeTypes)) {
95  if (mt.inherits(type)) {
96  found = true;
97  break;
98  }
99  }
100  }
101  }
102 
103  if (found) {
104  break;
105  }
106  }
107 
108  if (!found) {
109  return false;
110  }
111  }
112 
113  if (!d->capabilities.isEmpty()) {
114  bool found = false;
116  for (const QString &capability : lst) {
117  if (d->capabilities.contains(capability)) {
118  found = true;
119  break;
120  }
121  }
122 
123  if (!found) {
124  return false;
125  }
126 
127  if (!d->excludeCapabilities.isEmpty()) {
129  for (const QString &capability : lstCapabilities) {
130  if (d->excludeCapabilities.contains(capability)) {
131  found = false;
132  break;
133  }
134  }
135 
136  if (!found) {
137  return false;
138  }
139  }
140  }
141 
142  return d->filterAcceptRegExp(index, filterRegularExpression());
143 }
QString pattern() const const
bool inherits(const QString &mimeTypeName) const const
AgentFilterProxyModel(QObject *parent=nullptr)
Create a new agent filter proxy model.
@ CapabilitiesRole
A list of supported capabilities.
@ MimeTypesRole
A list of supported mimetypes.
@ CapabilitiesRole
A list of supported capabilities.
@ MimeTypesRole
A list of supported mimetypes.
void excludeCapabilities(const QString &capability)
Excludes agents with the given capability.
QVariant data(int role) const const
QMimeType mimeTypeForName(const QString &nameOrAlias) const const
bool isEmpty() const const
void setDynamicSortFilter(bool enable)
@ IdentifierRole
The identifier of the agent type.
bool isValid() const const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
void addMimeTypeFilter(const QString &mimeType)
Accept agents supporting mimeType.
~AgentFilterProxyModel() override
Destroys the agent filter proxy model.
void clearFilters()
Clear the filters ( mimeTypes & capabilities ).
Capabilities capabilities()
QStringList mimeTypes(Mode mode=Writing)
void addCapabilityFilter(const QString &capability)
Accept agents with the given capability.
QStringList toStringList() const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
QString toString() const const
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jun 25 2022 06:00:31 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.