diff options
Diffstat (limited to 'src/network')
-rw-r--r-- | src/network/access/qnetworkaccessmanager.cpp | 84 | ||||
-rw-r--r-- | src/network/access/qnetworkaccessmanager.h | 8 | ||||
-rw-r--r-- | src/network/access/qnetworkaccessmanager_p.h | 4 |
3 files changed, 95 insertions, 1 deletions
diff --git a/src/network/access/qnetworkaccessmanager.cpp b/src/network/access/qnetworkaccessmanager.cpp index 8df580d..7f36570 100644 --- a/src/network/access/qnetworkaccessmanager.cpp +++ b/src/network/access/qnetworkaccessmanager.cpp @@ -57,6 +57,7 @@ #include "QtCore/qbuffer.h" #include "QtCore/qurl.h" #include "QtCore/qvector.h" +#include "QtCore/qcoreapplication.h" #include "QtNetwork/qauthenticator.h" #include "QtNetwork/qsslconfiguration.h" #include "QtNetwork/qnetworkconfigmanager.h" @@ -162,6 +163,29 @@ static void ensureInitialized() */ /*! + \property QNetworkAccessManager::networkAccess + \brief wheather network access is enabled or disabled through this network access manager. + \since 4.7 + + Network access is enabled by default. + + 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. + + This property can be used to enable and disable network access for all clients of a single + network access manager instance. +*/ + +/*! + \fn void QNetworkAccessManager::networkAccessChanged(bool enabled) + + 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. +*/ + +/*! \fn void QNetworkAccessManager::proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator) This signal is emitted whenever a proxy requests authentication @@ -719,6 +743,63 @@ QNetworkConfiguration QNetworkAccessManager::activeConfiguration() const } } +void QNetworkAccessManager::setNetworkAccessEnabled(bool enabled) +{ + Q_D(QNetworkAccessManager); + + if (d->networkAccessEnabled != enabled) { + d->networkAccessEnabled = enabled; + emit networkAccessChanged(enabled); + } +} + +bool QNetworkAccessManager::networkAccessEnabled() const +{ + Q_D(const QNetworkAccessManager); + + return d->networkAccessEnabled; +} + +class QDisabledNetworkReply : public QNetworkReply +{ + Q_OBJECT + +public: + QDisabledNetworkReply(QObject *parent, const QNetworkRequest &req, + const QNetworkAccessManager::Operation op); + ~QDisabledNetworkReply(); + + void abort() { } +protected: + qint64 readData(char *, qint64) { return 0; } +}; + +QDisabledNetworkReply::QDisabledNetworkReply(QObject *parent, + const QNetworkRequest &req, + QNetworkAccessManager::Operation op) +: QNetworkReply(parent) +{ + setRequest(req); + setUrl(req.url()); + setOperation(op); + + qRegisterMetaType<QNetworkReply::NetworkError>("QNetworkReply::NetworkError"); + + QString msg = QCoreApplication::translate("QNetworkAccessManager", + "Network access is disabled."); + setError(UnknownNetworkError, msg); + + QMetaObject::invokeMethod(this, "error", Qt::QueuedConnection, + Q_ARG(QNetworkReply::NetworkError, UnknownNetworkError)); + QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection); +} + +QDisabledNetworkReply::~QDisabledNetworkReply() +{ +} + +#include "qnetworkaccessmanager.moc" + /*! Returns a new QNetworkReply object to handle the operation \a op and request \a req. The device \a outgoingData is always 0 for Get and @@ -748,6 +829,9 @@ QNetworkReply *QNetworkAccessManager::createRequest(QNetworkAccessManager::Opera return new QFileNetworkReply(this, req, op); } + if (!d->networkAccessEnabled) + return new QDisabledNetworkReply(this, req, op); + QNetworkRequest request = req; if (!request.header(QNetworkRequest::ContentLengthHeader).isValid() && outgoingData && !outgoingData->isSequential()) { diff --git a/src/network/access/qnetworkaccessmanager.h b/src/network/access/qnetworkaccessmanager.h index 14aaf78..371f729 100644 --- a/src/network/access/qnetworkaccessmanager.h +++ b/src/network/access/qnetworkaccessmanager.h @@ -69,6 +69,9 @@ class QNetworkAccessManagerPrivate; class Q_NETWORK_EXPORT QNetworkAccessManager: public QObject { Q_OBJECT + + Q_PROPERTY(bool networkAccess READ networkAccessEnabled WRITE setNetworkAccessEnabled NOTIFY networkAccessChanged) + public: enum Operation { HeadOperation = 1, @@ -108,6 +111,9 @@ public: QNetworkConfiguration configuration() const; QNetworkConfiguration activeConfiguration() const; + void setNetworkAccessEnabled(bool enabled); + bool networkAccessEnabled() const; + Q_SIGNALS: #ifndef QT_NO_NETWORKPROXY void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator); @@ -118,6 +124,8 @@ Q_SIGNALS: void sslErrors(QNetworkReply *reply, const QList<QSslError> &errors); #endif + void networkAccessChanged(bool enabled); + void debugMessage(const QString &message); protected: diff --git a/src/network/access/qnetworkaccessmanager_p.h b/src/network/access/qnetworkaccessmanager_p.h index e916158..eae08e8 100644 --- a/src/network/access/qnetworkaccessmanager_p.h +++ b/src/network/access/qnetworkaccessmanager_p.h @@ -76,7 +76,8 @@ public: proxyFactory(0), #endif cookieJarCreated(false), - session(0) + session(0), + networkAccessEnabled(true) { } ~QNetworkAccessManagerPrivate(); @@ -130,6 +131,7 @@ public: bool cookieJarCreated; QNetworkSession *session; + bool networkAccessEnabled; // this cache can be used by individual backends to cache e.g. their TCP connections to a server // and use the connections for multiple requests. |