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 #include "mimetypechecker.h"
11 
12 #include <QString>
13 #include <QStringList>
14 
15 using namespace Akonadi;
16 
17 namespace Akonadi
18 {
19 /**
20  * @internal
21  */
22 class EntityMimeTypeFilterModelPrivate
23 {
24 public:
25  explicit EntityMimeTypeFilterModelPrivate(EntityMimeTypeFilterModel *parent)
26  : q_ptr(parent)
27  , m_headerGroup(EntityTreeModel::EntityTreeHeaders)
28  {
29  }
30 
31  Q_DECLARE_PUBLIC(EntityMimeTypeFilterModel)
33 
34  QStringList includedMimeTypes;
35  QStringList excludedMimeTypes;
36 
37  QPersistentModelIndex m_rootIndex;
38 
39  EntityTreeModel::HeaderGroup m_headerGroup;
40 };
41 
42 } // namespace Akonadi
43 
45  : QSortFilterProxyModel(parent)
46  , d_ptr(new EntityMimeTypeFilterModelPrivate(this))
47 {
48 }
49 
51 
53 {
55  d->includedMimeTypes << typeList;
57 }
58 
60 {
62  d->excludedMimeTypes << typeList;
64 }
65 
67 {
69  d->includedMimeTypes << type;
71 }
72 
74 {
76  d->excludedMimeTypes << type;
78 }
79 
80 bool EntityMimeTypeFilterModel::filterAcceptsColumn(int sourceColumn, const QModelIndex &sourceParent) const
81 {
82  if (sourceColumn >= columnCount(mapFromSource(sourceParent))) {
83  return false;
84  }
85  return QSortFilterProxyModel::filterAcceptsColumn(sourceColumn, sourceParent);
86 }
87 
88 bool EntityMimeTypeFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
89 {
91  const QModelIndex idx = sourceModel()->index(sourceRow, 0, sourceParent);
92 
93  const QString rowMimetype = idx.data(EntityTreeModel::MimeTypeRole).toString();
94 
95  if (d->excludedMimeTypes.contains(rowMimetype)) {
96  return false;
97  }
98 
99  if (d->includedMimeTypes.isEmpty() || d->includedMimeTypes.contains(rowMimetype)) {
100  const auto item = idx.data(EntityTreeModel::ItemRole).value<Akonadi::Item>();
101 
102  if (item.isValid() && !item.hasPayload()) {
103  qCDebug(AKONADICORE_LOG) << "Item " << item.id() << " doesn't have payload";
104  return false;
105  }
106 
107  return true;
108  }
109 
110  return false;
111 }
112 
114 {
116  return d->includedMimeTypes;
117 }
118 
120 {
122  return d->excludedMimeTypes;
123 }
124 
126 {
128  d->includedMimeTypes.clear();
129  d->excludedMimeTypes.clear();
131 }
132 
134 {
136  d->m_headerGroup = headerGroup;
137 }
138 
139 QVariant EntityMimeTypeFilterModel::headerData(int section, Qt::Orientation orientation, int role) const
140 {
141  if (!sourceModel()) {
142  return QVariant();
143  }
144 
146  role += (EntityTreeModel::TerminalUserRole * d->m_headerGroup);
147  return sourceModel()->headerData(section, orientation, role);
148 }
149 
150 QModelIndexList EntityMimeTypeFilterModel::match(const QModelIndex &start, int role, const QVariant &value, int hits, Qt::MatchFlags flags) const
151 {
152  if (!sourceModel()) {
153  return QModelIndexList();
154  }
155 
156  if (role < Qt::UserRole) {
157  return QSortFilterProxyModel::match(start, role, value, hits, flags);
158  }
159 
160  QModelIndexList list;
161  QModelIndex proxyIndex;
162  const auto matches = sourceModel()->match(mapToSource(start), role, value, hits, flags);
163  for (const auto &idx : matches) {
164  proxyIndex = mapFromSource(idx);
165  if (proxyIndex.isValid()) {
166  list.push_back(proxyIndex);
167  }
168  }
169 
170  return list;
171 }
172 
173 int EntityMimeTypeFilterModel::columnCount(const QModelIndex &parent) const
174 {
176 
177  if (!sourceModel()) {
178  return 0;
179  }
180 
182  if (!value.isValid()) {
183  return 0;
184  }
185 
186  return value.toInt();
187 }
188 
189 bool EntityMimeTypeFilterModel::hasChildren(const QModelIndex &parent) const
190 {
191  if (!sourceModel()) {
192  return false;
193  }
194 
195  // QSortFilterProxyModel implementation is buggy in that it emits rowsAboutToBeInserted etc
196  // only after the source model has emitted rowsInserted, instead of emitting it when the
197  // source model emits rowsAboutToBeInserted. That means that the source and the proxy are out
198  // of sync around the time of insertions, so we can't use the optimization below.
199  return rowCount(parent) > 0;
200 #if 0
201 
202  if (!parent.isValid()) {
203  return sourceModel()->hasChildren(parent);
204  }
205 
207  if (EntityTreeModel::ItemListHeaders == d->m_headerGroup) {
208  return false;
209  }
210 
211  if (EntityTreeModel::CollectionTreeHeaders == d->m_headerGroup) {
212  QModelIndex childIndex = parent.child(0, 0);
213  while (childIndex.isValid()) {
215  if (col.isValid()) {
216  return true;
217  }
218  childIndex = childIndex.sibling(childIndex.row() + 1, childIndex.column());
219  }
220  }
221  return false;
222 #endif
223 }
224 
225 bool EntityMimeTypeFilterModel::canFetchMore(const QModelIndex &parent) const
226 {
228  if (EntityTreeModel::CollectionTreeHeaders == d->m_headerGroup) {
229  return false;
230  }
232 }
@ 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:104
Q_D(Todo)
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:06 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.