QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
22 QgsNetworkContentFetcherTask::QgsNetworkContentFetcherTask( const QUrl &url, const QString &authcfg, QgsTask::Flags flags )
23  : QgsNetworkContentFetcherTask( QNetworkRequest( url ), authcfg, flags )
24 {
25 }
26 
27 QgsNetworkContentFetcherTask::QgsNetworkContentFetcherTask( const QNetworkRequest &request, const QString &authcfg, QgsTask::Flags flags )
28  : QgsTask( tr( "Fetching %1" ).arg( request.url().toString() ), flags )
29  , mRequest( request )
30  , mAuthcfg( authcfg )
31 {
32 }
33 
35 {
36  if ( mFetcher )
37  mFetcher->deleteLater();
38 }
39 
41 {
42  mFetcher = new QgsNetworkContentFetcher();
43  QEventLoop loop;
44  connect( mFetcher, &QgsNetworkContentFetcher::finished, &loop, &QEventLoop::quit );
45  connect( mFetcher, &QgsNetworkContentFetcher::downloadProgress, this, [ = ]( qint64 bytesReceived, qint64 bytesTotal )
46  {
47  if ( !isCanceled() && bytesTotal > 0 )
48  {
49  int progress = ( bytesReceived * 100 ) / bytesTotal;
50  // don't emit 100% progress reports until completely fetched - otherwise we get
51  // intermediate 100% reports from redirects
52  if ( progress < 100 )
54  }
55  } );
56  mFetcher->fetchContent( mRequest, mAuthcfg );
57  loop.exec();
58  if ( !isCanceled() )
59  setProgress( 100 );
60  emit fetched();
61  return true;
62 }
63 
65 {
66  if ( mFetcher )
67  mFetcher->cancel();
68 
70 }
71 
73 {
74  return mFetcher ? mFetcher->reply() : nullptr;
75 }
76 
78 {
79  return mFetcher ? mFetcher->contentAsString() : QString();
80 }
QgsNetworkContentFetcherTask::fetched
void fetched()
Emitted when the network content has been fetched, regardless of whether the fetch was successful or ...
QgsNetworkContentFetcherTask::reply
QNetworkReply * reply()
Returns the network reply.
Definition: qgsnetworkcontentfetchertask.cpp:72
QgsNetworkContentFetcherTask
Handles HTTP network content fetching in a background task.
Definition: qgsnetworkcontentfetchertask.h:48
qgsnetworkcontentfetchertask.h
QgsNetworkContentFetcherTask::cancel
void cancel() override
Notifies the task that it should terminate.
Definition: qgsnetworkcontentfetchertask.cpp:64
QgsTask::cancel
virtual void cancel()
Notifies the task that it should terminate.
Definition: qgstaskmanager.cpp:91
QgsNetworkContentFetcher
HTTP network content fetcher.
Definition: qgsnetworkcontentfetcher.h:38
QgsNetworkContentFetcher::fetchContent
void fetchContent(const QUrl &url, const QString &authcfg=QString())
Fetches content from a remote URL and handles redirects.
Definition: qgsnetworkcontentfetcher.cpp:37
QgsNetworkContentFetcher::reply
QNetworkReply * reply()
Returns a reference to the network reply.
Definition: qgsnetworkcontentfetcher.cpp:76
QgsNetworkContentFetcher::cancel
void cancel()
Cancels any ongoing request.
Definition: qgsnetworkcontentfetcher.cpp:100
QgsTask::setProgress
void setProgress(double progress)
Sets the task's current progress.
Definition: qgstaskmanager.cpp:232
QgsNetworkContentFetcher::downloadProgress
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
Emitted when data is received.
QgsNetworkContentFetcherTask::contentAsString
QString contentAsString() const
Returns the fetched content as a string.
Definition: qgsnetworkcontentfetchertask.cpp:77
QgsNetworkContentFetcher::contentAsString
QString contentAsString() const
Returns the fetched content as a string.
Definition: qgsnetworkcontentfetcher.cpp:86
QgsNetworkContentFetcher::finished
void finished()
Emitted when content has loaded.
QgsNetworkContentFetcherTask::QgsNetworkContentFetcherTask
QgsNetworkContentFetcherTask(const QUrl &url, const QString &authcfg=QString(), QgsTask::Flags flags=QgsTask::CanCancel)
Constructor for a QgsNetworkContentFetcherTask which fetches the specified url.
Definition: qgsnetworkcontentfetchertask.cpp:22
qgsnetworkcontentfetcher.h
QgsNetworkContentFetcherTask::~QgsNetworkContentFetcherTask
~QgsNetworkContentFetcherTask() override
Definition: qgsnetworkcontentfetchertask.cpp:34
QgsNetworkContentFetcherTask::run
bool run() override
Performs the task's operation.
Definition: qgsnetworkcontentfetchertask.cpp:40
QgsTask::progress
double progress() const
Returns the task's progress (between 0.0 and 100.0)
Definition: qgstaskmanager.h:123
QgsTask::isCanceled
bool isCanceled() const
Will return true if task should terminate ASAP.
Definition: qgstaskmanager.cpp:118
QgsTask
Abstract base class for long running background tasks.
Definition: qgstaskmanager.h:53