Messagelib

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

KDE's Doxygen guidelines are available online.