QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 }
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.
void fetched()
Emitted when the network content has been fetched, regardless of whether the fetch was successful or ...
QNetworkReply * reply()
Returns the network reply.
QgsNetworkContentFetcherTask(const QUrl &url, const QString &authcfg=QString(), QgsTask::Flags flags=QgsTask::CanCancel)
Constructor for a QgsNetworkContentFetcherTask which fetches the specified url.
void cancel() override
Notifies the task that it should terminate.
HTTP network content fetcher.
void finished()
Emitted when content has loaded.
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)
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.