QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 <QEventLoop>
22 
23 QgsNetworkContentFetcherTask::QgsNetworkContentFetcherTask( const QUrl &url, const QString &authcfg, QgsTask::Flags flags )
24  : QgsNetworkContentFetcherTask( QNetworkRequest( url ), authcfg, flags )
25 {
26 }
27 
28 QgsNetworkContentFetcherTask::QgsNetworkContentFetcherTask( const QNetworkRequest &request, const QString &authcfg, QgsTask::Flags flags )
29  : QgsTask( tr( "Fetching %1" ).arg( request.url().toString() ), flags )
30  , mRequest( request )
31  , mAuthcfg( authcfg )
32 {
33 }
34 
36 {
37  if ( mFetcher )
38  mFetcher->deleteLater();
39 }
40 
42 {
43  mFetcher = new QgsNetworkContentFetcher();
44  QEventLoop loop;
45 
46  // We need to set the event loop (and not 'this') as receiver for all signal to ensure execution
47  // in the same thread and in the same order of emission. Indeed 'this' and 'loop' lives in
48  // different thread because they have been created in different thread.
49 
50  connect( mFetcher, &QgsNetworkContentFetcher::finished, &loop, &QEventLoop::quit );
51  connect( mFetcher, &QgsNetworkContentFetcher::downloadProgress, &loop, [ = ]( qint64 bytesReceived, qint64 bytesTotal )
52  {
53  if ( !isCanceled() && bytesTotal > 0 )
54  {
55  const int progress = ( bytesReceived * 100 ) / bytesTotal;
56  // don't emit 100% progress reports until completely fetched - otherwise we get
57  // intermediate 100% reports from redirects
58  if ( progress < 100 )
60  }
61  } );
62 
63 
64  bool hasErrorOccurred = false;
65  connect( mFetcher, &QgsNetworkContentFetcher::errorOccurred, &loop, [ &hasErrorOccurred, this ]( QNetworkReply::NetworkError code, const QString & errorMsg )
66  {
67  hasErrorOccurred = true;
68  emit errorOccurred( code, errorMsg );
69  } );
70 
71  mFetcher->fetchContent( mRequest, mAuthcfg );
72  loop.exec();
73  if ( !isCanceled() )
74  setProgress( 100 );
75  emit fetched();
76 
77  return !isCanceled() && !hasErrorOccurred;
78 }
79 
81 {
82  if ( mFetcher )
83  mFetcher->cancel();
84 
86 }
87 
89 {
90  return mFetcher ? mFetcher->reply() : nullptr;
91 }
92 
94 {
95  return mFetcher ? mFetcher->contentAsString() : QString();
96 }
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:88
QgsNetworkContentFetcherTask
Handles HTTP network content fetching in a background task.
Definition: qgsnetworkcontentfetchertask.h:47
qgsnetworkcontentfetchertask.h
QgsNetworkContentFetcherTask::cancel
void cancel() override
Notifies the task that it should terminate.
Definition: qgsnetworkcontentfetchertask.cpp:80
QgsTask::cancel
virtual void cancel()
Notifies the task that it should terminate.
Definition: qgstaskmanager.cpp:91
QgsNetworkContentFetcher::errorOccurred
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...
QgsNetworkContentFetcher
HTTP network content fetcher. A simple method for fetching remote HTTP content and converting the con...
Definition: qgsnetworkcontentfetcher.h:39
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:89
QgsNetworkContentFetcher::cancel
void cancel()
Cancels any ongoing request.
Definition: qgsnetworkcontentfetcher.cpp:113
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:93
QgsNetworkContentFetcher::contentAsString
QString contentAsString() const
Returns the fetched content as a string.
Definition: qgsnetworkcontentfetcher.cpp:99
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:23
qgsnetworkcontentfetcher.h
QgsNetworkContentFetcherTask::~QgsNetworkContentFetcherTask
~QgsNetworkContentFetcherTask() override
Definition: qgsnetworkcontentfetchertask.cpp:35
QgsNetworkContentFetcherTask::run
bool run() override
Performs the task's operation.
Definition: qgsnetworkcontentfetchertask.cpp:41
QgsTask::progress
double progress() const
Returns the task's progress (between 0.0 and 100.0)
Definition: qgstaskmanager.h:127
QgsNetworkContentFetcherTask::errorOccurred
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...
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. Tasks can be controlled directly,...
Definition: qgstaskmanager.h:54