Akonadi Contacts

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

KDE's Doxygen guidelines are available online.