File indexing completed on 2025-01-19 04:46:51
0001 /* 0002 SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "ktexttemplateextension.h" 0008 0009 #include <KContacts/Address> 0010 #include <KItinerary/Place> 0011 0012 #include <KTextTemplate/Exception> 0013 #include <KTextTemplate/Parser> 0014 0015 #include <QDateTime> 0016 #include <QTimeZone> 0017 0018 QVariant AddressFormatter::doFilter(const QVariant &input, const QVariant &arg, bool autoescape) const 0019 { 0020 Q_UNUSED(arg) 0021 Q_UNUSED(autoescape) 0022 0023 if (!KItinerary::JsonLd::isA<KItinerary::PostalAddress>(input)) { 0024 return {}; 0025 } 0026 const auto a = input.value<KItinerary::PostalAddress>(); 0027 0028 KContacts::Address address; 0029 address.setStreet(a.streetAddress()); 0030 address.setPostalCode(a.postalCode()); 0031 address.setLocality(a.addressLocality()); 0032 address.setRegion(a.addressRegion()); 0033 address.setCountry(a.addressCountry()); 0034 0035 auto s = escape(address.formatted(KContacts::AddressFormatStyle::MultiLineInternational)); 0036 return s.get().replace(QLatin1Char('\n'), QLatin1StringView("<br/>")); 0037 } 0038 0039 bool AddressFormatter::isSafe() const 0040 { 0041 return true; 0042 } 0043 0044 QVariant DateFormatter::doFilter(const QVariant &input, const QVariant &arg, bool autoescape) const 0045 { 0046 Q_UNUSED(arg) 0047 Q_UNUSED(autoescape) 0048 0049 const auto dt = input.value<QDateTime>(); 0050 if (!dt.isValid()) { 0051 return {}; 0052 } 0053 0054 return QLocale().toString(dt.date(), QLocale::ShortFormat); 0055 } 0056 0057 QVariant DateTimeFormatter::doFilter(const QVariant &input, const QVariant &arg, bool autoescape) const 0058 { 0059 Q_UNUSED(arg) 0060 Q_UNUSED(autoescape) 0061 0062 const auto dt = input.value<QDateTime>(); 0063 if (!dt.isValid()) { 0064 return {}; 0065 } 0066 0067 auto s = QLocale().toString(dt, QLocale::ShortFormat); 0068 if (dt.timeSpec() == Qt::TimeZone || dt.timeSpec() == Qt::OffsetFromUTC || dt.timeSpec() == Qt::UTC) { 0069 s += QLatin1Char(' ') + dt.timeZone().abbreviation(dt); 0070 } 0071 return s; 0072 } 0073 0074 QVariant TimeFormatter::doFilter(const QVariant &input, const QVariant &arg, bool autoescape) const 0075 { 0076 Q_UNUSED(arg) 0077 Q_UNUSED(autoescape) 0078 0079 const auto dt = input.value<QDateTime>(); 0080 if (!dt.isValid()) { 0081 return {}; 0082 } 0083 0084 auto s = QLocale().toString(dt.time(), QLocale::ShortFormat); 0085 if (dt.timeSpec() == Qt::TimeZone || dt.timeSpec() == Qt::OffsetFromUTC || dt.timeSpec() == Qt::UTC) { 0086 s += QLatin1Char(' ') + dt.timeZone().abbreviation(dt); 0087 } 0088 return s; 0089 } 0090 0091 TagLibrary::TagLibrary(QObject *parent) 0092 : QObject(parent) 0093 { 0094 } 0095 QHash<QString, KTextTemplate::Filter *> TagLibrary::filters(const QString &name) 0096 { 0097 Q_UNUSED(name) 0098 QHash<QString, KTextTemplate::Filter *> filters; 0099 filters.insert(QStringLiteral("formatAddress"), new AddressFormatter()); 0100 filters.insert(QStringLiteral("formatDate"), new DateFormatter()); 0101 filters.insert(QStringLiteral("formatDateTime"), new DateTimeFormatter()); 0102 filters.insert(QStringLiteral("formatTime"), new TimeFormatter()); 0103 return filters; 0104 } 0105 0106 #include "moc_ktexttemplateextension.cpp"