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

0001 // SPDX-FileCopyrightText: 2007 Tobias Koenig <tokoe@kde.org>
0002 // SPDX-FileCopyrightText: 2022 Carl Schwan <carl@carlschwan.eu>
0003 // SPDX-License-Identifier: GPL-2.0-or-later
0004 
0005 #include "contactcollectionmodel.h"
0006 #include <KContacts/Addressee>
0007 #include <KContacts/ContactGroup>
0008 
0009 static bool isContactCollection(const Akonadi::Collection &collection)
0010 {
0011     const QStringList mimeTypes = {KContacts::Addressee::mimeType(), KContacts::ContactGroup::mimeType()};
0012     const QStringList collectionMimeTypes = collection.contentMimeTypes();
0013     for (const QString &mimeType : mimeTypes) {
0014         if (collectionMimeTypes.contains(mimeType)) {
0015             return false;
0016         }
0017     }
0018     return true;
0019 }
0020 
0021 ContactCollectionModel::ContactCollectionModel(QObject *parent)
0022     : KCheckableProxyModel(parent)
0023 {
0024 }
0025 
0026 QVariant ContactCollectionModel::data(const QModelIndex &index, int role) const
0027 {
0028     if (!index.isValid()) {
0029         return {};
0030     }
0031 
0032     if (role == Qt::CheckStateRole) {
0033         // Don't show the checkbox if the collection can't contain incidences
0034         const auto collection = index.data(Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>();
0035         if (collection.isValid() && isContactCollection(collection)) {
0036             return {};
0037         }
0038     }
0039     return KCheckableProxyModel::data(index, role);
0040 }