reservationmanager.h 4.08 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
    QVector<QString> reservations() const;
51
    Q_INVOKABLE QVariant reservation(const QString &id) const;
52

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

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

61
    const std::vector<QString>& batches() const;
62
    bool hasBatch(const QString &batchId) const;
63 64 65
    QString batchForReservation(const QString &resId) const;
    Q_INVOKABLE QStringList reservationsForBatch(const QString &batchId) const;
    Q_INVOKABLE void removeBatch(const QString &batchId);
66
Q_SIGNALS:
67 68 69 70
    void reservationAdded(const QString &id);
    void reservationUpdated(const QString &id);
    void reservationRemoved(const QString &id);

71
    void batchAdded(const QString &batchId);
72 73 74
    /** This is emitted when elements are added or removed from the batch,
     *  but its content otherwise stays untouched.
     */
75
    void batchChanged(const QString &batchId);
76 77 78 79
    /** This is emitted when the batch content changed, but the batching
     *  as such remains the same.
     */
    void batchContentChanged(const QString &batchId);
80 81 82 83 84 85
    /** 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);

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

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

95 96 97 98 99 100 101 102
    void loadBatches();
    void initialBatchCreate();
    void storeBatch(const QString &batchId) const;
    void storeRemoveBatch(const QString &batchId) const;

    void updateBatch(const QString &resId, const QVariant &res);
    void removeFromBatch(const QString &resId, const QString &batchId);

103
    mutable QHash<QString, QVariant> m_reservations;
104 105 106 107 108

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

109
    PkPassManager *m_passMgr = nullptr;
110 111 112
};

#endif // RESERVATIONMANAGER_H