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 <item.h>
13 #include <kcontacts/addressee.h>
14 #include <kcontacts/contactgroup.h>
15 #include <KLocalizedString>
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>")
42  .arg(name.isEmpty() ? QString() : name + QLatin1String("<br/>"))
43  .arg(email);
44 }
45 
46 EmailAddressSelectionProxyModel::EmailAddressSelectionProxyModel(QObject *parent)
47  : LeafExtensionProxyModel(parent)
48 {
49 }
50 
51 EmailAddressSelectionProxyModel::~EmailAddressSelectionProxyModel()
52 {
53 }
54 
55 QVariant EmailAddressSelectionProxyModel::data(const QModelIndex &index, int role) const
56 {
57  const QVariant value = LeafExtensionProxyModel::data(index, role);
58 
59  if (!value.isValid()) { // index is not a leaf child
60  if (role == NameRole) {
61  const Akonadi::Item item = index.data(ContactsTreeModel::ItemRole).value<Akonadi::Item>();
62  if (item.hasPayload<KContacts::Addressee>()) {
63  const KContacts::Addressee contact = item.payload<KContacts::Addressee>();
64  return contact.realName();
65  } else if (item.hasPayload<KContacts::ContactGroup>()) {
66  const KContacts::ContactGroup group = item.payload<KContacts::ContactGroup>();
67  return group.name();
68  }
69  } else if (role == EmailAddressRole) {
70  const Akonadi::Item item = index.data(ContactsTreeModel::ItemRole).value<Akonadi::Item>();
71  if (item.hasPayload<KContacts::Addressee>()) {
72  const KContacts::Addressee contact = item.payload<KContacts::Addressee>();
73  return contact.preferredEmail();
74  } else if (item.hasPayload<KContacts::ContactGroup>()) {
75  const KContacts::ContactGroup group = item.payload<KContacts::ContactGroup>();
76  return group.name(); // the name must be resolved by the caller
77  }
78  } else if (role == Qt::ToolTipRole) {
79  const Akonadi::Item item = index.data(ContactsTreeModel::ItemRole).value<Akonadi::Item>();
80  if (item.hasPayload<KContacts::Addressee>()) {
81  const KContacts::Addressee contact = item.payload<KContacts::Addressee>();
82  return createToolTip(contact.realName(), contact.preferredEmail());
83  } else if (item.hasPayload<KContacts::ContactGroup>()) {
84  return createToolTip(item.payload<KContacts::ContactGroup>());
85  }
86  }
87  }
88 
89  return value;
90 }
91 
92 int EmailAddressSelectionProxyModel::leafRowCount(const QModelIndex &index) const
93 {
94  const Akonadi::Item item = index.data(ContactsTreeModel::ItemRole).value<Akonadi::Item>();
95  if (item.hasPayload<KContacts::Addressee>()) {
96  const KContacts::Addressee contact = item.payload<KContacts::Addressee>();
97  if (contact.emails().count() == 1) {
98  return 0;
99  } else {
100  return contact.emails().count();
101  }
102  } else if (item.hasPayload<KContacts::ContactGroup>()) {
103  const KContacts::ContactGroup group = item.payload<KContacts::ContactGroup>();
104  return group.dataCount();
105  } else {
106  return 0;
107  }
108 }
109 
110 int EmailAddressSelectionProxyModel::leafColumnCount(const QModelIndex &index) const
111 {
112  const Akonadi::Item item = index.data(ContactsTreeModel::ItemRole).value<Akonadi::Item>();
113  if (item.hasPayload<KContacts::Addressee>()) {
114  return 1;
115  } else if (item.hasPayload<KContacts::ContactGroup>()) {
116  return 1;
117  } else {
118  return 0;
119  }
120 }
121 
122 QVariant EmailAddressSelectionProxyModel::leafData(const QModelIndex &index, int row, int, int role) const
123 {
124  if (role == Qt::DisplayRole) {
125  const Akonadi::Item item = index.data(ContactsTreeModel::ItemRole).value<Akonadi::Item>();
126  if (item.hasPayload<KContacts::Addressee>()) {
127  const KContacts::Addressee contact = item.payload<KContacts::Addressee>();
128  if (row >= 0 && row < contact.emails().count()) {
129  return contact.emails().at(row);
130  }
131  } else if (item.hasPayload<KContacts::ContactGroup>()) {
132  const KContacts::ContactGroup group = item.payload<KContacts::ContactGroup>();
133  if (row >= 0 && row < (int)group.dataCount()) {
134  return i18nc("Name and email address of a contact", "%1 <%2>",
135  group.data(row).name(), group.data(row).email());
136  }
137  }
138  } else if (role == NameRole) {
139  const Akonadi::Item item = index.data(ContactsTreeModel::ItemRole).value<Akonadi::Item>();
140  if (item.hasPayload<KContacts::Addressee>()) {
141  const KContacts::Addressee contact = item.payload<KContacts::Addressee>();
142  return contact.realName();
143  } else if (item.hasPayload<KContacts::ContactGroup>()) {
144  const KContacts::ContactGroup group = item.payload<KContacts::ContactGroup>();
145  if (row >= 0 && row < (int)group.dataCount()) {
146  return group.data(row).name();
147  }
148  }
149  } else if (role == EmailAddressRole) {
150  const Akonadi::Item item = index.data(ContactsTreeModel::ItemRole).value<Akonadi::Item>();
151  if (item.hasPayload<KContacts::Addressee>()) {
152  const KContacts::Addressee contact = item.payload<KContacts::Addressee>();
153  if (row >= 0 && row < contact.emails().count()) {
154  return contact.emails().at(row);
155  }
156  } else if (item.hasPayload<KContacts::ContactGroup>()) {
157  const KContacts::ContactGroup group = item.payload<KContacts::ContactGroup>();
158  if (row >= 0 && row < (int)group.dataCount()) {
159  return group.data(row).email();
160  }
161  }
162  } else if (role == Qt::ToolTipRole) {
163  const Akonadi::Item item = index.data(ContactsTreeModel::ItemRole).value<Akonadi::Item>();
164  if (item.hasPayload<KContacts::Addressee>()) {
165  const KContacts::Addressee contact = item.payload<KContacts::Addressee>();
166  if (row >= 0 && row < contact.emails().count()) {
167  return createToolTip(contact.realName(), contact.emails().at(row));
168  }
169  } else if (item.hasPayload<KContacts::ContactGroup>()) {
170  const KContacts::ContactGroup group = item.payload<KContacts::ContactGroup>();
171  if (row >= 0 && row < (int)group.dataCount()) {
172  return createToolTip(group.data(row).name(), group.data(row).email());
173  }
174  }
175  } else {
176  return index.data(role);
177  }
178 
179  return QVariant();
180 }
QStringList emails() const
const T & at(int i) const const
T value() const 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-2020 The KDE developers.
Generated on Wed Sep 23 2020 23:08:59 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.