Kdepim-apps-lib

grantleeprint.cpp
1 /*
2  Copyright (C) 2015-2020 Laurent Montel <[email protected]>
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; see the file COPYING. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "grantleeprint.h"
21 #include "contactobject/contactgrantleewrapper.h"
22 
23 #include <grantlee/context.h>
24 #include <grantlee/engine.h>
25 #include <grantlee/metatype.h>
26 #include <grantlee/templateloader.h>
27 
28 #include <QMetaProperty>
29 #include <QVariant>
30 
31 // Grantlee has no Q_GADGET support yet
32 #define GRANTLEE_MAKE_GADGET(Class) \
33  GRANTLEE_BEGIN_LOOKUP(Class) \
34  const auto idx = Class::staticMetaObject.indexOfProperty(property.toUtf8().constData()); \
35  if (idx < 0) { \
36  return {};} \
37  const auto mp = Class::staticMetaObject.property(idx); \
38  return mp.readOnGadget(&object); \
39  GRANTLEE_END_LOOKUP
40 
41 GRANTLEE_MAKE_GADGET(KContacts::Address)
42 GRANTLEE_MAKE_GADGET(KContacts::Email)
43 GRANTLEE_MAKE_GADGET(KContacts::Geo)
44 GRANTLEE_MAKE_GADGET(KContacts::PhoneNumber)
45 GRANTLEE_MAKE_GADGET(KContacts::ResourceLocatorUrl)
46 GRANTLEE_MAKE_GADGET(KAddressBookGrantlee::ContactGrantleeWrapper)
47 
48 GRANTLEE_BEGIN_LOOKUP(QUrl)
49 if (property == QLatin1String("scheme")) {
50  return object.scheme();
51 }
52 GRANTLEE_END_LOOKUP
53 
54 using namespace KAddressBookGrantlee;
55 
56 GrantleePrint::GrantleePrint()
57 {
58  init();
59 }
60 
61 GrantleePrint::GrantleePrint(const QString &themePath)
62  : GrantleeTheme::GenericFormatter(QStringLiteral("theme.html"), themePath)
63 {
64  init();
65 }
66 
67 GrantleePrint::~GrantleePrint() = default;
68 
69 void GrantleePrint::init()
70 {
71  Grantlee::registerMetaType<KContacts::Address>();
72  Grantlee::registerMetaType<KContacts::Email>();
73  Grantlee::registerMetaType<KContacts::Geo>();
74  Grantlee::registerMetaType<KContacts::PhoneNumber>();
75  Grantlee::registerMetaType<KContacts::ResourceLocatorUrl>();
76  Grantlee::registerMetaType<QUrl>();
77  Grantlee::registerMetaType<ContactGrantleeWrapper>();
78 }
79 
80 QString GrantleePrint::contactsToHtml(const KContacts::Addressee::List &contacts)
81 {
82  if (!errorMessage().isEmpty()) {
83  return errorMessage();
84  }
85 
86  if (contacts.isEmpty()) {
87  return QString();
88  }
89  QVariantList contactsList;
90  contactsList.reserve(contacts.count());
91  for (const KContacts::Addressee &contact : contacts) {
92  contactsList.push_back(QVariant::fromValue(ContactGrantleeWrapper(contact)));
93  }
94  QVariantHash mapping;
95  mapping.insert(QStringLiteral("contacts"), contactsList);
96 
97  return render(mapping);
98 }
Akonadi::Item contact() const
Additional properties for the KContacts::Addressee Grantlee model.
KCALUTILS_EXPORT QString errorMessage(const KCalendarCore::Exception &exception)
QVariant fromValue(const T &value)
QCA_EXPORT void init()
AddresseeList List
void render(QPaintDevice *target, const QPoint &targetOffset, const QRegion &sourceRegion, QWidget::RenderFlags renderFlags)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Jul 1 2020 23:16:38 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.