Akonadi Contacts

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

KDE's Doxygen guidelines are available online.