KCalUtils

grantleetemplatemanager.cpp
1/*
2 * SPDX-FileCopyrightText: 2015 Daniel Vrátil <dvratil@redhat.com>
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 <KTextTemplate/Engine>
15#include <KTextTemplate/Template>
16#include <KTextTemplate/TemplateLoader>
17#include <QDebug>
18#include <QStandardPaths>
19#include <QString>
20
21#include <KLocalizedString>
22
23GrantleeTemplateManager *GrantleeTemplateManager::sInstance = nullptr;
24
25GrantleeTemplateManager::GrantleeTemplateManager()
26 : mEngine(new KTextTemplate::Engine)
27 , mLoader(new KCalUtils::QtResourceTemplateLoader)
28 , mLocalizer(new GrantleeKi18nLocalizer)
29{
31 if (!path.isEmpty()) {
32 mLoader->setTemplateDirs({path});
33 mLoader->setTheme(QStringLiteral("default"));
34 }
35
36 mEngine->addTemplateLoader(mLoader);
37 mEngine->addPluginPath(QStringLiteral(GRANTLEE_PLUGIN_INSTALL_DIR));
38 mEngine->addDefaultLibrary(QStringLiteral("ktexttemplate_i18ntags"));
39 mEngine->addDefaultLibrary(QStringLiteral("kcalendar_grantlee_plugin"));
40 mEngine->setSmartTrimEnabled(true);
41}
42
43GrantleeTemplateManager::~GrantleeTemplateManager()
44{
45 delete mEngine;
46}
47
48GrantleeTemplateManager *GrantleeTemplateManager::instance()
49{
50 if (!sInstance) {
51 sInstance = new GrantleeTemplateManager;
52 }
53 return sInstance;
54}
55
56void GrantleeTemplateManager::setTemplatePath(const QString &path)
57{
58 mLoader->setTemplateDirs({path});
59 mLoader->setTheme(QString());
60}
61
62void GrantleeTemplateManager::setPluginPath(const QString &path)
63{
64 QStringList pluginPaths = mEngine->pluginPaths();
65 pluginPaths.prepend(path);
66 mEngine->setPluginPaths(pluginPaths);
67}
68KTextTemplate::Context GrantleeTemplateManager::createContext(const QVariantHash &hash) const
69{
71 ctx.insert(QStringLiteral("incidence"), hash);
72 ctx.setLocalizer(mLocalizer);
73 return ctx;
74}
75
76QString GrantleeTemplateManager::errorTemplate(const QString &reason, const QString &origTemplateName, const KTextTemplate::Template &failedTemplate) const
77{
78 KTextTemplate::Template tpl = mEngine->newTemplate(QStringLiteral("<h1>{{ error }}</h1>\n"
79 "<b>%1:</b> {{ templateName }}<br>\n"
80 "<b>%2:</b> {{ errorMessage }}")
81 .arg(i18n("Template"), i18n("Error message")),
82 QStringLiteral("TemplateError"));
83
84 KTextTemplate::Context ctx = createContext();
85 ctx.insert(QStringLiteral("error"), reason);
86 ctx.insert(QStringLiteral("templateName"), origTemplateName);
87 ctx.insert(QStringLiteral("errorMessage"), failedTemplate->errorString());
88 return tpl->render(&ctx);
89}
90
91QString GrantleeTemplateManager::render(const QString &templateName, const QVariantHash &data) const
92{
93 if (!mLoader->canLoadTemplate(templateName)) {
94 qWarning() << "Cannot load template" << templateName << ", please check your installation";
95 return QString();
96 }
97 KTextTemplate::Template tpl = mLoader->loadByName(templateName, mEngine);
98 if (tpl->error()) {
99 return errorTemplate(i18n("Template parsing error"), templateName, tpl);
100 }
101 KTextTemplate::Context ctx = createContext(data);
102 const QString result = tpl->render(&ctx);
103 if (tpl->error()) {
104 return errorTemplate(i18n("Template rendering error"), templateName, tpl);
105 }
106 return result;
107}
void setLocalizer(QSharedPointer< AbstractLocalizer > localizer)
void insert(const QString &name, const QVariant &variant)
QString errorString() const
QString render(Context *c) const
QString i18n(const char *text, const TYPE &arg...)
QString path(const QString &relativePath)
QCA_EXPORT QStringList pluginPaths()
void prepend(parameter_type value)
QString locate(StandardLocation type, const QString &fileName, LocateOptions options)
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:09:16 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.