Akonadi Contacts

contactgrantleewrapper.cpp
1/*
2 SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
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
14using namespace KAddressBookGrantlee;
15
17 "Grantlee wrapper must not have member variables to prevent slicing issues");
18
19ContactGrantleeWrapper::ContactGrantleeWrapper() = default;
20
21ContactGrantleeWrapper::ContactGrantleeWrapper(const KContacts::Addressee &addr)
22 : KContacts::Addressee(addr)
23{
24}
25
26QString ContactGrantleeWrapper::addressBookLabel() const
27{
28 return i18n("Address Book");
29}
30
31QString ContactGrantleeWrapper::anniversaryLabel() const
32{
33 return i18n("Anniversary");
34}
35
36QString ContactGrantleeWrapper::assistantLabel() const
37{
38 return i18n("Assistant's Name");
39}
40
41QString ContactGrantleeWrapper::managerLabel() const
42{
43 return i18n("Manager's Name");
44}
45
46QString ContactGrantleeWrapper::officeLabel() const
47{
48 return i18n("Office");
49}
50
51QString ContactGrantleeWrapper::professionLabel() const
52{
53 return i18n("Profession");
54}
55
56QString ContactGrantleeWrapper::spouseLabel() const
57{
58 return i18nc("Wife/Husband/...", "Partner's Name");
59}
60
61QString ContactGrantleeWrapper::addressBookName() const
62{
63 return custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("AddressBook"));
64}
65
66int 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
76static 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
85QString 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
93QString 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
101QString ContactGrantleeWrapper::formattedBirthday() const
102{
103 return QLocale().toString(birthday().date());
104}
105
106QString ContactGrantleeWrapper::formattedAnniversary() const
107{
108 return QLocale().toString(anniversary());
109}
110
111#include "moc_contactgrantleewrapper.cpp"
Additional properties for the KContacts::Addressee Grantlee model.
QString custom(const QString &app, const QString &name) const
QDateTime birthday() const
bool isEmpty() const
QDate anniversary() const
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
QByteArray toBase64(Base64Options options) const const
QDate addYears(int nyears) const const
QDate currentDate()
int year() const const
QDate date() const const
bool save(QIODevice *device, const char *format, int quality) const const
QString toString(QDate date, FormatType format) const const
QString arg(Args &&... args) const const
QString fromLatin1(QByteArrayView str)
QString number(double n, char format, int precision)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:08 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.