Akonadi Contacts

grantleecontactgroupformatter.cpp
1 /*
2  This file is part of KAddressBook.
3 
4  SPDX-FileCopyrightText: 2010 Tobias Koenig <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #include "grantleecontactgroupformatter.h"
10 
11 #include <GrantleeTheme/GrantleeTheme>
12 
13 #include <grantlee/context.h>
14 #include <grantlee/engine.h>
15 #include <grantlee/templateloader.h>
16 
17 #include <Akonadi/Contact/ContactGroupExpandJob>
18 #include <AkonadiCore/Item>
19 
20 #include <KColorScheme>
21 
22 using namespace KAddressBookGrantlee;
23 
24 class Q_DECL_HIDDEN GrantleeContactGroupFormatter::Private
25 {
26 public:
27  Private()
28  : mEngine(new Grantlee::Engine)
29  {
30 
31  mTemplateLoader = QSharedPointer<Grantlee::FileSystemTemplateLoader>(new Grantlee::FileSystemTemplateLoader);
32  }
33 
34  ~Private()
35  {
36  delete mEngine;
37  mTemplateLoader.clear();
38  }
39 
40  void changeGrantleePath(const QString &path)
41  {
42  mTemplateLoader->setTemplateDirs(QStringList() << path);
43  mEngine->addTemplateLoader(mTemplateLoader);
44 
45  mSelfcontainedTemplate = mEngine->loadByName(QStringLiteral("contactgroup.html"));
46  if (mSelfcontainedTemplate->error()) {
47  mErrorMessage += mSelfcontainedTemplate->errorString() + QStringLiteral("<br>");
48  }
49 
50  mEmbeddableTemplate = mEngine->loadByName(QStringLiteral("contactgroup_embedded.html"));
51  if (mEmbeddableTemplate->error()) {
52  mErrorMessage += mEmbeddableTemplate->errorString() + QStringLiteral("<br>");
53  }
54  }
55 
56  QVector<QObject *> mObjects;
57  Grantlee::Engine *const mEngine;
59  Grantlee::Template mSelfcontainedTemplate;
60  Grantlee::Template mEmbeddableTemplate;
61  QString mErrorMessage;
62 };
63 
65  : d(new Private)
66 {
67 }
68 
70 {
71  delete d;
72 }
73 
74 void GrantleeContactGroupFormatter::setAbsoluteThemePath(const QString &path)
75 {
76  d->changeGrantleePath(path);
77 }
78 
79 void GrantleeContactGroupFormatter::setGrantleeTheme(const GrantleeTheme::Theme &theme)
80 {
81  d->changeGrantleePath(theme.absolutePath());
82 }
83 
84 inline static void setHashField(QVariantHash &hash, const QString &name, const QString &value)
85 {
86  if (!value.isEmpty()) {
87  hash.insert(name, value);
88  }
89 }
90 
91 static QVariantHash memberHash(const KContacts::ContactGroup::Data &data)
92 {
93  QVariantHash memberObject;
94 
95  setHashField(memberObject, QStringLiteral("name"), data.name());
96  setHashField(memberObject, QStringLiteral("email"), data.email());
97 
98  KContacts::Addressee contact;
99  contact.setFormattedName(data.name());
100  contact.insertEmail(data.email());
101 
102  const QString emailLink = QStringLiteral("<a href=\"mailto:") + QString::fromLatin1(QUrl::toPercentEncoding(contact.fullEmail()))
103  + QStringLiteral("\">%1</a>").arg(contact.preferredEmail());
104 
105  setHashField(memberObject, QStringLiteral("emailLink"), emailLink);
106 
107  return memberObject;
108 }
109 
111 {
112  if (!d->mErrorMessage.isEmpty()) {
113  return d->mErrorMessage;
114  }
115 
117  const Akonadi::Item localItem = item();
118  if (localItem.isValid() && localItem.hasPayload<KContacts::ContactGroup>()) {
119  group = localItem.payload<KContacts::ContactGroup>();
120  } else {
121  group = contactGroup();
122  }
123 
124  if (group.name().isEmpty() && group.count() == 0) { // empty group
125  return QString();
126  }
127 
128  if (group.contactReferenceCount() != 0) {
129  // we got a contact group with unresolved references -> we have to resolve
130  // it ourself. this shouldn't be the normal case, actually the calling
131  // code should pass in an already resolved contact group
132  auto job = new Akonadi::ContactGroupExpandJob(group);
133  if (job->exec()) {
134  group.removeAllContactData();
135  const KContacts::Addressee::List lstContacts = job->contacts();
136  for (const KContacts::Addressee &contact : lstContacts) {
137  group.append(KContacts::ContactGroup::Data(contact.realName(), contact.preferredEmail()));
138  }
139  }
140  }
141 
142  QVariantHash contactGroupObject;
143 
144  setHashField(contactGroupObject, QStringLiteral("name"), group.name());
145 
146  // Group members
147  QVariantList members;
148  const int numberOfData(group.dataCount());
149  members.reserve(numberOfData);
150  for (int i = 0; i < numberOfData; ++i) {
151  members << memberHash(group.data(i));
152  }
153 
154  contactGroupObject.insert(QStringLiteral("members"), members);
155 
156  // Additional fields
157  QVariantList fields;
158  for (int i = 0; i < additionalFields().size(); ++i) {
159  const QVariantMap field = additionalFields().at(i);
160  QVariantHash fieldObject;
161  setHashField(fieldObject, QStringLiteral("key"), field.value(QStringLiteral("key")).toString());
162 
163  setHashField(fieldObject, QStringLiteral("title"), field.value(QStringLiteral("title")).toString());
164 
165  setHashField(fieldObject, QStringLiteral("value"), field.value(QStringLiteral("value")).toString());
166 
167  fields << fieldObject;
168  }
169 
170  contactGroupObject.insert(QStringLiteral("additionalFields"), fields);
171 
172  QVariantHash colorsObject;
173  colorsObject.insert(QStringLiteral("linkColor"), KColorScheme(QPalette::Active, KColorScheme::View).foreground().color().name());
174 
175  colorsObject.insert(QStringLiteral("textColor"), KColorScheme(QPalette::Active, KColorScheme::View).foreground().color().name());
176 
177  colorsObject.insert(QStringLiteral("backgroundColor"), KColorScheme(QPalette::Active, KColorScheme::View).background().color().name());
178 
179  QVariantHash mapping;
180  mapping.insert(QStringLiteral("contactGroup"), contactGroupObject);
181  mapping.insert(QStringLiteral("colors"), colorsObject);
182 
183  Grantlee::Context context(mapping);
184 
185  if (form == SelfcontainedForm) {
186  return d->mSelfcontainedTemplate->render(&context);
187  } else if (form == EmbeddableForm) {
188  return d->mEmbeddableTemplate->render(&context);
189  } else {
190  return QString();
191  }
192 }
void setFormattedName(const QString &formattedName)
QString toHtml(HtmlForm form=SelfcontainedForm) const override
Returns the contact group formatted as HTML.
bool isValid() const
int contactReferenceCount() const
T payload() const
QString name() const
GrantleeContactGroupFormatter()
Creates a new grantlee contact group formatter.
Akonadi::Item item() const
Returns the item who&#39;s payload will be formatted.
bool isEmpty() const const
QVector< QVariantMap > additionalFields() const
Returns the additional fields that will be shown.
KContacts::ContactGroup contactGroup() const
Returns the contact group that will be formatted.
A class that formats a contact group as HTML code.
Creates a div HTML element that can be embedded.
const T & at(int i) const const
AddresseeList List
Job that expands a ContactGroup to a list of contacts.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QByteArray toPercentEncoding(const QString &input, const QByteArray &exclude, const QByteArray &include)
HtmlForm
Describes the form of the HTML that is created.
QString fromLatin1(const char *str, int size)
void append(const ContactReference &reference)
Data & data(int index)
bool hasPayload() const
int size() const const
~GrantleeContactGroupFormatter() override
Destroys the grantlee contact group formatter.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Wed Jun 23 2021 23:09:25 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.