Akonadi Contacts

contactcompletionmodel.cpp
1/*
2 This file is part of Akonadi Contact.
3
4 SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "contactcompletionmodel_p.h"
10
11#include <Akonadi/EntityMimeTypeFilterModel>
12#include <Akonadi/ItemFetchScope>
13#include <Akonadi/Monitor>
14#include <Akonadi/Session>
15
16#include <KContacts/Addressee>
17
18using namespace Akonadi;
19
20QAbstractItemModel *ContactCompletionModel::mSelf = nullptr;
21
22QAbstractItemModel *ContactCompletionModel::self()
23{
24 if (mSelf) {
25 return mSelf;
26 }
27
28 auto monitor = new Monitor;
29 monitor->setObjectName(QLatin1StringView("ContactCompletionModelMonitor"));
30 monitor->fetchCollection(true);
31 monitor->itemFetchScope().fetchFullPayload();
32 monitor->setCollectionMonitored(Akonadi::Collection::root());
33 monitor->setMimeTypeMonitored(KContacts::Addressee::mimeType());
34
35 auto model = new ContactCompletionModel(monitor);
36
38 filter->setSourceModel(model);
39 filter->addMimeTypeExclusionFilter(Akonadi::Collection::mimeType());
40 filter->addMimeTypeExclusionFilter(Akonadi::Collection::virtualMimeType());
42
43 mSelf = filter;
44
45 return mSelf;
46}
47
48ContactCompletionModel::ContactCompletionModel(Monitor *monitor, QObject *parent)
49 : EntityTreeModel(monitor, parent)
50{
51 setCollectionFetchStrategy(InvisibleCollectionFetch);
52}
53
54ContactCompletionModel::~ContactCompletionModel() = default;
55
56QVariant ContactCompletionModel::entityData(const Item &item, int column, int role) const
57{
58 if (!item.hasPayload<KContacts::Addressee>()) {
59 // Pass modeltest
60 if (role == Qt::DisplayRole) {
61 return item.remoteId();
62 }
63
64 return {};
65 }
66
67 if (role == Qt::DisplayRole || role == Qt::EditRole) {
68 const auto contact = item.payload<KContacts::Addressee>();
69
70 switch (column) {
71 case NameColumn:
72 if (!contact.formattedName().isEmpty()) {
73 return contact.formattedName();
74 } else {
75 return contact.assembledName();
76 }
77 break;
78 case NameAndEmailColumn: {
79 QString name = QStringLiteral("%1 %2").arg(contact.givenName(), contact.familyName()).simplified();
80 if (name.isEmpty()) {
81 name = contact.organization().simplified();
82 }
83 if (name.isEmpty()) {
84 return QString();
85 }
86
87 const QString email = contact.preferredEmail().simplified();
88 if (email.isEmpty()) {
89 return QString();
90 }
91
92 return QStringLiteral("%1 <%2>").arg(name, email);
93 break;
94 }
95 case EmailColumn:
96 return contact.preferredEmail();
97 break;
98 }
99 }
100
101 return EntityTreeModel::entityData(item, column, role);
102}
103
104QVariant ContactCompletionModel::entityData(const Collection &collection, int column, int role) const
105{
106 return EntityTreeModel::entityData(collection, column, role);
107}
108
109int ContactCompletionModel::columnCount(const QModelIndex &parent) const
110{
111 if (!parent.isValid()) {
112 return 3;
113 } else {
114 return 0;
115 }
116}
117
118int ContactCompletionModel::entityColumnCount(HeaderGroup) const
119{
120 return 3;
121}
122
123#include "moc_contactcompletionmodel_p.cpp"
static QString mimeType()
static Collection root()
static QString virtualMimeType()
virtual QVariant entityData(const Collection &collection, int column, int role=Qt::DisplayRole) const
static QString mimeType()
A widget for editing the display name of a contact.
QString name(StandardAction id)
bool isValid() const const
void setObjectName(QAnyStringView name)
QString arg(Args &&... args) const const
bool isEmpty() const const
QString simplified() const const
DisplayRole
QFuture< void > filter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&filterFunction)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:05:46 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.