field.cpp 2.66 KB
Newer Older
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"
Volker Krause's avatar
Volker Krause committed
8
#include "pass.h"
9
#include "pass_p.h"
10

11
#include <QGuiApplication>
12
13
14
15
#include <QJsonObject>

using namespace KPkPass;

Laurent Montel's avatar
Laurent Montel committed
16
17
18
19
namespace KPkPass
{
class FieldPrivate
{
Volker Krause's avatar
Volker Krause committed
20
public:
Volker Krause's avatar
Volker Krause committed
21
    const Pass *pass = nullptr;
Volker Krause's avatar
Volker Krause committed
22
23
24
25
26
27
28
29
30
    QJsonObject obj;
};
}

Field::Field()
    : d(new FieldPrivate)
{
}

Laurent Montel's avatar
Laurent Montel committed
31
32
Field::Field(const Field &) = default;
Field::Field(Field &&) = default;
Volker Krause's avatar
Volker Krause committed
33
Field::~Field() = default;
Laurent Montel's avatar
Laurent Montel committed
34
Field &Field::operator=(const Field &) = default;
Volker Krause's avatar
Volker Krause committed
35

Volker Krause's avatar
Volker Krause committed
36
Field::Field(const QJsonObject &obj, const Pass *pass)
Volker Krause's avatar
Volker Krause committed
37
    : d(new FieldPrivate)
38
{
Volker Krause's avatar
Volker Krause committed
39
    d->pass = pass;
Volker Krause's avatar
Volker Krause committed
40
41
42
43
44
    d->obj = obj;
}

QString Field::key() const
{
Volker Krause's avatar
Volker Krause committed
45
    return d->obj.value(QLatin1String("key")).toString();
46
47
48
49
}

QString Field::label() const
{
Volker Krause's avatar
Volker Krause committed
50
    if (d->pass) {
51
        return d->pass->d->message(d->obj.value(QLatin1String("label")).toString());
Volker Krause's avatar
Volker Krause committed
52
53
54
55
56
57
    }
    return {};
}

QVariant Field::value() const
{
Volker Krause's avatar
Volker Krause committed
58
    if (!d->pass) {
Volker Krause's avatar
Volker Krause committed
59
60
        return {};
    }
61
    auto v = d->pass->d->message(d->obj.value(QLatin1String("attributedValue")).toString());
Volker Krause's avatar
Volker Krause committed
62
    if (v.isEmpty()) {
63
        v = d->pass->d->message(d->obj.value(QLatin1String("value")).toString());
Volker Krause's avatar
Volker Krause committed
64
    }
65
66
67
68
69
70
71

    const auto dt = QDateTime::fromString(v, Qt::ISODate);
    if (dt.isValid()) {
        return dt;
    }

    // TODO number detection
Volker Krause's avatar
Volker Krause committed
72
73
74
75
76
    return v;
}

QString Field::valueDisplayString() const
{
77
78
79
80
81
82
83
84
85
86
87
    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
Volker Krause's avatar
Volker Krause committed
88
    return value().toString().trimmed();
89
90
}

Volker Krause's avatar
Volker Krause committed
91
QString Field::changeMessage() const
92
{
Volker Krause's avatar
Volker Krause committed
93
    if (!d->pass) {
Volker Krause's avatar
Volker Krause committed
94
95
        return {};
    }
96
    auto msg = d->pass->d->message(d->obj.value(QLatin1String("changeMessage")).toString());
97
    msg.replace(QLatin1String("%@"), valueDisplayString());
Volker Krause's avatar
Volker Krause committed
98
    return msg;
99
}
100
101
102
103
104
105
106
107
108
109
110
111
112

Qt::Alignment Field::textAlignment() const
{
    const auto alignStr = d->obj.value(QLatin1String("textAlignment")).toString();
    if (alignStr == QLatin1String("PKTextAlignmentLeft")) {
        return Qt::AlignLeft;
    } else if (alignStr == QLatin1String("PKTextAlignmentCenter")) {
        return Qt::AlignHCenter;
    } else if (alignStr == QLatin1String("PKTextAlignmentRight")) {
        return Qt::AlignRight;
    }
    return QGuiApplication::layoutDirection() == Qt::LeftToRight ? Qt::AlignLeft : Qt::AlignRight;
}
Volker Krause's avatar
Volker Krause committed
113
114

#include "moc_field.cpp"