Akonadi

collectionfilterproxymodel.cpp
1/*
2 SPDX-FileCopyrightText: 2007 Bruno Virlet <bruno.virlet@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "collectionfilterproxymodel.h"
8#include "akonadicore_debug.h"
9#include "entitytreemodel.h"
10#include "mimetypechecker.h"
11
12#include <QString>
13#include <QStringList>
14
15using namespace Akonadi;
16
17/**
18 * @internal
19 */
20class Akonadi::CollectionFilterProxyModelPrivate
21{
22public:
23 explicit CollectionFilterProxyModelPrivate(CollectionFilterProxyModel *parent)
24 : mParent(parent)
25 {
26 mimeChecker.addWantedMimeType(QStringLiteral("text/uri-list"));
27 }
28
29 bool collectionAccepted(const QModelIndex &index, bool checkResourceVisibility = true);
30
31 QList<QModelIndex> acceptedResources;
32 CollectionFilterProxyModel *const mParent;
33 MimeTypeChecker mimeChecker;
34 bool mExcludeVirtualCollections = false;
35};
36
37bool CollectionFilterProxyModelPrivate::collectionAccepted(const QModelIndex &index, bool checkResourceVisibility)
38{
39 // Retrieve supported mimetypes
40 const auto collection = mParent->sourceModel()->data(index, EntityTreeModel::CollectionRole).value<Collection>();
41
42 if (!collection.isValid()) {
43 return false;
44 }
45
46 if (collection.isVirtual() && mExcludeVirtualCollections) {
47 return false;
48 }
49
50 // If this collection directly contains one valid mimetype, it is accepted
51 if (mimeChecker.isWantedCollection(collection)) {
52 // The folder will be accepted, but we need to make sure the resource is visible too.
53 if (checkResourceVisibility) {
54 // find the resource
55 QModelIndex resource = index;
56 while (resource.parent().isValid()) {
57 resource = resource.parent();
58 }
59
60 // See if that resource is visible, if not, invalidate the filter.
61 if (resource != index && !acceptedResources.contains(resource)) {
62 qCDebug(AKONADICORE_LOG) << "We got a new collection:" << mParent->sourceModel()->data(index).toString()
63 << "but the resource is not visible:" << mParent->sourceModel()->data(resource).toString();
64 acceptedResources.clear();
65 // defer reset, the model might still be supplying new items at this point which crashes
66 mParent->invalidateFilter();
67 return true;
68 }
69 }
70
71 // Keep track of all the resources that are visible.
72 if (!index.parent().isValid()) {
73 acceptedResources.append(index);
74 }
75
76 return true;
77 }
78
79 // If this collection has a child which contains valid mimetypes, it is accepted
80 QModelIndex childIndex = mParent->sourceModel()->index(0, 0, index);
81 while (childIndex.isValid()) {
82 if (collectionAccepted(childIndex, false /* don't check visibility of the parent, as we are checking the child now */)) {
83 // Keep track of all the resources that are visible.
84 if (!index.parent().isValid()) {
85 acceptedResources.append(index);
86 }
87
88 return true;
89 }
90 childIndex = childIndex.sibling(childIndex.row() + 1, 0);
91 }
92
93 // Or else, no reason to keep this collection.
94 return false;
95}
96
98 : QSortFilterProxyModel(parent)
99 , d(new CollectionFilterProxyModelPrivate(this))
100{
101}
102
104
106{
107 const QStringList mimeTypes = d->mimeChecker.wantedMimeTypes() + typeList;
108 d->mimeChecker.setWantedMimeTypes(mimeTypes);
110}
111
113{
114 d->mimeChecker.addWantedMimeType(type);
116}
117
118bool CollectionFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
119{
120 return d->collectionAccepted(sourceModel()->index(sourceRow, 0, sourceParent));
121}
122
124{
125 return d->mimeChecker.wantedMimeTypes();
126}
127
129{
130 d->mimeChecker = MimeTypeChecker();
132}
133
135{
136 if (exclude != d->mExcludeVirtualCollections) {
137 d->mExcludeVirtualCollections = exclude;
139 }
140}
141
142bool CollectionFilterProxyModel::excludeVirtualCollections() const
143{
144 return d->mExcludeVirtualCollections;
145}
146
147Qt::ItemFlags CollectionFilterProxyModel::flags(const QModelIndex &index) const
148{
149 if (!index.isValid()) {
150 // Don't crash
151 return Qt::NoItemFlags;
152 }
153
154 const auto collection = sourceModel()->data(mapToSource(index), EntityTreeModel::CollectionRole).value<Collection>();
155
156 // If this collection directly contains one valid mimetype, it is accepted
157 if (d->mimeChecker.isWantedCollection(collection)) {
159 } else {
161 }
162}
163
164#include "moc_collectionfilterproxymodel.cpp"
A proxy model that filters collections by mime type.
void clearFilters()
Clears all mime type filters.
~CollectionFilterProxyModel() override
Destroys the collection proxy filter model.
QStringList mimeTypeFilters() const
Returns the list of mime type filters.
CollectionFilterProxyModel(QObject *parent=nullptr)
Creates a new collection proxy filter model.
void addMimeTypeFilters(const QStringList &mimeTypes)
Adds a list of mime types to be shown by the filter.
void addMimeTypeFilter(const QString &mimeType)
Adds a mime type to be shown by the filter.
void setExcludeVirtualCollections(bool exclude)
Sets whether we want virtual collections to be filtered or not.
Represents a collection of PIM items.
Definition collection.h:62
@ CollectionRole
The collection.
Helper for checking MIME types of Collections and Items.
void addWantedMimeType(const QString &mimeType)
Adds another MIME type to the list of wanted MIME types this instance checks against.
bool isWantedCollection(const Collection &collection) const
Checks whether a given collection has one of the wanted MIME types.
Helper integration between Akonadi and Qt.
void append(QList< T > &&value)
void clear()
bool contains(const AT &value) const const
bool isValid() const const
QModelIndex parent() const const
int row() const const
QModelIndex sibling(int row, int column) const const
virtual Qt::ItemFlags flags(const QModelIndex &index) const const override
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const const override
virtual QStringList mimeTypes() const const override
typedef ItemFlags
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.