Messagelib

messagepartrenderermanager.cpp
1/*
2 SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "messagepartrenderermanager.h"
8#include "messageviewer_debug.h"
9#include <GrantleeTheme/GrantleeKi18nLocalizer>
10#include <GrantleeTheme/GrantleeThemeEngine>
11#include <KIconLoader>
12#include <QStandardPaths>
13
14#include <gpgme++/decryptionresult.h>
15#include <gpgme++/key.h>
16#include <gpgme++/verificationresult.h>
17
18#include <QGpgME/Protocol>
19
20#include <KTextTemplate/Context>
21#include <KTextTemplate/Engine>
22#include <KTextTemplate/MetaType>
23#include <KTextTemplate/TemplateLoader>
24
25#include <QGuiApplication>
26Q_DECLARE_METATYPE(GpgME::DecryptionResult::Recipient)
27Q_DECLARE_METATYPE(const QGpgME::Protocol *)
28Q_DECLARE_METATYPE(GpgME::Key)
29
30// Read-only introspection of GpgME::DecryptionResult::Recipient object.
31KTEXTTEMPLATE_BEGIN_LOOKUP(GpgME::DecryptionResult::Recipient)
32if (property == QLatin1StringView("keyID")) {
33 return QString::fromLatin1(object.keyID());
34}
36// Read-only introspection of QGpgME::Protocol object.
37namespace KTextTemplate
38{
39template<>
40inline QVariant TypeAccessor<const QGpgME::Protocol *>::lookUp(const QGpgME::Protocol *const object, const QString &property)
41{
42 if (property == QLatin1StringView("name")) {
43 return object->name();
44 } else if (property == QLatin1StringView("displayName")) {
45 return object->displayName();
46 }
47 return {};
48}
49}
50
51// Read-only introspection of std::pair<GpgME::DecryptionResult::Recipient, GpgME::Key> object.
52namespace KTextTemplate
53{
54template<>
55inline QVariant
56TypeAccessor<std::pair<GpgME::DecryptionResult::Recipient, GpgME::Key> &>::lookUp(std::pair<GpgME::DecryptionResult::Recipient, GpgME::Key> const &object,
57 const QString &property)
58{
59 if (property == QLatin1StringView("keyID")) {
60 return QString::fromLatin1(object.first.keyID());
61 }
62 if (property == QLatin1StringView("id")) {
63 return QString::fromUtf8(object.second.userID(0).id());
64 }
65 if (property == QLatin1StringView("mainID")) {
66 return QString::fromLatin1(object.second.keyID());
67 }
68 return {};
69}
70}
71
72namespace MessageViewer
73{
74class GlobalContext : public QObject
75{
77 Q_PROPERTY(QString dir READ layoutDirection CONSTANT)
78 Q_PROPERTY(int iconSize READ iconSize CONSTANT)
79public:
80 explicit GlobalContext(QObject *parent)
82 {
83 }
84
85 [[nodiscard]] QString layoutDirection() const
86 {
87 return QGuiApplication::isRightToLeft() ? QStringLiteral("rtl") : QStringLiteral("ltr");
88 }
89
90 [[nodiscard]] int iconSize() const
91 {
93 }
94};
95}
96
97using namespace MessageViewer;
98
99MessagePartRendererManager::MessagePartRendererManager(QObject *parent)
100 : QObject(parent)
101 , m_globalContext(new GlobalContext(this))
102{
103 initializeRenderer();
104}
105
106MessagePartRendererManager::~MessagePartRendererManager()
107{
108 delete m_engine;
109}
110
111MessagePartRendererManager *MessagePartRendererManager::self()
112{
113 static MessagePartRendererManager s_self;
114 return &s_self;
115}
116
117void MessagePartRendererManager::initializeRenderer()
118{
122 m_engine = new GrantleeTheme::Engine;
124 const QString subPath = QStringLiteral("/pim6/messageviewer");
125 for (const auto &p : libraryPaths) {
126 m_engine->addPluginPath(p + subPath);
127 }
128 m_engine->addDefaultLibrary(QStringLiteral("messageviewer_ktexttemplate_extension"));
129 m_engine->localizer()->setApplicationDomain(QByteArrayLiteral("libmessageviewer6"));
131 loader->setTemplateDirs({QStringLiteral(":/")});
132 m_engine->addTemplateLoader(loader);
133}
134KTextTemplate::Template MessagePartRendererManager::loadByName(const QString &name)
135{
136 KTextTemplate::Template t = m_engine->loadByName(name);
137 if (t->error()) {
138 qCWarning(MESSAGEVIEWER_LOG) << t->errorString() << ". Searched in subdir mimetreeparser/themes/default in these locations"
140 }
141 return t;
142}
143KTextTemplate::Context MessagePartRendererManager::createContext()
144{
146
147 // careful, m_engine->localizer() is actually a factory function!
148 auto localizer = m_engine->localizer();
149 localizer->setApplicationDomain(QByteArrayLiteral("libmessageviewer"));
150 c.setLocalizer(localizer);
151
152 c.insert(QStringLiteral("global"), m_globalContext);
153 return c;
154}
155
156#include "messagepartrenderermanager.moc"
157
158#include "moc_messagepartrenderermanager.cpp"
int currentSize(KIconLoader::Group group) const
static KIconLoader * global()
QSharedPointer< AbstractLocalizer > localizer() const
void setLocalizer(QSharedPointer< AbstractLocalizer > localizer)
void insert(const QString &name, const QVariant &variant)
QString errorString() const
The MessagePartRendererManager class.
#define KTEXTTEMPLATE_END_LOOKUP
#define KTEXTTEMPLATE_BEGIN_LOOKUP(Type)
KDB_EXPORT QStringList libraryPaths()
int registerMetaType()
QStringList libraryPaths()
bool isRightToLeft()
Q_OBJECTQ_OBJECT
Q_PROPERTY(...)
QObject * parent() const const
QStringList standardLocations(StandardLocation type)
QString fromLatin1(QByteArrayView str)
QString fromUtf8(QByteArrayView str)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 8 2024 11:56:32 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.