reservationmanager.h 4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
    Copyright (C) 2018 Volker Krause <vkrause@kde.org>

    This program is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This program is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU General Public License
15
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
16 17 18 19 20 21 22
*/

#ifndef RESERVATIONMANAGER_H
#define RESERVATIONMANAGER_H

#include <QHash>
#include <QObject>
23
#include <QVariant>
24

25 26
class PkPassManager;

Volker Krause's avatar
Volker Krause committed
27 28
class QUrl;

29 30 31 32 33 34 35 36 37 38 39 40
/** Manages JSON-LD reservation data.
 *  This is done on two levels:
 *  - the raw individual reservation elements (one per traveler and per trip)
 *  - reservation batches for multi-traveler trips
 *  Most consumers probably want to work with the multi-traveler batches rather
 *  than the raw elements.
 *  Batches are identified by a reservation id of a random element in that batch,
 *  that means you can directly retrieve reservation data using the batch id too.
 *
 *  Identifiers are QStrings, which is super ugly, but needed for direct consumption
 *  by QML.
 */
41 42 43 44
class ReservationManager : public QObject
{
    Q_OBJECT
public:
Laurent Montel's avatar
Laurent Montel committed
45
    explicit ReservationManager(QObject *parent = nullptr);
46 47
    ~ReservationManager();

48 49
    void setPkPassManager(PkPassManager *mgr);

50
    Q_INVOKABLE QVariant reservation(const QString &id) const;
51

52
    Q_INVOKABLE void addReservation(const QVariant &res);
53
    Q_INVOKABLE void updateReservation(const QString &resId, const QVariant &res);
Volker Krause's avatar
Volker Krause committed
54
    Q_INVOKABLE void removeReservation(const QString &id);
55

56
    void importReservation(const QByteArray &data);
57
    void importReservations(const QVector<QVariant> &resData);
58

59
    const std::vector<QString>& batches() const;
60
    bool hasBatch(const QString &batchId) const;
61 62 63
    QString batchForReservation(const QString &resId) const;
    Q_INVOKABLE QStringList reservationsForBatch(const QString &batchId) const;
    Q_INVOKABLE void removeBatch(const QString &batchId);
64
Q_SIGNALS:
65
    void reservationAdded(const QString &id);
Volker Krause's avatar
Volker Krause committed
66
    void reservationChanged(const QString &id);
67 68
    void reservationRemoved(const QString &id);

69
    void batchAdded(const QString &batchId);
70 71 72
    /** This is emitted when elements are added or removed from the batch,
     *  but its content otherwise stays untouched.
     */
73
    void batchChanged(const QString &batchId);
74 75 76 77
    /** This is emitted when the batch content changed, but the batching
     *  as such remains the same.
     */
    void batchContentChanged(const QString &batchId);
78 79 80 81 82 83
    /** This is emitted when the reservation with @p oldBatchId was removed and
     *  it has been used to identify a non-empty batch.
     */
    void batchRenamed(const QString &oldBatchId, const QString &newBatchId);
    void batchRemoved(const QString &batchId);

84
private:
85
    static QString reservationsBasePath();
86
    static QString batchesBasePath();
87
    void storeReservation(const QString &resId, const QVariant &res) const;
88 89 90 91 92

    void passAdded(const QString &passId);
    void passUpdated(const QString &passId);
    void passRemoved(const QString &passId);

93 94 95 96 97
    void loadBatches();
    void initialBatchCreate();
    void storeBatch(const QString &batchId) const;
    void storeRemoveBatch(const QString &batchId) const;

98
    void updateBatch(const QString &resId, const QVariant &newRes, const QVariant &oldRes);
99 100
    void removeFromBatch(const QString &resId, const QString &batchId);

101
    mutable QHash<QString, QVariant> m_reservations;
102 103 104 105 106

    std::vector<QString> m_batches;
    QHash<QString, QStringList> m_batchToResMap; // ### QStringList for direct consumption by QML
    QHash<QString, QString> m_resToBatchMap;

107
    PkPassManager *m_passMgr = nullptr;
108 109 110
};

#endif // RESERVATIONMANAGER_H