Akonadi Contacts

standardcontactgroupformatter.cpp
1/*
2 This file is part of Akonadi Contact.
3
4 SPDX-FileCopyrightText: 2010 Tobias Koenig <tokoe@kde.org>
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>
15using namespace Akonadi;
16
17class Akonadi::StandardContactGroupFormatterPrivate
18{
19};
20
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()) {
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 += QLatin1StringView("<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 {
79 contact.setFormattedName(data.name());
80 KContacts::Email email(data.email());
81 contact.addEmail(email);
82 const QString fullEmail = QLatin1StringView("<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(QLatin1StringView("</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}
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.
Job that expands a ContactGroup to a list of contacts.
bool hasPayload() const
T payload() const
bool isValid() const
QString toHtml(HtmlForm form=SelfcontainedForm) const override
Returns the contact group formatted as HTML.
StandardContactGroupFormatter()
Creates a new standard contact group formatter.
~StandardContactGroupFormatter() override
Destroys the standard contact group formatter.
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
A widget for editing the display name of a contact.
QString & append(QChar ch)
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 Sat Dec 21 2024 17:05:46 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.