KCalUtils

grantleetemplatemanager.cpp
1 /*
2  * SPDX-FileCopyrightText: 2015 Daniel Vr├ítil <[email protected]>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 
8 #include <config-kcalutils.h>
9 
10 #include "grantleeki18nlocalizer_p.h"
11 #include "grantleetemplatemanager_p.h"
12 #include "qtresourcetemplateloader.h"
13 
14 #include <QDebug>
15 #include <QStandardPaths>
16 #include <QString>
17 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
18 #include <grantlee/engine.h>
19 #include <grantlee/template.h>
20 #include <grantlee/templateloader.h>
21 #else
22 #include <KTextTemplate/Engine>
23 #include <KTextTemplate/Template>
24 #include <KTextTemplate/TemplateLoader>
25 #endif
26 
27 #include <KLocalizedString>
28 
29 GrantleeTemplateManager *GrantleeTemplateManager::sInstance = nullptr;
30 
31 GrantleeTemplateManager::GrantleeTemplateManager()
32 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
33  : mEngine(new Grantlee::Engine)
34 #else
35  : mEngine(new KTextTemplate::Engine)
36 #endif
37  , mLoader(new KCalUtils::QtResourceTemplateLoader)
38  , mLocalizer(new GrantleeKi18nLocalizer)
39 {
41  if (!path.isEmpty()) {
42  mLoader->setTemplateDirs({path});
43  mLoader->setTheme(QStringLiteral("default"));
44  }
45 
46  mEngine->addTemplateLoader(mLoader);
47  mEngine->addPluginPath(QStringLiteral(GRANTLEE_PLUGIN_INSTALL_DIR));
48 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
49  mEngine->addDefaultLibrary(QStringLiteral("grantlee_i18ntags"));
50 #else
51  mEngine->addDefaultLibrary(QStringLiteral("ktexttemplate_i18ntags"));
52 #endif
53  mEngine->addDefaultLibrary(QStringLiteral("kcalendar_grantlee_plugin"));
54  mEngine->setSmartTrimEnabled(true);
55 }
56 
57 GrantleeTemplateManager::~GrantleeTemplateManager()
58 {
59  delete mEngine;
60 }
61 
62 GrantleeTemplateManager *GrantleeTemplateManager::instance()
63 {
64  if (!sInstance) {
65  sInstance = new GrantleeTemplateManager;
66  }
67  return sInstance;
68 }
69 
70 void GrantleeTemplateManager::setTemplatePath(const QString &path)
71 {
72  mLoader->setTemplateDirs({path});
73  mLoader->setTheme(QString());
74 }
75 
76 void GrantleeTemplateManager::setPluginPath(const QString &path)
77 {
78  QStringList pluginPaths = mEngine->pluginPaths();
79  pluginPaths.prepend(path);
80  mEngine->setPluginPaths(pluginPaths);
81 }
82 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
83 Grantlee::Context GrantleeTemplateManager::createContext(const QVariantHash &hash) const
84 #else
85 KTextTemplate::Context GrantleeTemplateManager::createContext(const QVariantHash &hash) const
86 #endif
87 {
88 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
89  Grantlee::Context ctx;
90 #else
92 #endif
93  ctx.insert(QStringLiteral("incidence"), hash);
94  ctx.setLocalizer(mLocalizer);
95  return ctx;
96 }
97 
98 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
99 QString GrantleeTemplateManager::errorTemplate(const QString &reason, const QString &origTemplateName, const Grantlee::Template &failedTemplate) const
100 #else
101 QString GrantleeTemplateManager::errorTemplate(const QString &reason, const QString &origTemplateName, const KTextTemplate::Template &failedTemplate) const
102 #endif
103 {
104 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
105  Grantlee::Template tpl = mEngine->newTemplate(QStringLiteral("<h1>{{ error }}</h1>\n"
106  "<b>%1:</b> {{ templateName }}<br>\n"
107  "<b>%2:</b> {{ errorMessage }}")
108  .arg(i18n("Template"), i18n("Error message")),
109  QStringLiteral("TemplateError"));
110 
111  Grantlee::Context ctx = createContext();
112 #else
113  KTextTemplate::Template tpl = mEngine->newTemplate(QStringLiteral("<h1>{{ error }}</h1>\n"
114  "<b>%1:</b> {{ templateName }}<br>\n"
115  "<b>%2:</b> {{ errorMessage }}")
116  .arg(i18n("Template"), i18n("Error message")),
117  QStringLiteral("TemplateError"));
118 
119  KTextTemplate::Context ctx = createContext();
120 #endif
121  ctx.insert(QStringLiteral("error"), reason);
122  ctx.insert(QStringLiteral("templateName"), origTemplateName);
123  ctx.insert(QStringLiteral("errorMessage"), failedTemplate->errorString());
124  return tpl->render(&ctx);
125 }
126 
127 QString GrantleeTemplateManager::render(const QString &templateName, const QVariantHash &data) const
128 {
129  if (!mLoader->canLoadTemplate(templateName)) {
130  qWarning() << "Cannot load template" << templateName << ", please check your installation";
131  return QString();
132  }
133 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
134  Grantlee::Template tpl = mLoader->loadByName(templateName, mEngine);
135 #else
136  KTextTemplate::Template tpl = mLoader->loadByName(templateName, mEngine);
137 #endif
138  if (tpl->error()) {
139  return errorTemplate(i18n("Template parsing error"), templateName, tpl);
140  }
141 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
142  Grantlee::Context ctx = createContext(data);
143 #else
144  KTextTemplate::Context ctx = createContext(data);
145 #endif
146  const QString result = tpl->render(&ctx);
147  if (tpl->error()) {
148  return errorTemplate(i18n("Template rendering error"), templateName, tpl);
149  }
150  return result;
151 }
void insert(const QString &name, const QVariant &variant)
void setLocalizer(QSharedPointer< AbstractLocalizer > localizer)
QString locate(QStandardPaths::StandardLocation type, const QString &fileName, QStandardPaths::LocateOptions options)
void prepend(const T &value)
QString i18n(const char *text, const TYPE &arg...)
QCA_EXPORT QStringList pluginPaths()
bool isEmpty() const const
QString path(const QString &relativePath)
QString render(Context *c) const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Mar 26 2023 04:09:43 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.