QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgsnetworkcontentfetcherregistry.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsnetworkcontentfetcherregistry.h
3  -------------------
4  begin : April, 2018
5  copyright : (C) 2018 by Denis Rouzaud
6  email : [email protected]
7 
8  ***************************************************************************/
9 
10 /***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 
19 #ifndef QGSNETWORKCONTENTFETCHERREGISTRY_H
20 #define QGSNETWORKCONTENTFETCHERREGISTRY_H
21 
22 #include <QObject>
23 #include <QMap>
24 #include <QMutex>
25 #include <QNetworkReply>
26 
27 #include "qgis_core.h"
28 #include "qgstaskmanager.h"
30 
31 class QTemporaryFile;
32 
40 class CORE_EXPORT QgsFetchedContent : public QObject
41 {
42  Q_OBJECT
43  public:
46  {
50  Failed
51  };
52 
54  explicit QgsFetchedContent( const QString &url, QTemporaryFile *file = nullptr, ContentStatus status = NotStarted )
55  : mUrl( url )
56  , mFile( file )
57  , mStatus( status )
58  {}
59 
60  ~QgsFetchedContent() override
61  {
62  if ( mFile )
63  mFile->close();
64  delete mFile;
65  }
66 
67 
68 #ifndef SIP_RUN
69  QFile *file() const {return mFile;}
71 #endif
72 
74  const QString filePath() const {return mFilePath;}
75 
77  ContentStatus status() const {return mStatus;}
78 
80  QNetworkReply::NetworkError error() const {return mError;}
81 
82  public slots:
83 
88  void download( bool redownload = false );
89 
93  void cancel();
94 
95  signals:
97  void fetched();
98 
99  private slots:
100  void taskCompleted();
101 
102  private:
103  QString mUrl;
104  QTemporaryFile *mFile = nullptr;
105  QString mFilePath;
106  QgsNetworkContentFetcherTask *mFetchingTask = nullptr;
107  ContentStatus mStatus = NotStarted;
108  QNetworkReply::NetworkError mError = QNetworkReply::NoError;
109 };
110 
123 class CORE_EXPORT QgsNetworkContentFetcherRegistry : public QObject
124 {
125  Q_OBJECT
126  public:
129  {
132  };
133  Q_ENUM( FetchingMode )
134 
135 
136  explicit QgsNetworkContentFetcherRegistry() = default;
137 
139 
146  const QgsFetchedContent *fetch( const QString &url, FetchingMode fetchingMode = DownloadLater );
147 
148 #ifndef SIP_RUN
149 
154  QFile *localFile( const QString &filePathOrUrl );
155 #endif
156 
161  QString localPath( const QString &filePathOrUrl );
162 
163  private:
164  QMap<QString, QgsFetchedContent *> mFileRegistry;
165 
166 };
167 
168 #endif // QGSNETWORKCONTENTFETCHERREGISTRY_H
QgsFetchedContent::error
QNetworkReply::NetworkError error() const
Returns the potential error of the download.
Definition: qgsnetworkcontentfetcherregistry.h:80
qgstaskmanager.h
QgsNetworkContentFetcherTask
Handles HTTP network content fetching in a background task.
Definition: qgsnetworkcontentfetchertask.h:47
qgsnetworkcontentfetchertask.h
QgsNetworkContentFetcherRegistry::DownloadImmediately
@ DownloadImmediately
The download will start immediately, not need to run QgsFecthedContent::download()
Definition: qgsnetworkcontentfetcherregistry.h:131
QgsNetworkContentFetcherRegistry::DownloadLater
@ DownloadLater
Do not start immediately the download to properly connect the fetched signal.
Definition: qgsnetworkcontentfetcherregistry.h:130
QgsFetchedContent::QgsFetchedContent
QgsFetchedContent(const QString &url, QTemporaryFile *file=nullptr, ContentStatus status=NotStarted)
Constructs a FetchedContent with pointer to the downloaded file and status of the download.
Definition: qgsnetworkcontentfetcherregistry.h:54
QgsFetchedContent::Downloading
@ Downloading
Currently downloading.
Definition: qgsnetworkcontentfetcherregistry.h:48
QgsNetworkContentFetcherRegistry::FetchingMode
FetchingMode
Enum to determine when the download should start.
Definition: qgsnetworkcontentfetcherregistry.h:128
QgsFetchedContent::ContentStatus
ContentStatus
Status of fetched content.
Definition: qgsnetworkcontentfetcherregistry.h:45
QgsFetchedContent::NotStarted
@ NotStarted
No download started for such URL.
Definition: qgsnetworkcontentfetcherregistry.h:47
QgsFetchedContent::status
ContentStatus status() const
Returns the status of the download.
Definition: qgsnetworkcontentfetcherregistry.h:77
QgsFetchedContent
Definition: qgsnetworkcontentfetcherregistry.h:40
QgsFetchedContent::~QgsFetchedContent
~QgsFetchedContent() override
Definition: qgsnetworkcontentfetcherregistry.h:60
QgsFetchedContent::Finished
@ Finished
Download finished and successful.
Definition: qgsnetworkcontentfetcherregistry.h:49
QgsFetchedContent::filePath
const QString filePath() const
Returns the path to the local file, an empty string if the file is not accessible yet.
Definition: qgsnetworkcontentfetcherregistry.h:74
QgsNetworkContentFetcherRegistry
Registry for temporary fetched files.
Definition: qgsnetworkcontentfetcherregistry.h:123