Akonadi

collectionfilterproxymodel.cpp
1 /*
2  SPDX-FileCopyrightText: 2007 Bruno Virlet <[email protected]>
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 
15 using namespace Akonadi;
16 
17 /**
18  * @internal
19  */
20 class Akonadi::CollectionFilterProxyModelPrivate
21 {
22 public:
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  QVector<QModelIndex> acceptedResources;
32  CollectionFilterProxyModel *const mParent;
33  MimeTypeChecker mimeChecker;
34  bool mExcludeVirtualCollections = false;
35 };
36 
37 bool 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 
118 bool 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 
142 bool CollectionFilterProxyModel::excludeVirtualCollections() const
143 {
144  return d->mExcludeVirtualCollections;
145 }
146 
147 Qt::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 }
QModelIndex sibling(int row, int column) const const
Represents a collection of PIM items.
Definition: collection.h:61
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const const override
QStringList mimeTypeFilters() const
Returns the list of mime type filters.
A proxy model that filters collections by mime type.
QVariant data(int role) const const
typedef ItemFlags
virtual QStringList mimeTypes() const const override
virtual Qt::ItemFlags flags(const QModelIndex &index) const const override
@ CollectionRole
The collection.
void setExcludeVirtualCollections(bool exclude)
Sets whether we want virtual collections to be filtered or not.
bool isValid() const const
int row() const const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
void addMimeTypeFilter(const QString &mimeType)
Adds a mime type to be shown by the filter.
void addMimeTypeFilters(const QStringList &mimeTypes)
Adds a list of mime types to be shown by the filter.
Helper for checking MIME types of Collections and Items.
void clearFilters()
Clears all mime type filters.
QModelIndex parent() const const
QChar * data()
~CollectionFilterProxyModel() override
Destroys the collection proxy filter model.
CollectionFilterProxyModel(QObject *parent=nullptr)
Creates a new collection proxy filter model.
QString toString() const const
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Mon Jun 27 2022 04:01:05 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.