Akonadi Contacts

contactstreemodel.cpp
1/*
2 This file is part of Akonadi Contact.
3
4 SPDX-FileCopyrightText: 2009 Stephen Kelly <steveire@gmail.com>
5 SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
6
7 SPDX-License-Identifier: LGPL-2.0-or-later
8*/
9
10#include "contactstreemodel.h"
11
12#include <KContacts/Addressee>
13#include <KContacts/ContactGroup>
14
15#include <KIconLoader>
16#include <KLocalizedString>
17
18#include <QIcon>
19#include <QLocale>
20
21using namespace Akonadi;
22
23class Akonadi::ContactsTreeModelPrivate
24{
25public:
26 ContactsTreeModelPrivate()
27 : mColumns(ContactsTreeModel::Columns() << ContactsTreeModel::FullName)
28 , mIconSize(KIconLoader::global()->currentSize(KIconLoader::Small))
29 {
30 }
31
33 const int mIconSize;
34};
35
37 : EntityTreeModel(monitor, parent)
38 , d(new ContactsTreeModelPrivate)
39{
40}
41
43
45{
47 d->mColumns = columns;
49}
50
52{
53 return d->mColumns;
54}
55
56QVariant ContactsTreeModel::entityData(const Item &item, int column, int role) const
57{
59 if (!item.hasPayload<KContacts::Addressee>()) {
60 // Pass modeltest
61 if (role == Qt::DisplayRole) {
62 return item.remoteId();
63 }
64
65 return {};
66 }
67
68 const auto contact = item.payload<KContacts::Addressee>();
69
70 if (role == Qt::DecorationRole) {
71 if (column == 0) {
72 const KContacts::Picture picture = contact.photo();
73 if (picture.isIntern()) {
74 return picture.data().scaled(QSize(d->mIconSize, d->mIconSize), Qt::KeepAspectRatio);
75 } else {
76 return QIcon::fromTheme(QStringLiteral("user-identity"));
77 }
78 }
79 return {};
80 } else if ((role == Qt::DisplayRole) || (role == Qt::EditRole)) {
81 switch (d->mColumns.at(column)) {
82 case FullName:
83 if (contact.realName().isEmpty()) {
84 if (contact.preferredEmail().isEmpty()) {
85 return contact.familyName();
86 }
87 return contact.preferredEmail();
88 }
89 return contact.realName();
90 case FamilyName:
91 return contact.familyName();
92 case GivenName:
93 return contact.givenName();
94 case Birthday:
95 if (contact.birthday().date().isValid()) {
96 return QLocale().toString(contact.birthday().date(), QLocale::ShortFormat);
97 }
98 break;
99 case HomeAddress: {
100 const KContacts::Address address = contact.address(KContacts::Address::Home);
101 if (!address.isEmpty()) {
102 return address.formatted(KContacts::AddressFormatStyle::Postal);
103 }
104 break;
105 }
106 case BusinessAddress: {
107 const KContacts::Address address = contact.address(KContacts::Address::Work);
108 if (!address.isEmpty()) {
109 return address.formatted(KContacts::AddressFormatStyle::Postal);
110 }
111 break;
112 }
113 case PhoneNumbers: {
114 QStringList values;
115
116 const KContacts::PhoneNumber::List numbers = contact.phoneNumbers();
117 values.reserve(numbers.count());
118 for (const KContacts::PhoneNumber &number : numbers) {
119 values += number.number();
120 }
121
122 return values.join(QLatin1Char('\n'));
123 break;
124 }
125 case PreferredEmail:
126 return contact.preferredEmail();
127 case AllEmails:
128 return contact.emails().join(QLatin1Char('\n'));
129 case Organization:
130 return contact.organization();
131 case Role:
132 return contact.role();
133 case Homepage:
134 return contact.url().url();
135 case Note:
136 return contact.note();
137 }
138 } else if (role == DateRole) {
139 if (d->mColumns.at(column) == Birthday) {
140 return contact.birthday();
141 } else {
142 return QDate();
143 }
144 }
145 } else if (item.mimeType() == KContacts::ContactGroup::mimeType()) {
146 if (!item.hasPayload<KContacts::ContactGroup>()) {
147 // Pass modeltest
148 if (role == Qt::DisplayRole) {
149 return item.remoteId();
150 }
151
152 return {};
153 }
154
155 if (role == Qt::DecorationRole) {
156 if (column == 0) {
157 return QIcon::fromTheme(QStringLiteral("x-mail-distribution-list"));
158 } else {
159 return {};
160 }
161 } else if ((role == Qt::DisplayRole) || (role == Qt::EditRole)) {
162 switch (d->mColumns.at(column)) {
163 case FullName: {
164 const auto group = item.payload<KContacts::ContactGroup>();
165 return group.name();
166 }
167 default:
168 return {};
169 }
170 }
171 }
172
173 return EntityTreeModel::entityData(item, column, role);
174}
175
176QVariant ContactsTreeModel::entityData(const Collection &collection, int column, int role) const
177{
178 if (role == Qt::DisplayRole) {
179 switch (column) {
180 case 0:
181 return EntityTreeModel::entityData(collection, column, role);
182 default:
183 return QString(); // pass model test
184 }
185 }
186
187 return EntityTreeModel::entityData(collection, column, role);
188}
189
190int ContactsTreeModel::entityColumnCount(HeaderGroup headerGroup) const
191{
192 if (headerGroup == EntityTreeModel::CollectionTreeHeaders) {
193 return 1;
194 } else if (headerGroup == EntityTreeModel::ItemListHeaders) {
195 return d->mColumns.count();
196 } else {
197 return EntityTreeModel::entityColumnCount(headerGroup);
198 }
199}
200
201QVariant ContactsTreeModel::entityHeaderData(int section, Qt::Orientation orientation, int role, HeaderGroup headerGroup) const
202{
203 if (role == Qt::DisplayRole) {
204 if (orientation == Qt::Horizontal) {
205 if (headerGroup == EntityTreeModel::CollectionTreeHeaders) {
206 if (section >= 1) {
207 return {};
208 }
209
210 switch (section) {
211 case 0:
212 return i18nc("@title:column address books overview", "Address Books");
213 break;
214 }
215 } else if (headerGroup == EntityTreeModel::ItemListHeaders) {
216 if (section < 0 || section >= d->mColumns.count()) {
217 return {};
218 }
219
220 switch (d->mColumns.at(section)) {
221 case FullName:
222 return i18nc("@title:column name of a person", "Name");
223 case FamilyName:
224 return i18nc("@title:column family name of a person", "Family Name");
225 case GivenName:
226 return i18nc("@title:column given name of a person", "Given Name");
227 case Birthday:
229 case HomeAddress:
230 return i18nc("@title:column home address of a person", "Home");
231 case BusinessAddress:
232 return i18nc("@title:column work address of a person", "Work");
233 case PhoneNumbers:
234 return i18nc("@title:column phone numbers of a person", "Phone Numbers");
235 case PreferredEmail:
236 return i18nc("@title:column the preferred email addresses of a person", "Preferred EMail");
237 case AllEmails:
238 return i18nc("@title:column all email addresses of a person", "All EMails");
239 case Organization:
241 case Role:
243 case Homepage:
245 case Note:
247 }
248 }
249 }
250 }
251
252 return EntityTreeModel::entityHeaderData(section, orientation, role, headerGroup);
253}
254
255#include "moc_contactstreemodel.cpp"
void setColumns(const Columns &columns)
Sets the columns that the model should show.
~ContactsTreeModel() override
Destroys the contacts tree model.
ContactsTreeModel(Monitor *monitor, QObject *parent=nullptr)
Creates a new contacts tree model.
Columns columns() const
Returns the columns that the model currently shows.
QList< Column > Columns
Describes a list of columns of the contacts tree model.
@ PreferredEmail
Shows the preferred email address.
@ GivenName
Shows the given name.
@ Homepage
Shows homepage url.
@ HomeAddress
Shows the formatted home address.
@ Role
Shows the role of a contact.
@ BusinessAddress
Shows the formatted business address.
@ Birthday
Shows the birthday.
@ FamilyName
Shows the family name.
@ AllEmails
Shows all email address.
@ PhoneNumbers
Shows the phone numbers.
@ FullName
Shows the formatted name or, if empty, the assembled name.
@ Organization
Shows organization name.
@ DateRole
The QDate object for the current index.
EntityTreeModel(Monitor *monitor, QObject *parent=nullptr)
virtual QVariant entityData(const Collection &collection, int column, int role=Qt::DisplayRole) const
virtual QVariant entityHeaderData(int section, Qt::Orientation orientation, int role, HeaderGroup headerGroup) const
QString mimeType() const
bool hasPayload() const
T payload() const
QString remoteId() const
static QString noteLabel()
static QString organizationLabel()
static QString mimeType()
static QString urlLabel()
static QString roleLabel()
static QString birthdayLabel()
static QString mimeType()
QList< PhoneNumber > List
QImage data() const
bool isIntern() const
QString i18nc(const char *context, const char *text, const TYPE &arg...)
A widget for editing the display name of a contact.
KIOCORE_EXPORT QString number(KIO::filesize_t size)
PostalAddress address(const QVariant &location)
virtual QModelIndex parent(const QModelIndex &index) const const=0
QIcon fromTheme(const QString &name)
QImage scaled(const QSize &size, Qt::AspectRatioMode aspectRatioMode, Qt::TransformationMode transformMode) const const
qsizetype count() const const
void reserve(qsizetype size)
QObject(QObject *parent)
QString number(double n, char format, int precision)
QString join(QChar separator) const const
KeepAspectRatio
DisplayRole
Orientation
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:55:33 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.