Akonadi

agentfilterproxymodel.cpp
1/*
2 SPDX-FileCopyrightText: 2007 Volker Krause <vkrause@kde.org>
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
16using namespace Akonadi;
17
18// ensure the role numbers are equivalent for both source models
19static_assert(static_cast<int>(AgentTypeModel::CapabilitiesRole) == static_cast<int>(AgentInstanceModel::CapabilitiesRole),
20 "AgentTypeModel::CapabilitiesRole does not match AgentInstanceModel::CapabilitiesRole");
21static_assert(static_cast<int>(AgentTypeModel::MimeTypesRole) == static_cast<int>(AgentInstanceModel::MimeTypesRole),
22 "AgentTypeModel::MimeTypesRole does not match AgentInstanceModel::MimeTypesRole");
23
24/**
25 * @internal
26 */
27class Akonadi::AgentFilterProxyModelPrivate
28{
29public:
30 QStringList mimeTypes;
31 QStringList capabilities;
32 QStringList excludeCapabilities;
33 [[nodiscard]] bool filterAcceptRegExp(const QModelIndex &index, const QRegularExpression &filterRegExpStr);
34};
35
37 : QSortFilterProxyModel(parent)
38 , d(new AgentFilterProxyModelPrivate)
39{
40}
41
43
45{
46 d->mimeTypes << mimeType;
48}
49
51{
52 d->capabilities << capability;
54}
55
57{
58 d->excludeCapabilities << capability;
60}
61
63{
64 d->capabilities.clear();
65 d->mimeTypes.clear();
66 d->excludeCapabilities.clear();
68}
69
70bool AgentFilterProxyModelPrivate::filterAcceptRegExp(const QModelIndex &index, const QRegularExpression &filterRegExpStr)
71{
72 // First see if the name matches a set regexp filter.
73 if (!filterRegExpStr.pattern().isEmpty()) {
74 return index.data(AgentTypeModel::IdentifierRole).toString().contains(filterRegExpStr) || index.data().toString().contains(filterRegExpStr);
75 }
76 return true;
77}
78
79bool AgentFilterProxyModel::filterAcceptsRow(int row, const QModelIndex & /*source_parent*/) const
80{
81 const QModelIndex index = sourceModel()->index(row, 0);
82
83 if (!d->mimeTypes.isEmpty()) {
84 QMimeDatabase mimeDb;
85 bool found = false;
87 for (const QString &mimeType : lst) {
88 if (d->mimeTypes.contains(mimeType)) {
89 found = true;
90 } else {
91 const QMimeType mt = mimeDb.mimeTypeForName(mimeType);
92 if (mt.isValid()) {
93 for (const QString &type : std::as_const(d->mimeTypes)) {
94 if (mt.inherits(type)) {
95 found = true;
96 break;
97 }
98 }
99 }
100 }
101
102 if (found) {
103 break;
104 }
105 }
106
107 if (!found) {
108 return false;
109 }
110 }
111
112 if (!d->capabilities.isEmpty()) {
113 bool found = false;
115 for (const QString &capability : lst) {
116 if (d->capabilities.contains(capability)) {
117 found = true;
118 break;
119 }
120 }
121
122 if (!found) {
123 return false;
124 }
125
126 if (!d->excludeCapabilities.isEmpty()) {
128 for (const QString &capability : lstCapabilities) {
129 if (d->excludeCapabilities.contains(capability)) {
130 found = false;
131 break;
132 }
133 }
134
135 if (!found) {
136 return false;
137 }
138 }
139 }
140 const bool result = d->filterAcceptRegExp(index, filterRegularExpression());
141 return result;
142}
143
144#include "moc_agentfilterproxymodel.cpp"
AgentFilterProxyModel(QObject *parent=nullptr)
Create a new agent filter proxy model.
void addMimeTypeFilter(const QString &mimeType)
Accept agents supporting mimeType.
~AgentFilterProxyModel() override
Destroys the agent filter proxy model.
void addCapabilityFilter(const QString &capability)
Accept agents with the given capability.
void excludeCapabilities(const QString &capability)
Excludes agents with the given capability.
void clearFilters()
Clear the filters ( mimeTypes & capabilities ).
@ MimeTypesRole
A list of supported mimetypes.
@ CapabilitiesRole
A list of supported capabilities.
@ MimeTypesRole
A list of supported mimetypes.
@ IdentifierRole
The icon name of the agent.
@ CapabilitiesRole
A list of supported capabilities.
Helper integration between Akonadi and Qt.
QMimeType mimeTypeForName(const QString &nameOrAlias) const const
bool inherits(const QString &mimeTypeName) const const
bool isValid() const const
QVariant data(int role) const const
QString pattern() const const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
virtual QStringList mimeTypes() const const override
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QString toString() const const
QStringList toStringList() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:49:11 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.