Akonadi Contacts

standardcontactgroupformatter.cpp
1 /*
2  This file is part of Akonadi Contact.
3 
4  SPDX-FileCopyrightText: 2010 Tobias Koenig <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #include "standardcontactgroupformatter.h"
10 
11 #include "job/contactgroupexpandjob.h"
12 #include <Akonadi/Item>
13 #include <KColorScheme>
14 #include <KContacts/Addressee>
15 using namespace Akonadi;
16 
17 class Akonadi::StandardContactGroupFormatterPrivate
18 {
19 };
20 
22  : d(nullptr)
23 {
24 }
25 
27 
29 {
31  const Akonadi::Item localItem = item();
32  if (localItem.isValid() && localItem.hasPayload<KContacts::ContactGroup>()) {
33  group = localItem.payload<KContacts::ContactGroup>();
34  } else {
35  group = contactGroup();
36  }
37 
38  if (group.name().isEmpty() && group.count() == 0) { // empty group
39  return {};
40  }
41 
42  if (group.contactReferenceCount() != 0) {
43  // we got a contact group with unresolved references -> we have to resolve it ourself
44  // this shouldn't be the normal case, actually the calling code should pass in an already resolved
45  // contact group
46  auto job = new ContactGroupExpandJob(group);
47  if (job->exec()) {
48  group.removeAllContactData();
49  const KContacts::Addressee::List listContact = job->contacts();
50  for (const KContacts::Addressee &contact : listContact) {
51  group.append(KContacts::ContactGroup::Data(contact.realName(), contact.preferredEmail()));
52  }
53  }
54  }
55 
56  // Assemble all parts
57  QString strGroup = QStringLiteral(
58  "<table cellpadding=\"3\" cellspacing=\"0\" width=\"100%\">"
59  "<tr>"
60  "<td align=\"right\" valign=\"top\" width=\"30%\">"
61  "<img src=\"%1\" width=\"100\" vspace=\"1\">" // image
62  "</td>"
63  "<td align=\"left\" width=\"70%\"><font size=\"+2\"><b>%2</b></font></td>" // name
64  "</tr>"
65  "</table>")
66  .arg(QStringLiteral("group_photo"), group.name());
67 
68  strGroup += QLatin1String("<table width=\"100%\">");
69 
70  for (int i = 0; i < group.dataCount(); ++i) {
71  const KContacts::ContactGroup::Data data = group.data(i);
72 
73  if (data.email().isEmpty()) {
74  strGroup.append(QStringLiteral("<tr><td align=\"right\" width=\"50%\"><b><font color=\"grey\">%1</font></b></td>"
75  "<td width=\"50%\"></td></tr>")
76  .arg(data.name()));
77  } else {
78  KContacts::Addressee contact;
79  contact.setFormattedName(data.name());
80  KContacts::Email email(data.email());
81  contact.addEmail(email);
82  const QString fullEmail = QLatin1String("<a href=\"mailto:") + QString::fromLatin1(QUrl::toPercentEncoding(contact.fullEmail()))
83  + QStringLiteral("\">%1</a>").arg(contact.preferredEmail());
84 
85  strGroup.append(QStringLiteral("<tr><td align=\"right\" width=\"50%\"><b><font color=\"grey\">%1</font></b></td>"
86  "<td valign=\"bottom\" align=\"left\" width=\"50%\"><font>&lt;%2&gt;</font></td></tr>")
87  .arg(contact.realName(), fullEmail));
88  }
89  }
90 
91  const auto additionalFieldsLst = additionalFields();
92  for (const QVariantMap &map : additionalFieldsLst) {
93  strGroup.append(QStringLiteral("<tr><td colspan=\"2\">&nbsp;</td></tr><tr><td align=\"right\" width=\"30%\"><b><font color=\"grey\">%1</font></b></td>"
94  "<td valign=\"bottom\" align=\"left\" width=\"50%\"><font>%2</font></td></tr>")
95  .arg(map.value(QStringLiteral("title")).toString(), map.value(QStringLiteral("value")).toString()));
96  }
97 
98  strGroup.append(QLatin1String("</table>\n"));
99 
100  QString document = QStringLiteral("<div align=\"center\">%1</div>").arg(strGroup);
101 
102  if (form == EmbeddableForm) {
103  return document;
104  }
105 
106  document = QStringLiteral(
107  "<html>"
108  "<head>"
109  " <style type=\"text/css\">"
110  " a {text-decoration:none; color:%1}"
111  " </style>"
112  "</head>"
113  "<body text=\"%1\" bgcolor=\"%2\">" // text and background color
114  "%3" // contact group part
115  "</body>"
116  "</html>")
117  .arg(KColorScheme(QPalette::Active, KColorScheme::View).foreground().color().name(),
118  KColorScheme(QPalette::Active, KColorScheme::View).background().color().name(),
119  document);
120 
121  return document;
122 }
bool isValid() const
HtmlForm
Describes the form of the HTML that is created.
~StandardContactGroupFormatter() override
Destroys the standard contact group formatter.
int contactReferenceCount() const
KContacts::ContactGroup contactGroup() const
Returns the contact group that will be formatted.
QString preferredEmail() const
@ EmbeddableForm
Creates a div HTML element that can be embedded.
QString realName() const
void setFormattedName(const QString &formattedName)
bool hasPayload() const
StandardContactGroupFormatter()
Creates a new standard contact group formatter.
bool isEmpty() const const
Akonadi::Item item() const
Returns the item who's payload will be formatted.
Data & data(int index)
QByteArray toPercentEncoding(const QString &input, const QByteArray &exclude, const QByteArray &include)
QString toHtml(HtmlForm form=SelfcontainedForm) const override
Returns the contact group formatted as HTML.
void append(const ContactGroupReference &reference)
Job that expands a ContactGroup to a list of contacts.
QString name() const
QVector< QVariantMap > additionalFields() const
Returns the additional fields that will be shown.
QString fullEmail(const QString &email=QString()) const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString fromLatin1(const char *str, int size)
void addEmail(const Email &email)
AddresseeList List
T payload() const
QString & append(QChar ch)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sat Apr 1 2023 04:09:05 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.