Akonadi Contacts

contactcompletionmodel.cpp
1 /*
2  This file is part of Akonadi Contact.
3 
4  SPDX-FileCopyrightText: 2009 Tobias Koenig <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #include "contactcompletionmodel_p.h"
10 
11 #include <entitymimetypefiltermodel.h>
12 #include <itemfetchscope.h>
13 #include <monitor.h>
14 #include <session.h>
15 
16 #include <kcontacts/addressee.h>
17 
18 using namespace Akonadi;
19 
20 QAbstractItemModel *ContactCompletionModel::mSelf = nullptr;
21 
22 QAbstractItemModel *ContactCompletionModel::self()
23 {
24  if (mSelf) {
25  return mSelf;
26  }
27 
28  auto monitor = new Monitor;
29  monitor->setObjectName(QStringLiteral("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 
48 ContactCompletionModel::ContactCompletionModel(Monitor *monitor, QObject *parent)
49  : EntityTreeModel(monitor, parent)
50 {
51  setCollectionFetchStrategy(InvisibleCollectionFetch);
52 }
53 
54 ContactCompletionModel::~ContactCompletionModel()
55 {
56 }
57 
58 QVariant ContactCompletionModel::entityData(const Item &item, int column, int role) const
59 {
60  if (!item.hasPayload<KContacts::Addressee>()) {
61  // Pass modeltest
62  if (role == Qt::DisplayRole) {
63  return item.remoteId();
64  }
65 
66  return QVariant();
67  }
68 
69  if (role == Qt::DisplayRole || role == Qt::EditRole) {
70  const auto contact = item.payload<KContacts::Addressee>();
71 
72  switch (column) {
73  case NameColumn:
74  if (!contact.formattedName().isEmpty()) {
75  return contact.formattedName();
76  } else {
77  return contact.assembledName();
78  }
79  break;
80  case NameAndEmailColumn: {
81  QString name = QStringLiteral("%1 %2").arg(contact.givenName(), contact.familyName()).simplified();
82  if (name.isEmpty()) {
83  name = contact.organization().simplified();
84  }
85  if (name.isEmpty()) {
86  return QString();
87  }
88 
89  const QString email = contact.preferredEmail().simplified();
90  if (email.isEmpty()) {
91  return QString();
92  }
93 
94  return QStringLiteral("%1 <%2>").arg(name, email);
95  break;
96  }
97  case EmailColumn:
98  return contact.preferredEmail();
99  break;
100  }
101  }
102 
103  return EntityTreeModel::entityData(item, column, role);
104 }
105 
106 QVariant ContactCompletionModel::entityData(const Collection &collection, int column, int role) const
107 {
108  return EntityTreeModel::entityData(collection, column, role);
109 }
110 
111 int ContactCompletionModel::columnCount(const QModelIndex &parent) const
112 {
113  if (!parent.isValid()) {
114  return 3;
115  } else {
116  return 0;
117  }
118 }
119 
120 int ContactCompletionModel::entityColumnCount(HeaderGroup) const
121 {
122  return 3;
123 }
124 
125 #include "moc_contactcompletionmodel_p.cpp"
static QString mimeType()
QString name(const QVariant &location)
QString simplified() const const
static QString virtualMimeType()
static QString mimeType()
T payload() const
bool isValid() const const
QString remoteId() const
void setObjectName(const QString &name)
DisplayRole
bool isEmpty() const const
static Collection root()
QString formattedName() const
QFuture< void > filter(Sequence &sequence, KeepFunctor filterFunction)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
bool hasPayload() const
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Wed Jun 23 2021 23:09:24 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.