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

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_MANAGER_H
0008 #define KPUBLICTRANSPORT_MANAGER_H
0009 
0010 #include "kpublictransport_export.h"
0011 
0012 #include <QObject>
0013 
0014 #include <memory>
0015 
0016 class QNetworkAccessManager;
0017 
0018 /** Query operations and data types for accessing realtime public transport information
0019  *  from online services.
0020  */
0021 namespace KPublicTransport {
0022 
0023 class Attribution;
0024 class Backend;
0025 class JourneyReply;
0026 class JourneyRequest;
0027 class LocationReply;
0028 class LocationRequest;
0029 class ManagerPrivate;
0030 class StopoverReply;
0031 class StopoverRequest;
0032 class VehicleLayoutReply;
0033 class VehicleLayoutRequest;
0034 
0035 /** Entry point for starting public transport queries.
0036  *
0037  *  Queries return reply objects, you are responsible for deleting those,
0038  *  typically by calling deleteLater() on them after having retrieved their
0039  *  result (similar to how QNetworkAccessManager works).
0040  */
0041 class KPUBLICTRANSPORT_EXPORT Manager : public QObject
0042 {
0043     Q_OBJECT
0044     /** QML-compatible access to attributions(). */
0045     Q_PROPERTY(QVariantList attributions READ attributionsVariant NOTIFY attributionsChanged)
0046     /** Allow usage of insecure backends (default: off). */
0047     Q_PROPERTY(bool allowInsecureBackends READ allowInsecureBackends WRITE setAllowInsecureBackends NOTIFY configurationChanged)
0048 
0049     /** @see enabledBackends() */
0050     Q_PROPERTY(QStringList enabledBackends READ enabledBackends WRITE setEnabledBackends NOTIFY configurationChanged)
0051     /** @see disabledBackends() */
0052     Q_PROPERTY(QStringList disabledBackends READ disabledBackends WRITE setDisabledBackends NOTIFY configurationChanged)
0053     /** @see backendsEnabledByDefault() */
0054     Q_PROPERTY(bool backendsEnabledByDefault READ backendsEnabledByDefault WRITE setBackendsEnabledByDefault NOTIFY configurationChanged)
0055 
0056     /** QML-compatible access to backends(). */
0057     Q_PROPERTY(QVariantList backends READ backendsVariant CONSTANT)
0058 
0059 public:
0060     explicit Manager(QObject *parent = nullptr);
0061     ~Manager() override;
0062 
0063     /** Set the network access manager to use for network operations.
0064      *  If not set, an instance is created internally.
0065      *  Ownership is not transferred.
0066      */
0067     void setNetworkAccessManager(QNetworkAccessManager *nam);
0068 
0069     /** Returns whether access to insecure backends is allowed. */
0070     bool allowInsecureBackends() const;
0071     /** Allow usage of insecure backends, that is services not using
0072      *  transport encryption.
0073      */
0074     void setAllowInsecureBackends(bool insecure);
0075 
0076     /** Query a journey. */
0077     JourneyReply* queryJourney(const JourneyRequest &req) const;
0078 
0079     /** Query arrivals or departures from a specific station. */
0080     StopoverReply* queryStopover(const StopoverRequest &req) const;
0081 
0082     /** Query location information based on coordinates or (parts of) the name. */
0083     LocationReply* queryLocation(const LocationRequest &req) const;
0084 
0085     /** Query vehicle and platform layout information.
0086      *  This is only available for some trains and some operators, so be prepared
0087      *  for empty results.
0088      */
0089     VehicleLayoutReply* queryVehicleLayout(const VehicleLayoutRequest &req) const;
0090 
0091     /** Returns all static attribution information, as well as all dynamic ones
0092      *  found in the cache or accumulated during the lifetime of this instance.
0093      */
0094     const std::vector<Attribution>& attributions() const;
0095 
0096     /** Returns information about all available backends. */
0097     const std::vector<Backend>& backends() const;
0098 
0099     /** Returns whether the use of the backend with a given identifier is enabled. */
0100     Q_INVOKABLE bool isBackendEnabled(const QString &backendId) const;
0101     /** Sets whether the backend with the given identifier should be used.
0102      *  @note If allowInsecureBackends() is @c false, this has precedence.
0103      */
0104     void setBackendEnabled(const QString &backendId, bool enabled);
0105 
0106     /** Returns the identifiers of explicitly enabled backends.
0107      *  Use this for persisting the settings, not for checking for enabled backends.
0108      */
0109     QStringList enabledBackends() const;
0110     /** Sets the explicitly enabled backends.
0111      *  Use for restoring persisted settings.
0112      */
0113     void setEnabledBackends(const QStringList &backendIds);
0114     /** Returns the identifiers of explicitly disabled backends.
0115      *  Use this for persisting settings, not for checking for disabled backends.
0116      */
0117     QStringList disabledBackends() const;
0118     /** Sets the explicitly disabled backends.
0119      *  Use for restoring persisted settings.
0120      */
0121     void setDisabledBackends(const QStringList &backendIds);
0122     /**
0123      * Returns wheter backends are enabled by default.
0124      * Defaults to true.
0125      */
0126     bool backendsEnabledByDefault() const;
0127     /**
0128      * Set wheter backends are enabled by default.
0129      */
0130     void setBackendsEnabledByDefault(bool byDefault);
0131 
0132 
0133 Q_SIGNALS:
0134     void attributionsChanged();
0135     void configurationChanged();
0136 
0137 private:
0138     Q_DECL_HIDDEN QVariantList attributionsVariant() const;
0139     Q_DECL_HIDDEN QVariantList backendsVariant() const;
0140 
0141     std::unique_ptr<ManagerPrivate> d;
0142 };
0143 
0144 }
0145 
0146 #endif // KPUBLICTRANSPORT_MANAGER_H