Akonadi Contacts

grantleecontactgroupformatter.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 "grantleecontactgroupformatter.h"
10
11#include <GrantleeTheme/GrantleeTheme>
12
13#include <KTextTemplate/Context>
14#include <KTextTemplate/Engine>
15#include <KTextTemplate/TemplateLoader>
16
17#include <Akonadi/ContactGroupExpandJob>
18#include <Akonadi/Item>
19
20#include <KColorScheme>
21
22using namespace KAddressBookGrantlee;
23
24class KAddressBookGrantlee::GrantleeContactGroupFormatterPrivate
25{
26public:
27 GrantleeContactGroupFormatterPrivate()
28 : mEngine(new KTextTemplate::Engine)
29 {
31 }
32
33 ~GrantleeContactGroupFormatterPrivate()
34 {
35 delete mEngine;
36 mTemplateLoader.clear();
37 }
38
39 void changeGrantleePath(const QString &path)
40 {
41 mTemplateLoader->setTemplateDirs(QStringList() << path);
42 mEngine->addTemplateLoader(mTemplateLoader);
43
44 mSelfcontainedTemplate = mEngine->loadByName(QStringLiteral("contactgroup.html"));
45 if (mSelfcontainedTemplate->error()) {
46 mErrorMessage += mSelfcontainedTemplate->errorString() + QStringLiteral("<br>");
47 }
48
49 mEmbeddableTemplate = mEngine->loadByName(QStringLiteral("contactgroup_embedded.html"));
50 if (mEmbeddableTemplate->error()) {
51 mErrorMessage += mEmbeddableTemplate->errorString() + QStringLiteral("<br>");
52 }
53 }
54
55 QList<QObject *> mObjects;
56 KTextTemplate::Engine *const mEngine;
58 KTextTemplate::Template mSelfcontainedTemplate;
59 KTextTemplate::Template mEmbeddableTemplate;
60 QString mErrorMessage;
61};
62
64 : d(new GrantleeContactGroupFormatterPrivate)
65{
66}
67
69
70void GrantleeContactGroupFormatter::setAbsoluteThemePath(const QString &path)
71{
72 d->changeGrantleePath(path);
73}
74
75void GrantleeContactGroupFormatter::setGrantleeTheme(const GrantleeTheme::Theme &theme)
76{
77 d->changeGrantleePath(theme.absolutePath());
78}
79
80inline static void setHashField(QVariantHash &hash, const QString &name, const QString &value)
81{
82 if (!value.isEmpty()) {
83 hash.insert(name, value);
84 }
85}
86
87static QVariantHash memberHash(const KContacts::ContactGroup::Data &data)
88{
89 QVariantHash memberObject;
90
91 setHashField(memberObject, QStringLiteral("name"), data.name());
92 setHashField(memberObject, QStringLiteral("email"), data.email());
93
95 contact.setFormattedName(data.name());
96 KContacts::Email email(data.email());
97 contact.addEmail(email);
98 const QString emailLink = QStringLiteral("<a href=\"mailto:") + QString::fromLatin1(QUrl::toPercentEncoding(contact.fullEmail()))
99 + QStringLiteral("\">%1</a>").arg(contact.preferredEmail());
100
101 setHashField(memberObject, QStringLiteral("emailLink"), emailLink);
102
103 return memberObject;
104}
105
107{
108 if (!d->mErrorMessage.isEmpty()) {
109 return d->mErrorMessage;
110 }
111
113 const Akonadi::Item localItem = item();
114 if (localItem.isValid() && localItem.hasPayload<KContacts::ContactGroup>()) {
115 group = localItem.payload<KContacts::ContactGroup>();
116 } else {
117 group = contactGroup();
118 }
119
120 if (group.name().isEmpty() && group.count() == 0) { // empty group
121 return {};
122 }
123
124 if (group.contactReferenceCount() != 0) {
125 // we got a contact group with unresolved references -> we have to resolve
126 // it ourself. this shouldn't be the normal case, actually the calling
127 // code should pass in an already resolved contact group
128 auto job = new Akonadi::ContactGroupExpandJob(group);
129 if (job->exec()) {
130 group.removeAllContactData();
131 const KContacts::Addressee::List lstContacts = job->contacts();
132 for (const KContacts::Addressee &contact : lstContacts) {
133 group.append(KContacts::ContactGroup::Data(contact.realName(), contact.preferredEmail()));
134 }
135 }
136 }
137
138 QVariantHash contactGroupObject;
139
140 setHashField(contactGroupObject, QStringLiteral("name"), group.name());
141
142 // Group members
143 QVariantList members;
144 const int numberOfData(group.dataCount());
145 members.reserve(numberOfData);
146 for (int i = 0; i < numberOfData; ++i) {
147 members << memberHash(group.data(i));
148 }
149
150 contactGroupObject.insert(QStringLiteral("members"), members);
151
152 // Additional fields
153 QVariantList fields;
154 for (int i = 0; i < additionalFields().size(); ++i) {
155 const QVariantMap field = additionalFields().at(i);
156 QVariantHash fieldObject;
157 setHashField(fieldObject, QStringLiteral("key"), field.value(QStringLiteral("key")).toString());
158
159 setHashField(fieldObject, QStringLiteral("title"), field.value(QStringLiteral("title")).toString());
160
161 setHashField(fieldObject, QStringLiteral("value"), field.value(QStringLiteral("value")).toString());
162
163 fields << fieldObject;
164 }
165
166 contactGroupObject.insert(QStringLiteral("additionalFields"), fields);
167
168 QVariantHash colorsObject;
169 colorsObject.insert(QStringLiteral("linkColor"), KColorScheme(QPalette::Active, KColorScheme::View).foreground().color().name());
170
171 colorsObject.insert(QStringLiteral("textColor"), KColorScheme(QPalette::Active, KColorScheme::View).foreground().color().name());
172
173 colorsObject.insert(QStringLiteral("backgroundColor"), KColorScheme(QPalette::Active, KColorScheme::View).background().color().name());
174
175 QVariantHash mapping;
176 mapping.insert(QStringLiteral("contactGroup"), contactGroupObject);
177 mapping.insert(QStringLiteral("colors"), colorsObject);
178 KTextTemplate::Context context(mapping);
179
180 if (form == SelfcontainedForm) {
181 return d->mSelfcontainedTemplate->render(&context);
182 } else if (form == EmbeddableForm) {
183 return d->mEmbeddableTemplate->render(&context);
184 } else {
185 return {};
186 }
187}
QList< QVariantMap > additionalFields() const
Returns the additional fields that will be shown.
Akonadi::Item item() const
Returns the item who's payload will be formatted.
KContacts::ContactGroup contactGroup() const
Returns the contact group that will be formatted.
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.
Job that expands a ContactGroup to a list of contacts.
T payload() const
bool isValid() const
bool hasPayload() const
GrantleeContactGroupFormatter()
Creates a new grantlee contact group formatter.
~GrantleeContactGroupFormatter() override
Destroys the grantlee contact group formatter.
QString toHtml(HtmlForm form=SelfcontainedForm) const override
Returns the contact group formatted as HTML.
void addEmail(const Email &email)
AddresseeList List
QString preferredEmail() const
QString realName() const
QString fullEmail(const QString &email=QString()) const
void setFormattedName(const QString &formattedName)
int contactReferenceCount() const
Data & data(int index)
void append(const ContactGroupReference &reference)
QString name() const
Template loadByName(const QString &name) const
void addTemplateLoader(QSharedPointer< AbstractTemplateLoader > loader)
QString errorString() const
const_reference at(qsizetype i) const const
qsizetype size() const const
QString arg(Args &&... args) const const
QString fromLatin1(QByteArrayView str)
bool isEmpty() const const
QByteArray toPercentEncoding(const QString &input, const QByteArray &exclude, const QByteArray &include)
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.