KItinerary

iatabcbpparser.cpp
1 /*
2  SPDX-FileCopyrightText: 2018 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "iatabcbpparser.h"
8 #include "logging.h"
9 #include "iata/iatabcbp.h"
10 
11 #include <KItinerary/Flight>
12 #include <KItinerary/Organization>
13 #include <KItinerary/Person>
14 #include <KItinerary/Place>
15 #include <KItinerary/Reservation>
16 #include <KItinerary/Ticket>
17 
18 #include <QVariant>
19 
20 using namespace KItinerary;
21 
22 static QString stripLeadingZeros(const QString &s)
23 {
24  const auto it = std::find_if(s.begin(), s.end(), [](const QChar &c) { return c != QLatin1Char('0'); });
25  const auto d = std::distance(s.begin(), it);
26  return s.mid(d);
27 }
28 
30 {
31  IataBcbp bcbp(message);
32  if (!bcbp.isValid()) {
33  return {};
34  }
35  return parse(bcbp, externalIssueDate.isValid() ? externalIssueDate : QDateTime({1970, 1, 1}, {}));
36 }
37 
38 QVector<QVariant> IataBcbpParser::parse(const IataBcbp &bcbp, const QDateTime &contextDate)
39 {
40  const auto count = bcbp.uniqueMandatorySection().numberOfLegs();
41  const auto issueDate = bcbp.uniqueConditionalSection().dateOfIssue(contextDate);
42 
43  QVector<QVariant> result;
44  result.reserve(count);
45 
46  Person person;
47  {
48  const auto fullName = bcbp.uniqueMandatorySection().passengerName();
49  const auto idx = fullName.indexOf(QLatin1Char('/'));
50  if (idx > 0 && idx < fullName.size() - 1) {
51  person.setFamilyName(fullName.left(idx));
52  person.setGivenName(fullName.mid(idx + 1));
53  } else {
54  person.setName(fullName);
55  }
56  }
57 
58  Ticket ticket;
59  ticket.setTicketToken(QStringLiteral("aztecCode:") + bcbp.rawData());
60 
61  for (auto i = 0; i < count; ++i) {
62  Flight flight;
63 
64  const auto rms = bcbp.repeatedMandatorySection(i);
65  flight.setDepartureDay(rms.dateOfFlight(issueDate.isValid() ? QDateTime(issueDate, {}) : contextDate));
66 
67  Airport dep;
68  dep.setIataCode(rms.fromCityAirportCode());
69  flight.setDepartureAirport(dep);
70  Airport arr;
71  arr.setIataCode(rms.toCityAirportCode());
72  flight.setArrivalAirport(arr);
73  Airline airline;
74  airline.setIataCode(rms.operatingCarrierDesignator());
75  flight.setAirline(airline);
76  flight.setFlightNumber(stripLeadingZeros(rms.flightNumber()));
77 
79  res.setReservationFor(flight);
80  res.setPassengerSequenceNumber(stripLeadingZeros(rms.checkinSequenceNumber()));
81  res.setAirplaneSeat(stripLeadingZeros(rms.seatNumber()));
82  res.setReservationNumber(rms.operatingCarrierPNRCode());
83  res.setUnderName(person);
84  res.setReservedTicket(ticket);
85 
86  const auto rcs = bcbp.repeatedConditionalSection(i);
87  if (!rcs.frequenFlyerNumber().isEmpty()) {
88  ProgramMembership program;
89  program.setMembershipNumber(rcs.frequenFlyerNumber());
90  program.setProgramName(rcs.frequentFlyerAirlineDesignator());
91  program.setMember(person);
92  res.setProgramMembershipUsed(program);
93  }
94 
95  result.push_back(std::move(res));
96  }
97 
98  return result;
99 }
QString::iterator begin()
int size() const const
A booked ticket.
Definition: ticket.h:38
void push_back(const T &value)
QVector< QVariant > parse(const QString &message, const QDateTime &externalIssueDateTime=QDateTime())
Parses the bar coded boarding pass message message into a list of FlightReservation instances.
A person.
Definition: person.h:19
QString::iterator end()
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
void reserve(int size)
A frequent traveler, bonus points or discount scheme program membership.
Q_INVOKABLE KItinerary::IataBcbpRepeatedMandatorySection repeatedMandatorySection(int leg) const
Mandatory section of leg.
Definition: iatabcbp.cpp:82
A IATA BarCoded Boarding Pass (BCBP)
Definition: iatabcbp.h:20
Airport.
Definition: place.h:102
Q_INVOKABLE KItinerary::IataBcbpRepeatedConditionalSection repeatedConditionalSection(int leg) const
Conditional (optional) section of leg.
Definition: iatabcbp.cpp:91
A flight.
Definition: flight.h:24
QString left(int n) const const
bool isValid() const const
QString mid(int position, int n) const const
A flight reservation.
Definition: reservation.h:81
QString message
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Wed Aug 17 2022 03:58:04 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.