KItinerary

ssbv3ticket.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 "ssbv3ticket.h"
8
9#include <QDebug>
10
11#include <cstring>
12
13using namespace KItinerary;
14
15enum {
16 SSBV3_MIN_DATA_SIZE = 114,
17 SSBV3_MAX_DATA_SIZE = 122,
18 SSBV3_CHAR_WIDTH = 6,
19 SSBV3_VERSION = 3,
20};
21
22SSBv3Ticket::SSBv3Ticket() = default;
23
24SSBv3Ticket::SSBv3Ticket(const QByteArray &data)
25{
26 if (maybeSSB(data)) {
27 m_data = data;
28 } else {
29 qWarning() << "Trying to construct an SSB ticket from invalid data!";
30 }
31}
32
33SSBv3Ticket::~SSBv3Ticket() = default;
34
36{
37 return !m_data.isEmpty();
38}
39
40QString SSBv3Ticket::readString(int start, int length) const
41{
42 QString res;
43 res.resize(length);
44 for (int i = 0; i < length; ++i) {
45 res[i] = QLatin1Char((uint8_t)readNumber(start + SSBV3_CHAR_WIDTH * i, SSBV3_CHAR_WIDTH) + 32);
46 }
47 return res;
48}
49
51{
52 if (data.size() < SSBV3_MIN_DATA_SIZE || data.size() > SSBV3_MAX_DATA_SIZE) {
53 return false;
54 }
55 return (data.at(0) >> 4) == SSBV3_VERSION;
56}
57
58QDate SSBv3Ticket::issueDate(const QDateTime &contextDate) const
59{
60 if (m_data.isEmpty() || ticketTypeCode() > SSBv3Ticket::RPT) {
61 return {};
62 }
63
64 int year = contextDate.date().year();
65 if (year % 10 != yearOfIssue()) {
66 year += (10 + yearOfIssue() - year % 10) % 10;
67 }
68 QDate d(year, 1, 1);
69 d = d.addDays(issuingDay() - 1);
70 return d;
71}
72
74{
75 if (ticketTypeCode() != SSBv3Ticket::IRT_RES_BOA) {
76 return {};
77 }
78
79 const auto d = issueDate(contextDate);
80 return d.addDays(type1DepartureDate());
81}
82
84{
85 if (ticketTypeCode() != SSBv3Ticket::NRT) {
86 return {};
87 }
88
89 return issueDate(contextDate).addDays(type2FirstDayOfValidity());
90}
91
93{
94 if (ticketTypeCode() != SSBv3Ticket::NRT) {
95 return {};
96 }
97
98 return issueDate(contextDate).addDays(type2LastDayOfValidity());
99}
100
102{
103 if (ticketTypeCode() != SSBv3Ticket::GRT) {
104 return {};
105 }
106
107 return issueDate(contextDate).addDays(type3FirstDayOfValidity());
108}
109
111{
112 if (ticketTypeCode() != SSBv3Ticket::GRT) {
113 return {};
114 }
115
116 return issueDate(contextDate).addDays(type3LastDayOfValidity());
117}
118
119QByteArray SSBv3Ticket::rawData() const
120{
121 return m_data;
122}
123
124#include "moc_ssbv3ticket.cpp"
Q_INVOKABLE QDate issueDate(const QDateTime &contextDate=QDateTime::currentDateTime()) const
Date of issue.
Q_INVOKABLE QDate type2ValidFrom(const QDateTime &contextDate=QDateTime::currentDateTime()) const
First day of validity for type 2 (NRT) tickets.
Q_INVOKABLE QDate type3ValidUntil(const QDateTime &contextDate=QDateTime::currentDateTime()) const
Last day of validity for type 3 (GRP) tickets.
bool isValid() const
Returns true if this is a valid SSB ticket.
Q_INVOKABLE QDate type3ValidFrom(const QDateTime &contextDate=QDateTime::currentDateTime()) const
First day of validity for type 3 (GRP) tickets.
Q_INVOKABLE QDate type2ValidUntil(const QDateTime &contextDate=QDateTime::currentDateTime()) const
Last day of validity for type 2 (NRT) tickets.
static bool maybeSSB(const QByteArray &data)
Returns true if data might be an ERA SSB ticket.
Q_INVOKABLE QDate type1DepartureDay(const QDateTime &contextDate=QDateTime::currentDateTime()) const
Departure day for type 1 (IRT/RES/BOA) tickets.
Q_SCRIPTABLE QString start(QString train="")
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
char at(qsizetype i) const const
qsizetype size() const const
QDate addDays(qint64 ndays) const const
int year() const const
QDate date() const const
void resize(qsizetype newSize, QChar fillChar)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:52:35 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.