QGIS API Documentation 3.41.0-Master (3440c17df1d)
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 <QObject>
23#include <QMap>
24#include <QMutex>
25#include <QNetworkReply>
26#include <QFile>
27#include <QTemporaryFile>
28#include <QPointer>
29
30#include "qgis_core.h"
31#include "qgstaskmanager.h"
33
41class CORE_EXPORT QgsFetchedContent : public QObject
42{
43 Q_OBJECT
44 public:
53
55 explicit QgsFetchedContent( const QString &url, QTemporaryFile *file = nullptr, ContentStatus status = NotStarted,
56 const QString &authConfig = QString() )
57 : mUrl( url )
58 , mFile( file )
59 , mStatus( status )
60 , mAuthConfig( authConfig )
61 {}
62
64 {
65 if ( mFile )
66 mFile->close();
67 delete mFile;
68 }
69
70
71#ifndef SIP_RUN
73 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
88 QString authConfig() const {return mAuthConfig;}
89
90 public slots:
91
96 void download( bool redownload = false );
97
101 void cancel();
102
103 signals:
105 void fetched();
106
112 void errorOccurred( QNetworkReply::NetworkError code, const QString &errorMsg );
113
114 private slots:
115 void taskCompleted();
116
117 private:
118 QString mUrl;
119 QTemporaryFile *mFile = nullptr;
120 QString mFilePath;
121 QPointer< QgsNetworkContentFetcherTask > mFetchingTask;
122 ContentStatus mStatus = NotStarted;
123 QNetworkReply::NetworkError mError = QNetworkReply::NoError;
124 QString mAuthConfig;
125 QString mErrorString;
126};
127
140class CORE_EXPORT QgsNetworkContentFetcherRegistry : public QObject
141{
142 Q_OBJECT
143 public:
144
147
149
157 QgsFetchedContent *fetch( const QString &url, Qgis::ActionStart fetchingMode = Qgis::ActionStart::Deferred, const QString &authConfig = QString() );
158
159#ifndef SIP_RUN
160
165 QFile *localFile( const QString &filePathOrUrl );
166#endif
167
172 QString localPath( const QString &filePathOrUrl );
173
174 private:
175 QMap<QString, QgsFetchedContent *> mFileRegistry;
176
177};
178
179#endif // QGSNETWORKCONTENTFETCHERREGISTRY_H
ActionStart
Enum to determine when an operation would begin.
Definition qgis.h:1074
@ Deferred
Do not start immediately the action.
FetchedContent holds useful information about a network content being fetched.
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.
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.
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.