Akonadi Contacts

contactgrantleewrapper.cpp
1 /*
2  SPDX-FileCopyrightText: 2019 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "contactgrantleewrapper.h"
8 #include <KLocalizedString>
9 
10 #include <QBuffer>
11 #include <QImage>
12 #include <QLocale>
13 
14 using namespace KAddressBookGrantlee;
15 
17  "Grantlee wrapper must not have member variables to prevent slicing issues");
18 
19 ContactGrantleeWrapper::ContactGrantleeWrapper() = default;
20 
21 ContactGrantleeWrapper::ContactGrantleeWrapper(const KContacts::Addressee &addr)
22  : KContacts::Addressee(addr)
23 {
24 }
25 
26 QString ContactGrantleeWrapper::addressBookLabel() const
27 {
28  return i18n("Address Book");
29 }
30 
31 QString ContactGrantleeWrapper::anniversaryLabel() const
32 {
33  return i18n("Anniversary");
34 }
35 
36 QString ContactGrantleeWrapper::assistantLabel() const
37 {
38  return i18n("Assistant's Name");
39 }
40 
41 QString ContactGrantleeWrapper::managerLabel() const
42 {
43  return i18n("Manager's Name");
44 }
45 
46 QString ContactGrantleeWrapper::officeLabel() const
47 {
48  return i18n("Office");
49 }
50 
51 QString ContactGrantleeWrapper::professionLabel() const
52 {
53  return i18n("Profession");
54 }
55 
56 QString ContactGrantleeWrapper::spouseLabel() const
57 {
58  return i18nc("Wife/Husband/...", "Partner's Name");
59 }
60 
61 QString ContactGrantleeWrapper::addressBookName() const
62 {
63  return custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("AddressBook"));
64 }
65 
66 int ContactGrantleeWrapper::age() const
67 {
68  const QDate now = QDate::currentDate();
69  int age = now.year() - birthday().date().year();
70  if (birthday().date() > now.addYears(-age)) {
71  age--;
72  }
73  return age;
74 }
75 
76 static QString imgToDataUrl(const QImage &image)
77 {
78  QByteArray ba;
79  QBuffer buffer(&ba);
80  buffer.open(QIODevice::WriteOnly);
81  image.save(&buffer, "PNG");
82  return QStringLiteral("data:image/%1;base64,%2").arg(QStringLiteral("PNG"), QString::fromLatin1(ba.toBase64()));
83 }
84 
85 QString ContactGrantleeWrapper::logoImgElement() const
86 {
87  if (logo().isEmpty()) {
88  return {};
89  }
90  return QStringLiteral("<img src=\"%1\" width=\"%2\" height=\"%3\"> &nbsp;").arg(imgToDataUrl(logo().data()), QString::number(60), QString::number(60));
91 }
92 
93 QString ContactGrantleeWrapper::photoImgElement() const
94 {
95  if (photo().isEmpty()) {
96  return {};
97  }
98  return QStringLiteral("<img src=\"%1\" width=\"%2\" height=\"%3\"> &nbsp;").arg(imgToDataUrl(photo().data()), QString::number(60), QString::number(60));
99 }
100 
101 QString ContactGrantleeWrapper::formattedBirthday() const
102 {
103  return QLocale().toString(birthday().date());
104 }
105 
106 QString ContactGrantleeWrapper::formattedAnniversary() const
107 {
108  return QLocale().toString(anniversary());
109 }
110 
111 #include "moc_contactgrantleewrapper.cpp"
QString number(int n, int base)
QString custom(const QString &app, const QString &name) const
int year() const const
QByteArray toBase64(QByteArray::Base64Options options) const const
bool isEmpty() const
QString i18n(const char *text, const TYPE &arg...)
QDateTime birthday() const
QString toString(qlonglong i) const const
QDate currentDate()
QDate addYears(int nyears) const const
QDate anniversary() const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString fromLatin1(const char *str, int size)
QDate date() const const
Additional properties for the KContacts::Addressee Grantlee model.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
bool save(const QString &fileName, const char *format, int quality) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sat Apr 1 2023 04:09:04 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.