QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 
23  : QgsNetworkContentFetcherTask( QNetworkRequest( url ) )
24 {
25 }
26 
28  : QgsTask( tr( "Fetching %1" ).arg( request.url().toString() ) )
29  , mRequest( request )
30 {
31 }
32 
34 {
35  if ( mFetcher )
36  mFetcher->deleteLater();
37 }
38 
40 {
41  mFetcher = new QgsNetworkContentFetcher();
42  QEventLoop loop;
43  connect( mFetcher, &QgsNetworkContentFetcher::finished, &loop, &QEventLoop::quit );
44  connect( mFetcher, &QgsNetworkContentFetcher::downloadProgress, this, [ = ]( qint64 bytesReceived, qint64 bytesTotal )
45  {
46  if ( !isCanceled() && bytesTotal > 0 )
47  {
48  int progress = ( bytesReceived * 100 ) / bytesTotal;
49  // don't emit 100% progress reports until completely fetched - otherwise we get
50  // intermediate 100% reports from redirects
51  if ( progress < 100 )
52  setProgress( progress );
53  }
54  } );
55  mFetcher->fetchContent( mRequest );
56  loop.exec();
57  if ( !isCanceled() )
58  setProgress( 100 );
59  emit fetched();
60  return true;
61 }
62 
64 {
65  if ( mFetcher )
66  mFetcher->cancel();
67 
69 }
70 
72 {
73  return mFetcher ? mFetcher->reply() : nullptr;
74 }
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.
Handles HTTP network content fetching in a background task.
QgsNetworkContentFetcherTask(const QUrl &url)
Constructor for a QgsNetworkContentFetcherTask which fetches the specified url.
HTTP network content fetcher.
Abstract base class for long running background tasks.
void finished()
Emitted when content has loaded.
void cancel()
Cancels any ongoing request.
virtual void cancel()
Notifies the task that it should terminate.
double progress() const
Returns the task&#39;s progress (between 0.0 and 100.0)
void fetchContent(const QUrl &url)
Fetches content from a remote URL and handles redirects.
bool run() override
Performs the task&#39;s operation.
void cancel() override
Notifies the task that it should terminate.