QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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
The download will start immediately, not need to run QgsFecthedContent::download() ...
Handles HTTP network content fetching in a background task.
QNetworkReply::NetworkError error() const
Returns the potential error of the download.
Do not start immediately the download to properly connect the fetched signal.
Registry for temporary fetched files.
FetchedContent holds useful information about a network content being fetched.
Download finished and successful.
ContentStatus
Status of fetched content.
No download started for such URL.
QgsFetchedContent(const QString &url, QTemporaryFile *file=nullptr, ContentStatus status=NotStarted)
Constructs a FetchedContent with pointer to the downloaded file and status of the download...
FetchingMode
Enum to determine when the download should start.
ContentStatus status() const
Returns the status of the download.
const QString filePath() const
Returns the path to the local file, an empty string if the file is not accessible yet...