Akonadi Contacts

grantleecontactformatter.cpp
1/*
2 This file is part of KAddressBook.
3
4 SPDX-FileCopyrightText: 2010 Tobias Koenig <tokoe@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "grantleecontactformatter.h"
10#include "contactgrantleewrapper.h"
11
12#include <KContacts/Addressee>
13
14#include <GrantleeTheme/GrantleeKi18nLocalizer>
15#include <GrantleeTheme/GrantleeTheme>
16#include <GrantleeTheme/GrantleeThemeEngine>
17#include <KTextTemplate/Context>
18#include <KTextTemplate/MetaType>
19#include <KTextTemplate/TemplateLoader>
20
21#include <Akonadi/Item>
22
23#include <KColorScheme>
24
25#include <KConfigGroup>
26#include <KLocalizedString>
27#include <KStringHandler>
28
29#include <QLocale>
30#include <QMetaProperty>
31#include <QSet>
32
33using namespace KAddressBookGrantlee;
34
36if (property == QLatin1StringView("scheme")) {
37 return object.scheme();
38} else if (property == QLatin1StringView("path")) {
39 return object.path();
40}
42
43class KAddressBookGrantlee::GrantleeContactFormatterPrivate
44{
45public:
46 GrantleeContactFormatterPrivate()
47 {
48 KConfig config(QStringLiteral("akonadi_contactrc"));
49 KConfigGroup group(&config, QStringLiteral("View"));
50 showQRCode = group.readEntry("QRCodes", true);
51 mEngine = std::make_unique<GrantleeTheme::Engine>();
53 }
54
55 ~GrantleeContactFormatterPrivate()
56 {
57 mTemplateLoader.clear();
58 }
59
60 void changeGrantleePath(const QString &path)
61 {
62 mTemplateLoader->setTemplateDirs(QStringList() << path);
63 mEngine->addTemplateLoader(mTemplateLoader);
64
65 mSelfcontainedTemplate = mEngine->loadByName(QStringLiteral("contact.html"));
66 if (mSelfcontainedTemplate->error()) {
67 mErrorMessage += mSelfcontainedTemplate->errorString() + QStringLiteral("<br>");
68 }
69
70 mEmbeddableTemplate = mEngine->loadByName(QStringLiteral("contact_embedded.html"));
71 if (mEmbeddableTemplate->error()) {
72 mErrorMessage += mEmbeddableTemplate->errorString() + QStringLiteral("<br>");
73 }
74 }
75
76 QList<QObject *> mObjects;
77 std::unique_ptr<GrantleeTheme::Engine> mEngine;
79 KTextTemplate::Template mSelfcontainedTemplate;
80 KTextTemplate::Template mEmbeddableTemplate;
81 QString mErrorMessage;
82 bool forceDisableQRCode = false;
83 bool showQRCode = true;
84};
85
87 : d(new GrantleeContactFormatterPrivate)
88{
90}
91
93
94void GrantleeContactFormatter::setAbsoluteThemePath(const QString &path)
95{
96 d->changeGrantleePath(path);
97}
98
99void GrantleeContactFormatter::setGrantleeTheme(const GrantleeTheme::Theme &theme)
100{
101 d->changeGrantleePath(theme.absolutePath());
102}
103
104void GrantleeContactFormatter::setForceDisableQRCode(bool b)
105{
106 d->forceDisableQRCode = b;
107}
108
109bool GrantleeContactFormatter::forceDisableQRCode() const
110{
111 return d->forceDisableQRCode;
112}
113
114void GrantleeContactFormatter::setShowQRCode(bool b)
115{
116 d->showQRCode = b;
117}
118
120{
121 if (!d->mErrorMessage.isEmpty()) {
122 return d->mErrorMessage;
123 }
124
125 KContacts::Addressee rawContact;
126 const Akonadi::Item localItem = item();
127 if (localItem.isValid() && localItem.hasPayload<KContacts::Addressee>()) {
128 rawContact = localItem.payload<KContacts::Addressee>();
129 } else {
130 rawContact = contact();
131 }
132
133 if (rawContact.isEmpty()) {
134 return {};
135 }
136
137 // Custom fields
138 QVariantList customFields;
139 QVariantList customFieldsUrl;
140 static QSet<QString> blacklistedKeys = {QStringLiteral("CRYPTOPROTOPREF"),
141 QStringLiteral("OPENPGPFP"),
142 QStringLiteral("SMIMEFP"),
143 QStringLiteral("CRYPTOSIGNPREF"),
144 QStringLiteral("CRYPTOENCRYPTPREF"),
145 QStringLiteral("Anniversary"),
146 QStringLiteral("BlogFeed"),
147 QStringLiteral("Profession"),
148 QStringLiteral("Office"),
149 QStringLiteral("ManagersName"),
150 QStringLiteral("AssistantsName"),
151 QStringLiteral("SpousesName"),
152 QStringLiteral("IMAddress"),
153 QStringLiteral("AddressBook"),
154 QStringLiteral("MailPreferedFormatting"),
155 QStringLiteral("MailAllowToRemoteContent"),
156 QStringLiteral("MAILPREFEREDFORMATTING"),
157 QStringLiteral("MAILALLOWTOREMOTECONTENT")};
158
159 const auto customs = rawContact.customs();
160 for (QString custom : customs) {
161 if (custom.startsWith(QLatin1StringView("KADDRESSBOOK-"))) {
162 custom.remove(QStringLiteral("KADDRESSBOOK-X-"));
163 custom.remove(QStringLiteral("KADDRESSBOOK-"));
164
165 int pos = custom.indexOf(QLatin1Char(':'));
166 QString key = custom.left(pos);
167 QString value = custom.mid(pos + 1);
168
169 if (blacklistedKeys.contains(key)) {
170 continue;
171 }
172
173 bool addUrl = false;
174 // check whether it is a custom local field
175 for (int i = 0; i < customFieldDescriptions().size(); ++i) {
176 const QVariantMap description = customFieldDescriptions().at(i);
177 if (description.value(QStringLiteral("key")).toString() == key) {
178 key = description.value(QStringLiteral("title")).toString();
179 const QString descriptionType = description.value(QStringLiteral("type")).toString();
180 if (descriptionType == QLatin1StringView("boolean")) {
181 if (value == QLatin1StringView("true")) {
182 value = i18nc("Boolean value", "yes");
183 } else {
184 value = i18nc("Boolean value", "no");
185 }
186 } else if (descriptionType == QLatin1StringView("date")) {
187 const QDate date = QDate::fromString(value, Qt::ISODate);
188 value = QLocale().toString(date, QLocale::ShortFormat);
189 } else if (descriptionType == QLatin1StringView("time")) {
190 const QTime time = QTime::fromString(value, Qt::ISODate);
192 } else if (descriptionType == QLatin1StringView("datetime")) {
193 const QDateTime dateTime = QDateTime::fromString(value, Qt::ISODate);
194 value = QLocale().toString(dateTime, QLocale::ShortFormat);
195 } else if (descriptionType == QLatin1StringView("url")) {
196 value = KStringHandler::tagUrls(value.toHtmlEscaped());
197 addUrl = true;
198 }
199 break;
200 }
201 }
202 QVariantHash customFieldObject;
203 customFieldObject.insert(QStringLiteral("title"), key);
204 customFieldObject.insert(QStringLiteral("value"), value);
205
206 if (addUrl) {
207 customFieldsUrl.append(customFieldObject);
208 } else {
209 customFields.append(customFieldObject);
210 }
211 }
212 }
213
214 QVariantHash colorsObject;
215
216 colorsObject.insert(QStringLiteral("linkColor"), KColorScheme(QPalette::Active, KColorScheme::View).foreground().color().name());
217
218 colorsObject.insert(QStringLiteral("textColor"), KColorScheme(QPalette::Active, KColorScheme::View).foreground().color().name());
219
220 colorsObject.insert(QStringLiteral("backgroundColor"), KColorScheme(QPalette::Active, KColorScheme::View).background().color().name());
221
222 QVariantHash mapping;
223 mapping.insert(QStringLiteral("contact"), QVariant::fromValue(ContactGrantleeWrapper(rawContact)));
224 mapping.insert(QStringLiteral("colors"), colorsObject);
225 mapping.insert(QStringLiteral("customFields"), customFields);
226 mapping.insert(QStringLiteral("customFieldsUrl"), customFieldsUrl);
227 mapping.insert(QStringLiteral("hasqrcode"), !d->forceDisableQRCode && d->showQRCode);
228 KTextTemplate::Context context(mapping);
229 context.setLocalizer(d->mEngine->localizer());
230
231 if (form == SelfcontainedForm) {
232 return d->mSelfcontainedTemplate->render(&context);
233 } else if (form == EmbeddableForm) {
234 return d->mEmbeddableTemplate->render(&context);
235 } else {
236 return {};
237 }
238}
239
241{
242 d->mEngine->localizer()->setApplicationDomain(domain);
243}
KContacts::Addressee contact() const
Returns the contact that will be formatted.
Akonadi::Item item() const
Returns the item who's payload will be formatted.
QList< QVariantMap > customFieldDescriptions() const
Returns the custom field descriptions that will be used.
HtmlForm
Describes the form of the HTML that is created.
@ EmbeddableForm
Creates a div HTML element that can be embedded.
@ SelfcontainedForm
Creates a complete HTML document.
bool hasPayload() const
T payload() const
bool isValid() const
Additional properties for the KContacts::Addressee Grantlee model.
GrantleeContactFormatter()
Creates a new grantlee contact formatter.
~GrantleeContactFormatter() override
Destroys the grantlee contact formatter.
void setApplicationDomain(const QByteArray &domain)
Translation domain for the Grantlee localizer.
QString toHtml(HtmlForm form=SelfcontainedForm) const override
Returns the contact formatted as HTML.
QStringList customs() const
bool isEmpty() const
void setLocalizer(QSharedPointer< AbstractLocalizer > localizer)
QString errorString() const
QString i18nc(const char *context, const char *text, const TYPE &arg...)
#define KTEXTTEMPLATE_END_LOOKUP
#define KTEXTTEMPLATE_BEGIN_LOOKUP(Type)
KCOREADDONS_EXPORT QString tagUrls(const QString &text)
int registerMetaType()
QDate fromString(QStringView string, QStringView format, QCalendar cal)
QDateTime fromString(QStringView string, QStringView format, QCalendar cal)
const_reference at(qsizetype i) const const
iterator insert(const_iterator before, parameter_type value)
qsizetype size() const const
QLocale system()
QString toString(QDate date, FormatType format) const const
bool contains(const QSet< T > &other) const const
QString & insert(qsizetype position, QChar ch)
QString left(qsizetype n) const const
QString mid(qsizetype position, qsizetype n) const const
QString toHtmlEscaped() const const
QTime fromString(QStringView string, QStringView format)
QVariant fromValue(T &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:08:53 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.