QGIS API Documentation  3.21.0-Master (909859188c)
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 #include <QFile>
27 #include <QTemporaryFile>
28 
29 #include "qgis_core.h"
30 #include "qgstaskmanager.h"
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  const QString &authConfig = QString() )
56  : mUrl( url )
57  , mFile( file )
58  , mStatus( status )
59  , mAuthConfig( authConfig )
60  {}
61 
62  ~QgsFetchedContent() override
63  {
64  if ( mFile )
65  mFile->close();
66  delete mFile;
67  }
68 
69 
70 #ifndef SIP_RUN
72  QFile *file() const {return mFile;}
73 #endif
74 
76  const QString filePath() const {return mFilePath;}
77 
79  ContentStatus status() const {return mStatus;}
80 
82  QNetworkReply::NetworkError error() const {return mError;}
83 
87  QString authConfig() const {return mAuthConfig;}
88 
89  public slots:
90 
95  void download( bool redownload = false );
96 
100  void cancel();
101 
102  signals:
104  void fetched();
105 
111  void errorOccurred( QNetworkReply::NetworkError code, const QString &errorMsg );
112 
113  private slots:
114  void taskCompleted();
115 
116  private:
117  QString mUrl;
118  QTemporaryFile *mFile = nullptr;
119  QString mFilePath;
120  QgsNetworkContentFetcherTask *mFetchingTask = nullptr;
121  ContentStatus mStatus = NotStarted;
122  QNetworkReply::NetworkError mError = QNetworkReply::NoError;
123  QString mAuthConfig;
124  QString mErrorString;
125 };
126 
139 class CORE_EXPORT QgsNetworkContentFetcherRegistry : public QObject
140 {
141  Q_OBJECT
142  public:
143 
145  explicit QgsNetworkContentFetcherRegistry() = default;
146 
148 
156  QgsFetchedContent *fetch( const QString &url, Qgis::ActionStart fetchingMode = Qgis::ActionStart::Deferred, const QString &authConfig = QString() );
157 
158 #ifndef SIP_RUN
159 
164  QFile *localFile( const QString &filePathOrUrl );
165 #endif
166 
171  QString localPath( const QString &filePathOrUrl );
172 
173  private:
174  QMap<QString, QgsFetchedContent *> mFileRegistry;
175 
176 };
177 
178 #endif // QGSNETWORKCONTENTFETCHERREGISTRY_H
ActionStart
Enum to determine when an operation would begin.
Definition: qgis.h:399
FetchedContent holds useful information about a network content being fetched.
QNetworkReply::NetworkError error() const
Returns the potential error of the download.
ContentStatus status() const
Returns the status of the download.
QgsFetchedContent(const QString &url, QTemporaryFile *file=nullptr, ContentStatus status=NotStarted, const QString &authConfig=QString())
Constructs a FetchedContent with pointer to the downloaded file and status of the download.
QFile * file() const
Returns a pointer to the local file, or nullptr if the file is not accessible yet.
const QString filePath() const
Returns the path to the local file, an empty string if the file is not accessible yet.
void errorOccurred(QNetworkReply::NetworkError code, const QString &errorMsg)
Emitted when an error with code error occurred while processing the request errorMsg is a textual des...
ContentStatus
Status of fetched content.
@ Finished
Download finished and successful.
@ NotStarted
No download started for such URL.
@ Downloading
Currently downloading.
void fetched()
Emitted when the file is fetched and accessible.
QString authConfig() const
Returns the authentication configuration id use for this fetched content.
Registry for temporary fetched files.
QgsNetworkContentFetcherRegistry()=default
Create the registry for temporary downloaded files.
Handles HTTP network content fetching in a background task.