MauiKit Calendar

collectioncomboboxmodel.cpp
1/*
2 This file is part of Akonadi Contact.
3
4 SPDX-FileCopyrightText: 2007-2009 Tobias Koenig <tokoe@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8
9#include "collectioncomboboxmodel.h"
10
11#include <Akonadi/CollectionFetchScope>
12#include <Akonadi/CollectionFilterProxyModel>
13#include <Akonadi/CollectionUtils>
14#include <Akonadi/EntityRightsFilterModel>
15#include <Akonadi/EntityTreeModel>
16#include <Akonadi/Monitor>
17#include <Akonadi/Session>
18
19#include <KDescendantsProxyModel>
20
21#include <QAbstractItemModel>
22#include "colorproxymodel.h"
23
24using namespace Akonadi::Quick;
25
26class Akonadi::Quick::CollectionComboBoxModelPrivate
27{
28public:
29 CollectionComboBoxModelPrivate(CollectionComboBoxModel *parent)
30 : mParent(parent)
31 {
32 mMonitor = new Akonadi::Monitor(mParent);
33 mMonitor->setObjectName(QStringLiteral("CollectionComboBoxMonitor"));
34 mMonitor->fetchCollection(true);
36
37 // This ETM will be set to only show collections with the wanted mimetype in setMimeTypeFilter
38 auto entityModel = new Akonadi::EntityTreeModel(mMonitor, mParent);
39 entityModel->setItemPopulationStrategy(Akonadi::EntityTreeModel::NoItemPopulation);
40 entityModel->setListFilter(Akonadi::CollectionFetchScope::Display);
41
42 // Display color
43 auto colorProxy = new ColorProxyModel(mParent);
44 colorProxy->setObjectName(QStringLiteral("Show collection colors"));
45 colorProxy->setDynamicSortFilter(true);
46 colorProxy->setSourceModel(entityModel);
47
48 // Flatten the tree, e.g.
49 // Kolab
50 // Kolab / Inbox
51 // Kolab / Inbox / Calendar
52 auto proxyModel = new KDescendantsProxyModel(parent);
53 proxyModel->setDisplayAncestorData(true);
54 proxyModel->setSourceModel(colorProxy);
55
56 // Filter it by mimetype again, to only keep
57 // Kolab / Inbox / Calendar
58 mMimeTypeFilterModel = new Akonadi::CollectionFilterProxyModel(parent);
59 mMimeTypeFilterModel->setSourceModel(proxyModel);
60
61 // Filter by access rights. TODO: maybe this functionality could be provided by CollectionFilterProxyModel, to save one proxy?
62 mRightsFilterModel = new Akonadi::EntityRightsFilterModel(parent);
63 mRightsFilterModel->setSourceModel(mMimeTypeFilterModel);
64
65 mParent->setSourceModel(mRightsFilterModel);
66 // mRightsFilterModel->sort(mParent->modelColumn());
67
68 mParent->connect(mRightsFilterModel, &QAbstractItemModel::rowsInserted, mParent, [this](const QModelIndex &parent, int start, int end) {
69 Q_UNUSED(parent)
70 Q_UNUSED(start)
71 Q_UNUSED(end)
72 scanSubTree();
73 });
74 }
75
76 ~CollectionComboBoxModelPrivate() = default;
77
78 bool scanSubTree();
79
80 CollectionComboBoxModel *const mParent;
81
82 Akonadi::Monitor *mMonitor = nullptr;
83 Akonadi::CollectionFilterProxyModel *mMimeTypeFilterModel = nullptr;
84 Akonadi::EntityRightsFilterModel *mRightsFilterModel = nullptr;
85 qint64 mDefaultCollectionId = -1;
86 int mCurrentIndex = -1;
87};
88
89bool CollectionComboBoxModelPrivate::scanSubTree()
90{
91 for (int row = 0; row < mRightsFilterModel->rowCount(); ++row) {
92 const Akonadi::Collection::Id id = mRightsFilterModel->data(mRightsFilterModel->index(row, 0), EntityTreeModel::CollectionIdRole).toLongLong();
93
94 if (mDefaultCollectionId == id && id > 0) {
95 mParent->setCurrentIndex(row);
96 return true;
97 }
98 }
99
100 return false;
101}
102
103CollectionComboBoxModel::CollectionComboBoxModel(QObject *parent)
104 : QSortFilterProxyModel(parent)
105 , d(new CollectionComboBoxModelPrivate(this))
106{
107}
108
109CollectionComboBoxModel::~CollectionComboBoxModel() = default;
110
112{
113 d->mMimeTypeFilterModel->clearFilters();
114 d->mMimeTypeFilterModel->addMimeTypeFilters(contentMimeTypes);
115
116 if (d->mMonitor) {
117 for (const QString &mimeType : contentMimeTypes) {
118 d->mMonitor->setMimeTypeMonitored(mimeType, true);
119 }
120 }
121}
122
123QStringList CollectionComboBoxModel::mimeTypeFilter() const
124{
125 return d->mMimeTypeFilterModel->mimeTypeFilters();
126}
127
129{
130 d->mRightsFilterModel->setAccessRights((Collection::Right)rights);
131 Q_EMIT accessRightsFilterChanged();
132}
133
134int CollectionComboBoxModel::accessRightsFilter() const
135{
136 return (int)d->mRightsFilterModel->accessRights();
137}
138
139qint64 CollectionComboBoxModel::defaultCollectionId() const
140{
141 return d->mDefaultCollectionId;
142}
143
145{
146 if (d->mDefaultCollectionId == collectionId) {
147 return;
148 }
149 d->mDefaultCollectionId = collectionId;
150 d->scanSubTree();
151 Q_EMIT defaultCollectionIdChanged();
152}
153
155{
156 d->mMimeTypeFilterModel->setExcludeVirtualCollections(b);
157}
158
160{
161 return d->mMimeTypeFilterModel->excludeVirtualCollections();
162}
163
164int CollectionComboBoxModel::currentIndex() const
165{
166 return d->mCurrentIndex;
167}
168
169void CollectionComboBoxModel::setCurrentIndex(int currentIndex)
170{
171 if (d->mCurrentIndex == currentIndex) {
172 return;
173 }
174 d->mCurrentIndex = currentIndex;
175 Q_EMIT currentIndexChanged();
176}
177
178#include "moc_collectioncomboboxmodel.cpp"
static Collection root()
void setCollectionMonitored(const Collection &collection, bool monitored=true)
void fetchCollection(bool enable)
The model for a combobox for selecting an Akonadi collection.
void setDefaultCollectionId(qint64 collectionId)
Sets the collection that shall be selected by default.
void setMimeTypeFilter(const QStringList &mimetypes)
Sets the content mimetypes the collections shall be filtered by.
bool excludeVirtualCollections() const
Returns if the virual exollections are excluded.
void setExcludeVirtualCollections(bool b)
Sets if the virtual collections are excluded.
void setAccessRightsFilter(int rights)
Sets the access rights the collections shall be filtered by.
Despite the name, this handles the presentation of collections including display text and icons,...
Q_SCRIPTABLE Q_NOREPLY void start()
void rowsInserted(const QModelIndex &parent, int first, int last)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void setObjectName(QAnyStringView name)
virtual QVariant data(const QModelIndex &index, int role) const const override
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
virtual int rowCount(const QModelIndex &parent) const const override
virtual void setSourceModel(QAbstractItemModel *sourceModel) override
qlonglong toLongLong(bool *ok) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:53:27 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.