File indexing completed on 2024-11-24 04:50:45

0001 // This file is part of Akonadi Contact.
0002 //
0003 // SPDX-FileCopyrightText: 2007-2009 Tobias Koenig <tokoe@kde.org>
0004 // SPDX-FileCopyrightText: 2022 Carl Schwan <carl@carlschwan.eu>
0005 //
0006 // SPDX-License-Identifier: LGPL-2.0-or-later
0007 
0008 #include "collectionpickermodel.h"
0009 
0010 #include <Akonadi/CollectionFetchScope>
0011 #include <Akonadi/CollectionFilterProxyModel>
0012 #include <Akonadi/CollectionUtils>
0013 #include <Akonadi/EntityRightsFilterModel>
0014 #include <Akonadi/EntityTreeModel>
0015 #include <Akonadi/Monitor>
0016 #include <Akonadi/Session>
0017 
0018 #include "colorproxymodel.h"
0019 #include "sortedcollectionproxymodel.h"
0020 #include <QAbstractItemModel>
0021 
0022 using namespace Akonadi::Quick;
0023 
0024 class Akonadi::Quick::CollectionPickerModelPrivate
0025 {
0026 public:
0027     CollectionPickerModelPrivate(CollectionPickerModel *parent)
0028         : mParent(parent)
0029     {
0030         mMonitor = new Akonadi::Monitor(mParent);
0031         mMonitor->setObjectName(QLatin1StringView("CollectionPickerMonitor"));
0032         mMonitor->fetchCollection(true);
0033         mMonitor->setCollectionMonitored(Akonadi::Collection::root());
0034 
0035         // This ETM will be set to only show collections with the wanted mimetype in setMimeTypeFilter
0036         mModel = new Akonadi::EntityTreeModel(mMonitor, mParent);
0037         mModel->setItemPopulationStrategy(Akonadi::EntityTreeModel::NoItemPopulation);
0038         mModel->setListFilter(Akonadi::CollectionFetchScope::Display);
0039 
0040         mBaseModel = mModel;
0041 
0042         // Display color
0043         auto colorProxy = new ColorProxyModel(mParent);
0044         colorProxy->setObjectName(QLatin1StringView("Show collection colors"));
0045         colorProxy->setDynamicSortFilter(true);
0046         colorProxy->setSourceModel(mBaseModel);
0047 
0048         // Filter by access rights. TODO: maybe this functionality could be provided by CollectionFilterProxyModel, to save one proxy?
0049         mRightsFilterModel = new Akonadi::EntityRightsFilterModel(parent);
0050         mRightsFilterModel->setSourceModel(colorProxy);
0051 
0052         mMimeTypeFilterModel = new SortedCollectionProxModel(parent);
0053         mMimeTypeFilterModel->setSourceModel(mRightsFilterModel);
0054         mMimeTypeFilterModel->setSortCaseSensitivity(Qt::CaseInsensitive);
0055         mMimeTypeFilterModel->sort(0, Qt::AscendingOrder);
0056 
0057         mParent->setSourceModel(mMimeTypeFilterModel);
0058     }
0059 
0060     ~CollectionPickerModelPrivate() = default;
0061 
0062     void activated(int index);
0063     void activated(const QModelIndex &index);
0064 
0065     CollectionPickerModel *const mParent;
0066 
0067     Akonadi::Monitor *mMonitor = nullptr;
0068     Akonadi::EntityTreeModel *mModel = nullptr;
0069     QAbstractItemModel *mBaseModel = nullptr;
0070     Akonadi::CollectionFilterProxyModel *mMimeTypeFilterModel = nullptr;
0071     Akonadi::EntityRightsFilterModel *mRightsFilterModel = nullptr;
0072 };
0073 
0074 CollectionPickerModel::CollectionPickerModel(QObject *parent)
0075     : QSortFilterProxyModel(parent)
0076     , d(new CollectionPickerModelPrivate(this))
0077 {
0078 }
0079 
0080 CollectionPickerModel::~CollectionPickerModel() = default;
0081 
0082 void CollectionPickerModel::setMimeTypeFilter(const QStringList &contentMimeTypes)
0083 {
0084     d->mMimeTypeFilterModel->clearFilters();
0085     d->mMimeTypeFilterModel->addMimeTypeFilters(contentMimeTypes);
0086 
0087     if (d->mMonitor) {
0088         for (const QString &mimeType : contentMimeTypes) {
0089             d->mMonitor->setMimeTypeMonitored(mimeType, true);
0090         }
0091     }
0092 }
0093 
0094 QStringList CollectionPickerModel::mimeTypeFilter() const
0095 {
0096     return d->mMimeTypeFilterModel->mimeTypeFilters();
0097 }
0098 
0099 void CollectionPickerModel::setAccessRightsFilter(Collection::Right rights)
0100 {
0101     d->mRightsFilterModel->setAccessRights(rights);
0102     Q_EMIT accessRightsFilterChanged();
0103 }
0104 
0105 Akonadi::Collection::Right CollectionPickerModel::accessRightsFilter() const
0106 {
0107     return (Akonadi::Collection::Right)(int)d->mRightsFilterModel->accessRights();
0108 }
0109 
0110 void CollectionPickerModel::setExcludeVirtualCollections(bool b)
0111 {
0112     d->mMimeTypeFilterModel->setExcludeVirtualCollections(b);
0113     Q_EMIT excludeVirtualCollectionsChanged();
0114 }
0115 
0116 bool CollectionPickerModel::excludeVirtualCollections() const
0117 {
0118     return d->mMimeTypeFilterModel->excludeVirtualCollections();
0119 }
0120 
0121 #include "moc_collectionpickermodel.cpp"