File indexing completed on 2024-12-29 04:51:01

0001 /*
0002     SPDX-FileCopyrightText: 2019-2021 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "vdvdocumentprocessor.h"
0008 
0009 #include "asn1/berelement.h"
0010 
0011 #include <KItinerary/ExtractorResult>
0012 #include <KItinerary/Ticket>
0013 #include <KItinerary/VdvTicket>
0014 #include <KItinerary/VdvTicketContent>
0015 #include <KItinerary/VdvTicketParser>
0016 
0017 #include <KLocalizedString>
0018 
0019 #include <QByteArray>
0020 
0021 using namespace KItinerary;
0022 
0023 bool VdvDocumentProcessor::canHandleData(const QByteArray &encodedData, [[maybe_unused]] QStringView fileName) const
0024 {
0025     return VdvTicketParser::maybeVdvTicket(encodedData);
0026 }
0027 
0028 ExtractorDocumentNode VdvDocumentProcessor::createNodeFromData(const QByteArray &encodedData) const
0029 {
0030     VdvTicketParser p;
0031     if (!p.parse(encodedData)) {
0032         return {};
0033     }
0034 
0035     ExtractorDocumentNode node;
0036     node.setContent(p.ticket());
0037     return node;
0038 }
0039 
0040 void VdvDocumentProcessor::preExtract(ExtractorDocumentNode &node, [[maybe_unused]] const ExtractorEngine *engine) const
0041 {
0042     const auto vdv = node.content<VdvTicket>();
0043 
0044     Seat seat;
0045     switch (vdv.serviceClass()) {
0046         case VdvTicket::UnknownClass:
0047             break;
0048         case VdvTicket::FirstClass:
0049         case VdvTicket::FirstClassUpgrade:
0050             seat.setSeatingType(QStringLiteral("1"));
0051             break;
0052         case VdvTicket::SecondClass:
0053             seat.setSeatingType(QStringLiteral("2"));
0054             break;
0055         default:
0056             break;
0057     }
0058 
0059     Ticket ticket;
0060     ticket.setTicketToken(QLatin1StringView("aztecbin:") +
0061                           QString::fromLatin1(vdv.rawData().toBase64()));
0062     ticket.setTicketedSeat(seat);
0063     if (vdv.serviceClass() == VdvTicket::FirstClassUpgrade) {
0064         ticket.setName(i18n("Upgrade"));
0065     } else if (const auto hdr = vdv.header(); hdr && hdr->productId == 9999) {
0066       ticket.setName(QLatin1StringView("Deutschlandticket"));
0067     } else {
0068         ticket.setName(i18n("Ticket"));
0069     }
0070     ticket.setTicketNumber(vdv.ticketNumber());
0071     ticket.setValidFrom(vdv.beginDateTime());
0072     ticket.setValidUntil(vdv.endDateTime());
0073     ticket.setUnderName(vdv.person());
0074 
0075     if (const auto basicData = vdv.productData().find(VdvTicketBasicData::Tag).contentAt<VdvTicketBasicData>(); basicData && basicData->price) {
0076         ticket.setTotalPrice(basicData->price / 100.0);
0077         ticket.setPriceCurrency(QStringLiteral("EUR"));
0078     }
0079 
0080     node.addResult(QList<QVariant>({ticket}));
0081 }