Commit a0b30823 authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Port to KTextTemplate

parent dfab3748
Pipeline #131336 passed with stage
in 45 seconds
......@@ -7,8 +7,11 @@
#include "datetimefilters.h"
#include "../incidenceformatter.h"
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <grantlee/safestring.h>
#else
#include <KTextTemplate/safestring.h>
#endif
KDateFilter::KDateFilter()
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
......
......@@ -9,13 +9,20 @@
#include "kcalutils_debug.h"
#include <QLocale>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <grantlee/safestring.h>
#else
#include <KTextTemplate/safestring.h>
#endif
#include <KLocalizedString>
GrantleeKi18nLocalizer::GrantleeKi18nLocalizer()
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
: Grantlee::QtLocalizer()
#else
: KTextTemplate::QtLocalizer()
#endif
{
}
......@@ -50,10 +57,17 @@ QString GrantleeKi18nLocalizer::processArguments(const KLocalizedString &kstr, c
str = str.subs(iter->toDouble());
break;
case QVariant::UserType:
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
if (iter->canConvert<Grantlee::SafeString>()) {
str = str.subs(iter->value<Grantlee::SafeString>().get());
break;
}
#else
if (iter->canConvert<KTextTemplate::SafeString>()) {
str = str.subs(iter->value<KTextTemplate::SafeString>().get());
break;
}
#endif
// fall-through
Q_FALLTHROUGH();
default:
......
......@@ -6,14 +6,21 @@
*/
#pragma once
#include <QObject>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <grantlee/qtlocalizer.h>
#else
#include <KTextTemplate/qtlocalizer.h>
#endif
#include <QLocale>
class KLocalizedString;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
class GrantleeKi18nLocalizer : public Grantlee::QtLocalizer
#else
class GrantleeKi18nLocalizer : public KTextTemplate::QtLocalizer
#endif
{
public:
explicit GrantleeKi18nLocalizer();
......
......@@ -14,17 +14,26 @@
#include <QDebug>
#include <QStandardPaths>
#include <QString>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <grantlee/engine.h>
#include <grantlee/template.h>
#include <grantlee/templateloader.h>
#else
#include <KTextTemplate/engine.h>
#include <KTextTemplate/template.h>
#include <KTextTemplate/templateloader.h>
#endif
#include <KLocalizedString>
GrantleeTemplateManager *GrantleeTemplateManager::sInstance = nullptr;
GrantleeTemplateManager::GrantleeTemplateManager()
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
: mEngine(new Grantlee::Engine)
#else
: mEngine(new KTextTemplate::Engine)
#endif
, mLoader(new KCalUtils::QtResourceTemplateLoader)
, mLocalizer(new GrantleeKi18nLocalizer)
{
......@@ -66,17 +75,29 @@ void GrantleeTemplateManager::setPluginPath(const QString &path)
pluginPaths.prepend(path);
mEngine->setPluginPaths(pluginPaths);
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Grantlee::Context GrantleeTemplateManager::createContext(const QVariantHash &hash) const
#else
KTextTemplate::Context GrantleeTemplateManager::createContext(const QVariantHash &hash) const
#endif
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Grantlee::Context ctx;
#else
KTextTemplate::Context ctx;
#endif
ctx.insert(QStringLiteral("incidence"), hash);
ctx.setLocalizer(mLocalizer);
return ctx;
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QString GrantleeTemplateManager::errorTemplate(const QString &reason, const QString &origTemplateName, const Grantlee::Template &failedTemplate) const
#else
QString GrantleeTemplateManager::errorTemplate(const QString &reason, const QString &origTemplateName, const KTextTemplate::Template &failedTemplate) const
#endif
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Grantlee::Template tpl = mEngine->newTemplate(QStringLiteral("<h1>{{ error }}</h1>\n"
"<b>%1:</b> {{ templateName }}<br>\n"
"<b>%2:</b> {{ errorMessage }}")
......@@ -84,6 +105,15 @@ QString GrantleeTemplateManager::errorTemplate(const QString &reason, const QStr
QStringLiteral("TemplateError"));
Grantlee::Context ctx = createContext();
#else
KTextTemplate::Template tpl = mEngine->newTemplate(QStringLiteral("<h1>{{ error }}</h1>\n"
"<b>%1:</b> {{ templateName }}<br>\n"
"<b>%2:</b> {{ errorMessage }}")
.arg(i18n("Template"), i18n("Error message")),
QStringLiteral("TemplateError"));
KTextTemplate::Context ctx = createContext();
#endif
ctx.insert(QStringLiteral("error"), reason);
ctx.insert(QStringLiteral("templateName"), origTemplateName);
ctx.insert(QStringLiteral("errorMessage"), failedTemplate->errorString());
......@@ -96,12 +126,19 @@ QString GrantleeTemplateManager::render(const QString &templateName, const QVari
qWarning() << "Cannot load template" << templateName << ", please check your installation";
return QString();
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Grantlee::Template tpl = mLoader->loadByName(templateName, mEngine);
#else
KTextTemplate::Template tpl = mLoader->loadByName(templateName, mEngine);
#endif
if (tpl->error()) {
return errorTemplate(i18n("Template parsing error"), templateName, tpl);
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Grantlee::Context ctx = createContext(data);
#else
KTextTemplate::Context ctx = createContext(data);
#endif
const QString result = tpl->render(&ctx);
if (tpl->error()) {
return errorTemplate(i18n("Template rendering error"), templateName, tpl);
......
......@@ -11,7 +11,11 @@
#include <QSharedPointer>
#include <QVariantHash>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
namespace Grantlee
#else
namespace KTextTemplate
#endif
{
class Engine;
class FileSystemTemplateLoader;
......@@ -38,12 +42,21 @@ public:
private:
Q_DISABLE_COPY(GrantleeTemplateManager)
GrantleeTemplateManager();
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QString errorTemplate(const QString &reason, const QString &origTemplateName, const Grantlee::Template &failedTemplate) const;
Grantlee::Context createContext(const QVariantHash &hash = QVariantHash()) const;
#else
QString errorTemplate(const QString &reason, const QString &origTemplateName, const KTextTemplate::Template &failedTemplate) const;
KTextTemplate::Context createContext(const QVariantHash &hash = QVariantHash()) const;
#endif
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Grantlee::Engine *const mEngine;
QSharedPointer<Grantlee::FileSystemTemplateLoader> mLoader;
#else
KTextTemplate::Engine *const mEngine;
QSharedPointer<KTextTemplate::FileSystemTemplateLoader> mLoader;
#endif
QSharedPointer<GrantleeKi18nLocalizer> mLocalizer;
static GrantleeTemplateManager *sInstance;
......
......@@ -9,23 +9,41 @@
#include <QFile>
#include <QTextStream>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <grantlee/engine.h>
#else
#include <KTextTemplate/engine.h>
#endif
// TODO: remove this class when Grantlee support it
using namespace KCalUtils;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QtResourceTemplateLoader::QtResourceTemplateLoader(const QSharedPointer<Grantlee::AbstractLocalizer> &localizer)
: Grantlee::FileSystemTemplateLoader(localizer)
{
}
#else
QtResourceTemplateLoader::QtResourceTemplateLoader(const QSharedPointer<KTextTemplate::AbstractLocalizer> &localizer)
: KTextTemplate::FileSystemTemplateLoader(localizer)
{
}
#endif
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Grantlee::Template QtResourceTemplateLoader::loadByName(const QString &fileName, const Grantlee::Engine *engine) const
#else
KTextTemplate::Template QtResourceTemplateLoader::loadByName(const QString &fileName, const KTextTemplate::Engine *engine) const
#endif
{
// Qt resource file
if (fileName.startsWith(QLatin1String(":/"))) {
QFile file;
file.setFileName(fileName);
if (!file.exists() || !file.open(QIODevice::ReadOnly | QIODevice::Text)) {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
return Grantlee::Template();
#else
return KTextTemplate::Template();
#endif
}
QTextStream fstream(&file);
......@@ -36,7 +54,11 @@ Grantlee::Template QtResourceTemplateLoader::loadByName(const QString &fileName,
return engine->newTemplate(fileContent, fileName);
} else {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
return Grantlee::FileSystemTemplateLoader::loadByName(fileName, engine);
#else
return KTextTemplate::FileSystemTemplateLoader::loadByName(fileName, engine);
#endif
}
}
......@@ -53,6 +75,10 @@ bool QtResourceTemplateLoader::canLoadTemplate(const QString &name) const
file.close();
return true;
} else {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
return Grantlee::FileSystemTemplateLoader::canLoadTemplate(name);
#else
return KTextTemplate::FileSystemTemplateLoader::canLoadTemplate(name);
#endif
}
}
......@@ -6,16 +6,31 @@
*/
#pragma once
#include "grantlee/templateloader.h"
#include <QObject>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <grantlee/templateloader.h>
#else
#include <KTextTemplate/templateloader.h>
#endif
namespace KCalUtils
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
class QtResourceTemplateLoader : public Grantlee::FileSystemTemplateLoader
#else
class QtResourceTemplateLoader : public KTextTemplate::FileSystemTemplateLoader
#endif
{
public:
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QtResourceTemplateLoader(const QSharedPointer<Grantlee::AbstractLocalizer> &localizer = QSharedPointer<Grantlee::AbstractLocalizer>());
Q_REQUIRED_RESULT Grantlee::Template loadByName(const QString &fileName, const Grantlee::Engine *engine) const override;
#else
QtResourceTemplateLoader(const QSharedPointer<KTextTemplate::AbstractLocalizer> &localizer = QSharedPointer<KTextTemplate::AbstractLocalizer>());
Q_REQUIRED_RESULT KTextTemplate::Template loadByName(const QString &fileName, const KTextTemplate::Engine *engine) const override;
#endif
Q_REQUIRED_RESULT bool canLoadTemplate(const QString &name) const override;
};
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment