QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
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
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 "qgis_core.h"
23#include "qgshttpheaders.h"
25#include "qgstaskmanager.h"
26
27#include <QFile>
28#include <QMap>
29#include <QMutex>
30#include <QNetworkReply>
31#include <QObject>
32#include <QPointer>
33#include <QTemporaryFile>
34
42class CORE_EXPORT QgsFetchedContent : public QObject
43{
44 Q_OBJECT
45 public:
54
56 explicit QgsFetchedContent( const QString &url, QTemporaryFile *file SIP_TRANSFER = nullptr, ContentStatus status = NotStarted,
57 const QString &authConfig = QString(), const QgsHttpHeaders &headers = QgsHttpHeaders() )
58 : mUrl( url )
59 , mFile( file )
60 , mStatus( status )
61 , mAuthConfig( authConfig )
62 , mHeaders( headers )
63 {}
64
66 {
67 if ( mFile )
68 mFile->close();
69
70 }
71
72
73#ifndef SIP_RUN
75 QFile *file() const {return mFile.get();}
76#endif
77
79 const QString filePath() const {return mFilePath;}
80
82 ContentStatus status() const {return mStatus;}
83
85 QNetworkReply::NetworkError error() const {return mError;}
86
90 QString authConfig() const {return mAuthConfig;}
91
92 public slots:
93
98 void download( bool redownload = false );
99
103 void cancel();
104
105 signals:
107 void fetched();
108
114 void errorOccurred( QNetworkReply::NetworkError code, const QString &errorMsg );
115
116 private slots:
117 void taskCompleted();
118
119 private:
120 QString mUrl;
121 std::unique_ptr<QTemporaryFile> mFile;
122 QString mFilePath;
123 QPointer< QgsNetworkContentFetcherTask > mFetchingTask;
124 ContentStatus mStatus = NotStarted;
125 QNetworkReply::NetworkError mError = QNetworkReply::NoError;
126 QString mAuthConfig;
127 QString mErrorString;
128 QgsHttpHeaders mHeaders;
129};
130
143class CORE_EXPORT QgsNetworkContentFetcherRegistry : public QObject
144{
145 Q_OBJECT
146 public:
147
150
152
161 QgsFetchedContent *fetch( const QString &url, Qgis::ActionStart fetchingMode = Qgis::ActionStart::Deferred, const QString &authConfig = QString(), const QgsHttpHeaders &headers = QgsHttpHeaders() );
162
163#ifndef SIP_RUN
164
169 QFile *localFile( const QString &filePathOrUrl );
170#endif
171
176 QString localPath( const QString &filePathOrUrl );
177
178 private:
179 QMap<QString, QgsFetchedContent *> mFileRegistry;
180
181};
182
183#endif // QGSNETWORKCONTENTFETCHERREGISTRY_H
ActionStart
Enum to determine when an operation would begin.
Definition qgis.h:1162
@ Deferred
Do not start immediately the action.
Definition qgis.h:1163
Holds information about fetched network content.
QNetworkReply::NetworkError error() const
Returns the potential error of the download.
QFile * file() const
Returns a pointer to the local file, or nullptr if the file is not accessible yet.
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.
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...
QgsFetchedContent(const QString &url, QTemporaryFile *file=nullptr, ContentStatus status=NotStarted, const QString &authConfig=QString(), const QgsHttpHeaders &headers=QgsHttpHeaders())
Constructs a FetchedContent with pointer to the downloaded file and status of the download.
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.
Implements simple HTTP header management.
QString localPath(const QString &filePathOrUrl)
Returns the path to a local file or to a temporary file previously fetched by the registry.
QgsFetchedContent * fetch(const QString &url, Qgis::ActionStart fetchingMode=Qgis::ActionStart::Deferred, const QString &authConfig=QString(), const QgsHttpHeaders &headers=QgsHttpHeaders())
Initialize a download for the given URL.
QFile * localFile(const QString &filePathOrUrl)
Returns a QFile from a local file or to a temporary file previously fetched by the registry.
QgsNetworkContentFetcherRegistry()=default
Create the registry for temporary downloaded files.
#define SIP_TRANSFER
Definition qgis_sip.h:36