Skip to content

Commit e4959a6

Browse files
committed
New class QgsNetworkReplyContent
Encapsulates a network reply within a container which is inexpensive to copy and safe to pass around between threads. The default Qt QNetworkReply class is a QObject, which prevents it from being copied and passed between threads. This class grabs all the useful information from a QNetworkReply, allowing the reply's content to be stored indefinetly without concern for the lifetime of the QNetworkReply object itself.
1 parent a2b5008 commit e4959a6

File tree

5 files changed

+291
-0
lines changed

5 files changed

+291
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/core/qgsnetworkreply.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
class QgsNetworkReplyContent
12+
{
13+
%Docstring
14+
Encapsulates a network reply within a container which is inexpensive to copy and safe to pass between threads.
15+
16+
.. versionadded:: 3.6
17+
%End
18+
19+
%TypeHeaderCode
20+
#include "qgsnetworkreply.h"
21+
%End
22+
public:
23+
24+
QgsNetworkReplyContent();
25+
%Docstring
26+
Default constructor for an empty reply.
27+
%End
28+
29+
explicit QgsNetworkReplyContent( QNetworkReply *reply );
30+
%Docstring
31+
Constructor for QgsNetworkReplyContent, populated from the specified ``reply``.
32+
%End
33+
34+
void clear();
35+
%Docstring
36+
Clears the reply, resetting it back to a default, empty reply.
37+
%End
38+
39+
QByteArray content() const;
40+
%Docstring
41+
Returns the raw reply content.
42+
%End
43+
44+
QNetworkReply::NetworkError error() const;
45+
%Docstring
46+
Returns the reply's error message, or QNetworkReply.NoError if no
47+
error was encountered.
48+
49+
.. seealso:: :py:func:`errorString`
50+
%End
51+
52+
QString errorString() const;
53+
%Docstring
54+
Returns the error text for the reply, or an empty string if no
55+
error was encountered.
56+
57+
.. seealso:: :py:func:`error`
58+
%End
59+
60+
61+
bool hasRawHeader( const QByteArray &headerName ) const;
62+
%Docstring
63+
Returns true if the reply contains a header with the specified ``headerName``.
64+
65+
.. seealso:: :py:func:`rawHeaderPairs`
66+
67+
.. seealso:: :py:func:`rawHeaderList`
68+
69+
.. seealso:: :py:func:`rawHeader`
70+
%End
71+
72+
QList<QByteArray> rawHeaderList() const;
73+
%Docstring
74+
Returns a list of raw header names contained within the reply.
75+
76+
.. seealso:: :py:func:`rawHeaderPairs`
77+
78+
.. seealso:: :py:func:`hasRawHeader`
79+
80+
.. seealso:: :py:func:`rawHeader`
81+
%End
82+
83+
QByteArray rawHeader( const QByteArray &headerName ) const;
84+
%Docstring
85+
Returns the content of the header with the specified ``headerName``, or an
86+
empty QByteArray if the specified header was not found in the reply.
87+
88+
.. seealso:: :py:func:`rawHeaderPairs`
89+
90+
.. seealso:: :py:func:`hasRawHeader`
91+
92+
.. seealso:: :py:func:`rawHeaderList`
93+
%End
94+
95+
};
96+
97+
/************************************************************************
98+
* This file has been generated automatically from *
99+
* *
100+
* src/core/qgsnetworkreply.h *
101+
* *
102+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
103+
************************************************************************/

python/core/core_auto.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
%Include auto_generated/qgsmargins.sip
8181
%Include auto_generated/qgsmimedatautils.sip
8282
%Include auto_generated/qgsmultirenderchecker.sip
83+
%Include auto_generated/qgsnetworkreply.sip
8384
%Include auto_generated/qgsobjectcustomproperties.sip
8485
%Include auto_generated/qgsogcutils.sip
8586
%Include auto_generated/qgsoptional.sip

