Clazy fix++
[kdepim.git] / kaddressbook / categoryfilterproxymodel.cpp
blobfe801df4678067e399f1cd22af0d6a1fb4a8f551
1 /*
2 Copyright (c) 2014 Jonathan Marten <jjm@keelhaul.me.uk>
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.
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.
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.
20 #include "categoryfilterproxymodel.h"
22 #include "kaddressbook_debug.h"
23 #include <KLocalizedString>
25 #include <AkonadiCore/entitytreemodel.h>
26 #include <AkonadiCore/item.h>
28 #include <kcontacts/addressee.h>
29 #include <kcontacts/contactgroup.h>
31 #include "categoryselectwidget.h"
33 using namespace Akonadi;
35 class CategoryFilterProxyModelPrivate : public QObject
37 Q_OBJECT
38 Q_DECLARE_PUBLIC(CategoryFilterProxyModel)
40 public:
41 CategoryFilterProxyModelPrivate(CategoryFilterProxyModel *parent);
43 QList<Tag::Id> filterIdList;
44 bool filterEnabled;
46 private:
47 CategoryFilterProxyModel *q_ptr;
50 CategoryFilterProxyModelPrivate::CategoryFilterProxyModelPrivate(CategoryFilterProxyModel *parent)
51 : QObject(),
52 filterEnabled(false),
53 q_ptr(parent)
57 CategoryFilterProxyModel::CategoryFilterProxyModel(QObject *parent)
58 : QSortFilterProxyModel(parent),
59 d_ptr(new CategoryFilterProxyModelPrivate(this))
61 setDynamicSortFilter(true);
64 CategoryFilterProxyModel::~CategoryFilterProxyModel()
66 delete d_ptr;
69 bool CategoryFilterProxyModel::filterAcceptsRow(int row, const QModelIndex &parent) const
71 Q_D(const CategoryFilterProxyModel);
73 const QModelIndex index = sourceModel()->index(row, 0, parent);
74 const Akonadi::Item item = index.data(EntityTreeModel::ItemRole).value<Akonadi::Item>();
76 if (!d->filterEnabled) {
77 return true; // filter not enabled
79 if (d->filterIdList.isEmpty()) {
80 return false; // nothing accepted
82 // all accepted
83 if (d->filterIdList.at(0) == CategorySelectWidget::FilterAll) {
84 return true;
87 //qCDebug(KADDRESSBOOK_LOG) << "for row" << row << "item" << item.url() << "filter" << d->filterIdList;
88 if (item.hasPayload<KContacts::Addressee>()) {
89 const KContacts::Addressee contact = item.payload<KContacts::Addressee>();
91 const QStringList categories = contact.categories();
92 //qCDebug(KADDRESSBOOK_LOG) << "is contact" << contact.assembledName() << "cats" << categories;
94 int validCategories = 0;
95 int count = categories.count();
96 for (int i = 0; i < count; ++i) {
97 const QString cat = categories.at(i);
98 if (cat.startsWith(QStringLiteral("akonadi:"))) {
99 const int idx = cat.indexOf(QStringLiteral("?tag="));
100 if (idx >= 0) {
101 ++validCategories;
102 Tag::Id id = cat.midRef(idx + 5).toInt();
103 if (d->filterIdList.contains(id)) {
104 //qCDebug(KADDRESSBOOK_LOG) << "matches category" << cat;
105 return true; // a category matches filter
111 if (validCategories > 0) {
112 //qCDebug(KADDRESSBOOK_LOG) << "valid item but no match";
113 return false; // categorised but no match
114 } else {
115 //qCDebug(KADDRESSBOOK_LOG) << "item with no categories";
116 return d->filterIdList.contains(CategorySelectWidget::FilterUntagged);
118 } else if (item.hasPayload<KContacts::ContactGroup>()) { // a contact group item
119 return d->filterIdList.contains(CategorySelectWidget::FilterGroups);
122 return true; // not a recognised item
125 void CategoryFilterProxyModel::setFilterCategories(const QList<Akonadi::Tag::Id> &idList)
127 Q_D(CategoryFilterProxyModel);
129 if (idList != d->filterIdList) {
130 //qCDebug(KADDRESSBOOK_LOG) << idList;
131 d->filterIdList = idList;
132 invalidateFilter();
136 void CategoryFilterProxyModel::setFilterEnabled(bool enable)
138 Q_D(CategoryFilterProxyModel);
140 if (enable != d->filterEnabled) {
141 //qCDebug(KADDRESSBOOK_LOG) << enable;
142 d->filterEnabled = enable;
143 invalidateFilter();
147 #include "categoryfilterproxymodel.moc"