Messagelib

messagepartrenderermanager.cpp
1 /*
2  SPDX-FileCopyrightText: 2016-2023 Laurent Montel <[email protected]>
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 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
21 #include <grantlee/context.h>
22 #include <grantlee/engine.h>
23 #include <grantlee/metatype.h>
24 #include <grantlee/templateloader.h>
25 #else
26 #include <KTextTemplate/Context>
27 #include <KTextTemplate/Engine>
28 #include <KTextTemplate/MetaType>
29 #include <KTextTemplate/TemplateLoader>
30 #endif
31 
32 #include <QGuiApplication>
33 Q_DECLARE_METATYPE(GpgME::DecryptionResult::Recipient)
34 Q_DECLARE_METATYPE(const QGpgME::Protocol *)
35 Q_DECLARE_METATYPE(GpgME::Key)
36 
37 // Read-only introspection of GpgME::DecryptionResult::Recipient object.
38 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
39 GRANTLEE_BEGIN_LOOKUP(GpgME::DecryptionResult::Recipient)
40 #else
41 KTEXTTEMPLATE_BEGIN_LOOKUP(GpgME::DecryptionResult::Recipient)
42 #endif
43 if (property == QLatin1String("keyID")) {
44  return QString::fromLatin1(object.keyID());
45 }
46 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
47 GRANTLEE_END_LOOKUP
48 #else
50 #endif
51 // Read-only introspection of QGpgME::Protocol object.
52 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
53 namespace Grantlee
54 #else
55 namespace KTextTemplate
56 #endif
57 {
58 template<>
59 inline QVariant TypeAccessor<const QGpgME::Protocol *>::lookUp(const QGpgME::Protocol *const object, const QString &property)
60 {
61  if (property == QLatin1String("name")) {
62  return object->name();
63  } else if (property == QLatin1String("displayName")) {
64  return object->displayName();
65  }
66  return {};
67 }
68 }
69 
70 // Read-only introspection of std::pair<GpgME::DecryptionResult::Recipient, GpgME::Key> object.
71 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
72 namespace Grantlee
73 #else
74 namespace KTextTemplate
75 #endif
76 {
77 template<>
78 inline QVariant
79 TypeAccessor<std::pair<GpgME::DecryptionResult::Recipient, GpgME::Key> &>::lookUp(std::pair<GpgME::DecryptionResult::Recipient, GpgME::Key> const &object,
80  const QString &property)
81 {
82  if (property == QLatin1String("keyID")) {
83  return QString::fromLatin1(object.first.keyID());
84  }
85  if (property == QLatin1String("id")) {
86  return QString::fromUtf8(object.second.userID(0).id());
87  }
88  if (property == QLatin1String("mainID")) {
89  return QString::fromLatin1(object.second.keyID());
90  }
91  return {};
92 }
93 }
94 
95 namespace MessageViewer
96 {
97 class GlobalContext : public QObject
98 {
99  Q_OBJECT
100  Q_PROPERTY(QString dir READ layoutDirection CONSTANT)
101  Q_PROPERTY(int iconSize READ iconSize CONSTANT)
102 public:
103  explicit GlobalContext(QObject *parent)
104  : QObject(parent)
105  {
106  }
107 
108  Q_REQUIRED_RESULT QString layoutDirection() const
109  {
110  return QGuiApplication::isRightToLeft() ? QStringLiteral("rtl") : QStringLiteral("ltr");
111  }
112 
113  Q_REQUIRED_RESULT int iconSize() const
114  {
116  }
117 };
118 }
119 
120 using namespace MessageViewer;
121 
122 MessagePartRendererManager::MessagePartRendererManager(QObject *parent)
123  : QObject(parent)
124  , m_globalContext(new GlobalContext(this))
125 {
126  initializeRenderer();
127 }
128 
129 MessagePartRendererManager::~MessagePartRendererManager()
130 {
131  delete m_engine;
132 }
133 
134 MessagePartRendererManager *MessagePartRendererManager::self()
135 {
136  static MessagePartRendererManager s_self;
137  return &s_self;
138 }
139 
140 void MessagePartRendererManager::initializeRenderer()
141 {
142 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
143  Grantlee::registerMetaType<GpgME::DecryptionResult::Recipient>();
144  Grantlee::registerMetaType<const QGpgME::Protocol *>();
145  Grantlee::registerMetaType<std::pair<GpgME::DecryptionResult::Recipient, GpgME::Key>>();
146 #else
147  KTextTemplate::registerMetaType<GpgME::DecryptionResult::Recipient>();
148  KTextTemplate::registerMetaType<const QGpgME::Protocol *>();
149  KTextTemplate::registerMetaType<std::pair<GpgME::DecryptionResult::Recipient, GpgME::Key>>();
150 #endif
151  m_engine = new GrantleeTheme::Engine;
153  const QString subPath = QStringLiteral("/pim" QT_STRINGIFY(QT_VERSION_MAJOR)) + QStringLiteral("/messageviewer");
154  for (const auto &p : libraryPaths) {
155  m_engine->addPluginPath(p + subPath);
156  }
157  m_engine->addDefaultLibrary(QStringLiteral("messageviewer_grantlee_extension"));
158  m_engine->localizer()->setApplicationDomain(QByteArrayLiteral("libmessageviewer"));
159 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
160  auto loader = QSharedPointer<Grantlee::FileSystemTemplateLoader>(new Grantlee::FileSystemTemplateLoader());
161 #else
163 #endif
164  loader->setTemplateDirs({QStringLiteral(":/")});
165  m_engine->addTemplateLoader(loader);
166 }
167 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
168 Grantlee::Template MessagePartRendererManager::loadByName(const QString &name)
169 #else
170 KTextTemplate::Template MessagePartRendererManager::loadByName(const QString &name)
171 #endif
172 {
173 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
174  Grantlee::Template t = m_engine->loadByName(name);
175 #else
176  KTextTemplate::Template t = m_engine->loadByName(name);
177 #endif
178  if (t->error()) {
179  qCWarning(MESSAGEVIEWER_LOG) << t->errorString() << ". Searched in subdir mimetreeparser/themes/default in these locations"
181  }
182  return t;
183 }
184 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
185 Grantlee::Context MessagePartRendererManager::createContext()
186 #else
187 KTextTemplate::Context MessagePartRendererManager::createContext()
188 #endif
189 {
190 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
191  Grantlee::Context c;
192 #else
194 #endif
195 
196  // careful, m_engine->localizer() is actually a factory function!
197  auto localizer = m_engine->localizer();
198  localizer->setApplicationDomain(QByteArrayLiteral("libmessageviewer"));
199  c.setLocalizer(localizer);
200 
201  c.insert(QStringLiteral("global"), m_globalContext);
202  return c;
203 }
204 
205 #include "messagepartrenderermanager.moc"
Q_OBJECTQ_OBJECT
void insert(const QString &name, const QVariant &variant)
The MessagePartRendererManager class.
Q_PROPERTY(...)
QString fromUtf8(const char *str, int size)
void setLocalizer(QSharedPointer< AbstractLocalizer > localizer)
KDB_EXPORT QStringList libraryPaths()
QStringList libraryPaths()
QStringList standardLocations(QStandardPaths::StandardLocation type)
static KIconLoader * global()
#define KTEXTTEMPLATE_BEGIN_LOOKUP(Type)
#define KTEXTTEMPLATE_END_LOOKUP
bool isRightToLeft()
int currentSize(KIconLoader::Group group) const
QString errorString() const
QString fromLatin1(const char *str, int size)
QSharedPointer< AbstractLocalizer > localizer() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Fri Mar 24 2023 04:08:31 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.