1 | /* |
2 | Copyright (c) 2007 Volker Krause <vkrause@kde.org> |
3 | |
4 | This library is free software; you can redistribute it and/or modify it |
5 | under the terms of the GNU Library General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or (at your |
7 | option) any later version. |
8 | |
9 | This library is distributed in the hope that it will be useful, but WITHOUT |
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public |
12 | License for more details. |
13 | |
14 | You should have received a copy of the GNU Library General Public License |
15 | along with this library; see the file COPYING.LIB. If not, write to the |
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
17 | 02110-1301, USA. |
18 | */ |
19 | |
20 | #include "agentfilterproxymodel.h" |
21 | |
22 | #include "agenttypemodel.h" |
23 | #include "agentinstancemodel.h" |
24 | |
25 | #include <kdebug.h> |
26 | #include <kmimetype.h> |
27 | |
28 | #include <QtCore/QStringList> |
29 | |
30 | #include <boost/static_assert.hpp> |
31 | |
32 | using namespace Akonadi; |
33 | |
34 | // ensure the role numbers are equivalent for both source models |
35 | BOOST_STATIC_ASSERT((int)AgentTypeModel::CapabilitiesRole == (int)AgentInstanceModel::CapabilitiesRole); |
36 | BOOST_STATIC_ASSERT((int)AgentTypeModel::MimeTypesRole == (int)AgentInstanceModel::MimeTypesRole); |
37 | |
38 | /** |
39 | * @internal |
40 | */ |
41 | class AgentFilterProxyModel::Private |
42 | { |
43 | public: |
44 | QStringList mimeTypes; |
45 | QStringList capabilities; |
46 | QStringList excludeCapabilities; |
47 | }; |
48 | |
49 | AgentFilterProxyModel::AgentFilterProxyModel(QObject *parent) |
50 | : QSortFilterProxyModel(parent) |
51 | , d(new Private) |
52 | { |
53 | setDynamicSortFilter(true); |
54 | } |
55 | |
56 | AgentFilterProxyModel::~AgentFilterProxyModel() |
57 | { |
58 | delete d; |
59 | } |
60 | |
61 | void AgentFilterProxyModel::addMimeTypeFilter(const QString &mimeType) |
62 | { |
63 | d->mimeTypes << mimeType; |
64 | invalidateFilter(); |
65 | } |
66 | |
67 | void AgentFilterProxyModel::addCapabilityFilter(const QString &capability) |
68 | { |
69 | d->capabilities << capability; |
70 | invalidateFilter(); |
71 | } |
72 | |
73 | void AgentFilterProxyModel::excludeCapabilities(const QString &capability) |
74 | { |
75 | d->excludeCapabilities << capability; |
76 | invalidateFilter(); |
77 | } |
78 | |
79 | void AgentFilterProxyModel::clearFilters() |
80 | { |
81 | d->capabilities.clear(); |
82 | d->mimeTypes.clear(); |
83 | d->excludeCapabilities.clear(); |
84 | invalidateFilter(); |
85 | } |
86 | |
87 | bool AgentFilterProxyModel::filterAcceptsRow(int row, const QModelIndex &) const |
88 | { |
89 | const QModelIndex index = sourceModel()->index(row, 0); |
90 | |
91 | // First see if the name matches a set regexp filter. |
92 | if (!filterRegExp().isEmpty()) { |
93 | if (index.data(AgentTypeModel::IdentifierRole).toString().contains(filterRegExp())) { |
94 | return true; |
95 | } else if (index.data().toString().contains(filterRegExp())) { |
96 | return true; |
97 | } else { |
98 | return false; |
99 | } |
100 | } |
101 | |
102 | if (!d->mimeTypes.isEmpty()) { |
103 | bool found = false; |
104 | foreach (const QString &mimeType, index.data(AgentTypeModel::MimeTypesRole).toStringList()) { |
105 | if (d->mimeTypes.contains(mimeType)) { |
106 | found = true; |
107 | } else { |
108 | KMimeType::Ptr mimeTypePtr = KMimeType::mimeType(mimeType, KMimeType::ResolveAliases); |
109 | if (!mimeTypePtr.isNull()) { |
110 | foreach (const QString &type, d->mimeTypes) { |
111 | if (mimeTypePtr->is(type)) { |
112 | found = true; |
113 | break; |
114 | } |
115 | } |
116 | } |
117 | } |
118 | |
119 | if (found) { |
120 | break; |
121 | } |
122 | } |
123 | |
124 | if (!found) { |
125 | return false; |
126 | } |
127 | } |
128 | |
129 | if (!d->capabilities.isEmpty()) { |
130 | bool found = false; |
131 | foreach (const QString &capability, index.data(AgentTypeModel::CapabilitiesRole).toStringList()) { |
132 | if (d->capabilities.contains(capability)) { |
133 | found = true; |
134 | break; |
135 | } |
136 | } |
137 | |
138 | if (!found) { |
139 | return false; |
140 | } |
141 | |
142 | if (found && !d->excludeCapabilities.isEmpty()) { |
143 | foreach (const QString &capability, index.data(AgentTypeModel::CapabilitiesRole).toStringList()) { |
144 | if (d->excludeCapabilities.contains(capability)) { |
145 | found = false; |
146 | break; |
147 | } |
148 | } |
149 | |
150 | if (!found) { |
151 | return false; |
152 | } |
153 | } |
154 | } |
155 | |
156 | return true; |
157 | } |
158 | |