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 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
71bool 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
80bool AgentFilterProxyModel::filterAcceptsRow(int row, const QModelIndex & /*source_parent*/) const
81{
82 const QModelIndex index = sourceModel()->index(row, 0);
83
84 if (!d->mimeTypes.isEmpty()) {
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}
144
145#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.
A glue between Qt and the standard library.
QVariant data(int role) const const
T qobject_cast(QObject *object)
QString pattern() const const
void setDynamicSortFilter(bool enable)
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 Jun 7 2024 11:56:44 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.