KCalUtils

datetimefilters.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 "datetimefilters.h"
9 #include "../incidenceformatter.h"
10 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
11 #include <grantlee/safestring.h>
12 #else
13 #include <KTextTemplate/SafeString>
14 #endif
15 
16 KDateFilter::KDateFilter()
17 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
18  : Grantlee::Filter()
19 #else
21 #endif
22 {
23 }
24 
25 KDateFilter::~KDateFilter()
26 {
27 }
28 
29 QVariant KDateFilter::doFilter(const QVariant &input, const QVariant &argument, bool autoescape) const
30 {
31  Q_UNUSED(autoescape)
32 
33  QDate date;
34 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
35  if (input.type() == QVariant::Date) {
36  date = input.toDate();
37  } else if (input.type() == QVariant::DateTime) {
38  date = input.toDateTime().date();
39  } else {
40  return QString();
41  }
42 #else
43  if (input.userType() == QMetaType::QDate) {
44  date = input.toDate();
45  } else if (input.userType() == QMetaType::QDateTime) {
46  date = input.toDateTime().date();
47  } else {
48  return QString();
49  }
50 #endif
51 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
52  const bool shortFmt = (argument.value<Grantlee::SafeString>().get().compare(QLatin1String("short"), Qt::CaseInsensitive) == 0);
53  return Grantlee::SafeString(KCalUtils::IncidenceFormatter::dateToString(date, shortFmt));
54 #else
55  const bool shortFmt = (argument.value<KTextTemplate::SafeString>().get().compare(QLatin1String("short"), Qt::CaseInsensitive) == 0);
57 #endif
58 }
59 
60 bool KDateFilter::isSafe() const
61 {
62  return true;
63 }
64 
65 KTimeFilter::KTimeFilter()
66 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
67  : Grantlee::Filter()
68 #else
70 #endif
71 {
72 }
73 
74 KTimeFilter::~KTimeFilter()
75 {
76 }
77 
78 QVariant KTimeFilter::doFilter(const QVariant &input, const QVariant &argument, bool autoescape) const
79 {
80  Q_UNUSED(autoescape)
81 
82  QTime time;
83 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
84  if (input.type() == QVariant::Time) {
85  time = input.toTime();
86  } else if (input.type() == QVariant::DateTime) {
87  time = input.toDateTime().time();
88  } else {
89  return QString();
90  }
91 #else
92  if (input.userType() == QMetaType::QTime) {
93  time = input.toTime();
94  } else if (input.userType() == QMetaType::QDateTime) {
95  time = input.toDateTime().time();
96  } else {
97  return QString();
98  }
99 
100 #endif
101 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
102  const bool shortFmt = (argument.value<Grantlee::SafeString>().get().compare(QLatin1String("short"), Qt::CaseInsensitive) == 0);
103  return Grantlee::SafeString(KCalUtils::IncidenceFormatter::timeToString(time, shortFmt));
104 #else
105  const bool shortFmt = (argument.value<KTextTemplate::SafeString>().get().compare(QLatin1String("short"), Qt::CaseInsensitive) == 0);
107 #endif
108 }
109 
110 bool KTimeFilter::isSafe() const
111 {
112  return true;
113 }
114 
115 KDateTimeFilter::KDateTimeFilter()
116 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
117  : Grantlee::Filter()
118 #else
120 #endif
121 {
122 }
123 
124 KDateTimeFilter::~KDateTimeFilter()
125 {
126 }
127 
128 QVariant KDateTimeFilter::doFilter(const QVariant &input, const QVariant &argument, bool autoescape) const
129 {
130  Q_UNUSED(autoescape)
131 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
132  if (input.type() != QVariant::DateTime) {
133  return QString();
134  }
135 #else
136  if (input.userType() != QMetaType::QDateTime) {
137  return QString();
138  }
139 #endif
140  const QDateTime dt = input.toDateTime();
141 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
142  const QStringList arguments = argument.value<Grantlee::SafeString>().get().split(QLatin1Char(','));
143  const bool shortFmt = arguments.contains(QLatin1String("short"), Qt::CaseInsensitive);
144  const bool dateOnly = arguments.contains(QLatin1String("dateonly"), Qt::CaseInsensitive);
145  return Grantlee::SafeString(KCalUtils::IncidenceFormatter::dateTimeToString(dt, dateOnly, shortFmt));
146 #else
147  const QStringList arguments = argument.value<KTextTemplate::SafeString>().get().split(QLatin1Char(','));
148  const bool shortFmt = arguments.contains(QLatin1String("short"), Qt::CaseInsensitive);
149  const bool dateOnly = arguments.contains(QLatin1String("dateonly"), Qt::CaseInsensitive);
151 #endif
152 }
153 
154 bool KDateTimeFilter::isSafe() const
155 {
156  return true;
157 }
CaseInsensitive
T value() const const
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
QTime time() const const
KCALUTILS_EXPORT QString timeToString(QTime time, bool shortfmt=true)
Build a QString time representation of a QTime object.
QDate toDate() const const
int userType() const const
KCALUTILS_EXPORT QString dateToString(QDate date, bool shortfmt=true)
Build a QString date representation of a QDate object.
QVariant::Type type() const const
KCALUTILS_EXPORT QString dateTimeToString(const QDateTime &date, bool dateOnly=false, bool shortfmt=true)
Build a QString date/time representation of a QDateTime object.
QDateTime toDateTime() const const
QTime toTime() const const
QDate date() const const
virtual QVariant get(ScriptableExtension *callerPrincipal, quint64 objId, const QString &propName)
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.