File indexing completed on 2025-02-02 05:02:34
0001 /* 0002 SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #ifndef RESERVATIONMANAGER_H 0008 #define RESERVATIONMANAGER_H 0009 0010 #include <KItinerary/ExtractorValidator> 0011 0012 #include <QHash> 0013 #include <QObject> 0014 #include <QVariant> 0015 0016 0017 /** Manages JSON-LD reservation data. 0018 * This is done on two levels: 0019 * - the raw individual reservation elements (one per traveler and per trip) 0020 * - reservation batches for multi-traveler trips 0021 * Most consumers probably want to work with the multi-traveler batches rather 0022 * than the raw elements. 0023 * Batches are identified by a reservation id of a random element in that batch, 0024 * that means you can directly retrieve reservation data using the batch id too. 0025 * 0026 * Identifiers are QStrings, which is super ugly, but needed for direct consumption 0027 * by QML. 0028 */ 0029 class ReservationManager : public QObject 0030 { 0031 Q_OBJECT 0032 public: 0033 explicit ReservationManager(QObject *parent = nullptr); 0034 ~ReservationManager() override; 0035 0036 Q_INVOKABLE bool isEmpty() const; 0037 Q_INVOKABLE QVariant reservation(const QString &id) const; 0038 0039 /** Adds @p res if it's new, or merges it with an existing reservation or reservation batch. 0040 * @param resIdHint If set, this is used as identifier for reservations that would need a new 0041 * one, assuming it is not already in use. Keep empty by default, only needs to be set when 0042 * importing existing data. 0043 * @returns The id of the new or existed merged reservation. 0044 */ 0045 Q_INVOKABLE QString addReservation(const QVariant &res, const QString &resIdHint = {}); 0046 Q_INVOKABLE void updateReservation(const QString &resId, const QVariant &res); 0047 Q_INVOKABLE void removeReservation(const QString &id); 0048 0049 /** Imports reservation in @p resData. 0050 * @returns A list of reservation ids for the extracted elements. Those can be reservation 0051 * ids that previously existed, in case the extracted elements could be merged. 0052 */ 0053 QVector<QString> importReservations(const QVector<QVariant> &resData); 0054 Q_INVOKABLE void importReservation(const QVariant &resData); 0055 0056 const std::vector<QString>& batches() const; 0057 bool hasBatch(const QString &batchId) const; 0058 QString batchForReservation(const QString &resId) const; 0059 Q_INVOKABLE QStringList reservationsForBatch(const QString &batchId) const; 0060 Q_INVOKABLE void removeBatch(const QString &batchId); 0061 0062 /** Returns the batch happening prior to @p batchId, if any. */ 0063 QString previousBatch(const QString &batchId) const; 0064 /** Returns the batch happening after @p batchId, if any. */ 0065 QString nextBatch(const QString &batchId) const; 0066 0067 Q_SIGNALS: 0068 void reservationAdded(const QString &id); 0069 void reservationChanged(const QString &id); 0070 void reservationRemoved(const QString &id); 0071 0072 void batchAdded(const QString &batchId); 0073 /** This is emitted when elements are added or removed from the batch, 0074 * but its content otherwise stays untouched. 0075 */ 0076 void batchChanged(const QString &batchId); 0077 /** This is emitted when the batch content changed, but the batching 0078 * as such remains the same. 0079 */ 0080 void batchContentChanged(const QString &batchId); 0081 /** This is emitted when the reservation with @p oldBatchId was removed and 0082 * it has been used to identify a non-empty batch. 0083 */ 0084 void batchRenamed(const QString &oldBatchId, const QString &newBatchId); 0085 void batchRemoved(const QString &batchId); 0086 0087 private: 0088 static QString reservationsBasePath(); 0089 static QString batchesBasePath(); 0090 void storeReservation(const QString &resId, const QVariant &res) const; 0091 0092 void loadBatches(); 0093 void initialBatchCreate(); 0094 void storeBatch(const QString &batchId) const; 0095 void storeRemoveBatch(const QString &batchId) const; 0096 0097 void updateBatch(const QString &resId, const QVariant &newRes, const QVariant &oldRes); 0098 void removeFromBatch(const QString &resId, const QString &batchId); 0099 0100 QString makeReservationId(const QString &resIdHint) const; 0101 0102 QVector<QString> applyPartialUpdate(const QVariant &res); 0103 0104 mutable QHash<QString, QVariant> m_reservations; 0105 0106 KItinerary::ExtractorValidator m_validator; 0107 0108 std::vector<QString> m_batches; 0109 QHash<QString, QStringList> m_batchToResMap; // ### QStringList for direct consumption by QML 0110 QHash<QString, QString> m_resToBatchMap; 0111 }; 0112 0113 #endif // RESERVATIONMANAGER_H