@@ -39,40 +39,53 @@ QgsNetworkContentFetcherRegistry::~QgsNetworkContentFetcherRegistry()
39
39
40
40
const QgsFetchedContent *QgsNetworkContentFetcherRegistry::fetch ( const QUrl &url, const FetchingMode &fetchingMode )
41
41
{
42
+ QMutexLocker locker ( &mMutex );
42
43
if ( mFileRegistry .contains ( url ) )
43
44
{
44
45
return mFileRegistry .value ( url );
45
46
}
46
47
47
48
QgsFetchedContent *content = new QgsFetchedContent ( nullptr , QgsFetchedContent::NotStarted );
48
- QgsNetworkContentFetcherTask *fetcher = new QgsNetworkContentFetcherTask ( url );
49
+ content-> mFetchingTask = new QgsNetworkContentFetcherTask ( url );
49
50
51
+ // start
50
52
QObject::connect ( content, &QgsFetchedContent::downloadStarted, this , [ = ]( const bool redownload )
51
53
{
52
54
QMutexLocker locker ( &mMutex );
53
55
if ( mFileRegistry .contains ( url ) && redownload )
54
56
{
55
- const QgsFetchedContent *content = mFileRegistry [url];
56
- if ( mFileRegistry .value ( url )->status () == QgsFetchedContent::Downloading && content-> mFetchingTask )
57
+ QgsFetchedContent *content = mFileRegistry [url];
58
+ if ( mFileRegistry .value ( url )->status () == QgsFetchedContent::Downloading )
57
59
{
58
- content->mFetchingTask ->cancel ();
59
- }
60
- if ( content->mFile )
61
- {
62
- content->mFile ->deleteLater ();
63
- mFileRegistry [url]->setFilePath ( QStringLiteral () );
60
+ content->cancel ();
64
61
}
65
62
}
66
63
if ( ( mFileRegistry .contains ( url ) && mFileRegistry .value ( url )->status () == QgsFetchedContent::NotStarted ) || redownload )
67
64
{
68
- QgsApplication::instance ()->taskManager ()->addTask ( fetcher );
65
+ QgsApplication::instance ()->taskManager ()->addTask ( content-> mFetchingTask );
69
66
}
70
67
} );
71
68
72
- QObject::connect ( fetcher, &QgsNetworkContentFetcherTask::fetched, this , [ = ]()
69
+ // cancel
70
+ QObject::connect ( content, &QgsFetchedContent::cancelTriggered, this , [ = ]()
73
71
{
74
72
QMutexLocker locker ( &mMutex );
75
- QNetworkReply *reply = fetcher->reply ();
73
+ if ( content->mFetchingTask )
74
+ {
75
+ content->mFetchingTask ->cancel ();
76
+ }
77
+ if ( content->mFile )
78
+ {
79
+ content->mFile ->deleteLater ();
80
+ mFileRegistry [url]->setFilePath ( QStringLiteral () );
81
+ }
82
+ } );
83
+
84
+ // finished
85
+ QObject::connect ( content->mFetchingTask , &QgsNetworkContentFetcherTask::fetched, this , [ = ]()
86
+ {
87
+ QMutexLocker locker ( &mMutex );
88
+ QNetworkReply *reply = content->mFetchingTask ->reply ();
76
89
QgsFetchedContent *content = mFileRegistry .value ( url );
77
90
if ( reply->error () == QNetworkReply::NoError )
78
91
{
@@ -96,6 +109,7 @@ const QgsFetchedContent *QgsNetworkContentFetcherRegistry::fetch( const QUrl &ur
96
109
97
110
if ( fetchingMode == DownloadImmediately )
98
111
content->download ();
112
+
99
113
mFileRegistry .insert ( url, content );
100
114
101
115
return content;
0 commit comments