File indexing completed on 2025-02-16 04:45:49

0001 /*
0002    SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "grantleeprint.h"
0008 #include "contactgrantleewrapper.h"
0009 
0010 #include <KTextTemplate/Context>
0011 #include <KTextTemplate/Engine>
0012 #include <KTextTemplate/MetaType>
0013 #include <KTextTemplate/TemplateLoader>
0014 
0015 #include <QMetaProperty>
0016 #include <QVariant>
0017 
0018 KTEXTTEMPLATE_BEGIN_LOOKUP(QUrl)
0019 if (property == QLatin1StringView("scheme")) {
0020     return object.scheme();
0021 }
0022 KTEXTTEMPLATE_END_LOOKUP
0023 
0024 using namespace KAddressBookGrantlee;
0025 
0026 GrantleePrint::GrantleePrint()
0027 {
0028     init();
0029 }
0030 
0031 GrantleePrint::GrantleePrint(const QString &themePath)
0032     : GrantleeTheme::GenericFormatter(QStringLiteral("theme.html"), themePath)
0033 {
0034     init();
0035 }
0036 
0037 GrantleePrint::~GrantleePrint() = default;
0038 
0039 void GrantleePrint::init()
0040 {
0041     KTextTemplate::registerMetaType<QUrl>();
0042 }
0043 
0044 QString GrantleePrint::contactsToHtml(const KContacts::Addressee::List &contacts)
0045 {
0046     if (!errorMessage().isEmpty()) {
0047         return errorMessage();
0048     }
0049 
0050     if (contacts.isEmpty()) {
0051         return {};
0052     }
0053     QVariantList contactsList;
0054     contactsList.reserve(contacts.count());
0055     for (const KContacts::Addressee &contact : contacts) {
0056         contactsList.push_back(QVariant::fromValue(ContactGrantleeWrapper(contact)));
0057     }
0058     QVariantHash mapping;
0059     mapping.insert(QStringLiteral("contacts"), contactsList);
0060 
0061     return render(mapping);
0062 }