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;
15using namespace Qt::Literals;
16
17namespace KPkPass
18{
19class FieldPrivate
20{
21public:
22 const Pass *pass = nullptr;
23 QJsonObject obj;
24};
25}
26
27Field::Field()
28 : d(new FieldPrivate)
29{
30}
31
32Field::Field(const Field &) = default;
33Field::Field(Field &&) = default;
34Field::~Field() = default;
35Field &Field::operator=(const Field &) = default;
36
37Field::Field(const QJsonObject &obj, const Pass *pass)
38 : d(new FieldPrivate)
39{
40 d->pass = pass;
41 d->obj = obj;
42}
43
44QString Field::key() const
45{
46 return d->obj.value(QLatin1StringView("key")).toString();
47}
48
49QString Field::label() const
50{
51 if (d->pass) {
52 return d->pass->d->message(d->obj.value(QLatin1StringView("label")).toString());
53 }
54 return {};
55}
56
57QVariant Field::value() const
58{
59 if (!d->pass) {
60 return {};
61 }
62 auto v = d->obj.value("attributedValue"_L1);
63 if (v.isUndefined()) {
64 v = d->obj.value("value"_L1);
65 }
66
67 if (v.isString()) {
68 const auto s = d->pass->d->message(v.toString());
69 const auto dt = QDateTime::fromString(s, Qt::ISODate);
70 if (dt.isValid()) {
71 return dt;
72 }
73 return s;
74 }
75
76 if (v.isDouble()) {
77 return v.toDouble();
78 }
79
80 return {};
81}
82
83QString Field::valueDisplayString() const
84{
85 const auto v = value();
86 // see
87 // https://developer.apple.com/library/archive/documentation/UserExperience/Reference/PassKit_Bundle/Chapters/FieldDictionary.html#//apple_ref/doc/uid/TP40012026-CH4-SW6
88 // however, real-world data doesn't strictly follow that, so we have to guess a bit here...
89 if (v.typeId() == QMetaType::QDateTime) {
90 const auto dt = v.toDateTime();
91 auto fmt = QLocale::ShortFormat;
92 const auto dtStyle = d->obj.value(QLatin1StringView("dateStyle")).toString();
93 if (dtStyle == QLatin1StringView("PKDateStyleLong") || dtStyle == QLatin1StringView("PKDateStyleFull")) {
95 }
96 const auto timeStyle = d->obj.value(QLatin1StringView("timeStyle")).toString();
97 if (timeStyle == QLatin1StringView("PKDateStyleNone") || (timeStyle.isEmpty() && !dtStyle.isEmpty() && dt.time() == QTime(0, 0))) {
98 return QLocale().toString(dt.date(), fmt);
99 }
100
101 return QLocale().toString(dt, fmt);
102 }
103 if (v.typeId() == QMetaType::Double) {
104 if (const auto currency = currencyCode(); !currency.isEmpty()) {
105 return QLocale().toCurrencyString(v.toDouble(), currency);
106 }
107
108 // TODO respect number formatting options
109 return QString::number(v.toDouble());
110 }
111
112 return v.toString().trimmed();
113}
114
115QString Field::changeMessage() const
116{
117 if (!d->pass) {
118 return {};
119 }
120 auto msg = d->pass->d->message(d->obj.value(QLatin1StringView("changeMessage")).toString());
121 msg.replace(QLatin1StringView("%@"), valueDisplayString());
122 return msg;
123}
124
125Qt::Alignment Field::textAlignment() const
126{
127 const auto alignStr = d->obj.value(QLatin1StringView("textAlignment")).toString();
128 if (alignStr == QLatin1StringView("PKTextAlignmentLeft")) {
129 return Qt::AlignLeft;
130 } else if (alignStr == QLatin1StringView("PKTextAlignmentCenter")) {
131 return Qt::AlignHCenter;
132 } else if (alignStr == QLatin1StringView("PKTextAlignmentRight")) {
133 return Qt::AlignRight;
134 }
136}
137
138QString Field::currencyCode() const
139{
140 return d->obj.value("currencyCode"_L1).toString();
141}
142
143#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 toCurrencyString(double value, const QString &symbol, int precision) const const
QString toString(QDate date, FormatType format) const const
bool isEmpty() const const
QString number(double n, char format, int precision)
typedef Alignment
LeftToRight
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:58:58 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.