File indexing completed on 2024-05-12 04:42:34

0001 /*
0002     SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "efaparser.h"
0008 #include "logging.h"
0009 #include "scopedxmlstreamreader.h"
0010 
0011 #include <KPublicTransport/IndividualTransport>
0012 #include <KPublicTransport/Path>
0013 
0014 using namespace KPublicTransport;
0015 
0016 bool EfaRequestContext::isEmpty() const
0017 {
0018     return sessionId.isEmpty() || requestId.isEmpty() || sessionId == QLatin1String("0");
0019 }
0020 
0021 
0022 EfaParser::~EfaParser() = default;
0023 
0024 void EfaParser::setLocationIdentifierType(const QString& locationIdentifierType)
0025 {
0026     m_locationIdentifierType = locationIdentifierType;
0027 }
0028 
0029 Reply::Error EfaParser::error() const
0030 {
0031     return m_error;
0032 }
0033 
0034 QString EfaParser::errorMessage() const
0035 {
0036     return m_errorMsg;
0037 }
0038 
0039 IndividualTransport EfaParser::motTypeToIndividualTransportMode(int mot)
0040 {
0041     switch (mot) {
0042         case 100: return { IndividualTransport::Walk, IndividualTransport::None };
0043         case 101: return { IndividualTransport::Bike, IndividualTransport::Park };
0044         case 102: return { IndividualTransport::Bike, IndividualTransport::None };
0045         case 103:
0046         case 104:
0047             return { IndividualTransport::Car, IndividualTransport::None }; // TODO
0048     }
0049     qCDebug(Log) << "Unknown means of individual transport: " << mot;
0050     return IndividualTransport::Walk;
0051 }
0052 
0053 QPolygonF EfaParser::parsePathCoordinatesElement(ScopedXmlStreamReader &reader)
0054 {
0055     QPolygonF poly;
0056     // TODO do we need to support the format attributes, or is this always the same anyway?
0057     const auto coords = reader.readElementText().split(QLatin1Char(' '), Qt::SkipEmptyParts);
0058     poly.reserve(coords.size());
0059     for (const auto &coord : coords) {
0060         const auto p = coord.split(QLatin1Char(','));
0061         if (p.size() == 2) {
0062             poly.push_back({p[0].toDouble(), p[1].toDouble()});
0063         }
0064     }
0065     return poly;
0066 }
0067 
0068 Path EfaParser::polygonToPath(const QPolygonF &poly)
0069 {
0070     PathSection section;
0071     section.setPath(poly);
0072     Path path;
0073     path.setSections({section});
0074     return path;
0075 }
0076 
0077 bool EfaParser::isDummyStopId(QStringView id)
0078 {
0079     return id == QLatin1String("99999997") || id == QLatin1String("99999998");
0080 }
0081 
0082 EfaRequestContext EfaParser::requestContext() const
0083 {
0084     return m_requestContext;
0085 }
0086 
0087 QHash<QString, QString> EfaParser::parseKeyValueList(ScopedXmlStreamReader &&reader, QLatin1String elemName, QLatin1String keyName, QLatin1String valueName)
0088 {
0089     QHash<QString, QString> attrs;
0090     while (reader.readNextSibling()) {
0091         if (reader.name() == elemName) {
0092             auto attrReader = reader.subReader();
0093             QString name, value;
0094             while (attrReader.readNextSibling()) {
0095                 if (attrReader.name() == keyName) {
0096                     name = attrReader.readElementText();
0097                 } else if (attrReader.name() == valueName) {
0098                     value = attrReader.readElementText();
0099                 }
0100             }
0101             attrs.insert(name, value);
0102         }
0103     }
0104     return attrs;
0105 }