Commit eb447ad7 authored by Volker Krause's avatar Volker Krause
Browse files

Implement support for date/time field detection and formatting

parent a182f778
......@@ -2,3 +2,4 @@ find_package(Qt5Test ${QT_REQUIRED_VERSION} CONFIG REQUIRED)
add_definitions(-DSOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}")
ecm_add_test(pkpasstest.cpp LINK_LIBRARIES Qt::Test KPim::PkPass)
ecm_add_test(fieldtest.cpp LINK_LIBRARIES Qt::Test KPim::PkPass)
/*
SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include <field.h>
#include <pass.h>
#include <QJsonDocument>
#include <QJsonObject>
#include <QLocale>
#include <QTest>
#include <cmath>
namespace KPkPass
{
class FieldTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase()
{
QLocale::setDefault(QLocale(QStringLiteral("fr_FR")));
}
void testBoardingPass()
{
std::unique_ptr<KPkPass::Pass> pass(KPkPass::Pass::fromFile(QStringLiteral(SOURCE_DIR "/data/boardingpass-v1.pkpass")));
QVERIFY(pass);
auto obj =
QJsonDocument::fromJson(R"({"key":"valid-date","label":"Datum","dateStyle":"PKDateStyleShort","value":"2021-06-27T14:30:00+02:00"})").object();
KPkPass::Field f(obj, pass.get());
QCOMPARE(f.value().type(), QVariant::DateTime);
QCOMPARE(f.value(), QDateTime({2021, 6, 27}, {14, 30}));
QCOMPARE(f.valueDisplayString(), QLatin1String("27/06/2021 14:30"));
}
};
}
QTEST_GUILESS_MAIN(KPkPass::FieldTest)
#include "fieldtest.moc"
/*
SPDX-FileCopyrightText: 2017-2018 Volker Krause <vkrause@kde.org>
SPDX-FileCopyrightText: 2017-2021 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
......@@ -62,13 +62,29 @@ QVariant Field::value() const
if (v.isEmpty()) {
v = d->pass->d->message(d->obj.value(QLatin1String("value")).toString());
}
// TODO number and date/time detection
const auto dt = QDateTime::fromString(v, Qt::ISODate);
if (dt.isValid()) {
return dt;
}
// TODO number detection
return v;
}
QString Field::valueDisplayString() const
{
// TODO respect number and date/time formatting options
const auto v = value();
if (v.type() == QVariant::DateTime) {
auto fmt = QLocale::ShortFormat;
const auto dtStyle = d->obj.value(QLatin1String("dateStyle")).toString();
if (dtStyle == QLatin1String("PKDateStyleLong") || dtStyle == QLatin1String("PKDateStyleFull")) {
fmt = QLocale::LongFormat;
}
return QLocale().toString(v.toDateTime(), fmt);
}
// TODO respect number formatting options
return value().toString();
}
......
......@@ -20,6 +20,7 @@ namespace KPkPass
class Pass;
class PassPrivate;
class FieldPrivate;
class FieldTest;
/** Field element in a KPkPass::Pass.
* @see https://developer.apple.com/library/content/documentation/UserExperience/Reference/PassKit_Bundle/Chapters/FieldDictionary.html
......@@ -64,6 +65,7 @@ public:
private:
friend class PassPrivate;
friend class FieldTest;
explicit Field(const QJsonObject &obj, const Pass *pass);
std::shared_ptr<FieldPrivate> d;
......
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