src/core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ SET(QGIS_CORE_SRCS
257257
qgsnetworkcontentfetcher.cpp
258258
qgsnetworkcontentfetcherregistry.cpp
259259
qgsnetworkcontentfetchertask.cpp
260+
qgsnetworkreply.cpp
260261
qgsnetworkreplyparser.cpp
261262
qgsobjectcustomproperties.cpp
262263
qgsofflineediting.cpp
@@ -908,6 +909,7 @@ SET(QGIS_CORE_HDRS
908909
qgsmargins.h
909910
qgsmimedatautils.h
910911
qgsmultirenderchecker.h
912+
qgsnetworkreply.h
911913
qgsobjectcustomproperties.h
912914
qgsogcutils.h
913915
qgsoptional.h

src/core/qgsnetworkreply.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/***************************************************************************
2+
qgsnetworkreply.cpp
3+
-------------------
4+
begin : November 2018
5+
copyright : (C) 2018 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#include "qgsnetworkreply.h"
17+
#include <QNetworkReply>
18+
19+
QgsNetworkReplyContent::QgsNetworkReplyContent( QNetworkReply *reply )
20+
: mContent( reply->readAll() )
21+
, mError( reply->error() )
22+
, mErrorString( reply->errorString() )
23+
, mRawHeaderPairs( reply->rawHeaderPairs() )
24+
{}
25+
26+
void QgsNetworkReplyContent::clear()
27+
{
28+
*this = QgsNetworkReplyContent();
29+
}
30+
31+
bool QgsNetworkReplyContent::hasRawHeader( const QByteArray &headerName ) const
32+
{
33+
for ( auto &header : mRawHeaderPairs )
34+
{
35+
if ( header.first == headerName )
36+
return true;
37+
}
38+
return false;
39+
}
40+
41+
QList<QByteArray> QgsNetworkReplyContent::rawHeaderList() const
42+
{
43+
QList< QByteArray > res;
44+
res.reserve( mRawHeaderPairs.length() );
45+
for ( auto &header : mRawHeaderPairs )
46+
{
47+
res << header.first;
48+
}
49+
return res;
50+
}
51+
52+
QByteArray QgsNetworkReplyContent::rawHeader( const QByteArray &headerName ) const
53+
{
54+
for ( auto &header : mRawHeaderPairs )
55+
{
56+
if ( header.first == headerName )
57+
return header.second;
58+
}
59+
return QByteArray();
60+
}

src/core/qgsnetworkreply.h

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/***************************************************************************
2+
qgsnetworkreply.h
3+
-----------------
4+
begin : November 2018
5+
copyright : (C) 2018 by Nyall Dawson
6+
email : nyall dot dawson at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
#ifndef QGSNETWORKREPLY_H
16+
#define QGSNETWORKREPLY_H
17+
18+
#include "qgis_core.h"
19+
20+
#include <QNetworkReply>
21+
22+
/**
23+
* Encapsulates a network reply within a container which is inexpensive to copy and safe to pass between threads.
24+
* \ingroup core
25+
* \since QGIS 3.6
26+
*/
27+
class CORE_EXPORT QgsNetworkReplyContent
28+
{
29+
public:
30+
31+
/**
32+
* Default constructor for an empty reply.
33+
*/
34+
QgsNetworkReplyContent() = default;
35+
36+
/**
37+
* Constructor for QgsNetworkReplyContent, populated from the specified \a reply.
38+
*/
39+
explicit QgsNetworkReplyContent( QNetworkReply *reply );
40+
41+
/**
42+
* Clears the reply, resetting it back to a default, empty reply.
43+
*/
44+
void clear();
45+
46+
/**
47+
* Returns the raw reply content.
48+
*/
49+
QByteArray content() const
50+
{
51+
return mContent;
52+
}
53+
54+
/**
55+
* Returns the reply's error message, or QNetworkReply::NoError if no
56+
* error was encountered.
57+
*
58+
* \see errorString()
59+
*/
60+
QNetworkReply::NetworkError error() const
61+
{
62+
return mError;
63+
}
64+
65+
/**
66+
* Returns the error text for the reply, or an empty string if no
67+
* error was encountered.
68+
*
69+
* \see error()
70+
*/
71+
QString errorString() const
72+
{
73+
return mErrorString;
74+
}
75+
76+
#ifndef SIP_RUN
77+
typedef QPair<QByteArray, QByteArray> RawHeaderPair;
78+
79+
/**
80+
* Returns the list of raw header pairs in the reply.
81+
* \see hasRawHeader()
82+
* \see rawHeaderList()
83+
* \see rawHeader()
84+
* \note Not available in Python bindings
85+
*/
86+
const QList<RawHeaderPair> &rawHeaderPairs() const
87+
{
88+
return mRawHeaderPairs;
89+
}
90+
#endif
91+
92+
/**
93+
* Returns true if the reply contains a header with the specified \a headerName.
94+
* \see rawHeaderPairs()
95+
* \see rawHeaderList()
96+
* \see rawHeader()
97+
*/
98+
bool hasRawHeader( const QByteArray &headerName ) const;
99+
100+
/**
101+
* Returns a list of raw header names contained within the reply.
102+
* \see rawHeaderPairs()
103+
* \see hasRawHeader()
104+
* \see rawHeader()
105+
*/
106+
QList<QByteArray> rawHeaderList() const;
107+
108+
/**
109+
* Returns the content of the header with the specified \a headerName, or an
110+
* empty QByteArray if the specified header was not found in the reply.
111+
* \see rawHeaderPairs()
112+
* \see hasRawHeader()
113+
* \see rawHeaderList()
114+
*/
115+
QByteArray rawHeader( const QByteArray &headerName ) const;
116+
117+
private:
118+
119+
QByteArray mContent;
120+
QNetworkReply::NetworkError mError = QNetworkReply::NoError;
121+
QString mErrorString;
122+
QList<RawHeaderPair> mRawHeaderPairs;
123+
};
124+
125+
#endif // QGSNETWORKREPLY_H

0 commit comments

Comments
 (0)