QGIS API Documentation  3.6.0-Noosa (5873452)
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 <QTemporaryFile>
27 
28 #include "qgis_core.h"
29 
30 class QTemporaryFile;
31 
32 #include "qgstaskmanager.h"
34 
35 
43 class CORE_EXPORT QgsFetchedContent : public QObject
44 {
45  Q_OBJECT
46  public:
49  {
53  Failed
54  };
55 
57  explicit QgsFetchedContent( const QString &url, QTemporaryFile *file = nullptr, ContentStatus status = NotStarted )
58  : mUrl( url )
59  , mFile( file )
60  , mStatus( status )
61  {}
62 
63  ~QgsFetchedContent() override
64  {
65  if ( mFile )
66  mFile->close();
67  delete mFile;
68  }
69 
70 
71 #ifndef SIP_RUN
72  QFile *file() const {return mFile;}
74 #endif
75 
77  const QString filePath() const {return mFilePath;}
78 
80  ContentStatus status() const {return mStatus;}
81 
83  QNetworkReply::NetworkError error() const {return mError;}
84 
85  public slots:
86 
91  void download( bool redownload = false );
92 
96  void cancel();
97 
98  signals:
100  void fetched();
101 
102  private slots:
103  void taskCompleted();
104 
105  private:
106  QString mUrl;
107  QTemporaryFile *mFile = nullptr;
108  QString mFilePath;
109  QgsNetworkContentFetcherTask *mFetchingTask = nullptr;
110  ContentStatus mStatus = NotStarted;
111  QNetworkReply::NetworkError mError = QNetworkReply::NoError;
112 };
113 
126 class CORE_EXPORT QgsNetworkContentFetcherRegistry : public QObject
127 {
128  Q_OBJECT
129  public:
132  {
135  };
136  Q_ENUM( FetchingMode )
137 
138 
139  explicit QgsNetworkContentFetcherRegistry() = default;
140 
142 
149  const QgsFetchedContent *fetch( const QString &url, FetchingMode fetchingMode = DownloadLater );
150 
151 #ifndef SIP_RUN
152 
157  QFile *localFile( const QString &filePathOrUrl );
158 #endif
159 
164  QString localPath( const QString &filePathOrUrl );
165 
166  private:
167  QMap<QString, QgsFetchedContent *> mFileRegistry;
168 
169 };
170 
171 #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...