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 #ifndef KPUBLICTRANSPORT_EFAPARSER_H
0008 #define KPUBLICTRANSPORT_EFAPARSER_H
0009 
0010 #include "kpublictransport_export.h"
0011 
0012 #include <KPublicTransport/Line>
0013 #include <KPublicTransport/Reply>
0014 
0015 #include <QHash>
0016 #include <QString>
0017 
0018 #include <vector>
0019 
0020 class QByteArray;
0021 class QPolygonF;
0022 
0023 namespace KPublicTransport {
0024 
0025 class IndividualTransport;
0026 class Journey;
0027 class JourneySection;
0028 class Location;
0029 class Path;
0030 class ScopedXmlStreamReader;
0031 class Stopover;
0032 
0033 /** Journey query context for previous/next queries.
0034  *  @internal only exported for unit tests
0035  */
0036 class KPUBLICTRANSPORT_EXPORT EfaRequestContext
0037 {
0038 public:
0039     QString sessionId;
0040     QString requestId;
0041 
0042     bool isEmpty() const;
0043 };
0044 
0045 /** Base class for parsers for responses from EFA services.
0046  *  @internal just exported for unit tests
0047  */
0048 class KPUBLICTRANSPORT_EXPORT EfaParser
0049 {
0050 public:
0051     virtual ~EfaParser();
0052 
0053     void setLocationIdentifierType(const  QString &locationIdentifierType);
0054 
0055     Reply::Error error() const;
0056     QString errorMessage() const;
0057 
0058     virtual std::vector<Location> parseStopFinderResponse(const QByteArray &data) = 0;
0059     virtual std::vector<Stopover> parseDmResponse(const QByteArray &data) = 0;
0060     virtual std::vector<Journey> parseTripResponse(const QByteArray &data) = 0;
0061 
0062     EfaRequestContext requestContext() const;
0063 
0064 protected:
0065     /** Same as the above, for individual transport modes. */
0066     static IndividualTransport motTypeToIndividualTransportMode(int mot);
0067 
0068     /** Parse path encoded as a space separated string of coordinate pairs. */
0069     static QPolygonF parsePathCoordinatesElement(ScopedXmlStreamReader &reader);
0070     /** Turn a polygon into a section path. */
0071     static Path polygonToPath(const QPolygonF &poly);
0072 
0073     /** Returns @c true if the given stop id is a dummy value used for non-stops. */
0074     static bool isDummyStopId(QStringView id);
0075 
0076     /** Parses a key/value list structure. */
0077     static QHash<QString, QString> parseKeyValueList(ScopedXmlStreamReader &&reader, QLatin1String elemName, QLatin1String keyName, QLatin1String valueName);
0078 
0079     QString m_locationIdentifierType;
0080     QString m_errorMsg;
0081     Reply::Error m_error = Reply::NoError;
0082     EfaRequestContext m_requestContext;
0083 };
0084 
0085 }
0086 
0087 Q_DECLARE_METATYPE(KPublicTransport::EfaRequestContext)
0088 
0089 #endif // KPUBLICTRANSPORT_EFAPARSER_H