livedatamanager.h 4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
    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
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

#ifndef LIVEDATAMANAGER_H
#define LIVEDATAMANAGER_H

21 22 23
#include <KPublicTransport/Departure>

#include <QDateTime>
24
#include <QHash>
25
#include <QObject>
26
#include <QTimer>
27 28 29 30 31 32 33 34

#include <vector>

namespace KItinerary {
class TrainTrip;
}

namespace KPublicTransport {
35
class Departure;
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
class Manager;
}

class PkPassManager;
class ReservationManager;

/** Handles querying live data sources for delays, etc. */
class LiveDataManager : public QObject
{
    Q_OBJECT
public:
    explicit LiveDataManager(QObject *parent = nullptr);
    ~LiveDataManager();

    void setReservationManager(ReservationManager *resMgr);
    void setPkPassManager(PkPassManager *pkPassMgr);
52
    void setPublicTransportManager(KPublicTransport::Manager *mgr);
53

54 55
    void setPollingEnabled(bool pollingEnabled);

56
    Q_INVOKABLE QVariant arrival(const QString &resId);
57 58
    Q_INVOKABLE QVariant departure(const QString &resId);

59
public Q_SLOTS:
60 61 62
    /** Checks all applicable elements for updates. */
    void checkForUpdates();

63
Q_SIGNALS:
64
    void arrivalUpdated(const QString &resId);
65 66
    void departureUpdated(const QString &resId);

67
private:
68 69
    bool isRelevant(const QString &resId) const;

70 71 72 73
    void batchAdded(const QString &resId);
    void batchChanged(const QString &resId);
    void batchRenamed(const QString &oldBatchId, const QString &newBatchId);
    void batchRemoved(const QString &resId);
74

75
    void checkTrainTrip(const QVariant &res, const QString &resId);
76

77 78 79
    void updateArrivalData(const KPublicTransport::Departure &arr, const QString &resId);
    void updateDepartureData(const KPublicTransport::Departure &dep, const QString &resId);

80 81 82 83
    /** Best known departure time. */
    QDateTime departureTime(const QString &resId, const QVariant &res) const;
    /** Best known arrival time. */
    QDateTime arrivalTime(const QString &resId, const QVariant &res) const;
84 85 86 87 88
    /** Check if the trip @p res has departed, based on the best knowledge we have. */
    bool hasDeparted(const QString &resId, const QVariant &res) const;
    /** Check if the trip @p res has arrived, based on the best knowledge we have. */
    bool hasArrived(const QString &resId, const QVariant &res) const;

89 90 91 92 93
    struct TrainChange {
        KPublicTransport::Departure change;
        QDateTime timestamp;
    };

94 95
    void storePublicTransportData(const QString &resId, const KPublicTransport::Departure &dep, const QString &type) const;
    void loadPublicTransportData();
96
    void loadPublicTransportData(const QString &prefix, QHash<QString, TrainChange>& data) const;
97

98
    void poll();
99 100
    /// @p force will bypass the check if the data is still up to date
    void pollForUpdates(bool force);
101 102 103
    int nextPollTime() const;
    int nextPollTimeForReservation(const QString &resId) const;

104 105 106
    /** Last time we queried any kind of departure information for this reservation batch. */
    QDateTime lastDeparturePollTime(const QString &batchId, const QVariant &res) const;

107 108 109
    /** Notifications handling for pkpass updates. */
    void pkPassUpdated(const QString &passId, const QStringList &changes);

110 111
    ReservationManager *m_resMgr;
    PkPassManager *m_pkPassMgr;
112
    KPublicTransport::Manager *m_ptMgr;
113
    std::vector<QString> m_reservations;
114 115
    QHash <QString, TrainChange> m_arrivals;
    QHash <QString, TrainChange> m_departures;
116 117

    QTimer m_pollTimer;
118 119 120
};

#endif // LIVEDATAMANAGER_H