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#include "collectiondialog.h"
13
14#include "collectionfetchscope.h"
15#include "collectionfilterproxymodel.h"
16#include "collectionutils.h"
17#include "entityrightsfiltermodel.h"
18#include "entitytreemodel.h"
19#include "monitor.h"
20#include "session.h"
21
22#include <KDescendantsProxyModel>
23
24#include <QAbstractItemModel>
25
26using namespace Akonadi;
27
28class Akonadi::CollectionComboBoxPrivate
29{
30public:
31 CollectionComboBoxPrivate(QAbstractItemModel *customModel, CollectionComboBox *parent)
32 : mParent(parent)
33 {
34 if (customModel) {
35 mBaseModel = customModel;
36 } else {
37 mMonitor = new Akonadi::Monitor(mParent);
38 mMonitor->setObjectName(QLatin1StringView("CollectionComboBoxMonitor"));
39 mMonitor->fetchCollection(true);
41
42 // This ETM will be set to only show collections with the wanted mimetype in setMimeTypeFilter
43 mModel = new EntityTreeModel(mMonitor, mParent);
46
47 mBaseModel = mModel;
48 }
49
50 // Flatten the tree, e.g.
51 // Kolab
52 // Kolab / Inbox
53 // Kolab / Inbox / Calendar
54 auto proxyModel = new KDescendantsProxyModel(parent);
55 proxyModel->setDisplayAncestorData(true);
56 proxyModel->setSourceModel(mBaseModel);
57
58 // Filter it by mimetype again, to only keep
59 // Kolab / Inbox / Calendar
60 mMimeTypeFilterModel = new CollectionFilterProxyModel(parent);
61 mMimeTypeFilterModel->setSourceModel(proxyModel);
62
63 // Filter by access rights. TODO: maybe this functionality could be provided by CollectionFilterProxyModel, to save one proxy?
64 mRightsFilterModel = new EntityRightsFilterModel(parent);
65 mRightsFilterModel->setSourceModel(mMimeTypeFilterModel);
66
67 mParent->setModel(mRightsFilterModel);
68 mParent->model()->sort(mParent->modelColumn());
69
70 mSelectionHandler = new AsyncSelectionHandler(mRightsFilterModel, mParent);
71 mParent->connect(mSelectionHandler, &AsyncSelectionHandler::collectionAvailable, mParent, [this](const auto &mi) {
72 activated(mi);
73 });
74 }
75
76 ~CollectionComboBoxPrivate() = default;
77
78 void activated(int index);
79 void activated(const QModelIndex &index);
80
81 CollectionComboBox *const mParent;
82
83 Monitor *mMonitor = nullptr;
84 EntityTreeModel *mModel = nullptr;
85 QAbstractItemModel *mBaseModel = nullptr;
86 CollectionFilterProxyModel *mMimeTypeFilterModel = nullptr;
87 EntityRightsFilterModel *mRightsFilterModel = nullptr;
88 AsyncSelectionHandler *mSelectionHandler = nullptr;
89};
90
91void CollectionComboBoxPrivate::activated(int index)
92{
93 const QModelIndex modelIndex = mParent->model()->index(index, 0);
94 if (modelIndex.isValid()) {
96 }
97}
98
99void CollectionComboBoxPrivate::activated(const QModelIndex &index)
100{
101 mParent->setCurrentIndex(index.row());
102}
103
105 : QComboBox(parent)
106 , d(new CollectionComboBoxPrivate(nullptr, this))
107{
108}
109
111 : QComboBox(parent)
112 , d(new CollectionComboBoxPrivate(model, this))
113{
114}
115
117
119{
120 d->mMimeTypeFilterModel->clearFilters();
121 d->mMimeTypeFilterModel->addMimeTypeFilters(contentMimeTypes);
122
123 if (d->mMonitor) {
124 for (const QString &mimeType : contentMimeTypes) {
125 d->mMonitor->setMimeTypeMonitored(mimeType, true);
126 }
127 }
128}
129
131{
132 return d->mMimeTypeFilterModel->mimeTypeFilters();
133}
134
136{
137 d->mRightsFilterModel->setAccessRights(rights);
138}
139
141{
142 return d->mRightsFilterModel->accessRights();
143}
144
146{
147 d->mSelectionHandler->waitForCollection(collection);
148}
149
151{
152 const QModelIndex modelIndex = model()->index(currentIndex(), 0);
153 if (modelIndex.isValid()) {
155 } else {
156 return Akonadi::Collection();
157 }
158}
159
161{
162 d->mMimeTypeFilterModel->setExcludeVirtualCollections(b);
163}
164
166{
167 return d->mMimeTypeFilterModel->excludeVirtualCollections();
168}
169
170#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 currentChanged(const Akonadi::Collection &collection)
This signal is emitted whenever the current selection has been changed.
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.
A proxy model that filters collections by mime type.
Represents a collection of PIM items.
Definition collection.h:62
static Collection root()
Returns the root collection.
A proxy model that filters entities by access rights.
A model for collections and items together.
@ NoItemPopulation
Do not include items in the model.
void setListFilter(Akonadi::CollectionFetchScope::ListFilter filter)
Sets the currently used listfilter.
void setItemPopulationStrategy(ItemPopulationStrategy strategy)
Sets the item population strategy of the model.
@ CollectionRole
The collection.
Monitors an item or collection for changes.
Definition monitor.h:71
void setCollectionMonitored(const Collection &collection, bool monitored=true)
Sets whether the specified collection shall be monitored for changes.
Definition monitor.cpp:48
void fetchCollection(bool enable)
Enables automatic fetching of changed collections from the Akonadi storage.
Definition monitor.cpp:193
Helper integration between Akonadi and Qt.
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
virtual void sort(int column, Qt::SortOrder order)
void setCurrentIndex(int index)
QAbstractItemModel * model() const const
virtual void setModel(QAbstractItemModel *model)
QVariant data(int role) const const
bool isValid() const const
int row() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void setObjectName(QAnyStringView name)
virtual void setSourceModel(QAbstractItemModel *sourceModel) override
T value() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:01:00 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.