KItinerary

ssbv2ticket.cpp
1/*
2 SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "ssbv2ticket.h"
8
9#include <QDebug>
10
11using namespace KItinerary;
12
13enum {
14 SSBV2_DATA_SIZE_MIN = 67,
15 SSBV2_DATA_SIZE_MAX = 114,
16 SSBV2_VERSION = 2,
17};
18
19SSBv2Ticket::SSBv2Ticket() = default;
20
21SSBv2Ticket::SSBv2Ticket(const QByteArray &data)
22{
23 if (maybeSSB(data)) {
24 m_data = data;
25
26 // additional sanity checking to catch the maybeSSB heuristic not being good enough
27 // trainNumber() > 99999 would also be an effective check, wouldn't it be for the Trenitalia
28 // deviations from the SSBv2 spec...
29 if (numberOfAdultPassengers() > 99 || numberOfChildPassengers() > 99)
30 {
31 m_data.clear();
32 }
33 }
34
35 if (m_data.isEmpty()) {
36 qWarning() << "Trying to construct an SSB ticket from invalid data!";
37 }
38}
39
40SSBv2Ticket::~SSBv2Ticket() = default;
41
43{
44 return !m_data.isEmpty();
45}
46
48{
49 if (data.size() < SSBV2_DATA_SIZE_MIN || data.size() > SSBV2_DATA_SIZE_MAX) {
50 return false;
51 }
52 return (data.at(0) >> 4) == SSBV2_VERSION;
53}
54
56{
57 if (!isValid()) {
58 return {};
59 }
60 return dayNumberToDate(firstDayOfValidityDay(), contextDate);
61}
62
64{
65 if (!isValid()) {
66 return {};
67 }
68 return dayNumberToDate(lastDayOfValidityDay(), contextDate);
69}
70
71QByteArray SSBv2Ticket::rawData() const
72{
73 return m_data;
74}
75
76#include "moc_ssbv2ticket.cpp"
static QDate dayNumberToDate(int days, const QDateTime &context)
Convert a SSBv1 or v2 day number to a date based on context.
Q_INVOKABLE QDate firstDayOfValidity(const QDateTime &contextDate=QDateTime::currentDateTime()) const
First day of validity.
Q_INVOKABLE QDate lastDayOfValidity(const QDateTime &contextDate=QDateTime::currentDateTime()) const
Last day of validity.
static bool maybeSSB(const QByteArray &data)
Returns true if data might be an ERA SSB ticket.
bool isValid() const
Returns true if this is a valid SSB ticket.
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
char at(qsizetype i) const const
void clear()
bool isEmpty() const const
qsizetype size() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:58 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.