Akonadi

collectioncombobox.cpp
1 /*
2  This file is part of Akonadi Contact.
3 
4  Copyright (c) 2007-2009 Tobias Koenig <[email protected]>
5 
6  This library is free software; you can redistribute it and/or modify it
7  under the terms of the GNU Library General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or (at your
9  option) any later version.
10 
11  This library is distributed in the hope that it will be useful, but WITHOUT
12  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14  License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301, USA.
20 */
21 
22 #include "collectioncombobox.h"
23 
24 #include "asyncselectionhandler_p.h"
25 #include "collectiondialog.h"
26 
27 #include "monitor.h"
28 #include "collectionfetchscope.h"
29 #include "collectionfilterproxymodel.h"
30 #include "entityrightsfiltermodel.h"
31 #include "entitytreemodel.h"
32 #include "session.h"
33 #include "collectionutils.h"
34 
35 #include <KDescendantsProxyModel>
36 
37 #include <QAbstractItemModel>
38 
39 using namespace Akonadi;
40 
41 class Q_DECL_HIDDEN CollectionComboBox::Private
42 {
43 public:
44  Private(QAbstractItemModel *customModel, CollectionComboBox *parent)
45  : mParent(parent)
46  {
47  if (customModel) {
48  mBaseModel = customModel;
49  } else {
50  mMonitor = new Akonadi::Monitor(mParent);
51  mMonitor->setObjectName(QStringLiteral("CollectionComboBoxMonitor"));
52  mMonitor->fetchCollection(true);
53  mMonitor->setCollectionMonitored(Akonadi::Collection::root());
54 
55  // This ETM will be set to only show collections with the wanted mimetype in setMimeTypeFilter
56  mModel = new EntityTreeModel(mMonitor, mParent);
57  mModel->setItemPopulationStrategy(EntityTreeModel::NoItemPopulation);
58  mModel->setListFilter(CollectionFetchScope::Display);
59 
60  mBaseModel = mModel;
61  }
62 
63  // Flatten the tree, e.g.
64  // Kolab
65  // Kolab / Inbox
66  // Kolab / Inbox / Calendar
67  KDescendantsProxyModel *proxyModel = new KDescendantsProxyModel(parent);
68  proxyModel->setDisplayAncestorData(true);
69  proxyModel->setSourceModel(mBaseModel);
70 
71  // Filter it by mimetype again, to only keep
72  // Kolab / Inbox / Calendar
73  mMimeTypeFilterModel = new CollectionFilterProxyModel(parent);
74  mMimeTypeFilterModel->setSourceModel(proxyModel);
75 
76  // Filter by access rights. TODO: maybe this functionality could be provided by CollectionFilterProxyModel, to save one proxy?
77  mRightsFilterModel = new EntityRightsFilterModel(parent);
78  mRightsFilterModel->setSourceModel(mMimeTypeFilterModel);
79 
80  mParent->setModel(mRightsFilterModel);
81  mParent->model()->sort(mParent->modelColumn());
82 
83  mSelectionHandler = new AsyncSelectionHandler(mRightsFilterModel, mParent);
84  mParent->connect(mSelectionHandler, SIGNAL(collectionAvailable(QModelIndex)),
85  mParent, SLOT(activated(QModelIndex)));
86 
87  mParent->connect(mParent, SIGNAL(activated(int)),
88  mParent, SLOT(activated(int)));
89  }
90 
91  ~Private()
92  {
93  }
94 
95  void activated(int index);
96  void activated(const QModelIndex &index);
97 
98  CollectionComboBox *mParent = nullptr;
99 
100  Monitor *mMonitor = nullptr;
101  EntityTreeModel *mModel = nullptr;
102  QAbstractItemModel *mBaseModel = nullptr;
103  CollectionFilterProxyModel *mMimeTypeFilterModel = nullptr;
104  EntityRightsFilterModel *mRightsFilterModel = nullptr;
105  AsyncSelectionHandler *mSelectionHandler = nullptr;
106 };
107 
108 void CollectionComboBox::Private::activated(int index)
109 {
110  const QModelIndex modelIndex = mParent->model()->index(index, 0);
111  if (modelIndex.isValid()) {
112  Q_EMIT mParent->currentChanged(modelIndex.data(EntityTreeModel::CollectionRole).value<Collection>());
113  }
114 }
115 
116 void CollectionComboBox::Private::activated(const QModelIndex &index)
117 {
118  mParent->setCurrentIndex(index.row());
119 }
120 
122  : QComboBox(parent)
123  , d(new Private(nullptr, this))
124 {
125 }
126 
128  : QComboBox(parent)
129  , d(new Private(model, this))
130 {
131 }
132 
134 {
135  delete d;
136 }
137 
139 {
140  d->mMimeTypeFilterModel->clearFilters();
141  d->mMimeTypeFilterModel->addMimeTypeFilters(contentMimeTypes);
142 
143  if (d->mMonitor) {
144  for (const QString &mimeType : contentMimeTypes) {
145  d->mMonitor->setMimeTypeMonitored(mimeType, true);
146  }
147  }
148 }
149 
151 {
152  return d->mMimeTypeFilterModel->mimeTypeFilters();
153 }
154 
156 {
157  d->mRightsFilterModel->setAccessRights(rights);
158 }
159 
161 {
162  return d->mRightsFilterModel->accessRights();
163 }
164 
166 {
167  d->mSelectionHandler->waitForCollection(collection);
168 }
169 
171 {
172  const QModelIndex modelIndex = model()->index(currentIndex(), 0);
173  if (modelIndex.isValid()) {
175  } else {
176  return Akonadi::Collection();
177  }
178 }
179 
181 {
182  d->mMimeTypeFilterModel->setExcludeVirtualCollections(b);
183 }
184 
186 {
187  return d->mMimeTypeFilterModel->excludeVirtualCollections();
188 }
189 
190 #include "moc_collectioncombobox.cpp"
QAbstractItemModel * model() const const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const =0
A proxy model that filters collections by mime type.
Represents a collection of PIM items.
Definition: collection.h:76
Do not include items in the model.
T value() const const
Only retrieve collections for display, taking the local preference and enabled into account...
A proxy model that filters entities by access rights.
A combobox for selecting an Akonadi collection.
void setDefaultCollection(const Collection &collection)
Sets the collection that shall be selected by default.
Collection::Rights accessRightsFilter() const
Returns the access rights the collections are filtered by.
Akonadi::Collection currentCollection() const
Returns the current selection.
bool isValid() const const
CollectionComboBox(QWidget *parent=nullptr)
Creates a new collection combobox.
~CollectionComboBox()
Destroys the collection combobox.
int row() const const
static Collection root()
Returns the root collection.
Definition: collection.cpp:303
QStringList mimeTypeFilter() const
Returns the content mimetype the collections are filtered by.
Monitors an item or collection for changes.
Definition: monitor.h:84
const QAbstractItemModel * model() const const
QVariant data(int role) const const
void setAccessRightsFilter(Collection::Rights rights)
Sets the access rights the collections shall be filtered by.
void setSourceModel(QAbstractItemModel *model) override
Helper integration between Akonadi and Qt.
A model for collections and items together.
int currentIndex() const const
void setDisplayAncestorData(bool display)
void setMimeTypeFilter(const QStringList &mimetypes)
Sets the content mimetypes the collections shall be filtered by.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue May 26 2020 22:46:17 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.