Warning, file /pim/itinerary/src/app/transfermanager.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef TRANSFERMANAGER_H
0008 #define TRANSFERMANAGER_H
0009 
0010 #include "transfer.h"
0011 
0012 #include <KPublicTransport/JourneyRequest>
0013 
0014 #include <QHash>
0015 #include <QObject>
0016 
0017 #include <cmath>
0018 
0019 class FavoriteLocation;
0020 class FavoriteLocationModel;
0021 class LiveDataManager;
0022 class ReservationManager;
0023 class TripGroupManager;
0024 
0025 /** Manages Transfer objects, including creation, removal and persistence. */
0026 class TransferManager : public QObject
0027 {
0028     Q_OBJECT
0029 
0030 public:
0031     explicit TransferManager(QObject *parent = nullptr);
0032     ~TransferManager() override;
0033     void setReservationManager(ReservationManager *resMgr);
0034     void setTripGroupManager(TripGroupManager *tgMgr);
0035     void setFavoriteLocationModel(FavoriteLocationModel *favLocModel);
0036     void setLiveDataManager(LiveDataManager *liveDataMgr);
0037 
0038     void setAutoAddTransfers(bool enabled);
0039     void setAutoFillTransfers(bool enabled);
0040 
0041     /** Returns the transfer for reservation @p resId with @p alignment. */
0042     Transfer transfer(const QString &resId, Transfer::Alignment alignment) const;
0043 
0044     /** Applies the given @p journey to @p transfer. */
0045     Q_INVOKABLE void setJourneyForTransfer(Transfer transfer, const KPublicTransport::Journey &journey);
0046     /** Applies the given @p favoriteLocation to the floating end of @p transfer. */
0047     Q_INVOKABLE Transfer setFavoriteLocationForTransfer(Transfer transfer, const FavoriteLocation &favoriteLocation);
0048     /** Discard the given @p transfer. */
0049     Q_INVOKABLE void discardTransfer(Transfer transfer);
0050 
0051     /** Checks if a transfer can be added before/after the given reservation.
0052      *  This is used to manual inserts, and might allow more inserts than the automatic code would perform.
0053      */
0054     Q_INVOKABLE bool canAddTransfer(const QString &resId, Transfer::Alignment alignment) const;
0055     /** Explicitly add a transfer before/after the given reservation. */
0056     Q_INVOKABLE Transfer addTransfer(const QString &resId, Transfer::Alignment alignment);
0057 
0058     /** Create a JourneyRequest for a given @p transfer. */
0059     Q_INVOKABLE KPublicTransport::JourneyRequest journeyRequestForTransfer(const Transfer &transfer) const;
0060 
0061     void importTransfer(const Transfer &transfer);
0062 
0063     // for unit tests only
0064     void overrideCurrentDateTime(const QDateTime &dt);
0065     static void clear();
0066 
0067 Q_SIGNALS:
0068     void transferAdded(const Transfer &transfer);
0069     void transferChanged(const Transfer &transfer);
0070     void transferRemoved(const QString &resId, Transfer::Alignment alignment);
0071 
0072     void homeLocationChanged();
0073 
0074 private:
0075     void rescan(bool force = false);
0076 
0077     void checkReservation(const QString &resId);
0078     void checkReservation(const QString &resId, const QVariant &res, Transfer::Alignment alignment);
0079 
0080     enum CheckTransferResult {
0081         ShouldAutoAdd, /// transfer should be added automatically
0082         CanAddManually, /// transfer is possible, but should only be added manually
0083         ShouldRemove /// invalid transfer, cannot be added or should be removed
0084     };
0085     /**  Those two are used in both the automatical and manual code paths.
0086      *   @param transfer is filled with all required parameters, but not added yet
0087      */
0088     CheckTransferResult checkTransferBefore(const QString &resId, const QVariant &res, Transfer &transfer) const;
0089     CheckTransferResult checkTransferAfter(const QString &resId, const QVariant &res, Transfer &transfer) const;
0090 
0091     void reservationRemoved(const QString &resId);
0092     void tripGroupChanged(const QString &tgId);
0093 
0094     bool isFirstInTripGroup(const QString &resId) const;
0095     bool isLastInTripGroup(const QString &resId) const;
0096     bool isNotInTripGroup(const QString &resId) const;
0097 
0098     void determineAnchorDeltaDefault(Transfer &transfer, const QVariant &res) const;
0099 
0100     /** Return the transfer anchor time before/after the reservation @p res. */
0101     QDateTime anchorTimeBefore(const QString &resId, const QVariant &res) const;
0102     QDateTime anchorTimeAfter(const QString &resId, const QVariant &res) const;
0103 
0104     static KPublicTransport::Location locationFromFavorite(const FavoriteLocation &favLoc);
0105     /** Pick the best favorite location for a given transfer. */
0106     FavoriteLocation pickFavorite(const QVariant &anchoredLoc, const QString &resId, Transfer::Alignment alignment) const;
0107 
0108     void addOrUpdateTransfer(Transfer &t);
0109     void removeTransfer(const Transfer &t);
0110 
0111     Transfer readFromFile(const QString &resId, Transfer::Alignment alignment) const;
0112     void writeToFile(const Transfer &transfer) const;
0113     void removeFile(const QString &resId, Transfer::Alignment alignment) const;
0114 
0115     void autoFillTransfer(Transfer &t);
0116 
0117     QDateTime currentDateTime() const;
0118 
0119     ReservationManager *m_resMgr = nullptr;
0120     TripGroupManager *m_tgMgr = nullptr;
0121     FavoriteLocationModel *m_favLocModel = nullptr;
0122     LiveDataManager *m_liveDataMgr = nullptr;
0123     mutable QHash<QString, Transfer> m_transfers[2];
0124     QDateTime m_nowOverride;
0125     bool m_autoAddTransfers = true;
0126     bool m_autoFillTransfers = false;
0127 };
0128 
0129 #endif // TRANSFERMANAGER_H