Akonadi

collectioncombobox.cpp
1 /*
2  This file is part of Akonadi Contact.
3 
4  SPDX-FileCopyrightText: 2007-2009 Tobias Koenig <[email protected]>
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 
26 using namespace Akonadi;
27 
28 class Akonadi::CollectionComboBoxPrivate
29 {
30 public:
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(QStringLiteral("CollectionComboBoxMonitor"));
39  mMonitor->fetchCollection(true);
40  mMonitor->setCollectionMonitored(Akonadi::Collection::root());
41 
42  // This ETM will be set to only show collections with the wanted mimetype in setMimeTypeFilter
43  mModel = new EntityTreeModel(mMonitor, mParent);
44  mModel->setItemPopulationStrategy(EntityTreeModel::NoItemPopulation);
45  mModel->setListFilter(CollectionFetchScope::Display);
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 
91 void CollectionComboBoxPrivate::activated(int index)
92 {
93  const QModelIndex modelIndex = mParent->model()->index(index, 0);
94  if (modelIndex.isValid()) {
95  Q_EMIT mParent->currentChanged(modelIndex.data(EntityTreeModel::CollectionRole).value<Collection>());
96  }
97 }
98 
99 void 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"
A model for collections and items together.
@ NoItemPopulation
Do not include items in the model.
T value() const const
Collection::Rights accessRightsFilter() const
Returns the access rights the collections are filtered by.
A combobox for selecting an Akonadi collection.
Represents a collection of PIM items.
Definition: collection.h:61
@ Display
Only retrieve collections for display, taking the local preference and enabled into account.
A proxy model that filters entities by access rights.
QAbstractItemModel * model() const const
A proxy model that filters collections by mime type.
Monitors an item or collection for changes.
Definition: monitor.h:71
CollectionComboBox(QWidget *parent=nullptr)
Creates a new collection combobox.
void setDefaultCollection(const Collection &collection)
Sets the collection that shall be selected by default.
QVariant data(int role) const const
~CollectionComboBox() override
Destroys the collection combobox.
@ CollectionRole
The collection.
bool isValid() const const
static Collection root()
Returns the root collection.
Definition: collection.cpp:287
QStringList mimeTypeFilter() const
Returns the content mimetype the collections are filtered by.
int row() const const
Akonadi::Collection currentCollection() const
Returns the current selection.
void setAccessRightsFilter(Collection::Rights rights)
Sets the access rights the collections shall be filtered by.
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
void setMimeTypeFilter(const QStringList &mimetypes)
Sets the content mimetypes the collections shall be filtered by.
const QAbstractItemModel * model() 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 Sat Jul 2 2022 06:41:47 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.