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

Expand KPkPass::Field API

parent f71477c8
......@@ -41,7 +41,10 @@ private slots:
QCOMPARE(headers.size(), 2);
auto field = headers.at(0);
QCOMPARE(field.label(), QLatin1String("Sitzplatz"));
QCOMPARE(field.value(), QLatin1String("10E"));
QCOMPARE(field.value(), QVariant(QLatin1String("10E")));
QCOMPARE(field.valueDisplayString(), QLatin1String("10E"));
QCOMPARE(field.key(), QLatin1String("seat"));
QCOMPARE(field.changeMessage(), QStringLiteral("Sitzplatznummer geändert in 10E"));
auto boardingPass = dynamic_cast<KPkPass::BoardingPass*>(file.get());
QVERIFY(boardingPass);
......
......@@ -24,18 +24,72 @@
using namespace KPkPass;
namespace KPkPass {
class FieldPrivate {
public:
const File *file = nullptr;
QJsonObject obj;
};
}
Field::Field()
: d(new FieldPrivate)
{
}
Field::Field(const Field&) = default;
Field::Field(Field&&) = default;
Field::~Field() = default;
Field& Field::operator=(const Field&) = default;
Field::Field(const QJsonObject &obj, const File *file)
: d(new FieldPrivate)
{
m_label = file->message(obj.value(QLatin1String("label")).toString());
m_value = file->message(obj.value(QLatin1String("value")).toString());
d->file = file;
d->obj = obj;
}
QString Field::key() const
{
if (d->file) {
return d->obj.value(QLatin1String("key")).toString();
}
return {};
}
QString Field::label() const
{
return m_label;
if (d->file) {
return d->file->message(d->obj.value(QLatin1String("label")).toString());
}
return {};
}
QVariant Field::value() const
{
if (!d->file) {
return {};
}
auto v = d->file->message(d->obj.value(QLatin1String("attributedValue")).toString());
if (v.isEmpty()) {
v = d->file->message(d->obj.value(QLatin1String("value")).toString());
}
// TODO number and date/time detection
return v;
}
QString Field::valueDisplayString() const
{
// TODO respect number and date/time formatting options
return value().toString();
}
QString Field::value() const
QString Field::changeMessage() const
{
return m_value;
if (!d->file) {
return {};
}
auto msg = d->file->message(d->obj.value(QLatin1String("changeMessage")).toString());
msg = msg.replace(QLatin1String("%@"), valueDisplayString());
return msg;
}
......@@ -25,29 +25,58 @@
#include <QMetaType>
#include <QString>
#include <memory>
class QJsonObject;
namespace KPkPass {
class File;
class FieldPrivate;
/** Field element in a KPkPass::File. */
/** Field element in a KPkPass::File.
* @see https://developer.apple.com/library/content/documentation/UserExperience/Reference/PassKit_Bundle/Chapters/FieldDictionary.html
*/
class KPKPASS_EXPORT Field
{
Q_GADGET
Q_PROPERTY(QString key READ key CONSTANT)
Q_PROPERTY(QString label READ label CONSTANT)
Q_PROPERTY(QString value READ value CONSTANT)
Q_PROPERTY(QVariant value READ value CONSTANT)
Q_PROPERTY(QString valueDisplayString READ valueDisplayString CONSTANT)
Q_PROPERTY(QString changeMessage READ changeMessage CONSTANT)
public:
Field() = default;
explicit Field(const QJsonObject &obj, const File *file);
~Field() = default;
Field();
Field(const Field&);
Field(Field&&);
~Field();
Field& operator=(const Field&);
/** Field key, unique in the pass but not meant for display. */
QString key() const;
/** Localized label for display describing this field. */
QString label() const;
QString value() const;
/** Value of this field.
* This can either be a localized string (most common), a date/time value or a number.
* Use this for data extraction, prefer valueDisplayString() for displaying data.
*/
QVariant value() const;
/** Value of this field, as a localized string for display.
* Use this rather than value() for display.
*/
QString valueDisplayString() const;
/** The localized change message for this value. */
QString changeMessage() const;
// TODO add textAlignment property
private:
QString m_label;
QString m_value;
friend class File;
explicit Field(const QJsonObject &obj, const File *file);
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