File indexing completed on 2024-04-28 04:41:39

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KPUBLICTRANSPORT_ASSETREPOSITORY_P_H
0008 #define KPUBLICTRANSPORT_ASSETREPOSITORY_P_H
0009 
0010 #include "kpublictransport_export.h"
0011 
0012 #include <QObject>
0013 #include <QUrl>
0014 
0015 #include <deque>
0016 #include <functional>
0017 
0018 class QNetworkAccessManager;
0019 
0020 namespace KPublicTransport {
0021 
0022 class Attribution;
0023 
0024 /**
0025  * Access and downloading of line logos.
0026  */
0027 class KPUBLICTRANSPORT_EXPORT AssetRepository : public QObject
0028 {
0029     Q_OBJECT
0030 public:
0031     ~AssetRepository() override;
0032 
0033     /** Returns the locally cached content for @p url
0034      *  if available, an empty string if none is available.
0035      */
0036     static QUrl localFile(const QUrl &url);
0037 
0038     /** Schedules the image at @p url for downloading.
0039      *  @returns @c true if a download job has been started,
0040      *  @c false if the requested item is already present or
0041      *  @p url is invalid.
0042      */
0043     bool download(const QUrl &url);
0044 
0045     /** Returns @c true if no further download jobs are queued or in progress. */
0046     bool isQueueEmpty();
0047 
0048     /** Returns attributions for the provided assets. */
0049     const std::vector<Attribution>& attributions() const;
0050 
0051     static AssetRepository* instance();
0052 
0053 Q_SIGNALS:
0054     /** Emitted whenever all pending downloads have been completed,
0055      *  successful or not.
0056      */
0057     void downloadFinished();
0058 
0059 private:
0060     friend class Manager;
0061 
0062     AssetRepository(QObject *parent = nullptr);
0063     void setNetworkAccessManagerProvider(std::function<QNetworkAccessManager*()> namProvider);
0064     void downloadNext();
0065 
0066     static AssetRepository *s_instance;
0067     std::deque<QUrl> m_queue;
0068     std::function<QNetworkAccessManager*()> m_namProvider;
0069     mutable std::vector<Attribution> m_attributions;
0070 };
0071 
0072 }
0073 
0074 #endif // KPUBLICTRANSPORT_ASSETREPOSITORY_P_H