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 }
73
74 ~CollectionComboBoxPrivate() = default;
75
76 void activated(int index);
77 void activated(const QModelIndex &index);
78
79 CollectionComboBox *const mParent;
80
81 Monitor *mMonitor = nullptr;
82 EntityTreeModel *mModel = nullptr;
83 QAbstractItemModel *mBaseModel = nullptr;
84 CollectionFilterProxyModel *mMimeTypeFilterModel = nullptr;
85 EntityRightsFilterModel *mRightsFilterModel = nullptr;
86 AsyncSelectionHandler *mSelectionHandler = nullptr;
87};
88
89void CollectionComboBoxPrivate::activated(int index)
90{
91 const QModelIndex modelIndex = mParent->model()->index(index, 0);
92 if (modelIndex.isValid()) {
93 Q_EMIT mParent->currentChanged(modelIndex.data(EntityTreeModel::CollectionRole).value<Collection>());
94 }
95}
96
97void CollectionComboBoxPrivate::activated(const QModelIndex &index)
98{
99 mParent->setCurrentIndex(index.row());
100}
101
104 , d(new CollectionComboBoxPrivate(nullptr, this))
105{
106}
107
110 , d(new CollectionComboBoxPrivate(model, this))
111{
112}
113
115
117{
118 d->mMimeTypeFilterModel->clearFilters();
119 d->mMimeTypeFilterModel->addMimeTypeFilters(contentMimeTypes);
120
121 if (d->mMonitor) {
122 for (const QString &mimeType : contentMimeTypes) {
123 d->mMonitor->setMimeTypeMonitored(mimeType, true);
124 }
125 }
126}
127
129{
130 return d->mMimeTypeFilterModel->mimeTypeFilters();
131}
132
133void CollectionComboBox::setAccessRightsFilter(Collection::Rights rights)
134{
135 d->mRightsFilterModel->setAccessRights(rights);
136}
137
138Akonadi::Collection::Rights CollectionComboBox::accessRightsFilter() const
139{
140 return d->mRightsFilterModel->accessRights();
141}
142
144{
145 d->mSelectionHandler->waitForCollection(collection);
146}
147
149{
150 const QModelIndex modelIndex = model()->index(currentIndex(), 0);
151 if (modelIndex.isValid()) {
153 } else {
154 return Akonadi::Collection();
155 }
156}
157
159{
160 d->mMimeTypeFilterModel->setExcludeVirtualCollections(b);
161}
162
164{
165 return d->mMimeTypeFilterModel->excludeVirtualCollections();
166}
167
168#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)
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 Feb 21 2025 11:50:41 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.