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

Prepare to use KTextTemplate

parent 6d2a5d3d
Pipeline #131330 passed with stage
in 1 minute and 17 seconds
......@@ -52,7 +52,14 @@ find_package(KF5Codecs ${KF5_MIN_VERSION} CONFIG REQUIRED)
find_package(KF5WidgetsAddons ${KF5_MIN_VERSION} CONFIG REQUIRED)
find_package(KF5ConfigWidgets ${KF5_MIN_VERSION} CONFIG REQUIRED)
find_package(KF5IconThemes ${KF5_MIN_VERSION} CONFIG REQUIRED)
find_package(Grantlee5 "5.2" CONFIG REQUIRED)
if (QT_MAJOR_VERSION STREQUAL "5")
find_package(Grantlee5 "5.2" CONFIG REQUIRED)
set(TEMPLATES_LIB Grantlee5::Templates)
else()
find_package(KF6TextTemplate ${KF5_MIN_VERSION} CONFIG REQUIRED)
set(TEMPLATES_LIB KF6TextTemplate::Templates)
endif()
find_package(KF5CalendarCore ${KF5_MIN_VERSION} CONFIG REQUIRED)
find_package(KF5IdentityManagement ${IDENTITYMANAGER_LIB_VERSION} CONFIG REQUIRED)
......
......@@ -19,7 +19,7 @@ ecm_add_tests(testdndfactory.cpp teststringify.cpp testtodotooltip.cpp
ecm_add_test(testincidenceformatter.cpp
TEST_NAME "testincidenceformatter"
NAME_PREFIX "kcalutils-"
LINK_LIBRARIES KF5CalendarUtils Qt${QT_MAJOR_VERSION}::Core Qt${QT_MAJOR_VERSION}::Test KF5::CalendarCore KF5::I18n KF5::IdentityManagement Grantlee5::Templates
LINK_LIBRARIES KF5CalendarUtils Qt${QT_MAJOR_VERSION}::Core Qt${QT_MAJOR_VERSION}::Test KF5::CalendarCore KF5::I18n KF5::IdentityManagement ${TEMPLATES_LIB}
)
# Make sure that dates are formatted in C locale
......
......@@ -63,7 +63,7 @@ PRIVATE
KF5::I18n
KF5::IdentityManagement
KF5::Codecs
Grantlee5::Templates
${TEMPLATES_LIB}
)
set_target_properties(KF5CalendarUtils PROPERTIES
......
......@@ -16,7 +16,7 @@ target_sources(kcalendar_grantlee_plugin PRIVATE
)
kpim_grantlee_adjust_plugin_name(kcalendar_grantlee_plugin)
target_link_libraries(kcalendar_grantlee_plugin
Grantlee5::Templates
${TEMPLATES_LIB}
KF5::IconThemes
KF5CalendarUtils
)
......
......@@ -11,7 +11,11 @@
#include <grantlee/safestring.h>
KDateFilter::KDateFilter()
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
: Grantlee::Filter()
#else
: KTextTemplate::Filter()
#endif
{
}
......@@ -31,9 +35,13 @@ QVariant KDateFilter::doFilter(const QVariant &input, const QVariant &argument,
} else {
return QString();
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
const bool shortFmt = (argument.value<Grantlee::SafeString>().get().compare(QLatin1String("short"), Qt::CaseInsensitive) == 0);
return Grantlee::SafeString(KCalUtils::IncidenceFormatter::dateToString(date, shortFmt));
#else
const bool shortFmt = (argument.value<KTextTemplate::SafeString>().get().compare(QLatin1String("short"), Qt::CaseInsensitive) == 0);
return KTextTemplate::SafeString(KCalUtils::IncidenceFormatter::dateToString(date, shortFmt));
#endif
}
bool KDateFilter::isSafe() const
......@@ -42,7 +50,11 @@ bool KDateFilter::isSafe() const
}
KTimeFilter::KTimeFilter()
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
: Grantlee::Filter()
#else
: KTextTemplate::Filter()
#endif
{
}
......@@ -62,10 +74,13 @@ QVariant KTimeFilter::doFilter(const QVariant &input, const QVariant &argument,
} else {
return QString();
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
const bool shortFmt = (argument.value<Grantlee::SafeString>().get().compare(QLatin1String("short"), Qt::CaseInsensitive) == 0);
return Grantlee::SafeString(KCalUtils::IncidenceFormatter::timeToString(time, shortFmt));
#else
const bool shortFmt = (argument.value<KTextTemplate::SafeString>().get().compare(QLatin1String("short"), Qt::CaseInsensitive) == 0);
return KTextTemplate::SafeString(KCalUtils::IncidenceFormatter::timeToString(time, shortFmt));
#endif
}
bool KTimeFilter::isSafe() const
......@@ -74,7 +89,11 @@ bool KTimeFilter::isSafe() const
}
KDateTimeFilter::KDateTimeFilter()
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
: Grantlee::Filter()
#else
: KTextTemplate::Filter()
#endif
{
}
......@@ -90,12 +109,17 @@ QVariant KDateTimeFilter::doFilter(const QVariant &input, const QVariant &argume
return QString();
}
const QDateTime dt = input.toDateTime();
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
const QStringList arguments = argument.value<Grantlee::SafeString>().get().split(QLatin1Char(','));
const bool shortFmt = arguments.contains(QLatin1String("short"), Qt::CaseInsensitive);
const bool dateOnly = arguments.contains(QLatin1String("dateonly"), Qt::CaseInsensitive);
return Grantlee::SafeString(KCalUtils::IncidenceFormatter::dateTimeToString(dt, dateOnly, shortFmt));
#else
const QStringList arguments = argument.value<KTextTemplate::SafeString>().get().split(QLatin1Char(','));
const bool shortFmt = arguments.contains(QLatin1String("short"), Qt::CaseInsensitive);
const bool dateOnly = arguments.contains(QLatin1String("dateonly"), Qt::CaseInsensitive);
return KTextTemplate::SafeString(KCalUtils::IncidenceFormatter::dateTimeToString(dt, dateOnly, shortFmt));
#endif
}
bool KDateTimeFilter::isSafe() const
......
......@@ -6,10 +6,18 @@
*/
#pragma once
#include <QObject>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <grantlee/filter.h>
#else
#include <KTextTemplate/filter.h>
#endif
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
class KDateFilter : public Grantlee::Filter
#else
class KDateFilter : public KTextTemplate::Filter
#endif
{
public:
KDateFilter();
......@@ -21,8 +29,11 @@ public:
private:
Q_DISABLE_COPY(KDateFilter)
};
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
class KTimeFilter : public Grantlee::Filter
#else
class KTimeFilter : public KTextTemplate::Filter
#endif
{
public:
KTimeFilter();
......@@ -34,8 +45,11 @@ public:
private:
Q_DISABLE_COPY(KTimeFilter)
};
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
class KDateTimeFilter : public Grantlee::Filter
#else
class KDateTimeFilter : public KTextTemplate::Filter
#endif
{
public:
KDateTimeFilter();
......
......@@ -7,22 +7,35 @@
#include "icon.h"
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <grantlee/exception.h>
#include <grantlee/parser.h>
#include <grantlee/variable.h>
#else
#include <KTextTemplate/exception.h>
#include <KTextTemplate/parser.h>
#include <KTextTemplate/variable.h>
#endif
#include <KIconLoader>
IconTag::IconTag(QObject *parent)
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
: Grantlee::AbstractNodeFactory(parent)
#else
: KTextTemplate::AbstractNodeFactory(parent)
#endif
{
}
IconTag::~IconTag()
{
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Grantlee::Node *IconTag::getNode(const QString &tagContent, Grantlee::Parser *p) const
#else
KTextTemplate::Node *IconTag::getNode(const QString &tagContent, KTextTemplate::Parser *p) const
#endif
{
Q_UNUSED(p)
......@@ -42,10 +55,18 @@ Grantlee::Node *IconTag::getNode(const QString &tagContent, Grantlee::Parser *p)
const QStringList parts = smartSplit(tagContent);
const int partsSize = parts.size();
if (partsSize < 2) {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
throw Grantlee::Exception(Grantlee::TagSyntaxError, QStringLiteral("icon tag takes at least 1 argument"));
#else
throw KTextTemplate::Exception(KTextTemplate::TagSyntaxError, QStringLiteral("icon tag takes at least 1 argument"));
#endif
}
if (partsSize > 4) {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
throw Grantlee::Exception(Grantlee::TagSyntaxError, QStringLiteral("icon tag takes at maximum 3 arguments, %1 given").arg(partsSize));
#else
throw KTextTemplate::Exception(KTextTemplate::TagSyntaxError, QStringLiteral("icon tag takes at maximum 3 arguments, %1 given").arg(partsSize));
#endif
}
int sizeOrGroup = KIconLoader::Small;
......@@ -74,13 +95,21 @@ Grantlee::Node *IconTag::getNode(const QString &tagContent, Grantlee::Parser *p)
}
IconNode::IconNode(QObject *parent)
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
: Grantlee::Node(parent)
#else
: KTextTemplate::Node(parent)
#endif
, mSizeOrGroup(KIconLoader::Small)
{
}
IconNode::IconNode(const QString &iconName, int sizeOrGroup, const QString &altText, QObject *parent)
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
: Grantlee::Node(parent)
#else
: KTextTemplate::Node(parent)
#endif
, mIconName(iconName)
, mAltText(altText)
, mSizeOrGroup(sizeOrGroup)
......@@ -90,8 +119,11 @@ IconNode::IconNode(const QString &iconName, int sizeOrGroup, const QString &altT
IconNode::~IconNode()
{
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
void IconNode::render(Grantlee::OutputStream *stream, Grantlee::Context *c) const
#else
void IconNode::render(KTextTemplate::OutputStream *stream, KTextTemplate::Context *c) const
#endif
{
Q_UNUSED(c)
......@@ -99,7 +131,11 @@ void IconNode::render(Grantlee::OutputStream *stream, Grantlee::Context *c) cons
if (iconName.startsWith(QLatin1Char('"')) && iconName.endsWith(QLatin1Char('"'))) {
iconName = iconName.mid(1, iconName.size() - 2);
} else {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
iconName = Grantlee::Variable(mIconName).resolve(c).toString();
#else
iconName = KTextTemplate::Variable(mIconName).resolve(c).toString();
#endif
}
QString altText;
......@@ -107,6 +143,7 @@ void IconNode::render(Grantlee::OutputStream *stream, Grantlee::Context *c) cons
if (mAltText.startsWith(QLatin1Char('"')) && mAltText.endsWith(QLatin1Char('"'))) {
altText = mAltText.mid(1, mAltText.size() - 2);
} else {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
const QVariant v = Grantlee::Variable(mAltText).resolve(c);
if (v.isValid()) {
if (v.canConvert<Grantlee::SafeString>()) {
......@@ -115,6 +152,17 @@ void IconNode::render(Grantlee::OutputStream *stream, Grantlee::Context *c) cons
altText = v.toString();
}
}
#else
const QVariant v = KTextTemplate::Variable(mAltText).resolve(c);
if (v.isValid()) {
if (v.canConvert<KTextTemplate::SafeString>()) {
altText = v.value<KTextTemplate::SafeString>().get();
} else {
altText = v.toString();
}
}
#endif
}
}
......@@ -123,5 +171,9 @@ void IconNode::render(Grantlee::OutputStream *stream, Grantlee::Context *c) cons
.arg(KIconLoader::global()->iconPath(iconName, mSizeOrGroup))
.arg(mSizeOrGroup < KIconLoader::LastGroup ? KIconLoader::global()->currentSize(static_cast<KIconLoader::Group>(mSizeOrGroup)) : mSizeOrGroup)
.arg(altText.isEmpty() ? iconName : altText, altText); // title is intentionally blank if no alt is provided
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
(*stream) << Grantlee::SafeString(html, Grantlee::SafeString::IsSafe);
#else
(*stream) << KTextTemplate::SafeString(html, KTextTemplate::SafeString::IsSafe);
#endif
}
......@@ -6,8 +6,12 @@
*/
#pragma once
#include <QObject>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <grantlee/node.h>
#else
#include <KTextTemplate/node.h>
#endif
/**
* @name icon tag
......@@ -35,26 +39,38 @@
* @note Support for nested variables inside tags is non-standard for Grantlee
* tags, but makes it easier to use {% icon %} in sub-templates.
*/
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
class IconTag : public Grantlee::AbstractNodeFactory
#else
class IconTag : public KTextTemplate::AbstractNodeFactory
#endif
{
Q_OBJECT
public:
explicit IconTag(QObject *parent = nullptr);
~IconTag() override;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Grantlee::Node *getNode(const QString &tagContent, Grantlee::Parser *p) const override;
#else
KTextTemplate::Node *getNode(const QString &tagContent, KTextTemplate::Parser *p) const override;
#endif
};
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
class IconNode : public Grantlee::Node
#else
class IconNode : public KTextTemplate::Node
#endif
{
Q_OBJECT
public:
explicit IconNode(QObject *parent = nullptr);
IconNode(const QString &iconName, int sizeOrGroup, const QString &altText, QObject *parent = nullptr);
~IconNode() override;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
void render(Grantlee::OutputStream *stream, Grantlee::Context *c) const override;
#else
void render(KTextTemplate::OutputStream *stream, KTextTemplate::Context *c) const override;
#endif
private:
QString mIconName;
......
......@@ -11,29 +11,45 @@
KCalendarGrantleePlugin::KCalendarGrantleePlugin(QObject *parent)
: QObject(parent)
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
, Grantlee::TagLibraryInterface()
#else
, KTextTemplate::TagLibraryInterface()
#endif
{
}
KCalendarGrantleePlugin::~KCalendarGrantleePlugin()
{
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QHash<QString, Grantlee::AbstractNodeFactory *> KCalendarGrantleePlugin::nodeFactories(const QString &name)
#else
QHash<QString, KTextTemplate::AbstractNodeFactory *> KCalendarGrantleePlugin::nodeFactories(const QString &name)
#endif
{
Q_UNUSED(name)
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QHash<QString, Grantlee::AbstractNodeFactory *> nodeFactories;
#else
QHash<QString, KTextTemplate::AbstractNodeFactory *> nodeFactories;
#endif
nodeFactories[QStringLiteral("icon")] = new IconTag();
return nodeFactories;
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QHash<QString, Grantlee::Filter *> KCalendarGrantleePlugin::filters(const QString &name)
#else
QHash<QString, KTextTemplate::Filter *> KCalendarGrantleePlugin::filters(const QString &name)
#endif
{
Q_UNUSED(name)
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QHash<QString, Grantlee::Filter *> filters;
#else
QHash<QString, KTextTemplate::Filter *> filters;
#endif
filters[QStringLiteral("kdate")] = new KDateFilter();
filters[QStringLiteral("ktime")] = new KTimeFilter();
filters[QStringLiteral("kdatetime")] = new KDateTimeFilter();
......
......@@ -6,20 +6,35 @@
*/
#pragma once
#include <QObject>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <grantlee/taglibraryinterface.h>
#else
#include <KTextTemplate/taglibraryinterface.h>
#endif
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
class KCalendarGrantleePlugin : public QObject, public Grantlee::TagLibraryInterface
#else
class KCalendarGrantleePlugin : public QObject, public KTextTemplate::TagLibraryInterface
#endif
{
Q_OBJECT
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Q_INTERFACES(Grantlee::TagLibraryInterface)
#else
Q_INTERFACES(KTextTemplate::TagLibraryInterface)
#endif
Q_PLUGIN_METADATA(IID "org.kde.KCalendarGrantleePlugin")
public:
explicit KCalendarGrantleePlugin(QObject *parent = nullptr);
~KCalendarGrantleePlugin() override;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QHash<QString, Grantlee::Filter *> filters(const QString &name) override;
QHash<QString, Grantlee::AbstractNodeFactory *> nodeFactories(const QString &name) override;
#else
QHash<QString, KTextTemplate::Filter *> filters(const QString &name) override;
QHash<QString, KTextTemplate::AbstractNodeFactory *> nodeFactories(const QString &name) override;
#endif
};
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