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>
26 Q_DECLARE_METATYPE(GpgME::DecryptionResult::Recipient)
27 Q_DECLARE_METATYPE(const QGpgME::Protocol *)
28 Q_DECLARE_METATYPE(GpgME::Key)
29 
30 // Read-only introspection of GpgME::DecryptionResult::Recipient object.
31 KTEXTTEMPLATE_BEGIN_LOOKUP(GpgME::DecryptionResult::Recipient)
32 if (property == QLatin1StringView("keyID")) {
33  return QString::fromLatin1(object.keyID());
34 }
36 // Read-only introspection of QGpgME::Protocol object.
37 namespace KTextTemplate
38 {
39 template<>
40 inline 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.
52 namespace KTextTemplate
53 {
54 template<>
55 inline QVariant
56 TypeAccessor<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 
72 namespace MessageViewer
73 {
74 class GlobalContext : public QObject
75 {
76  Q_OBJECT
77  Q_PROPERTY(QString dir READ layoutDirection CONSTANT)
78  Q_PROPERTY(int iconSize READ iconSize CONSTANT)
79 public:
80  explicit GlobalContext(QObject *parent)
81  : 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 
97 using namespace MessageViewer;
98 
99 MessagePartRendererManager::MessagePartRendererManager(QObject *parent)
100  : QObject(parent)
101  , m_globalContext(new GlobalContext(this))
102 {
103  initializeRenderer();
104 }
105 
106 MessagePartRendererManager::~MessagePartRendererManager()
107 {
108  delete m_engine;
109 }
110 
111 MessagePartRendererManager *MessagePartRendererManager::self()
112 {
113  static MessagePartRendererManager s_self;
114  return &s_self;
115 }
116 
117 void MessagePartRendererManager::initializeRenderer()
118 {
119  KTextTemplate::registerMetaType<GpgME::DecryptionResult::Recipient>();
120  KTextTemplate::registerMetaType<const QGpgME::Protocol *>();
121  KTextTemplate::registerMetaType<std::pair<GpgME::DecryptionResult::Recipient, GpgME::Key>>();
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 }
134 KTextTemplate::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 }
143 KTextTemplate::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"
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-2024 The KDE developers.
Generated on Thu Feb 15 2024 03:55:20 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.