File indexing completed on 2024-11-24 04:39:27
0001 /* 0002 This file is part of Akonadi Contact. 0003 0004 SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org> 0005 0006 SPDX-License-Identifier: LGPL-2.0-or-later 0007 */ 0008 0009 #include "contactcompletionmodel_p.h" 0010 0011 #include <Akonadi/EntityMimeTypeFilterModel> 0012 #include <Akonadi/ItemFetchScope> 0013 #include <Akonadi/Monitor> 0014 #include <Akonadi/Session> 0015 0016 #include <KContacts/Addressee> 0017 0018 using namespace Akonadi; 0019 0020 QAbstractItemModel *ContactCompletionModel::mSelf = nullptr; 0021 0022 QAbstractItemModel *ContactCompletionModel::self() 0023 { 0024 if (mSelf) { 0025 return mSelf; 0026 } 0027 0028 auto monitor = new Monitor; 0029 monitor->setObjectName(QLatin1StringView("ContactCompletionModelMonitor")); 0030 monitor->fetchCollection(true); 0031 monitor->itemFetchScope().fetchFullPayload(); 0032 monitor->setCollectionMonitored(Akonadi::Collection::root()); 0033 monitor->setMimeTypeMonitored(KContacts::Addressee::mimeType()); 0034 0035 auto model = new ContactCompletionModel(monitor); 0036 0037 auto filter = new Akonadi::EntityMimeTypeFilterModel(model); 0038 filter->setSourceModel(model); 0039 filter->addMimeTypeExclusionFilter(Akonadi::Collection::mimeType()); 0040 filter->addMimeTypeExclusionFilter(Akonadi::Collection::virtualMimeType()); 0041 filter->setHeaderGroup(Akonadi::EntityTreeModel::ItemListHeaders); 0042 0043 mSelf = filter; 0044 0045 return mSelf; 0046 } 0047 0048 ContactCompletionModel::ContactCompletionModel(Monitor *monitor, QObject *parent) 0049 : EntityTreeModel(monitor, parent) 0050 { 0051 setCollectionFetchStrategy(InvisibleCollectionFetch); 0052 } 0053 0054 ContactCompletionModel::~ContactCompletionModel() = default; 0055 0056 QVariant ContactCompletionModel::entityData(const Item &item, int column, int role) const 0057 { 0058 if (!item.hasPayload<KContacts::Addressee>()) { 0059 // Pass modeltest 0060 if (role == Qt::DisplayRole) { 0061 return item.remoteId(); 0062 } 0063 0064 return {}; 0065 } 0066 0067 if (role == Qt::DisplayRole || role == Qt::EditRole) { 0068 const auto contact = item.payload<KContacts::Addressee>(); 0069 0070 switch (column) { 0071 case NameColumn: 0072 if (!contact.formattedName().isEmpty()) { 0073 return contact.formattedName(); 0074 } else { 0075 return contact.assembledName(); 0076 } 0077 break; 0078 case NameAndEmailColumn: { 0079 QString name = QStringLiteral("%1 %2").arg(contact.givenName(), contact.familyName()).simplified(); 0080 if (name.isEmpty()) { 0081 name = contact.organization().simplified(); 0082 } 0083 if (name.isEmpty()) { 0084 return QString(); 0085 } 0086 0087 const QString email = contact.preferredEmail().simplified(); 0088 if (email.isEmpty()) { 0089 return QString(); 0090 } 0091 0092 return QStringLiteral("%1 <%2>").arg(name, email); 0093 break; 0094 } 0095 case EmailColumn: 0096 return contact.preferredEmail(); 0097 break; 0098 } 0099 } 0100 0101 return EntityTreeModel::entityData(item, column, role); 0102 } 0103 0104 QVariant ContactCompletionModel::entityData(const Collection &collection, int column, int role) const 0105 { 0106 return EntityTreeModel::entityData(collection, column, role); 0107 } 0108 0109 int ContactCompletionModel::columnCount(const QModelIndex &parent) const 0110 { 0111 if (!parent.isValid()) { 0112 return 3; 0113 } else { 0114 return 0; 0115 } 0116 } 0117 0118 int ContactCompletionModel::entityColumnCount(HeaderGroup) const 0119 { 0120 return 3; 0121 } 0122 0123 #include "moc_contactcompletionmodel_p.cpp"