Akonadi Contacts

emailaddressselectionproxymodel.cpp
1 /*
2  This file is part of Akonadi Contact.
3 
4  SPDX-FileCopyrightText: 2010 KDAB
5  SPDX-FileContributor: Tobias Koenig <[email protected]>
6 
7  SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9 
10 #include "emailaddressselectionproxymodel_p.h"
11 
12 #include <KLocalizedString>
13 #include <item.h>
14 #include <kcontacts/addressee.h>
15 #include <kcontacts/contactgroup.h>
16 
17 using namespace Akonadi;
18 
19 static QString createToolTip(const KContacts::ContactGroup &group)
20 {
21  QString txt = QStringLiteral("<qt>");
22 
23  txt += QStringLiteral("<b>%1</b>").arg(i18n("Distribution List %1", group.name()));
24  txt += QLatin1String("<ul>");
25  const uint groupDataCount(group.dataCount());
26  for (uint i = 0; i < groupDataCount; ++i) {
27  txt += QLatin1String("<li>");
28  txt += group.data(i).name() + QLatin1Char(' ');
29  txt += QLatin1String("<em>");
30  txt += group.data(i).email();
31  txt += QLatin1String("</em></li>");
32  }
33  txt += QLatin1String("</ul>");
34  txt += QLatin1String("</qt>");
35 
36  return txt;
37 }
38 
39 static QString createToolTip(const QString &name, const QString &email)
40 {
41  return QStringLiteral("<qt>%1<b>%2</b></qt>").arg(name.isEmpty() ? QString() : name + QLatin1String("<br/>"), email);
42 }
43 
44 EmailAddressSelectionProxyModel::EmailAddressSelectionProxyModel(QObject *parent)
45  : LeafExtensionProxyModel(parent)
46 {
47 }
48 
49 EmailAddressSelectionProxyModel::~EmailAddressSelectionProxyModel()
50 {
51 }
52 
53 QVariant EmailAddressSelectionProxyModel::data(const QModelIndex &index, int role) const
54 {
55  const QVariant value = LeafExtensionProxyModel::data(index, role);
56 
57  if (!value.isValid()) { // index is not a leaf child
58  if (role == NameRole) {
59  const auto item = index.data(ContactsTreeModel::ItemRole).value<Akonadi::Item>();
60  if (item.hasPayload<KContacts::Addressee>()) {
61  const auto contact = item.payload<KContacts::Addressee>();
62  return contact.realName();
63  } else if (item.hasPayload<KContacts::ContactGroup>()) {
64  const auto group = item.payload<KContacts::ContactGroup>();
65  return group.name();
66  }
67  } else if (role == EmailAddressRole) {
68  const auto item = index.data(ContactsTreeModel::ItemRole).value<Akonadi::Item>();
69  if (item.hasPayload<KContacts::Addressee>()) {
70  const auto contact = item.payload<KContacts::Addressee>();
71  return contact.preferredEmail();
72  } else if (item.hasPayload<KContacts::ContactGroup>()) {
73  const auto group = item.payload<KContacts::ContactGroup>();
74  return group.name(); // the name must be resolved by the caller
75  }
76  } else if (role == Qt::ToolTipRole) {
77  const auto item = index.data(ContactsTreeModel::ItemRole).value<Akonadi::Item>();
78  if (item.hasPayload<KContacts::Addressee>()) {
79  const auto contact = item.payload<KContacts::Addressee>();
80  return createToolTip(contact.realName(), contact.preferredEmail());
81  } else if (item.hasPayload<KContacts::ContactGroup>()) {
82  return createToolTip(item.payload<KContacts::ContactGroup>());
83  }
84  }
85  }
86 
87  return value;
88 }
89 
90 int EmailAddressSelectionProxyModel::leafRowCount(const QModelIndex &index) const
91 {
92  const auto item = index.data(ContactsTreeModel::ItemRole).value<Akonadi::Item>();
93  if (item.hasPayload<KContacts::Addressee>()) {
94  const auto contact = item.payload<KContacts::Addressee>();
95  if (contact.emails().count() == 1) {
96  return 0;
97  } else {
98  return contact.emails().count();
99  }
100  } else if (item.hasPayload<KContacts::ContactGroup>()) {
101  const auto group = item.payload<KContacts::ContactGroup>();
102  return group.dataCount();
103  } else {
104  return 0;
105  }
106 }
107 
108 int EmailAddressSelectionProxyModel::leafColumnCount(const QModelIndex &index) const
109 {
110  const auto item = index.data(ContactsTreeModel::ItemRole).value<Akonadi::Item>();
111  if (item.hasPayload<KContacts::Addressee>()) {
112  return 1;
113  } else if (item.hasPayload<KContacts::ContactGroup>()) {
114  return 1;
115  } else {
116  return 0;
117  }
118 }
119 
120 QVariant EmailAddressSelectionProxyModel::leafData(const QModelIndex &index, int row, int, int role) const
121 {
122  if (role == Qt::DisplayRole) {
123  const auto item = index.data(ContactsTreeModel::ItemRole).value<Akonadi::Item>();
124  if (item.hasPayload<KContacts::Addressee>()) {
125  const auto contact = item.payload<KContacts::Addressee>();
126  if (row >= 0 && row < contact.emails().count()) {
127  return contact.emails().at(row);
128  }
129  } else if (item.hasPayload<KContacts::ContactGroup>()) {
130  const auto group = item.payload<KContacts::ContactGroup>();
131  if (row >= 0 && row < (int)group.dataCount()) {
132  return i18nc("Name and email address of a contact", "%1 <%2>", group.data(row).name(), group.data(row).email());
133  }
134  }
135  } else if (role == NameRole) {
136  const auto item = index.data(ContactsTreeModel::ItemRole).value<Akonadi::Item>();
137  if (item.hasPayload<KContacts::Addressee>()) {
138  const auto contact = item.payload<KContacts::Addressee>();
139  return contact.realName();
140  } else if (item.hasPayload<KContacts::ContactGroup>()) {
141  const auto group = item.payload<KContacts::ContactGroup>();
142  if (row >= 0 && row < (int)group.dataCount()) {
143  return group.data(row).name();
144  }
145  }
146  } else if (role == EmailAddressRole) {
147  const auto item = index.data(ContactsTreeModel::ItemRole).value<Akonadi::Item>();
148  if (item.hasPayload<KContacts::Addressee>()) {
149  const auto contact = item.payload<KContacts::Addressee>();
150  if (row >= 0 && row < contact.emails().count()) {
151  return contact.emails().at(row);
152  }
153  } else if (item.hasPayload<KContacts::ContactGroup>()) {
154  const auto group = item.payload<KContacts::ContactGroup>();
155  if (row >= 0 && row < (int)group.dataCount()) {
156  return group.data(row).email();
157  }
158  }
159  } else if (role == Qt::ToolTipRole) {
160  const auto item = index.data(ContactsTreeModel::ItemRole).value<Akonadi::Item>();
161  if (item.hasPayload<KContacts::Addressee>()) {
162  const auto contact = item.payload<KContacts::Addressee>();
163  if (row >= 0 && row < contact.emails().count()) {
164  return createToolTip(contact.realName(), contact.emails().at(row));
165  }
166  } else if (item.hasPayload<KContacts::ContactGroup>()) {
167  const auto group = item.payload<KContacts::ContactGroup>();
168  if (row >= 0 && row < (int)group.dataCount()) {
169  return createToolTip(group.data(row).name(), group.data(row).email());
170  }
171  }
172  } else {
173  return index.data(role);
174  }
175 
176  return QVariant();
177 }
QStringList emails() const
const T & at(int i) const const
T value() const const
T payload() const
QString name() const
int count(const T &value) const const
QString i18nc(const char *context, const char *text, const TYPE &arg...)
ToolTipRole
bool isEmpty() const const
QString realName() const
QString preferredEmail() const
QString i18n(const char *text, const TYPE &arg...)
QVariant data(int role) const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
bool isValid() const const
Data & data(int index)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Jun 17 2021 23:09:02 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.