KPkPass

field.cpp
1/*
2 SPDX-FileCopyrightText: 2017-2021 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "field.h"
8#include "pass.h"
9#include "pass_p.h"
10
11#include <QGuiApplication>
12#include <QJsonObject>
13
14using namespace KPkPass;
15
16namespace KPkPass
17{
18class FieldPrivate
19{
20public:
21 const Pass *pass = nullptr;
22 QJsonObject obj;
23};
24}
25
26Field::Field()
27 : d(new FieldPrivate)
28{
29}
30
31Field::Field(const Field &) = default;
32Field::Field(Field &&) = default;
33Field::~Field() = default;
34Field &Field::operator=(const Field &) = default;
35
36Field::Field(const QJsonObject &obj, const Pass *pass)
37 : d(new FieldPrivate)
38{
39 d->pass = pass;
40 d->obj = obj;
41}
42
43QString Field::key() const
44{
45 return d->obj.value(QLatin1StringView("key")).toString();
46}
47
48QString Field::label() const
49{
50 if (d->pass) {
51 return d->pass->d->message(d->obj.value(QLatin1StringView("label")).toString());
52 }
53 return {};
54}
55
56QVariant Field::value() const
57{
58 if (!d->pass) {
59 return {};
60 }
61 auto v = d->pass->d->message(d->obj.value(QLatin1StringView("attributedValue")).toString());
62 if (v.isEmpty()) {
63 v = d->pass->d->message(d->obj.value(QLatin1StringView("value")).toString());
64 }
65
66 const auto dt = QDateTime::fromString(v, Qt::ISODate);
67 if (dt.isValid()) {
68 return dt;
69 }
70
71 // TODO number detection
72 return v;
73}
74
75QString Field::valueDisplayString() const
76{
77 const auto v = value();
78 // see
79 // https://developer.apple.com/library/archive/documentation/UserExperience/Reference/PassKit_Bundle/Chapters/FieldDictionary.html#//apple_ref/doc/uid/TP40012026-CH4-SW6
80 // however, real-world data doesn't strictly follow that, so we have to guess a bit here...
81 if (v.userType() == QMetaType::QDateTime) {
82 const auto dt = v.toDateTime();
83 auto fmt = QLocale::ShortFormat;
84 const auto dtStyle = d->obj.value(QLatin1StringView("dateStyle")).toString();
85 if (dtStyle == QLatin1StringView("PKDateStyleLong") || dtStyle == QLatin1StringView("PKDateStyleFull")) {
87 }
88 const auto timeStyle = d->obj.value(QLatin1StringView("timeStyle")).toString();
89 if (timeStyle == QLatin1StringView("PKDateStyleNone") || (timeStyle.isEmpty() && !dtStyle.isEmpty() && dt.time() == QTime(0, 0))) {
90 return QLocale().toString(dt.date(), fmt);
91 }
92
93 return QLocale().toString(dt, fmt);
94 }
95
96 // TODO respect number formatting options
97 return value().toString().trimmed();
98}
99
100QString Field::changeMessage() const
101{
102 if (!d->pass) {
103 return {};
104 }
105 auto msg = d->pass->d->message(d->obj.value(QLatin1StringView("changeMessage")).toString());
106 msg.replace(QLatin1StringView("%@"), valueDisplayString());
107 return msg;
108}
109
110Qt::Alignment Field::textAlignment() const
111{
112 const auto alignStr = d->obj.value(QLatin1StringView("textAlignment")).toString();
113 if (alignStr == QLatin1StringView("PKTextAlignmentLeft")) {
114 return Qt::AlignLeft;
115 } else if (alignStr == QLatin1StringView("PKTextAlignmentCenter")) {
116 return Qt::AlignHCenter;
117 } else if (alignStr == QLatin1StringView("PKTextAlignmentRight")) {
118 return Qt::AlignRight;
119 }
121}
122
123#include "moc_field.cpp"
Field element in a KPkPass::Pass.
Definition field.h:29
Base class for a pkpass file.
Definition pass.h:35
QDateTime fromString(QStringView string, QStringView format, QCalendar cal)
QString toString(QDate date, FormatType format) const const
QString trimmed() const const
typedef Alignment
LeftToRight
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:07:40 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.