QGIS API Documentation 3.39.0-Master (bca3cdb6021)
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
21#include "qgsnetworkreply.h"
22#include <QEventLoop>
23
24QgsNetworkContentFetcherTask::QgsNetworkContentFetcherTask( const QUrl &url, const QString &authcfg, QgsTask::Flags flags, const QString &description )
25 : QgsNetworkContentFetcherTask( QNetworkRequest( url ), authcfg, flags, description )
26{
27}
28
29QgsNetworkContentFetcherTask::QgsNetworkContentFetcherTask( const QNetworkRequest &request, const QString &authcfg, QgsTask::Flags flags, const QString &description )
30 : QgsTask( description.isEmpty() ? tr( "Fetching %1" ).arg( request.url().toString() ) : description, flags )
31 , mRequest( request )
32 , mAuthcfg( authcfg )
33{
34}
35
37{
38 if ( mFetcher )
39 mFetcher->deleteLater();
40}
41
43{
44 mFetcher = new QgsNetworkContentFetcher();
45 QEventLoop loop;
46
47 // We need to set the event loop (and not 'this') as receiver for all signal to ensure execution
48 // in the same thread and in the same order of emission. Indeed 'this' and 'loop' lives in
49 // different thread because they have been created in different thread.
50
51 connect( mFetcher, &QgsNetworkContentFetcher::finished, &loop, &QEventLoop::quit );
52 connect( mFetcher, &QgsNetworkContentFetcher::downloadProgress, &loop, [this]( qint64 bytesReceived, qint64 bytesTotal )
53 {
54 if ( !isCanceled() && bytesTotal > 0 )
55 {
56 const int progress = ( bytesReceived * 100 ) / bytesTotal;
57 // don't emit 100% progress reports until completely fetched - otherwise we get
58 // intermediate 100% reports from redirects
59 if ( progress < 100 )
61 }
62 } );
63
64
65 bool hasErrorOccurred = false;
66 connect( mFetcher, &QgsNetworkContentFetcher::errorOccurred, &loop, [ &hasErrorOccurred, this ]( QNetworkReply::NetworkError code, const QString & errorMsg )
67 {
68 hasErrorOccurred = true;
69 emit errorOccurred( code, errorMsg );
70 } );
71
72 mFetcher->fetchContent( mRequest, mAuthcfg );
73 loop.exec();
74 if ( !isCanceled() )
75 setProgress( 100 );
76 emit fetched();
77
78 return !isCanceled() && !hasErrorOccurred;
79}
80
82{
83 if ( mFetcher )
84 mFetcher->cancel();
85
87}
88
90{
91 return mFetcher ? mFetcher->reply() : nullptr;
92}
93
95{
96 return mFetcher ? mFetcher->contentDispositionFilename() : QString();
97}
98
100{
101 return mFetcher ? mFetcher->contentAsString() : QString();
102}
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.