File indexing completed on 2025-02-16 04:45:49

0001 /*
0002    SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "contactgrantleewrapper.h"
0008 #include <KLocalizedString>
0009 
0010 #include <QBuffer>
0011 #include <QImage>
0012 #include <QLocale>
0013 
0014 using namespace KAddressBookGrantlee;
0015 
0016 static_assert(sizeof(KContacts::Addressee) == sizeof(KAddressBookGrantlee::ContactGrantleeWrapper),
0017               "Grantlee wrapper must not have member variables to prevent slicing issues");
0018 
0019 ContactGrantleeWrapper::ContactGrantleeWrapper() = default;
0020 
0021 ContactGrantleeWrapper::ContactGrantleeWrapper(const KContacts::Addressee &addr)
0022     : KContacts::Addressee(addr)
0023 {
0024 }
0025 
0026 QString ContactGrantleeWrapper::addressBookLabel() const
0027 {
0028     return i18n("Address Book");
0029 }
0030 
0031 QString ContactGrantleeWrapper::anniversaryLabel() const
0032 {
0033     return i18n("Anniversary");
0034 }
0035 
0036 QString ContactGrantleeWrapper::assistantLabel() const
0037 {
0038     return i18n("Assistant's Name");
0039 }
0040 
0041 QString ContactGrantleeWrapper::managerLabel() const
0042 {
0043     return i18n("Manager's Name");
0044 }
0045 
0046 QString ContactGrantleeWrapper::officeLabel() const
0047 {
0048     return i18n("Office");
0049 }
0050 
0051 QString ContactGrantleeWrapper::professionLabel() const
0052 {
0053     return i18n("Profession");
0054 }
0055 
0056 QString ContactGrantleeWrapper::spouseLabel() const
0057 {
0058     return i18nc("Wife/Husband/...", "Partner's Name");
0059 }
0060 
0061 QString ContactGrantleeWrapper::addressBookName() const
0062 {
0063     return custom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("AddressBook"));
0064 }
0065 
0066 int ContactGrantleeWrapper::age() const
0067 {
0068     const QDate now = QDate::currentDate();
0069     int age = now.year() - birthday().date().year();
0070     if (birthday().date() > now.addYears(-age)) {
0071         age--;
0072     }
0073     return age;
0074 }
0075 
0076 static QString imgToDataUrl(const QImage &image)
0077 {
0078     QByteArray ba;
0079     QBuffer buffer(&ba);
0080     buffer.open(QIODevice::WriteOnly);
0081     image.save(&buffer, "PNG");
0082     return QStringLiteral("data:image/%1;base64,%2").arg(QStringLiteral("PNG"), QString::fromLatin1(ba.toBase64()));
0083 }
0084 
0085 QString ContactGrantleeWrapper::logoImgElement() const
0086 {
0087     if (logo().isEmpty()) {
0088         return {};
0089     }
0090     return QStringLiteral("<img src=\"%1\" width=\"%2\" height=\"%3\"> &nbsp;").arg(imgToDataUrl(logo().data()), QString::number(60), QString::number(60));
0091 }
0092 
0093 QString ContactGrantleeWrapper::photoImgElement() const
0094 {
0095     if (photo().isEmpty()) {
0096         return {};
0097     }
0098     return QStringLiteral("<img src=\"%1\" width=\"%2\" height=\"%3\"> &nbsp;").arg(imgToDataUrl(photo().data()), QString::number(60), QString::number(60));
0099 }
0100 
0101 QString ContactGrantleeWrapper::formattedBirthday() const
0102 {
0103     return QLocale().toString(birthday().date());
0104 }
0105 
0106 QString ContactGrantleeWrapper::formattedAnniversary() const
0107 {
0108     return QLocale().toString(anniversary());
0109 }
0110 
0111 #include "moc_contactgrantleewrapper.cpp"