KItinerary

uic9183flex.cpp
1/*
2 SPDX-FileCopyrightText: 2025 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "uic9183flex.h"
7
8#include "era/fcbutil.h"
9
10#include "variantvisitor_p.h"
11
12#include <KItinerary/Place>
13
14#include <optional>
15
16using namespace Qt::Literals;
17using namespace KItinerary;
18
19namespace KItinerary {
20class Uic9183FlexPrivate : public QSharedData
21{
22public:
23 std::optional<std::variant<Fcb::v13::UicRailTicketData, Fcb::v3::UicRailTicketData>> m_data;
24 Uic9183Block m_block;
25};
26}
27
28Uic9183Flex::Uic9183Flex() = default;
29Uic9183Flex::Uic9183Flex(const Uic9183Block &block)
30 : d(new Uic9183FlexPrivate)
31{
32 if (block.version() == 3) {
33 if (auto fcb = Fcb::v3::UicRailTicketData(block); fcb.isValid()) {
34 d->m_data = std::move(fcb);
35 d->m_block = block;
36 return;
37 }
38 }
39 if (auto fcb = Fcb::v13::UicRailTicketData(block); fcb.isValid()) {
40 d->m_data = std::move(fcb);
41 d->m_block = block;
42 }
43}
44
45Uic9183Flex::Uic9183Flex(const Uic9183Flex&) = default;
46Uic9183Flex& Uic9183Flex::operator=(const Uic9183Flex&) = default;
47Uic9183Flex::~Uic9183Flex() = default;
48
50{
51 return d && !d->m_block.isNull() && d->m_data.has_value();
52}
53
55{
56 return isValid() ? std::visit([](auto &&data) { return data.issuingDetail.issueingDateTime(); }, *d->m_data) : QDateTime();
57}
58
60{
61 return isValid() && !std::visit([](auto &&data) { return data.transportDocument.empty(); }, *d->m_data);
62}
63
65{
66 return isValid() ? std::visit([](auto &&data) {
68 l.reserve(data.transportDocument.size());
69 std::transform(data.transportDocument.begin(), data.transportDocument.end(), std::back_inserter(l), [](const auto &doc) { return doc.ticket; });
70 return l;
71 }, *d->m_data) : QList<QVariant>();
72}
73
74const std::variant<Fcb::v13::UicRailTicketData, Fcb::v3::UicRailTicketData>& Uic9183Flex::fcb() const
75{
76 return *d->m_data;
77}
78
79QVariant Uic9183Flex::fcbVariant() const
80{
81 return isValid() ? std::visit([](auto &&data) { return QVariant::fromValue(data); }, *d->m_data) : QVariant();
82}
83
85{
86 VariantVisitor([&station](auto &&data) {
87 station.setName(data.fromStationNameUTF8);
88 station.setIdentifier(FcbUtil::fromStationIdentifier(data));
89 }).visit<Fcb::v13::ReservationData, Fcb::v13::OpenTicketData, Fcb::v3::ReservationData, Fcb::v3::OpenTicketData>(doc);
90 fixStationCode(station);
91}
92
94{
95 VariantVisitor([&station](auto &&data) {
96 station.setName(data.toStationNameUTF8);
97 station.setIdentifier(FcbUtil::toStationIdentifier(data));
98 }).visit<Fcb::v13::ReservationData, Fcb::v13::OpenTicketData, Fcb::v3::ReservationData, Fcb::v3::OpenTicketData>(doc);
99 fixStationCode(station);
100}
101
103{
104 // UIC codes in Germany are wildly unreliable, there seem to be different
105 // code tables in use by different operators, so we unfortunately have to ignore
106 // those entirely
107 if (station.identifier().startsWith("uic:80"_L1)) {
108 PostalAddress addr;
109 addr.setAddressCountry(u"DE"_s);
110 station.setAddress(addr);
111 station.setIdentifier(QString());
112 }
113}
114
115#include "moc_uic9183flex.cpp"
static QString toStationIdentifier(CodeTableTypeT stationCodeTable, const T &doc)
Arrival station identifier for a travel document, in the format needed for output with our JSON-LD fo...
Definition fcbutil.h:48
static QString fromStationIdentifier(CodeTableTypeT stationCodeTable, const T &doc)
Departure station identifier for a travel document, in the format needed for output with our JSON-LD ...
Definition fcbutil.h:34
Top-level type for the ERA FCB ticket structure.
Definition fcbticket.h:1007
QString identifier
Identifier.
Definition place.h:85
Postal address.
Definition place.h:46
Train station.
Definition place.h:126
A data block from a UIC 918.3 ticket.
int version() const
Returns the version number of this block.
Represents a U_FLEX block holding different versions of an FCB payload.
Definition uic9183flex.h:24
QDateTime issuingDateTime() const
Issuing date/time.
bool isValid() const
Returns whether this is a valid U_FLEX layout block.
static void readArrivalStation(const QVariant &doc, TrainStation &station)
Read arrival station info from the given FCB travel document, if applicable.
static void fixStationCode(TrainStation &station)
Fix known issues with station identifiers.
static void readDepartureStation(const QVariant &doc, TrainStation &station)
Read departure station info from the given FCB travel document, if applicable.
QList< QVariant > transportDocuments() const
Transport documents of the contained FCB.
bool hasTransportDocument() const
true when this is a valid FCB with at least one transport document.
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
void reserve(qsizetype size)
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QVariant fromValue(T &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:52:36 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.