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 <KColorScheme>
13 #include <item.h>
14 #include <kcontacts/addressee.h>
15 
16 using namespace Akonadi;
17 
19  : d(nullptr)
20 {
21 }
22 
24 {
25 }
26 
28 {
30  const Akonadi::Item localItem = item();
31  if (localItem.isValid() && localItem.hasPayload<KContacts::ContactGroup>()) {
32  group = localItem.payload<KContacts::ContactGroup>();
33  } else {
34  group = contactGroup();
35  }
36 
37  if (group.name().isEmpty() && group.count() == 0) { // empty group
38  return QString();
39  }
40 
41  if (group.contactReferenceCount() != 0) {
42  // we got a contact group with unresolved references -> we have to resolve it ourself
43  // this shouldn't be the normal case, actually the calling code should pass in an already resolved
44  // contact group
45  auto job = new ContactGroupExpandJob(group);
46  if (job->exec()) {
47  group.removeAllContactData();
48  const KContacts::Addressee::List listContact = job->contacts();
49  for (const KContacts::Addressee &contact : listContact) {
50  group.append(KContacts::ContactGroup::Data(contact.realName(), contact.preferredEmail()));
51  }
52  }
53  }
54 
55  // Assemble all parts
56  QString strGroup = QStringLiteral(
57  "<table cellpadding=\"3\" cellspacing=\"0\" width=\"100%\">"
58  "<tr>"
59  "<td align=\"right\" valign=\"top\" width=\"30%\">"
60  "<img src=\"%1\" width=\"100\" vspace=\"1\">" // image
61  "</td>"
62  "<td align=\"left\" width=\"70%\"><font size=\"+2\"><b>%2</b></font></td>" // name
63  "</tr>"
64  "</table>")
65  .arg(QStringLiteral("group_photo"), group.name());
66 
67  strGroup += QLatin1String("<table width=\"100%\">");
68 
69  for (int i = 0; i < group.dataCount(); ++i) {
70  const KContacts::ContactGroup::Data data = group.data(i);
71 
72  if (data.email().isEmpty()) {
73  strGroup.append(QStringLiteral("<tr><td align=\"right\" width=\"50%\"><b><font color=\"grey\">%1</font></b></td>"
74  "<td width=\"50%\"></td></tr>")
75  .arg(data.name()));
76  } else {
77  KContacts::Addressee contact;
78  contact.setFormattedName(data.name());
79  contact.insertEmail(data.email());
80 
81  const QString fullEmail = QLatin1String("<a href=\"mailto:") + QString::fromLatin1(QUrl::toPercentEncoding(contact.fullEmail()))
82  + QStringLiteral("\">%1</a>").arg(contact.preferredEmail());
83 
84  strGroup.append(QStringLiteral("<tr><td align=\"right\" width=\"50%\"><b><font color=\"grey\">%1</font></b></td>"
85  "<td valign=\"bottom\" align=\"left\" width=\"50%\"><font>&lt;%2&gt;</font></td></tr>")
86  .arg(contact.realName(), fullEmail));
87  }
88  }
89 
90  const auto additionalFieldsLst = additionalFields();
91  for (const QVariantMap &map : additionalFieldsLst) {
92  strGroup.append(QStringLiteral("<tr><td colspan=\"2\">&nbsp;</td></tr><tr><td align=\"right\" width=\"30%\"><b><font color=\"grey\">%1</font></b></td>"
93  "<td valign=\"bottom\" align=\"left\" width=\"50%\"><font>%2</font></td></tr>")
94  .arg(map.value(QStringLiteral("title")).toString(), map.value(QStringLiteral("value")).toString()));
95  }
96 
97  strGroup.append(QLatin1String("</table>\n"));
98 
99  QString document = QStringLiteral("<div align=\"center\">%1</div>").arg(strGroup);
100 
101  if (form == EmbeddableForm) {
102  return document;
103  }
104 
105  document = QStringLiteral(
106  "<html>"
107  "<head>"
108  " <style type=\"text/css\">"
109  " a {text-decoration:none; color:%1}"
110  " </style>"
111  "</head>"
112  "<body text=\"%1\" bgcolor=\"%2\">" // text and background color
113  "%3" // contact group part
114  "</body>"
115  "</html>")
116  .arg(KColorScheme(QPalette::Active, KColorScheme::View).foreground().color().name(),
117  KColorScheme(QPalette::Active, KColorScheme::View).background().color().name(),
118  document);
119 
120  return document;
121 }
void setFormattedName(const QString &formattedName)
QString & append(QChar ch)
bool isValid() const
int contactReferenceCount() const
~StandardContactGroupFormatter() override
Destroys the standard contact group formatter.
T payload() const
void insertEmail(const QString &email, bool preferred=false, const QMap< QString, QStringList > &param=QMap< QString, QStringList >())
QString fullEmail(const QString &email=QString()) const
QString name() const
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.
QString realName() const
QString preferredEmail() const
StandardContactGroupFormatter()
Creates a new standard contact group formatter.
Creates a div HTML element that can be embedded.
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
QString toHtml(HtmlForm form=SelfcontainedForm) const override
Returns the contact group formatted as HTML.
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.