diff options
Diffstat (limited to 'src/network/access')
-rw-r--r-- | src/network/access/qnetworkaccessmanager.cpp | 138 | ||||
-rw-r--r-- | src/network/access/qnetworkaccessmanager.h | 17 | ||||
-rw-r--r-- | src/network/access/qnetworkaccessmanager_p.h | 13 | ||||
-rw-r--r-- | src/network/access/qnetworkreplyimpl.cpp | 12 | ||||
-rw-r--r-- | src/network/access/qnetworkreplyimpl_p.h | 4 |
5 files changed, 130 insertions, 54 deletions
diff --git a/src/network/access/qnetworkaccessmanager.cpp b/src/network/access/qnetworkaccessmanager.cpp index 1940dd1..197d89e 100644 --- a/src/network/access/qnetworkaccessmanager.cpp +++ b/src/network/access/qnetworkaccessmanager.cpp @@ -173,40 +173,58 @@ static void ensureInitialized() */ /*! - \property QNetworkAccessManager::networkAccess - \brief states whether network access is enabled or disabled through this network access - manager. + \enum QNetworkAccessManager::NetworkAccessibility + + Indicates whether the network is accessible via this network access manager. + + \value UnknownAccessibility The network accessibility cannot be determined. + \value NotAccessible The network is not currently accessible, either because there + is currently no network coverage or network access has been + explicitly disabled by a call to setNetworkAccessible(). + \value Accessible The network is accessible. + + \sa networkAccessible +*/ + +/*! + \property QNetworkAccessManager::networkAccessible + \brief whether the network is currently accessible via this network access manager. \since 4.7 - Network access is enabled by default. + If the network is \l {NotAccessible}{not accessible} the network access manager will not + process any new network requests, all such requests will fail with an error. Requests with + URLs with the file:// scheme will still be processed. + + By default the value of this property reflects the physical state of the device. Applications + may override it to disable all network requests via this network access manager by calling + + \snippet doc/src/snippets/code/src_network_access_qnetworkaccessmanager.cpp 4 + + Network requests can be reenabled again by calling - When network access is disabled the network access manager will not process any new network - requests, all such requests will fail with an error. Requests with URLs with the file:// scheme - will still be processed. + \snippet doc/src/snippets/code/src_network_access_qnetworkaccessmanager.cpp 5 - This property can be used to enable and disable network access for all clients of a single - network access manager instance. + \note Calling setNetworkAccessible() does not change the network state. */ /*! - \fn void QNetworkAccessManager::networkAccessChanged(bool enabled) + \fn void QNetworkAccessManager::networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility accessible) - This signal is emitted when the value of the \l networkAccess property changes. If \a enabled - is true new requests that access the network will be processed; otherwise new network requests - that require network access will fail with an error. + This signal is emitted when the value of the \l networkAccessible property changes. + \a accessible is the new network accessibility. */ /*! - \fn void QNetworkAccessManager::networkSessionOnline() + \fn void QNetworkAccessManager::networkSessionConnected() \since 4.7 \internal - This signal is emitted when the status of the network session changes into a usable state. - It is used to signal QNetworkReply's to start or migrate their network operation once the - network session has been opened / roamed. + This signal is emitted when the status of the network session changes into a usable (Connected) + state. It is used to signal to QNetworkReplys to start or migrate their network operation once + the network session has been opened or finished roaming. */ /*! @@ -792,30 +810,42 @@ QNetworkConfiguration QNetworkAccessManager::activeConfiguration() const /*! \since 4.7 - Enables network access via this QNetworkAccessManager if \a enabled is true; otherwise disables - access. + Overrides the reported network accessibility. If \a accessible is NotAccessible the reported + network accessiblity will always be NotAccessible. Otherwise the reported network + accessibility will reflect the actual device state. */ -void QNetworkAccessManager::setNetworkAccessEnabled(bool enabled) +void QNetworkAccessManager::setNetworkAccessible(QNetworkAccessManager::NetworkAccessibility accessible) { Q_D(QNetworkAccessManager); - if (d->networkAccessEnabled != enabled) { - d->networkAccessEnabled = enabled; - emit networkAccessChanged(enabled); + if (d->networkAccessible != accessible) { + NetworkAccessibility previous = networkAccessible(); + d->networkAccessible = accessible; + NetworkAccessibility current = networkAccessible(); + if (previous != current) + emit networkAccessibleChanged(current); } } /*! \since 4.7 - Returns true if network access via this QNetworkAccessManager is enabled; otherwise returns - false. + Returns the current network accessibility. */ -bool QNetworkAccessManager::networkAccessEnabled() const +QNetworkAccessManager::NetworkAccessibility QNetworkAccessManager::networkAccessible() const { Q_D(const QNetworkAccessManager); - return d->networkAccessEnabled; + if (d->networkSession) { + // d->online holds online/offline state of this network session. + if (d->online) + return d->networkAccessible; + else + return NotAccessible; + } else { + // Network accessibility is either disabled or unknown. + return (d->networkAccessible == NotAccessible) ? NotAccessible : UnknownAccessibility; + } } /*! @@ -875,20 +905,22 @@ QNetworkReply *QNetworkAccessManager::createRequest(QNetworkAccessManager::Opera // Return a disabled network reply if network access is disabled. // Except if the scheme is empty or file://. - if (!d->networkAccessEnabled && !(req.url().scheme() == QLatin1String("file") || + if (!d->networkAccessible && !(req.url().scheme() == QLatin1String("file") || req.url().scheme().isEmpty())) { return new QDisabledNetworkReply(this, req, op); } -#ifdef QT_QNAM_DEFAULT_NETWORK_SESSION if (!d->networkSession && (d->initializeSession || !d->networkConfiguration.isEmpty())) { QNetworkConfigurationManager manager; - if (d->networkConfiguration.isEmpty()) - d->createSession(manager.defaultConfiguration()); - else + if (!d->networkConfiguration.isEmpty()) { d->createSession(manager.configurationFromIdentifier(d->networkConfiguration)); + } else { + if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) + d->createSession(manager.defaultConfiguration()); + else + d->initializeSession = false; + } } -#endif if (d->networkSession) d->networkSession->setSessionProperty(QLatin1String("AutoCloseSessionTimeout"), -1); @@ -909,8 +941,10 @@ QNetworkReply *QNetworkAccessManager::createRequest(QNetworkAccessManager::Opera // first step: create the reply QUrl url = request.url(); QNetworkReplyImpl *reply = new QNetworkReplyImpl(this); - if (req.url().scheme() != QLatin1String("file") && !req.url().scheme().isEmpty()) - connect(this, SIGNAL(networkSessionOnline()), reply, SLOT(_q_networkSessionOnline())); + if (req.url().scheme() != QLatin1String("file") && !req.url().scheme().isEmpty()) { + connect(this, SIGNAL(networkSessionConnected()), + reply, SLOT(_q_networkSessionConnected())); + } QNetworkReplyImplPrivate *priv = reply->d_func(); priv->manager = this; @@ -1208,28 +1242,37 @@ void QNetworkAccessManagerPrivate::createSession(const QNetworkConfiguration &co { Q_Q(QNetworkAccessManager); -#ifdef QT_QNAM_DEFAULT_NETWORK_SESSION initializeSession = false; -#endif if (networkSession) delete networkSession; if (!config.isValid()) { networkSession = 0; + online = false; + + if (networkAccessible == QNetworkAccessManager::NotAccessible) + emit q->networkAccessibleChanged(QNetworkAccessManager::NotAccessible); + else + emit q->networkAccessibleChanged(QNetworkAccessManager::UnknownAccessibility); + return; } networkSession = new QNetworkSession(config, q); - QObject::connect(networkSession, SIGNAL(opened()), q, SIGNAL(networkSessionOnline())); + QObject::connect(networkSession, SIGNAL(opened()), q, SIGNAL(networkSessionConnected())); QObject::connect(networkSession, SIGNAL(closed()), q, SLOT(_q_networkSessionClosed())); + QObject::connect(networkSession, SIGNAL(stateChanged(QNetworkSession::State)), + q, SLOT(_q_networkSessionStateChanged(QNetworkSession::State))); QObject::connect(networkSession, SIGNAL(newConfigurationActivated()), q, SLOT(_q_networkSessionNewConfigurationActivated())); QObject::connect(networkSession, SIGNAL(preferredConfigurationChanged(QNetworkConfiguration,bool)), q, SLOT(_q_networkSessionPreferredConfigurationChanged(QNetworkConfiguration,bool))); + + _q_networkSessionStateChanged(networkSession->state()); } void QNetworkAccessManagerPrivate::_q_networkSessionClosed() @@ -1249,7 +1292,7 @@ void QNetworkAccessManagerPrivate::_q_networkSessionNewConfigurationActivated() if (networkSession) { networkSession->accept(); - emit q->networkSessionOnline(); + emit q->networkSessionConnected(); } } @@ -1259,6 +1302,23 @@ void QNetworkAccessManagerPrivate::_q_networkSessionPreferredConfigurationChange networkSession->migrate(); } +void QNetworkAccessManagerPrivate::_q_networkSessionStateChanged(QNetworkSession::State state) +{ + Q_Q(QNetworkAccessManager); + + if (online) { + if (state != QNetworkSession::Connected && state != QNetworkSession::Roaming) { + online = false; + emit q->networkAccessibleChanged(QNetworkAccessManager::NotAccessible); + } + } else { + if (state == QNetworkSession::Connected || state == QNetworkSession::Roaming) { + online = true; + emit q->networkAccessibleChanged(networkAccessible); + } + } +} + QT_END_NAMESPACE #include "moc_qnetworkaccessmanager.cpp" diff --git a/src/network/access/qnetworkaccessmanager.h b/src/network/access/qnetworkaccessmanager.h index 694a54f..1d794a2 100644 --- a/src/network/access/qnetworkaccessmanager.h +++ b/src/network/access/qnetworkaccessmanager.h @@ -70,7 +70,7 @@ class Q_NETWORK_EXPORT QNetworkAccessManager: public QObject { Q_OBJECT - Q_PROPERTY(bool networkAccess READ networkAccessEnabled WRITE setNetworkAccessEnabled NOTIFY networkAccessChanged) + Q_PROPERTY(NetworkAccessibility networkAccessible READ networkAccessible WRITE setNetworkAccessible NOTIFY networkAccessibleChanged) public: enum Operation { @@ -84,6 +84,12 @@ public: UnknownOperation = 0 }; + enum NetworkAccessibility { + UnknownAccessibility = -1, + NotAccessible = 0, + Accessible = 1 + }; + explicit QNetworkAccessManager(QObject *parent = 0); ~QNetworkAccessManager(); @@ -113,8 +119,8 @@ public: QNetworkConfiguration configuration() const; QNetworkConfiguration activeConfiguration() const; - void setNetworkAccessEnabled(bool enabled); - bool networkAccessEnabled() const; + void setNetworkAccessible(NetworkAccessibility accessible); + NetworkAccessibility networkAccessible() const; Q_SIGNALS: #ifndef QT_NO_NETWORKPROXY @@ -126,9 +132,9 @@ Q_SIGNALS: void sslErrors(QNetworkReply *reply, const QList<QSslError> &errors); #endif - void networkSessionOnline(); + void networkSessionConnected(); - void networkAccessChanged(bool enabled); + void networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility accessible); protected: virtual QNetworkReply *createRequest(Operation op, const QNetworkRequest &request, @@ -142,6 +148,7 @@ private: Q_PRIVATE_SLOT(d_func(), void _q_networkSessionClosed()) Q_PRIVATE_SLOT(d_func(), void _q_networkSessionNewConfigurationActivated()) Q_PRIVATE_SLOT(d_func(), void _q_networkSessionPreferredConfigurationChanged(QNetworkConfiguration,bool)) + Q_PRIVATE_SLOT(d_func(), void _q_networkSessionStateChanged(QNetworkSession::State)); }; QT_END_NAMESPACE diff --git a/src/network/access/qnetworkaccessmanager_p.h b/src/network/access/qnetworkaccessmanager_p.h index 4a2a840..1785685 100644 --- a/src/network/access/qnetworkaccessmanager_p.h +++ b/src/network/access/qnetworkaccessmanager_p.h @@ -58,6 +58,7 @@ #include "qnetworkaccessbackend_p.h" #include "private/qobject_p.h" #include "QtNetwork/qnetworkproxy.h" +#include "QtNetwork/qnetworksession.h" QT_BEGIN_NAMESPACE @@ -65,7 +66,6 @@ class QAuthenticator; class QAbstractNetworkCache; class QNetworkAuthenticationCredential; class QNetworkCookieJar; -class QNetworkSession; class QNetworkAccessManagerPrivate: public QObjectPrivate { @@ -76,10 +76,9 @@ public: proxyFactory(0), #endif networkSession(0), - networkAccessEnabled(true), -#ifdef QT_QNAM_DEFAULT_NETWORK_SESSION + networkAccessible(QNetworkAccessManager::Accessible), + online(false), initializeSession(true), -#endif cookieJarCreated(false) { } ~QNetworkAccessManagerPrivate(); @@ -111,6 +110,7 @@ public: void _q_networkSessionNewConfigurationActivated(); void _q_networkSessionPreferredConfigurationChanged(const QNetworkConfiguration &config, bool isSeamless); + void _q_networkSessionStateChanged(QNetworkSession::State state); // this is the cache for storing downloaded files QAbstractNetworkCache *networkCache; @@ -125,10 +125,9 @@ public: QNetworkSession *networkSession; QString networkConfiguration; - bool networkAccessEnabled; -#ifdef QT_QNAM_DEFAULT_NETWORK_SESSION + QNetworkAccessManager::NetworkAccessibility networkAccessible; + bool online; bool initializeSession; -#endif bool cookieJarCreated; diff --git a/src/network/access/qnetworkreplyimpl.cpp b/src/network/access/qnetworkreplyimpl.cpp index 8505a41..7fc0097 100644 --- a/src/network/access/qnetworkreplyimpl.cpp +++ b/src/network/access/qnetworkreplyimpl.cpp @@ -232,10 +232,20 @@ void QNetworkReplyImplPrivate::_q_bufferOutgoingData() } } -void QNetworkReplyImplPrivate::_q_networkSessionOnline() +void QNetworkReplyImplPrivate::_q_networkSessionConnected() { Q_Q(QNetworkReplyImpl); + if (manager.isNull()) + return; + + QNetworkSession *session = manager->d_func()->networkSession; + if (!session) + return; + + if (session->state() != QNetworkSession::Connected) + return; + switch (state) { case QNetworkReplyImplPrivate::Buffering: case QNetworkReplyImplPrivate::Working: diff --git a/src/network/access/qnetworkreplyimpl_p.h b/src/network/access/qnetworkreplyimpl_p.h index 6045ef4..fcb3397 100644 --- a/src/network/access/qnetworkreplyimpl_p.h +++ b/src/network/access/qnetworkreplyimpl_p.h @@ -99,7 +99,7 @@ public: Q_PRIVATE_SLOT(d_func(), void _q_copyReadChannelFinished()) Q_PRIVATE_SLOT(d_func(), void _q_bufferOutgoingData()) Q_PRIVATE_SLOT(d_func(), void _q_bufferOutgoingDataFinished()) - Q_PRIVATE_SLOT(d_func(), void _q_networkSessionOnline()) + Q_PRIVATE_SLOT(d_func(), void _q_networkSessionConnected()) Q_PRIVATE_SLOT(d_func(), void _q_networkSessionFailed()) }; @@ -133,7 +133,7 @@ public: void _q_copyReadChannelFinished(); void _q_bufferOutgoingData(); void _q_bufferOutgoingDataFinished(); - void _q_networkSessionOnline(); + void _q_networkSessionConnected(); void _q_networkSessionFailed(); void setup(QNetworkAccessManager::Operation op, const QNetworkRequest &request, |