Akonadi

collectioncombobox.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 "collectioncombobox.h"
10
11#include "asyncselectionhandler_p.h"
12
13#include "collectionfetchscope.h"
14#include "collectionfilterproxymodel.h"
15#include "entityrightsfiltermodel.h"
16#include "entitytreemodel.h"
17#include "monitor.h"
18#include "session.h"
19
20#include <KDescendantsProxyModel>
21
22#include <QAbstractItemModel>
23
24using namespace Akonadi;
25
26class Akonadi::CollectionComboBoxPrivate
27{
28public:
29 CollectionComboBoxPrivate(QAbstractItemModel *customModel, CollectionComboBox *parent)
30 : mParent(parent)
31 {
32 if (customModel) {
33 mBaseModel = customModel;
34 } else {
35 mMonitor = new Akonadi::Monitor(mParent);
36 mMonitor->setObjectName(QLatin1StringView("CollectionComboBoxMonitor"));
37 mMonitor->fetchCollection(true);
38 mMonitor->setCollectionMonitored(Akonadi::Collection::root());
39
40 // This ETM will be set to only show collections with the wanted mimetype in setMimeTypeFilter
41 mModel = new EntityTreeModel(mMonitor, mParent);
42 mModel->setItemPopulationStrategy(EntityTreeModel::NoItemPopulation);
43 mModel->setListFilter(CollectionFetchScope::Display);
44
45 mBaseModel = mModel;
46 }
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(mBaseModel);
55
56 // Filter it by mimetype again, to only keep
57 // Kolab / Inbox / Calendar
58 mMimeTypeFilterModel = new 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 EntityRightsFilterModel(parent);
63 mRightsFilterModel->setSourceModel(mMimeTypeFilterModel);
64
65 mParent->setModel(mRightsFilterModel);
66 mParent->model()->sort(mParent->modelColumn());
67
68 mSelectionHandler = new AsyncSelectionHandler(mRightsFilterModel, mParent);
69 mParent->connect(mSelectionHandler, &AsyncSelectionHandler::collectionAvailable, mParent, [this](const auto &mi) {
70 activated(mi);
71 });
72 mParent->connect(mParent, &QComboBox::currentIndexChanged, mParent, [this](const auto &mi) {
73 activated(mi);
74 });
75 }
76
77 ~CollectionComboBoxPrivate() = default;
78
79 void activated(int index);
80 void activated(const QModelIndex &index);
81
82 CollectionComboBox *const mParent;
83
84 Monitor *mMonitor = nullptr;
85 EntityTreeModel *mModel = nullptr;
86 QAbstractItemModel *mBaseModel = nullptr;
87 CollectionFilterProxyModel *mMimeTypeFilterModel = nullptr;
88 EntityRightsFilterModel *mRightsFilterModel = nullptr;
89 AsyncSelectionHandler *mSelectionHandler = nullptr;
90};
91
92void CollectionComboBoxPrivate::activated(int index)
93{
94 const QModelIndex modelIndex = mParent->model()->index(index, 0);
95 if (modelIndex.isValid()) {
96 Q_EMIT mParent->currentChanged(modelIndex.data(EntityTreeModel::CollectionRole).value<Collection>());
97 }
98}
99
100void CollectionComboBoxPrivate::activated(const QModelIndex &index)
101{
102 mParent->setCurrentIndex(index.row());
103}
104
107 , d(new CollectionComboBoxPrivate(nullptr, this))
108{
109}
110
113 , d(new CollectionComboBoxPrivate(model, this))
114{
115}
116
118
120{
121 d->mMimeTypeFilterModel->clearFilters();
122 d->mMimeTypeFilterModel->addMimeTypeFilters(contentMimeTypes);
123
124 if (d->mMonitor) {
125 for (const QString &mimeType : contentMimeTypes) {
126 d->mMonitor->setMimeTypeMonitored(mimeType, true);
127 }
128 }
129}
130
132{
133 return d->mMimeTypeFilterModel->mimeTypeFilters();
134}
135
136void CollectionComboBox::setAccessRightsFilter(Collection::Rights rights)
137{
138 d->mRightsFilterModel->setAccessRights(rights);
139}
140
141Akonadi::Collection::Rights CollectionComboBox::accessRightsFilter() const
142{
143 return d->mRightsFilterModel->accessRights();
144}
145
147{
148 d->mSelectionHandler->waitForCollection(collection);
149}
150
152{
153 const QModelIndex modelIndex = model()->index(currentIndex(), 0);
154 if (modelIndex.isValid()) {
156 } else {
157 return Akonadi::Collection();
158 }
159}
160
162{
163 d->mMimeTypeFilterModel->setExcludeVirtualCollections(b);
164}
165
167{
168 return d->mMimeTypeFilterModel->excludeVirtualCollections();
169}
170
171#include "moc_collectioncombobox.cpp"
void setMimeTypeFilter(const QStringList &mimetypes)
Sets the content mimetypes the collections shall be filtered by.
CollectionComboBox(QWidget *parent=nullptr)
Creates a new collection combobox.
QStringList mimeTypeFilter() const
Returns the content mimetype the collections are filtered by.
~CollectionComboBox() override
Destroys the collection combobox.
Collection::Rights accessRightsFilter() const
Returns the access rights the collections are filtered by.
void setAccessRightsFilter(Collection::Rights rights)
Sets the access rights the collections shall be filtered by.
Akonadi::Collection currentCollection() const
Returns the current selection.
void setDefaultCollection(const Collection &collection)
Sets the collection that shall be selected by default.
@ Display
Only retrieve collections for display, taking the local preference and enabled into account.
Represents a collection of PIM items.
Definition collection.h:62
static Collection root()
Returns the root collection.
@ NoItemPopulation
Do not include items in the model.
@ CollectionRole
The collection.
Helper integration between Akonadi and Qt.
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
QComboBox(QWidget *parent)
void currentIndexChanged(int index)
QAbstractItemModel * model() const const
QVariant data(int role) const const
bool isValid() const const
int row() const const
QObject * parent() const const
T value() const const
QWidget(QWidget *parent, Qt::WindowFlags f)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Mar 28 2025 11:53:22 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.