KItinerary

vdvdocumentprocessor.cpp
1/*
2 SPDX-FileCopyrightText: 2019-2021 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "vdvdocumentprocessor.h"
8
9#include "asn1/berelement.h"
10
11#include <KItinerary/ExtractorResult>
12#include <KItinerary/Ticket>
13#include <KItinerary/VdvTicket>
14#include <KItinerary/VdvTicketContent>
15#include <KItinerary/VdvTicketParser>
16
17#include <KLocalizedString>
18
19#include <QByteArray>
20
21using namespace Qt::Literals::StringLiterals;
22using namespace KItinerary;
23
24bool VdvDocumentProcessor::canHandleData(const QByteArray &encodedData, [[maybe_unused]] QStringView fileName) const
25{
26 return VdvTicketParser::maybeVdvTicket(encodedData);
27}
28
30{
32 if (!p.parse(encodedData)) {
33 return {};
34 }
35
37 node.setContent(p.ticket());
38 return node;
39}
40
41void VdvDocumentProcessor::preExtract(ExtractorDocumentNode &node, [[maybe_unused]] const ExtractorEngine *engine) const
42{
43 const auto vdv = node.content<VdvTicket>();
44
45 Seat seat;
46 switch (vdv.serviceClass()) {
47 case VdvTicket::UnknownClass:
48 break;
49 case VdvTicket::FirstClass:
50 case VdvTicket::FirstClassUpgrade:
51 seat.setSeatingType(QStringLiteral("1"));
52 break;
53 case VdvTicket::SecondClass:
54 seat.setSeatingType(QStringLiteral("2"));
55 break;
56 default:
57 break;
58 }
59
60 Ticket ticket;
61 ticket.setTicketToken("aztecbin:"_L1 + QString::fromLatin1(vdv.rawData().toBase64()));
62 ticket.setTicketedSeat(seat);
63 if (vdv.serviceClass() == VdvTicket::FirstClassUpgrade) {
64 ticket.setName(i18n("Upgrade"));
65 } else if (const auto hdr = vdv.header(); hdr) {
66 switch (hdr->productId) {
67 case 9996:
68 ticket.setName("Deutschlandsemesterticket"_L1);
69 break;
70 case 9999:
71 ticket.setName("Deutschlandticket"_L1);
72 break;
73 }
74 }
75 if (ticket.name().isEmpty()) {
76 ticket.setName(i18n("Ticket"));
77 }
78 ticket.setTicketNumber(vdv.ticketNumber());
79 ticket.setValidFrom(vdv.beginDateTime());
80 ticket.setValidUntil(vdv.endDateTime());
81 ticket.setUnderName(vdv.person());
82
83 if (const auto basicData = vdv.productData().find(VdvTicketBasicData::Tag).contentAt<VdvTicketBasicData>(); basicData && basicData->price) {
84 ticket.setTotalPrice(basicData->price / 100.0);
85 ticket.setPriceCurrency(QStringLiteral("EUR"));
86 }
87
88 node.addResult(QList<QVariant>({ticket}));
89}
A node in the extracted document object tree.
QJSValue content
The decoded content of this node.
void addResult(ExtractorResult &&result)
Add additional results from an extraction step.
void setContent(const QVariant &content)
Set decoded content.
Semantic data extraction engine.
A reserved seat.
Definition ticket.h:23
A booked ticket.
Definition ticket.h:41
ExtractorDocumentNode createNodeFromData(const QByteArray &encodedData) const override
Create a document node from raw data.
bool canHandleData(const QByteArray &encodedData, QStringView fileName) const override
Fast check whether the given encoded data can possibly be processed by this instance.
void preExtract(ExtractorDocumentNode &node, const ExtractorEngine *engine) const override
Called before extractors are applied to node.
Product specific data - basic information.
Parser for VDV tickets.
VdvTicket ticket() const
Returns the parsed ticket data.
bool parse(const QByteArray &data)
Tries to parse the ticket in data.
static bool maybeVdvTicket(const QByteArray &data)
Fast check if data might contain a VDV ticket.
Ticket information from a VDV barcode.
Definition vdvticket.h:30
QString i18n(const char *text, const TYPE &arg...)
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
QString fromLatin1(QByteArrayView str)
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:28:48 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.