File indexing completed on 2024-12-08 07:19:10

0001 /*
0002     SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KPUBLICTRANSPORT_JOURNEYREQUEST_H
0008 #define KPUBLICTRANSPORT_JOURNEYREQUEST_H
0009 
0010 #include "kpublictransport_export.h"
0011 
0012 #include <KPublicTransport/Datatypes>
0013 #include <KPublicTransport/IndividualTransport>
0014 #include <KPublicTransport/Journey>
0015 
0016 #include <QMetaType>
0017 #include <QSharedDataPointer>
0018 #include <QVariant>
0019 
0020 #include <vector>
0021 
0022 class QDateTime;
0023 
0024 namespace KPublicTransport {
0025 
0026 class AbstractBackend;
0027 class JourneyReply;
0028 class JourneyRequestPrivate;
0029 class Location;
0030 class Manager;
0031 class RequestContext;
0032 
0033 /** Describes a journey search.
0034  *  By default journeys departing now are searched.
0035  */
0036 class KPUBLICTRANSPORT_EXPORT JourneyRequest
0037 {
0038     KPUBLICTRANSPORT_GADGET(JourneyRequest)
0039 
0040     /** The starting point of the journey search. */
0041     KPUBLICTRANSPORT_PROPERTY(KPublicTransport::Location, from, setFrom)
0042     /** The journey destination. */
0043     KPUBLICTRANSPORT_PROPERTY(KPublicTransport::Location, to, setTo)
0044     /** Date/time at which the journey should start/end. */
0045     KPUBLICTRANSPORT_PROPERTY(QDateTime, dateTime, setDateTime)
0046 
0047     /** Modes of transportation that should be considered for this query.
0048      *  Only transit modes matter (public transport, rented vehicles, etc),
0049      *  values for tranfers/waits/etc are ignored.
0050      */
0051     KPUBLICTRANSPORT_PROPERTY(KPublicTransport::JourneySection::Modes, modes, setModes)
0052     /** The maximum amount of expected results.
0053      *  @note This is only an optimization hint for backends, not a guarantee
0054      *  that all results comply with this constraint.
0055      */
0056     KPUBLICTRANSPORT_PROPERTY(int, maximumResults, setMaximumResults)
0057     /** Retrieve intermediate stops for the queried journeys as well.
0058      *  @note This is only an optimization hint for backends, not a guarantee
0059      *  that all results will contain this information.
0060      */
0061     KPUBLICTRANSPORT_PROPERTY(bool, includeIntermediateStops, setIncludeIntermediateStops)
0062     /** Retrieve path details for the journeys.
0063      *  @note This is only an optimization hint for backends, not a guarantee
0064      *  that all results will contain this information.
0065      */
0066     KPUBLICTRANSPORT_PROPERTY(bool, includePaths, setIncludePaths)
0067 
0068     /** Access modes. */
0069     Q_PROPERTY(QVariantList accessModes READ accessModesVariant WRITE setAccessModesVariant)
0070     /** Egress modes. */
0071     Q_PROPERTY(QVariantList egressModes READ egressModesVariant WRITE setEgressModesVariant)
0072 
0073     /** Line modes. */
0074     Q_PROPERTY(QVariantList lineModes READ lineModesVariant WRITE setLineModesVariant)
0075 
0076 public:
0077     enum DateTimeMode {
0078         Arrival, ///< dateTime() represents the desired arriva time.
0079         Departure ///< dateTime() represents the desired departure time.
0080     };
0081     Q_ENUM(DateTimeMode)
0082     /** Controls whether to search for journeys starting or ending at the given time. */
0083     KPUBLICTRANSPORT_PROPERTY(DateTimeMode, dateTimeMode, setDateTimeMode)
0084 
0085     Q_PROPERTY(QStringList backends READ backendIds WRITE setBackendIds)
0086 
0087     /** Download graphic assets such as line logos for the data requested here.
0088      *  Default: @c false
0089      */
0090     KPUBLICTRANSPORT_PROPERTY(bool, downloadAssets, setDownloadAssets)
0091 
0092 public:
0093     /** Search a journey from @p from to @p to. */
0094     JourneyRequest(const Location &from, const Location &to);
0095 
0096     /** Returns @c true if this is a valid request, that is, it has enough parameters set to perform a query. */
0097     bool isValid() const;
0098 
0099     /** Set the desired departure time.
0100      *  This is mutually exclusive to setting a desired arrival time.
0101      */
0102     void setDepartureTime(const QDateTime &dt);
0103     /** Sets the desired arrival time.
0104      *  This is mutually exclusive to setting a desired departure time.
0105      */
0106     void setArrivalTime(const QDateTime &dt);
0107 
0108     /** Identifiers of the backends that should be queried.
0109      *  @see setBackendIds()
0110      */
0111     QStringList backendIds() const;
0112     /** Set identifiers of backends that should be queried.
0113      *  Settings this is only needed when you want explicit control over this, leaving
0114      *  this empty picks suitable backends automatically.
0115      */
0116     void setBackendIds(const QStringList &backendIds);
0117 
0118     /** Requested access modes.
0119      *  That is individual transport modes on the first (access) leg of the journey.
0120      *  Default: walking
0121      */
0122     const std::vector<IndividualTransport>& accessModes() const;
0123     /** Sets the requested access modes. */
0124     void setAccessModes(std::vector<IndividualTransport> &&accessModes);
0125 
0126     /** Requested egress modes.
0127      *  That is, individual transport modes for the last (egress) leg of the journey.
0128      *  Default: walking
0129      */
0130     const std::vector<IndividualTransport>& egressModes() const;
0131     /** Sets the requested egress modes. */
0132     void setEgressModes(std::vector<IndividualTransport> &&egressModes);
0133 
0134     /** Requested line modes.
0135      *  That is, the possible types of public transport lines to consider for public
0136      *  transports sections of the journey.
0137      *  Default: all
0138      */
0139     const std::vector<Line::Mode>& lineModes() const;
0140     /** Sets the requested line modes.
0141      *  An empty list is considered as all modes being allowed.
0142      *  @note This relies on backends actually supporting this and is thus does not
0143      *  provide any guarantee that the results wont contain other modes as well.
0144      */
0145     void setLineModes(std::vector <Line::Mode> &&modes);
0146 
0147     /** Unique string representation used for caching results. */
0148     QString cacheKey() const;
0149 
0150     ///@cond internal
0151     static QJsonObject toJson(const JourneyRequest &req);
0152     ///@endcond
0153 private:
0154     friend class AbstractBackend;
0155     friend class JourneyReply;
0156     friend class JourneyReplyPrivate;
0157     friend class Manager;
0158     friend class JourneyRequestTest;
0159 
0160     Q_DECL_HIDDEN QVariantList accessModesVariant() const;
0161     Q_DECL_HIDDEN void setAccessModesVariant(const QVariantList &accessModesVariant);
0162     Q_DECL_HIDDEN QVariantList egressModesVariant() const;
0163     Q_DECL_HIDDEN void setEgressModesVariant(const QVariantList &egressModesVariant);
0164     Q_DECL_HIDDEN QVariantList lineModesVariant() const;
0165     Q_DECL_HIDDEN void setLineModesVariant(const QVariantList &modes);
0166 
0167     Q_DECL_HIDDEN RequestContext context(const AbstractBackend *backend) const;
0168     Q_DECL_HIDDEN const std::vector<RequestContext>& contexts() const;
0169     Q_DECL_HIDDEN void setContext(const AbstractBackend *backend, RequestContext &&context);
0170     Q_DECL_HIDDEN void purgeLoops(const JourneyRequest &baseRequest);
0171 
0172     /** Check that the given request parameters are semantically sane, and fix that if needed. */
0173     void validate() const;
0174 };
0175 
0176 }
0177 
0178 Q_DECLARE_METATYPE(KPublicTransport::JourneyRequest)
0179 
0180 #endif // KPUBLICTRANSPORT_JOURNEYREQUEST_H