Akonadi

agentinstancefilterproxymodel.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 "agentinstancefilterproxymodel.h"
8
9#include "accountactivitiesabstract.h"
10#include "agentinstancemodel.h"
11
12#include <QMimeDatabase>
13#include <QMimeType>
14#include <QStringList>
15
16using namespace Akonadi;
17/**
18 * @internal
19 */
20class Akonadi::AgentInstanceFilterProxyModelPrivate
21{
22public:
23 QStringList mimeTypes;
24 QStringList capabilities;
25 QStringList excludeCapabilities;
26 bool enablePlasmaActivities = false;
27 AccountActivitiesAbstract *accountActivitiesAbstract = nullptr;
28 [[nodiscard]] bool filterAcceptRegExp(const QModelIndex &index, const QRegularExpression &filterRegExpStr);
29};
30
32 : QSortFilterProxyModel(parent)
33 , d(new AgentInstanceFilterProxyModelPrivate)
34{
35}
36
38
40{
41 d->mimeTypes << mimeType;
43}
44
46{
47 d->capabilities << capability;
49}
50
52{
53 d->excludeCapabilities << capability;
55}
56
57bool AgentInstanceFilterProxyModel::enablePlasmaActivities() const
58{
59 return d->enablePlasmaActivities;
60}
61
62void AgentInstanceFilterProxyModel::setEnablePlasmaActivities(bool newEnablePlasmaActivities)
63{
64 d->enablePlasmaActivities = newEnablePlasmaActivities;
66}
67
69{
70 d->capabilities.clear();
71 d->mimeTypes.clear();
72 d->excludeCapabilities.clear();
74}
75
76bool AgentInstanceFilterProxyModelPrivate::filterAcceptRegExp(const QModelIndex &index, const QRegularExpression &filterRegExpStr)
77{
78 // First see if the name matches a set regexp filter.
79 if (!filterRegExpStr.pattern().isEmpty()) {
80 return index.data(AgentInstanceModel::TypeIdentifierRole).toString().contains(filterRegExpStr) || index.data().toString().contains(filterRegExpStr);
81 }
82 return true;
83}
84
85bool AgentInstanceFilterProxyModel::filterAcceptsRow(int row, const QModelIndex & /*source_parent*/) const
86{
87 const QModelIndex index = sourceModel()->index(row, 0);
88
89 if (!d->mimeTypes.isEmpty()) {
90 QMimeDatabase mimeDb;
91 bool found = false;
93 for (const QString &mimeType : lst) {
94 if (d->mimeTypes.contains(mimeType)) {
95 found = true;
96 } else {
97 const QMimeType mt = mimeDb.mimeTypeForName(mimeType);
98 if (mt.isValid()) {
99 for (const QString &type : std::as_const(d->mimeTypes)) {
100 if (mt.inherits(type)) {
101 found = true;
102 break;
103 }
104 }
105 }
106 }
107
108 if (found) {
109 break;
110 }
111 }
112
113 if (!found) {
114 return false;
115 }
116 }
117
118 if (!d->capabilities.isEmpty()) {
119 bool found = false;
121 for (const QString &capability : lst) {
122 if (d->capabilities.contains(capability)) {
123 found = true;
124 break;
125 }
126 }
127
128 if (!found) {
129 return false;
130 }
131
132 if (!d->excludeCapabilities.isEmpty()) {
134 for (const QString &capability : lstCapabilities) {
135 if (d->excludeCapabilities.contains(capability)) {
136 found = false;
137 break;
138 }
139 }
140
141 if (!found) {
142 return false;
143 }
144 }
145 }
146 const bool result = d->filterAcceptRegExp(index, filterRegularExpression());
147 if (result) {
148 if (d->accountActivitiesAbstract && d->enablePlasmaActivities) {
149 const bool enableActivities = index.data(AgentInstanceModel::ActivitiesEnabledRole).toBool();
150 if (enableActivities) {
152 const bool result = d->accountActivitiesAbstract->filterAcceptsRow(activities);
153 // qDebug() << " result " << result << " identity name : " << sourceModel()->index(source_row,
154 // IdentityTreeModel::IdentityNameRole).data().toString();
155 return result;
156 }
157 }
158 }
159 return result;
160}
161
162AccountActivitiesAbstract *AgentInstanceFilterProxyModel::accountActivitiesAbstract() const
163{
164 return d->accountActivitiesAbstract;
165}
166
167void AgentInstanceFilterProxyModel::setAccountActivitiesAbstract(AccountActivitiesAbstract *abstract)
168{
169 if (d->accountActivitiesAbstract != abstract) {
170 d->accountActivitiesAbstract = abstract;
171 connect(d->accountActivitiesAbstract, &AccountActivitiesAbstract::activitiesChanged, this, &AgentInstanceFilterProxyModel::invalidateFilter);
173 }
174}
175
176#include "moc_agentinstancefilterproxymodel.cpp"
void excludeCapabilities(const QString &capability)
Excludes agents with the given capability.
void addMimeTypeFilter(const QString &mimeType)
Accept agents supporting mimeType.
void addCapabilityFilter(const QString &capability)
Accept agents with the given capability.
~AgentInstanceFilterProxyModel() override
Destroys the agent filter proxy model.
void clearFilters()
Clear the filters ( mimeTypes & capabilities ).
AgentInstanceFilterProxyModel(QObject *parent=nullptr)
Create a new agent filter proxy model.
@ ActivitiesRole
A list of activities.
@ MimeTypesRole
A list of supported mimetypes.
@ TypeIdentifierRole
The icon name of the agent.
@ CapabilitiesRole
A list of supported capabilities.
@ ActivitiesEnabledRole
Return true if activities is enabled on agent.
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
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
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
bool toBool() 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 Dec 13 2024 11:54:58 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.