QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 )
23  : QgsNetworkContentFetcherTask( QNetworkRequest( url ), authcfg )
24 {
25 }
26 
27 QgsNetworkContentFetcherTask::QgsNetworkContentFetcherTask( const QNetworkRequest &request, const QString &authcfg )
28  : QgsTask( tr( "Fetching %1" ).arg( request.url().toString() ) )
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 )
53  setProgress( progress );
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 }
void setProgress(double progress)
Sets the task&#39;s current progress.
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
Emitted when data is received.
void fetched()
Emitted when the network content has been fetched, regardless of whether the fetch was successful or ...
QNetworkReply * reply()
Returns a reference to the network reply.
bool isCanceled() const
Will return true if task should terminate ASAP.
QNetworkReply * reply()
Returns the network reply.
QString contentAsString() const
Returns the fetched content as a string.
Handles HTTP network content fetching in a background task.
HTTP network content fetcher.
Abstract base class for long running background tasks.
void finished()
Emitted when content has loaded.
QgsNetworkContentFetcherTask(const QUrl &url, const QString &authcfg=QString())
Constructor for a QgsNetworkContentFetcherTask which fetches the specified url.
void fetchContent(const QUrl &url, const QString &authcfg=QString())
Fetches content from a remote URL and handles redirects.
void cancel()
Cancels any ongoing request.
virtual void cancel()
Notifies the task that it should terminate.
QString contentAsString() const
Returns the fetched content as a string.
double progress() const
Returns the task&#39;s progress (between 0.0 and 100.0)
bool run() override
Performs the task&#39;s operation.
void cancel() override
Notifies the task that it should terminate.