QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
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
20#include "moc_qgsnetworkcontentfetchertask.cpp"
22#include "qgsnetworkreply.h"
23#include <QEventLoop>
24
25QgsNetworkContentFetcherTask::QgsNetworkContentFetcherTask( const QUrl &url, const QString &authcfg, QgsTask::Flags flags, const QString &description )
26 : QgsNetworkContentFetcherTask( QNetworkRequest( url ), authcfg, flags, description )
27{
28}
29
30QgsNetworkContentFetcherTask::QgsNetworkContentFetcherTask( const QNetworkRequest &request, const QString &authcfg, QgsTask::Flags flags, const QString &description )
31 : QgsTask( description.isEmpty() ? tr( "Fetching %1" ).arg( request.url().toString() ) : description, flags )
32 , mRequest( request )
33 , mAuthcfg( authcfg )
34{
35}
36
38{
39 if ( mFetcher )
40 mFetcher->deleteLater();
41}
42
44{
45 mFetcher = new QgsNetworkContentFetcher();
46 QEventLoop loop;
47
48 // We need to set the event loop (and not 'this') as receiver for all signal to ensure execution
49 // in the same thread and in the same order of emission. Indeed 'this' and 'loop' lives in
50 // different thread because they have been created in different thread.
51
52 connect( mFetcher, &QgsNetworkContentFetcher::finished, &loop, &QEventLoop::quit );
53 connect( mFetcher, &QgsNetworkContentFetcher::downloadProgress, &loop, [this]( qint64 bytesReceived, qint64 bytesTotal )
54 {
55 if ( !isCanceled() && bytesTotal > 0 )
56 {
57 const int progress = ( bytesReceived * 100 ) / bytesTotal;
58 // don't emit 100% progress reports until completely fetched - otherwise we get
59 // intermediate 100% reports from redirects
60 if ( progress < 100 )
62 }
63 } );
64
65
66 bool hasErrorOccurred = false;
67 connect( mFetcher, &QgsNetworkContentFetcher::errorOccurred, &loop, [ &hasErrorOccurred, this ]( QNetworkReply::NetworkError code, const QString & errorMsg )
68 {
69 hasErrorOccurred = true;
70 emit errorOccurred( code, errorMsg );
71 } );
72
73 mFetcher->fetchContent( mRequest, mAuthcfg );
74 loop.exec();
75 if ( !isCanceled() )
76 setProgress( 100 );
77 emit fetched();
78
79 return !isCanceled() && !hasErrorOccurred;
80}
81
83{
84 if ( mFetcher )
85 mFetcher->cancel();
86
88}
89
91{
92 return mFetcher ? mFetcher->reply() : nullptr;
93}
94
96{
97 return mFetcher ? mFetcher->contentDispositionFilename() : QString();
98}
99
101{
102 return mFetcher ? mFetcher->contentAsString() : QString();
103}
Handles HTTP network content fetching in a background task.
bool run() override
Performs the task's operation.
QString contentAsString() const
Returns the fetched content as a string.
QString contentDispositionFilename() const
Returns the associated filename from the reply's content disposition header, if present.
void fetched()
Emitted when the network content has been fetched, regardless of whether the fetch was successful or ...
QNetworkReply * reply()
Returns the network reply.
void cancel() override
Notifies the task that it should terminate.
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...
QgsNetworkContentFetcherTask(const QUrl &url, const QString &authcfg=QString(), QgsTask::Flags flags=QgsTask::CanCancel, const QString &description=QString())
Constructor for a QgsNetworkContentFetcherTask which fetches the specified url.
HTTP network content fetcher.
QString contentDispositionFilename() const
Returns the associated filename from the reply's content disposition header, if present.
void finished()
Emitted when content has loaded.
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...
void cancel()
Cancels any ongoing request.
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
Emitted when data is received.
QNetworkReply * reply()
Returns a reference to the network reply.
QString contentAsString() const
Returns the fetched content as a string.
void fetchContent(const QUrl &url, const QString &authcfg=QString())
Fetches content from a remote URL and handles redirects.
Abstract base class for long running background tasks.
double progress() const
Returns the task's progress (between 0.0 and 100.0)
QFlags< Flag > Flags
virtual void cancel()
Notifies the task that it should terminate.
bool isCanceled() const
Will return true if task should terminate ASAP.
void setProgress(double progress)
Sets the task's current progress.