Akonadi

entitymimetypefiltermodel.cpp
1 /*
2  SPDX-FileCopyrightText: 2007 Bruno Virlet <[email protected]>
3  SPDX-FileCopyrightText: 2009 Stephen Kelly <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "entitymimetypefiltermodel.h"
9 #include "akonadicore_debug.h"
10 
11 #include <QString>
12 #include <QStringList>
13 
14 using namespace Akonadi;
15 
16 namespace Akonadi
17 {
18 /**
19  * @internal
20  */
21 class EntityMimeTypeFilterModelPrivate
22 {
23 public:
24  explicit EntityMimeTypeFilterModelPrivate(EntityMimeTypeFilterModel *parent)
25  : q_ptr(parent)
26  , m_headerGroup(EntityTreeModel::EntityTreeHeaders)
27  {
28  }
29 
30  Q_DECLARE_PUBLIC(EntityMimeTypeFilterModel)
32 
33  QStringList includedMimeTypes;
34  QStringList excludedMimeTypes;
35 
36  QPersistentModelIndex m_rootIndex;
37 
38  EntityTreeModel::HeaderGroup m_headerGroup;
39 };
40 
41 } // namespace Akonadi
42 
44  : QSortFilterProxyModel(parent)
45  , d_ptr(new EntityMimeTypeFilterModelPrivate(this))
46 {
47 }
48 
50 
52 {
54  d->includedMimeTypes << typeList;
56 }
57 
59 {
61  d->excludedMimeTypes << typeList;
63 }
64 
66 {
68  d->includedMimeTypes << type;
70 }
71 
73 {
75  d->excludedMimeTypes << type;
77 }
78 
79 bool EntityMimeTypeFilterModel::filterAcceptsColumn(int sourceColumn, const QModelIndex &sourceParent) const
80 {
81  if (sourceColumn >= columnCount(mapFromSource(sourceParent))) {
82  return false;
83  }
84  return QSortFilterProxyModel::filterAcceptsColumn(sourceColumn, sourceParent);
85 }
86 
87 bool EntityMimeTypeFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
88 {
90  const QModelIndex idx = sourceModel()->index(sourceRow, 0, sourceParent);
91 
92  const QString rowMimetype = idx.data(EntityTreeModel::MimeTypeRole).toString();
93 
94  if (d->excludedMimeTypes.contains(rowMimetype)) {
95  return false;
96  }
97 
98  if (d->includedMimeTypes.isEmpty() || d->includedMimeTypes.contains(rowMimetype)) {
99  const auto item = idx.data(EntityTreeModel::ItemRole).value<Akonadi::Item>();
100 
101  if (item.isValid() && !item.hasPayload()) {
102  qCDebug(AKONADICORE_LOG) << "Item " << item.id() << " doesn't have payload";
103  return false;
104  }
105 
106  return true;
107  }
108 
109  return false;
110 }
111 
113 {
115  return d->includedMimeTypes;
116 }
117 
119 {
121  return d->excludedMimeTypes;
122 }
123 
125 {
127  d->includedMimeTypes.clear();
128  d->excludedMimeTypes.clear();
130 }
131 
133 {
135  d->m_headerGroup = headerGroup;
136 }
137 
138 QVariant EntityMimeTypeFilterModel::headerData(int section, Qt::Orientation orientation, int role) const
139 {
140  if (!sourceModel()) {
141  return QVariant();
142  }
143 
145  role += (EntityTreeModel::TerminalUserRole * d->m_headerGroup);
146  return sourceModel()->headerData(section, orientation, role);
147 }
148 
149 QModelIndexList EntityMimeTypeFilterModel::match(const QModelIndex &start, int role, const QVariant &value, int hits, Qt::MatchFlags flags) const
150 {
151  if (!sourceModel()) {
152  return QModelIndexList();
153  }
154 
155  if (role < Qt::UserRole) {
156  return QSortFilterProxyModel::match(start, role, value, hits, flags);
157  }
158 
159  QModelIndexList list;
160  QModelIndex proxyIndex;
161  const auto matches = sourceModel()->match(mapToSource(start), role, value, hits, flags);
162  for (const auto &idx : matches) {
163  proxyIndex = mapFromSource(idx);
164  if (proxyIndex.isValid()) {
165  list.push_back(proxyIndex);
166  }
167  }
168 
169  return list;
170 }
171 
172 int EntityMimeTypeFilterModel::columnCount(const QModelIndex &parent) const
173 {
175 
176  if (!sourceModel()) {
177  return 0;
178  }
179 
181  if (!value.isValid()) {
182  return 0;
183  }
184 
185  return value.toInt();
186 }
187 
188 bool EntityMimeTypeFilterModel::hasChildren(const QModelIndex &parent) const
189 {
190  if (!sourceModel()) {
191  return false;
192  }
193 
194  // QSortFilterProxyModel implementation is buggy in that it emits rowsAboutToBeInserted etc
195  // only after the source model has emitted rowsInserted, instead of emitting it when the
196  // source model emits rowsAboutToBeInserted. That means that the source and the proxy are out
197  // of sync around the time of insertions, so we can't use the optimization below.
198  return rowCount(parent) > 0;
199 #if 0
200 
201  if (!parent.isValid()) {
202  return sourceModel()->hasChildren(parent);
203  }
204 
206  if (EntityTreeModel::ItemListHeaders == d->m_headerGroup) {
207  return false;
208  }
209 
210  if (EntityTreeModel::CollectionTreeHeaders == d->m_headerGroup) {
211  QModelIndex childIndex = parent.child(0, 0);
212  while (childIndex.isValid()) {
214  if (col.isValid()) {
215  return true;
216  }
217  childIndex = childIndex.sibling(childIndex.row() + 1, childIndex.column());
218  }
219  }
220  return false;
221 #endif
222 }
223 
224 bool EntityMimeTypeFilterModel::canFetchMore(const QModelIndex &parent) const
225 {
227  if (EntityTreeModel::CollectionTreeHeaders == d->m_headerGroup) {
228  return false;
229  }
231 }
@ ItemListHeaders
Header information for a list of items.
A model for collections and items together.
bool isValid() const const
UserRole
QModelIndex sibling(int row, int column) const const
int column() const const
T value() const const
Q_SCRIPTABLE Q_NOREPLY void start()
Represents a collection of PIM items.
Definition: collection.h:61
~EntityMimeTypeFilterModel() override
Destroys the entity mime type filter model.
void push_back(const T &value)
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
virtual QModelIndex mapToSource(const QModelIndex &proxyIndex) const const override
void addMimeTypeExclusionFilter(const QString &mimeType)
Add mime type to be excluded by the filter.
A proxy model that filters entities by mime type.
QStringList mimeTypeExclusionFilters() const
Returns the list of mime type exclusion filters.
QVariant data(int role) const const
@ CollectionTreeHeaders
Header information for a collection-only tree.
@ MimeTypeRole
The mimetype of the entity.
void addMimeTypeExclusionFilters(const QStringList &mimeTypes)
Add mimetypes to filter out.
virtual Qt::ItemFlags flags(const QModelIndex &index) const const override
@ CollectionRole
The collection.
Orientation
int toInt(bool *ok) const const
virtual int rowCount(const QModelIndex &parent) const const override
virtual QModelIndexList match(const QModelIndex &start, int role, const QVariant &value, int hits, Qt::MatchFlags flags) const const override
bool isValid() const const
int row() const const
void addMimeTypeInclusionFilter(const QString &mimeType)
Add mime type to be shown by the filter.
Id id() const
Returns the unique identifier of the item.
Definition: item.cpp:63
QStringList mimeTypeInclusionFilters() const
Returns the list of mime type inclusion filters.
virtual QModelIndex mapFromSource(const QModelIndex &sourceIndex) const const override
void setHeaderGroup(EntityTreeModel::HeaderGroup headerGroup)
Sets the header set of the filter model.
void clearFilters()
Clear all mime type filters.
@ TerminalUserRole
Last role for user extensions. Don't use a role beyond this or headerData will break.
typedef MatchFlags
HeaderGroup
Describes what header information the model shall return.
virtual bool filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const const
EntityMimeTypeFilterModel(QObject *parent=nullptr)
Creates a new entity mime type filter model.
virtual bool canFetchMore(const QModelIndex &parent) const const override
QObject * parent() const const
void addMimeTypeInclusionFilters(const QStringList &mimeTypes)
Add mime types to be shown by the filter.
Represents a PIM item stored in Akonadi storage.
Definition: item.h:105
Q_D(Todo)
QString toString() const const
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Feb 7 2023 03:58:06 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.