QGIS API Documentation
4.3.0-Master (1c7cf7431bd)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
core
network
qgsnetworkcontentfetchertask.cpp
Go to the documentation of this file.
1
/***************************************************************************
2
qgsnetworkcontentfetchertask.cpp
3
-------------------
4
begin : March 2018
5
copyright : (C) 2018 by Nyall Dawson
6
email : nyall dot dawson at gmail dot com
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
#include "
qgsnetworkcontentfetchertask.h
"
20
21
#include "
qgsnetworkcontentfetcher.h
"
22
#include "
qgsnetworkreply.h
"
23
24
#include <QEventLoop>
25
26
#include "moc_qgsnetworkcontentfetchertask.cpp"
27
28
QgsNetworkContentFetcherTask::QgsNetworkContentFetcherTask
(
const
QUrl &url,
const
QString &authcfg,
QgsTask::Flags
flags
,
const
QString &
description
,
const
QgsHttpHeaders
&headers )
29
:
QgsNetworkContentFetcherTask
( QNetworkRequest( url ), authcfg,
flags
,
description
, headers )
30
{}
31
32
QgsNetworkContentFetcherTask::QgsNetworkContentFetcherTask
(
const
QNetworkRequest &request,
const
QString &authcfg,
QgsTask::Flags
flags
,
const
QString &
description
,
const
QgsHttpHeaders
&headers )
33
:
QgsTask
(
description
.isEmpty() ? tr(
"Fetching %1"
).arg( request.url().toString() ) :
description
,
flags
)
34
, mRequest( request )
35
, mAuthcfg( authcfg )
36
, mHeaders( headers )
37
{}
38
39
QgsNetworkContentFetcherTask::~QgsNetworkContentFetcherTask
()
40
{
41
if
( mFetcher )
42
mFetcher->deleteLater();
43
}
44
45
bool
QgsNetworkContentFetcherTask::run
()
46
{
47
mFetcher =
new
QgsNetworkContentFetcher
();
48
QEventLoop loop;
49
50
// We need to set the event loop (and not 'this') as receiver for all signal to ensure execution
51
// in the same thread and in the same order of emission. Indeed 'this' and 'loop' lives in
52
// different thread because they have been created in different thread.
53
54
connect( mFetcher, &
QgsNetworkContentFetcher::finished
, &loop, &QEventLoop::quit );
55
connect( mFetcher, &
QgsNetworkContentFetcher::downloadProgress
, &loop, [
this
]( qint64 bytesReceived, qint64 bytesTotal ) {
56
if
( !
isCanceled
() && bytesTotal > 0 )
57
{
58
const
int
progress
= ( bytesReceived * 100 ) / bytesTotal;
59
// don't emit 100% progress reports until completely fetched - otherwise we get
60
// intermediate 100% reports from redirects
61
if
(
progress
< 100 )
62
setProgress
(
progress
);
63
}
64
} );
65
66
67
bool
hasErrorOccurred =
false
;
68
connect( mFetcher, &
QgsNetworkContentFetcher::errorOccurred
, &loop, [&hasErrorOccurred,
this
]( QNetworkReply::NetworkError code,
const
QString &errorMsg ) {
69
hasErrorOccurred =
true
;
70
emit
errorOccurred
( code, errorMsg );
71
} );
72
73
// Apply custom headers to the request
74
QNetworkRequest request = mRequest;
75
mHeaders.updateNetworkRequest( request );
76
77
mFetcher->fetchContent( request, mAuthcfg );
78
loop.exec();
79
if
( !
isCanceled
() )
80
setProgress
( 100 );
81
emit
fetched
();
82
83
return
!
isCanceled
() && !hasErrorOccurred;
84
}
85
86
void
QgsNetworkContentFetcherTask::cancel
()
87
{
88
if
( mFetcher )
89
mFetcher->cancel();
90
91
QgsTask::cancel
();
92
}
93
94
QNetworkReply *
QgsNetworkContentFetcherTask::reply
()
95
{
96
return
mFetcher ? mFetcher->reply() :
nullptr
;
97
}
98
99
QString
QgsNetworkContentFetcherTask::contentDispositionFilename
()
const
100
{
101
return
mFetcher ? mFetcher->contentDispositionFilename() : QString();
102
}
103
104
QString
QgsNetworkContentFetcherTask::contentAsString
()
const
105
{
106
return
mFetcher ? mFetcher->contentAsString() : QString();
107
}
QgsHttpHeaders
Implements simple HTTP header management.
Definition
qgshttpheaders.h:41
QgsNetworkContentFetcherTask::QgsNetworkContentFetcherTask
QgsNetworkContentFetcherTask(const QUrl &url, const QString &authcfg=QString(), QgsTask::Flags flags=QgsTask::CanCancel, const QString &description=QString(), const QgsHttpHeaders &headers=QgsHttpHeaders())
Constructor for a QgsNetworkContentFetcherTask which fetches the specified url.
Definition
qgsnetworkcontentfetchertask.cpp:28
QgsNetworkContentFetcherTask::run
bool run() override
Performs the task's operation.
Definition
qgsnetworkcontentfetchertask.cpp:45
QgsNetworkContentFetcherTask::contentAsString
QString contentAsString() const
Returns the fetched content as a string.
Definition
qgsnetworkcontentfetchertask.cpp:104
QgsNetworkContentFetcherTask::contentDispositionFilename
QString contentDispositionFilename() const
Returns the associated filename from the reply's content disposition header, if present.
Definition
qgsnetworkcontentfetchertask.cpp:99
QgsNetworkContentFetcherTask::fetched
void fetched()
Emitted when the network content has been fetched, regardless of whether the fetch was successful or ...
QgsNetworkContentFetcherTask::~QgsNetworkContentFetcherTask
~QgsNetworkContentFetcherTask() override
Definition
qgsnetworkcontentfetchertask.cpp:39
QgsNetworkContentFetcherTask::reply
QNetworkReply * reply()
Returns the network reply.
Definition
qgsnetworkcontentfetchertask.cpp:94
QgsNetworkContentFetcherTask::cancel
void cancel() override
Notifies the task that it should terminate.
Definition
qgsnetworkcontentfetchertask.cpp:86
QgsNetworkContentFetcherTask::errorOccurred
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...
QgsNetworkContentFetcher
HTTP network content fetcher.
Definition
qgsnetworkcontentfetcher.h:43
QgsNetworkContentFetcher::finished
void finished()
Emitted when content has loaded.
QgsNetworkContentFetcher::errorOccurred
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...
QgsNetworkContentFetcher::downloadProgress
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
Emitted when data is received.
QgsTask::flags
Flags flags() const
Returns the flags associated with the task.
Definition
qgstaskmanager.h:93
QgsTask::progress
double progress() const
Returns the task's progress (between 0.0 and 100.0).
Definition
qgstaskmanager.h:126
QgsTask::Flags
QFlags< Flag > Flags
Definition
qgstaskmanager.h:79
QgsTask::cancel
virtual void cancel()
Notifies the task that it should terminate.
Definition
qgstaskmanager.cpp:98
QgsTask::QgsTask
QgsTask(const QString &description=QString(), QgsTask::Flags flags=AllFlags)
Constructor for QgsTask.
Definition
qgstaskmanager.cpp:36
QgsTask::description
QString description() const
Returns the task's description.
Definition
qgstaskmanager.h:121
QgsTask::isCanceled
bool isCanceled() const
Will return true if task should terminate ASAP.
Definition
qgstaskmanager.cpp:125
QgsTask::setProgress
void setProgress(double progress)
Sets the task's current progress.
Definition
qgstaskmanager.cpp:239
qgsnetworkcontentfetcher.h
qgsnetworkcontentfetchertask.h
qgsnetworkreply.h
Generated on
for QGIS API Documentation by
1.17.0