QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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
71  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
QgsFetchedContent::error
QNetworkReply::NetworkError error() const
Returns the potential error of the download.
Definition: qgsnetworkcontentfetcherregistry.h:82
qgstaskmanager.h
QgsNetworkContentFetcherTask
Handles HTTP network content fetching in a background task.
Definition: qgsnetworkcontentfetchertask.h:47
Qgis::ActionStart
ActionStart
Enum to determine when an operation would begin.
Definition: qgis.h:521
qgsnetworkcontentfetchertask.h
QgsFetchedContent::authConfig
QString authConfig() const
Returns the authentication configuration id use for this fetched content.
Definition: qgsnetworkcontentfetcherregistry.h:87
QgsFetchedContent::Downloading
@ Downloading
Currently downloading.
Definition: qgsnetworkcontentfetcherregistry.h:48
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:79
QgsFetchedContent
FetchedContent holds useful information about a network content being fetched.
Definition: qgsnetworkcontentfetcherregistry.h:40
QgsFetchedContent::QgsFetchedContent
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.
Definition: qgsnetworkcontentfetcherregistry.h:54
QgsFetchedContent::~QgsFetchedContent
~QgsFetchedContent() override
Definition: qgsnetworkcontentfetcherregistry.h:62
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:76
QgsNetworkContentFetcherRegistry
Registry for temporary fetched files.
Definition: qgsnetworkcontentfetcherregistry.h:139