summaryrefslogtreecommitdiffstats
path: root/src/network
diff options
context:
space:
mode:
authorMårten Nordheim <marten.nordheim@qt.io>2021-10-25 20:23:24 +0200
committerMårten Nordheim <marten.nordheim@qt.io>2021-11-04 12:02:25 +0100
commitbd52c1bba6f5a0b7e820596e566146b5cf8a4ee7 (patch)
tree728aa2010288daee21336992428d57e306f22282 /src/network
parent2148e1f0e6026ad7a87c7eec0362d1250c1f09e9 (diff)
QNI: Add API to check if connection is metered
This may be a useful factor in deciding whether or not you should perform communications over the network which are not purely essential. For example, if you have a logging mechanism you can delay uploading them until you are no longer on a metered network. Task-number: QTBUG-91024 Change-Id: I19d32f031a3893512dc440914133678004987fb1 Reviewed-by: Paul Wicking <paul.wicking@qt.io> Reviewed-by: Marc Mutz <marc.mutz@qt.io> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src/network')
-rw-r--r--src/network/kernel/qnetworkinformation.cpp23
-rw-r--r--src/network/kernel/qnetworkinformation.h5
-rw-r--r--src/network/kernel/qnetworkinformation_p.h11
3 files changed, 39 insertions, 0 deletions
diff --git a/src/network/kernel/qnetworkinformation.cpp b/src/network/kernel/qnetworkinformation.cpp
index 5d889ac574..034a277b3c 100644
--- a/src/network/kernel/qnetworkinformation.cpp
+++ b/src/network/kernel/qnetworkinformation.cpp
@@ -457,6 +457,11 @@ QNetworkInformationBackendFactory::~QNetworkInformationBackendFactory()
property will provide useful results. Otherwise it will always return
\c{TransportMedium::Unknown}.
See also QNetworkInformation::TransportMedium.
+
+ \value Metered
+ If the plugin supports this feature then the \c isMetered
+ property will provide useful results. Otherwise it will always return
+ \c{false}.
*/
/*!
@@ -520,6 +525,8 @@ QNetworkInformation::QNetworkInformation(QNetworkInformationBackend *backend)
&QNetworkInformation::isBehindCaptivePortalChanged);
connect(backend, &QNetworkInformationBackend::transportMediumChanged, this,
&QNetworkInformation::transportMediumChanged);
+ connect(backend, &QNetworkInformationBackend::isMeteredChanged, this,
+ &QNetworkInformation::isMeteredChanged);
}
/*!
@@ -580,6 +587,22 @@ QNetworkInformation::TransportMedium QNetworkInformation::transportMedium() cons
}
/*!
+ \property QNetworkInformation::isMetered
+ \brief Check if the current connection is metered
+ \since 6.3
+
+ This property returns whether the current connection is (known to be)
+ metered or not. You can use this as a guiding factor to decide whether your
+ application should perform certain network requests or uploads.
+ For instance, you may not want to upload logs or diagnostics while this
+ property is \c true.
+*/
+bool QNetworkInformation::isMetered() const
+{
+ return d_func()->backend->isMetered();
+}
+
+/*!
Returns the name of the currently loaded backend.
*/
QString QNetworkInformation::backendName() const
diff --git a/src/network/kernel/qnetworkinformation.h b/src/network/kernel/qnetworkinformation.h
index e280894fef..9b09fcee98 100644
--- a/src/network/kernel/qnetworkinformation.h
+++ b/src/network/kernel/qnetworkinformation.h
@@ -58,6 +58,7 @@ class Q_NETWORK_EXPORT QNetworkInformation : public QObject
Q_PROPERTY(bool isBehindCaptivePortal READ isBehindCaptivePortal
NOTIFY isBehindCaptivePortalChanged)
Q_PROPERTY(TransportMedium transportMedium READ transportMedium NOTIFY transportMediumChanged)
+ Q_PROPERTY(bool isMetered READ isMetered NOTIFY isMeteredChanged)
public:
enum class Reachability {
Unknown,
@@ -81,6 +82,7 @@ public:
Reachability = 0x1,
CaptivePortal = 0x2,
TransportMedium = 0x4,
+ Metered = 0x8,
};
Q_DECLARE_FLAGS(Features, Feature)
Q_FLAG(Features)
@@ -91,6 +93,8 @@ public:
TransportMedium transportMedium() const;
+ bool isMetered() const;
+
QString backendName() const;
bool supports(Features features) const;
@@ -106,6 +110,7 @@ Q_SIGNALS:
void reachabilityChanged(Reachability newReachability);
void isBehindCaptivePortalChanged(bool state);
void transportMediumChanged(TransportMedium current);
+ void isMeteredChanged(bool isMetered);
private:
friend struct QNetworkInformationDeleter;
diff --git a/src/network/kernel/qnetworkinformation_p.h b/src/network/kernel/qnetworkinformation_p.h
index 3493de3b44..36084b8e44 100644
--- a/src/network/kernel/qnetworkinformation_p.h
+++ b/src/network/kernel/qnetworkinformation_p.h
@@ -87,11 +87,13 @@ public:
Reachability reachability() const { return m_reachability; }
bool behindCaptivePortal() const { return m_behindCaptivePortal; }
TransportMedium transportMedium() const { return m_transportMedium; }
+ bool isMetered() const { return m_metered; }
Q_SIGNALS:
void reachabilityChanged(Reachability reachability);
void behindCaptivePortalChanged(bool behindPortal);
void transportMediumChanged(TransportMedium medium);
+ void isMeteredChanged(bool isMetered);
protected:
void setReachability(QNetworkInformation::Reachability reachability)
@@ -118,10 +120,19 @@ protected:
}
}
+ void setMetered(bool isMetered)
+ {
+ if (m_metered != isMetered) {
+ m_metered = isMetered;
+ emit isMeteredChanged(isMetered);
+ }
+ }
+
private:
Reachability m_reachability = Reachability::Unknown;
TransportMedium m_transportMedium = TransportMedium::Unknown;
bool m_behindCaptivePortal = false;
+ bool m_metered = false;
Q_DISABLE_COPY_MOVE(QNetworkInformationBackend)
friend class QNetworkInformation;