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() || firstDayOfValidityDay() > 366) {
58 return {};
59 }
60
61 QDate dt(contextDate.date().year(), 1, 1);
62 dt = dt.addDays(firstDayOfValidityDay() - 1);
63 if (dt < contextDate.date()) {
64 QDate dt(contextDate.date().year() + 1, 1, 1);
65 dt = dt.addDays(firstDayOfValidityDay() - 1);
66 }
67 return dt;
68}
69
71{
72 if (!isValid() || lastDayOfValidityDay() > 366) {
73 return {};
74 }
75
76 QDate dt(contextDate.date().year(), 1, 1);
77 dt = dt.addDays(lastDayOfValidityDay() - 1);
78 if (dt < contextDate.date() || dt < firstDayOfValidity()) {
79 QDate dt(contextDate.date().year() + 1, 1, 1);
80 dt = dt.addDays(firstDayOfValidityDay() - 1);
81 }
82 return dt;
83}
84
85QByteArray SSBv2Ticket::rawData() const
86{
87 return m_data;
88}
89
90#include "moc_ssbv2ticket.cpp"
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
QDate addDays(qint64 ndays) const const
int year() const const
QDate date() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:14:49 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.