KItinerary

tickettokencomparator.cpp
1/*
2 SPDX-FileCopyrightText: 2018-2023 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "tickettokencomparator_p.h"
7#include "compare-logging.h"
8
9#include "uic9183/uic9183block.h"
10#include "uic9183/uic9183header.h"
11#include "uic9183/uic9183parser.h"
12
13#include <cstring>
14
15using namespace Qt::Literals::StringLiterals;
16using namespace KItinerary;
17
18/** Checks that @p lhs and @p rhs have a different prefix is they are both set. */
19[[nodiscard]] static bool prefixConflictIfPresent(const QString &lhs, const QString &rhs, Qt::CaseSensitivity caseSensitive = Qt::CaseSensitive)
20{
21 return !lhs.isEmpty() && !rhs.isEmpty() && !lhs.startsWith(rhs, caseSensitive) && !rhs.startsWith(lhs, caseSensitive);
22}
23[[nodiscard]] static bool prefixConflictIfPresent(const QByteArray &lhs, const QByteArray &rhs)
24{
25 return !lhs.isEmpty() && !rhs.isEmpty() && !lhs.startsWith(rhs) && !rhs.startsWith(lhs);
26}
27
28[[nodiscard]] static bool isUicBlockSubset(const Uic9183Parser &lhs, const Uic9183Parser &rhs)
29{
30 for (auto block = lhs.firstBlock(); !block.isNull(); block = block.nextBlock()) {
31 // ignore 0080VU blocks, those change in DB online tickets on every retrieval
32 if (std::strncmp(block.name(), "0080VU", 6) == 0) {
33 continue;
34 }
35 const auto rhsBlock = rhs.findBlock(block.name());
36 if (rhsBlock.isNull() || rhsBlock != block) {
37 return false;
38 }
39 }
40 return true;
41}
42
43[[nodiscard]] static bool compareUic918Token(const QByteArray &lhs, const QByteArray &rhs)
44{
46 return false;
47 }
48
49 Uic9183Parser lhsUic;
50 lhsUic.parse(lhs);
51 Uic9183Parser rhsUic;
52 rhsUic.parse(rhs);
53 if (!lhsUic.isValid() || !rhsUic.isValid()) {
54 return false;
55 }
56
57 return lhsUic.header() == rhsUic.header() && isUicBlockSubset(lhsUic, rhsUic) && isUicBlockSubset(rhsUic, lhsUic);
58}
59
60[[nodiscard]] static bool compareFlixbusToken(const QString &lhs, const QString &rhs)
61{
62 // Flixbus ticket tokens are URLs with varying host and query parts, while the path contains the actual token
63 QUrl lhsUrl(lhs);
64 QUrl rhsUrl(rhs);
65 return lhsUrl.path() == rhsUrl.path();
66}
67
68bool KItinerary::TicketTokenComparator::isSame(const QVariant &lhs, const QVariant &rhs)
69{
70 if (lhs.isNull() || rhs.isNull()) {
71 return true;
72 }
73 if (lhs.userType() != rhs.userType()) {
74 return false;
75 }
76
77 if (lhs.userType() == QMetaType::QString) {
78 const auto lhsS = lhs.toString();
79 const auto rhsS = rhs.toString();
80 if (lhsS.contains("flixbus."_L1) && rhsS.contains("flixbus."_L1)) {
81 return compareFlixbusToken(lhsS, rhsS);
82 }
83
84 return !prefixConflictIfPresent(lhsS, rhsS, Qt::CaseInsensitive);
85 }
86 if (lhs.userType() == QMetaType::QByteArray) {
87 const auto lhsBA = lhs.toByteArray();
88 const auto rhsBA = rhs.toByteArray();
89 if (!prefixConflictIfPresent(lhsBA, rhsBA)) {
90 return true;
91 }
92
93 return compareUic918Token(lhsBA, rhsBA);
94 }
95
96 qCWarning(CompareLog) << "unhandled ticket token type" << lhs << rhs;
97 return false;
98}
bool isNull() const
Checks if the block is valid or empty/default constructed.
Parser for UIC 918.3 and 918.3* train tickets.
Uic9183Block findBlock(const char name[6]) const
Returns the first block with the given name.
static bool maybeUic9183(const QByteArray &data)
Quickly checks if might be UIC 918.3 content.
Uic9183Block firstBlock() const
First data block in this ticket.
Uic9183Header header() const
Header found before the compressed payload.
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
bool isEmpty() const const
bool startsWith(QByteArrayView bv) const const
bool isEmpty() const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
CaseSensitivity
bool isNull() const const
QByteArray toByteArray() const const
QString toString() const const
int userType() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:45:38 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.