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_REPLY_H
0008 #define KPUBLICTRANSPORT_REPLY_H
0009 
0010 #include "kpublictransport_export.h"
0011 
0012 #include <QObject>
0013 
0014 #include <memory>
0015 
0016 namespace KPublicTransport {
0017 
0018 class AbstractBackend;
0019 class Attribution;
0020 class Manager;
0021 class ReplyPrivate;
0022 
0023 /** Query response base class. */
0024 class KPUBLICTRANSPORT_EXPORT Reply : public QObject
0025 {
0026     Q_OBJECT
0027 public:
0028     ~Reply() override;
0029 
0030     /** Error types. */
0031     enum Error {
0032         NoError, ///< Nothing went wrong.
0033         NetworkError, ///< Error during network operations.
0034         NotFoundError, ///< The requested journey/departure/place could not be found.
0035         InvalidRequest, ///< Incomplete or otherwise invalid request.
0036         UnknownError ///< Anything else.
0037     };
0038 
0039     /** Error code. */
0040     Error error() const;
0041     /** Textual error message. */
0042     QString errorString() const;
0043 
0044     /** Returns the attributions for the provided data. */
0045     const std::vector<Attribution>& attributions() const;
0046     /** Returns the attributions for the provided data for moving them elsewhere. */
0047     std::vector<Attribution>&& takeAttributions();
0048 
0049 Q_SIGNALS:
0050     /** Emitted whenever the corresponding search has been completed. */
0051     void finished();
0052     /** Emitted whenever new results are available, even before the search has been completed.
0053      *  @note At this point no guarantees about the result apply, sorting/merging/etc might not have been applied yet
0054      *  and not all properties of the reply might be valid. Avoid the usage of this in general, unless you write
0055      *  dynamically updating models that need very quick results at the expensive of incompleteness.
0056      */
0057     void updated();
0058 
0059 protected:
0060     ///@cond internal
0061     Q_DECL_HIDDEN explicit Reply(ReplyPrivate *dd, QObject *parent);
0062     std::unique_ptr<ReplyPrivate> d_ptr;
0063 
0064     friend class AbstractBackend;
0065     /** Used for a backend to report it finished it's job with an error.
0066      *  Prefer to use the variants of this provided by the type-specific sub-classes
0067      *  which also add the corresponding negative cache entries if appropriate.
0068      */
0069     Q_DECL_HIDDEN void addError(Error error, const QString &errorMsg);
0070     Q_DECL_HIDDEN void addAttributions(std::vector<Attribution> &&attributions);
0071     Q_DECL_HIDDEN void addAttributions(const std::vector<Attribution> &attributions);
0072 
0073     friend class Manager;
0074     friend class ManagerPrivate;
0075     Q_DECL_HIDDEN void setPendingOps(int ops);
0076     Q_DECL_HIDDEN void addAttribution(const Attribution &attr);
0077     ///@endcond
0078 };
0079 
0080 }
0081 
0082 #endif // KPUBLICTRANSPORT_JOURNEYREPLY_H