diff options
Diffstat (limited to 'src/network')
27 files changed, 3259 insertions, 11 deletions
diff --git a/src/network/access/qnetworkaccessbackend.cpp b/src/network/access/qnetworkaccessbackend.cpp index 34d576a..1fcfebb 100644 --- a/src/network/access/qnetworkaccessbackend.cpp +++ b/src/network/access/qnetworkaccessbackend.cpp @@ -46,9 +46,11 @@ #include "qnetworkreply_p.h" #include "QtCore/qhash.h" #include "QtCore/qmutex.h" +#include "QtNetwork/qnetworksession.h" #include "qnetworkaccesscachebackend_p.h" #include "qabstractnetworkcache.h" +#include "qhostinfo.h" #include "private/qnoncontiguousbytedevice_p.h" @@ -344,4 +346,35 @@ void QNetworkAccessBackend::sslErrors(const QList<QSslError> &errors) #endif } +/*! + Starts the backend. Returns true if the backend is started. Returns false if the backend + could not be started due to an unopened or roaming session. The caller should recall this + function once the session has been opened or the roaming process has finished. +*/ +bool QNetworkAccessBackend::start() +{ + if (!manager->networkSession) { + open(); + return true; + } + + // This is not ideal. + const QString host = reply->url.host(); + if (host == QLatin1String("localhost") || + QHostAddress(host) == QHostAddress::LocalHost || + QHostAddress(host) == QHostAddress::LocalHostIPv6) { + // Don't need an open session for localhost access. + open(); + return true; + } + + if (manager->networkSession->isOpen() && + manager->networkSession->state() == QNetworkSession::Connected) { + open(); + return true; + } + + return false; +} + QT_END_NAMESPACE diff --git a/src/network/access/qnetworkaccessbackend_p.h b/src/network/access/qnetworkaccessbackend_p.h index 43d993c..9bc15e5 100644 --- a/src/network/access/qnetworkaccessbackend_p.h +++ b/src/network/access/qnetworkaccessbackend_p.h @@ -111,6 +111,7 @@ public: // socket). virtual void open() = 0; + virtual bool start(); virtual void closeDownstreamChannel() = 0; virtual bool waitForDownstreamReadyRead(int msecs) = 0; @@ -160,6 +161,10 @@ public: // This will possibly enable buffering of the upload data. virtual bool needsResetableUploadData() { return false; } + // Returns true if backend is able to resume downloads. + virtual bool canResume() const { return false; } + virtual void setResumeOffset(quint64 offset) { Q_UNUSED(offset); } + protected: // Create the device used for reading the upload data QNonContiguousByteDevice* createUploadByteDevice(); @@ -190,6 +195,7 @@ private: friend class QNetworkAccessManager; friend class QNetworkAccessManagerPrivate; friend class QNetworkAccessBackendUploadIODevice; + friend class QNetworkReplyImplPrivate; QNetworkAccessManagerPrivate *manager; QNetworkReplyImplPrivate *reply; }; diff --git a/src/network/access/qnetworkaccesshttpbackend.cpp b/src/network/access/qnetworkaccesshttpbackend.cpp index dcd4e19..7a48c2b 100644 --- a/src/network/access/qnetworkaccesshttpbackend.cpp +++ b/src/network/access/qnetworkaccesshttpbackend.cpp @@ -297,6 +297,7 @@ QNetworkAccessHttpBackend::QNetworkAccessHttpBackend() #ifndef QT_NO_OPENSSL , pendingSslConfiguration(0), pendingIgnoreAllSslErrors(false) #endif + , resumeOffset(0) { } @@ -542,6 +543,28 @@ void QNetworkAccessHttpBackend::postRequest() httpRequest.setUrl(url()); QList<QByteArray> headers = request().rawHeaderList(); + if (resumeOffset != 0) { + if (headers.contains("Range")) { + // Need to adjust resume offset for user specified range + + headers.removeOne("Range"); + + // We've already verified that requestRange starts with "bytes=", see canResume. + QByteArray requestRange = request().rawHeader("Range").mid(6); + + int index = requestRange.indexOf('-'); + + quint64 requestStartOffset = requestRange.left(index).toULongLong(); + quint64 requestEndOffset = requestRange.mid(index + 1).toULongLong(); + + requestRange = "bytes=" + QByteArray::number(resumeOffset + requestStartOffset) + + '-' + QByteArray::number(requestEndOffset); + + httpRequest.setHeaderField("Range", requestRange); + } else { + httpRequest.setHeaderField("Range", "bytes=" + QByteArray::number(resumeOffset) + '-'); + } + } foreach (const QByteArray &header, headers) httpRequest.setHeaderField(header, request().rawHeader(header)); @@ -1117,6 +1140,31 @@ QNetworkCacheMetaData QNetworkAccessHttpBackend::fetchCacheMetaData(const QNetwo return metaData; } +bool QNetworkAccessHttpBackend::canResume() const +{ + // Only GET operation supports resuming. + if (operation() != QNetworkAccessManager::GetOperation) + return false; + + // Can only resume if server/resource supports Range header. + if (httpReply->headerField("Accept-Ranges", "none") == "none") + return false; + + // We only support resuming for byte ranges. + if (request().hasRawHeader("Range")) { + QByteArray range = request().rawHeader("Range"); + if (!range.startsWith("bytes=")) + return false; + } + + return true; +} + +void QNetworkAccessHttpBackend::setResumeOffset(quint64 offset) +{ + resumeOffset = offset; +} + QT_END_NAMESPACE #endif // QT_NO_HTTP diff --git a/src/network/access/qnetworkaccesshttpbackend_p.h b/src/network/access/qnetworkaccesshttpbackend_p.h index 0eaf003..e5cc0ab 100644 --- a/src/network/access/qnetworkaccesshttpbackend_p.h +++ b/src/network/access/qnetworkaccesshttpbackend_p.h @@ -99,6 +99,9 @@ public: // we return true since HTTP needs to send PUT/POST data again after having authenticated bool needsResetableUploadData() { return true; } + bool canResume() const; + void setResumeOffset(quint64 offset); + private slots: void replyReadyRead(); void replyFinished(); @@ -120,6 +123,8 @@ private: QList<QSslError> pendingIgnoreSslErrorsList; #endif + quint64 resumeOffset; + void disconnectFromHttp(); void setupConnection(); void validateCache(QHttpNetworkRequest &httpRequest, bool &loadedFromCache); diff --git a/src/network/access/qnetworkaccessmanager.cpp b/src/network/access/qnetworkaccessmanager.cpp index cc4c977..b9bd52a 100644 --- a/src/network/access/qnetworkaccessmanager.cpp +++ b/src/network/access/qnetworkaccessmanager.cpp @@ -47,6 +47,8 @@ #include "qnetworkcookie.h" #include "qabstractnetworkcache.h" +#include "QtNetwork/qnetworksession.h" + #include "qnetworkaccesshttpbackend_p.h" #include "qnetworkaccessftpbackend_p.h" #include "qnetworkaccessfilebackend_p.h" @@ -59,6 +61,7 @@ #include "QtCore/qvector.h" #include "QtNetwork/qauthenticator.h" #include "QtNetwork/qsslconfiguration.h" +#include "QtNetwork/qnetworkconfigmanager.h" QT_BEGIN_NAMESPACE @@ -163,6 +166,43 @@ static void ensureInitialized() */ /*! + \property QNetworkAccessManager::networkAccess + \brief states whether 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::networkSessionOnline() + + \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. +*/ + +/*! \fn void QNetworkAccessManager::proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator) This signal is emitted whenever a proxy requests authentication @@ -670,6 +710,110 @@ QNetworkReply *QNetworkAccessManager::deleteResource(const QNetworkRequest &requ /*! \since 4.7 + Sets the network configuration that will be used when creating the + \l {QNetworkSession}{network session} to \a config. + + The network configuration is used to create and open a network session before any request that + requires network access is process. If no network configuration is explicitly set via this + function the network configuration returned by + QNetworkConfigurationManager::defaultConfiguration() will be used. + + To restore the default network configuration set the network configuration to the value + returned from QNetworkConfigurationManager::defaultConfiguration(). + + \snippet doc/src/snippets/code/src_network_access_qnetworkaccessmanager.cpp 2 + + If an invalid network configuration is set, a network session will not be created. In this + case network requests will be processed regardless, but may fail. For example: + + \snippet doc/src/snippets/code/src_network_access_qnetworkaccessmanager.cpp 3 + + \sa configuration(), QNetworkSession +*/ +void QNetworkAccessManager::setConfiguration(const QNetworkConfiguration &config) +{ + d_func()->createSession(config); +} + +/*! + \since 4.7 + + Returns the network configuration that will be used to create the + \l {QNetworkSession}{network session} which will be used when processing network requests. + + \sa setConfiguration(), activeConfiguration() +*/ +QNetworkConfiguration QNetworkAccessManager::configuration() const +{ + Q_D(const QNetworkAccessManager); + + if (d->networkSession) + return d->networkSession->configuration(); + else + return QNetworkConfiguration(); +} + +/*! + \since 4.7 + + Returns the current active network configuration. + + If the network configuration returned by configuration() is of type + QNetworkConfiguration::ServiceNetwork this function will return the current active child + network configuration of that configuration. Otherwise returns the same network configuration + as configuration(). + + Use this function to return the actual network configuration currently in use by the network + session. + + \sa configuration() +*/ +QNetworkConfiguration QNetworkAccessManager::activeConfiguration() const +{ + Q_D(const QNetworkAccessManager); + + if (d->networkSession) { + QNetworkConfigurationManager manager; + + return manager.configurationFromIdentifier( + d->networkSession->sessionProperty(QLatin1String("ActiveConfiguration")).toString()); + } else { + return QNetworkConfiguration(); + } +} + +/*! + \since 4.7 + + Enables network access via this QNetworkAccessManager if \a enabled is true; otherwise disables + access. +*/ +void QNetworkAccessManager::setNetworkAccessEnabled(bool enabled) +{ + Q_D(QNetworkAccessManager); + + if (d->networkAccessEnabled != enabled) { + d->networkAccessEnabled = enabled; + emit networkAccessChanged(enabled); + } +} + +/*! + \since 4.7 + + Returns true if network access via this QNetworkAccessManager is enabled; otherwise returns + false. +*/ +bool QNetworkAccessManager::networkAccessEnabled() const +{ + Q_D(const QNetworkAccessManager); + + return d->networkAccessEnabled; +} + +/*! + \since 4.7 + Sends a custom request to the server identified by the URL of \a request. It is the user's responsibility to send a \a verb to the server that is valid @@ -722,6 +866,20 @@ QNetworkReply *QNetworkAccessManager::createRequest(QNetworkAccessManager::Opera return new QFileNetworkReply(this, req, op); } + // 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") || + req.url().scheme().isEmpty())) { + return new QDisabledNetworkReply(this, req, op); + } + + if (d->initializeSession && !d->networkSession) { + QNetworkConfigurationManager manager; + d->createSession(manager.defaultConfiguration()); + + d->initializeSession = false; + } + QNetworkRequest request = req; if (!request.header(QNetworkRequest::ContentLengthHeader).isValid() && outgoingData && !outgoingData->isSequential()) { @@ -738,6 +896,8 @@ 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())); QNetworkReplyImplPrivate *priv = reply->d_func(); priv->manager = this; @@ -772,6 +932,7 @@ QNetworkReply *QNetworkAccessManager::createRequest(QNetworkAccessManager::Opera void QNetworkAccessManagerPrivate::_q_replyFinished() { Q_Q(QNetworkAccessManager); + QNetworkReply *reply = qobject_cast<QNetworkReply *>(q->sender()); if (reply) emit q->finished(reply); @@ -1027,6 +1188,43 @@ QNetworkAccessManagerPrivate::~QNetworkAccessManagerPrivate() { } +void QNetworkAccessManagerPrivate::createSession(const QNetworkConfiguration &config) +{ + Q_Q(QNetworkAccessManager); + + if (networkSession) + delete networkSession; + + if (!config.isValid()) { + networkSession = 0; + return; + } + + networkSession = new QNetworkSession(config, q); + + QObject::connect(networkSession, SIGNAL(opened()), q, SIGNAL(networkSessionOnline())); + QObject::connect(networkSession, SIGNAL(newConfigurationActivated()), + q, SLOT(_q_networkSessionNewConfigurationActivated())); + QObject::connect(networkSession, + SIGNAL(preferredConfigurationChanged(QNetworkConfiguration,bool)), + q, + SLOT(_q_networkSessionPreferredConfigurationChanged(QNetworkConfiguration,bool))); +} + +void QNetworkAccessManagerPrivate::_q_networkSessionNewConfigurationActivated() +{ + Q_Q(QNetworkAccessManager); + + networkSession->accept(); + + emit q->networkSessionOnline(); +} + +void QNetworkAccessManagerPrivate::_q_networkSessionPreferredConfigurationChanged(const QNetworkConfiguration &, bool) +{ + networkSession->migrate(); +} + QT_END_NAMESPACE #include "moc_qnetworkaccessmanager.cpp" diff --git a/src/network/access/qnetworkaccessmanager.h b/src/network/access/qnetworkaccessmanager.h index 3a8ba58..252dfd0 100644 --- a/src/network/access/qnetworkaccessmanager.h +++ b/src/network/access/qnetworkaccessmanager.h @@ -62,12 +62,16 @@ class QNetworkReply; class QNetworkProxy; class QNetworkProxyFactory; class QSslError; +class QNetworkConfiguration; class QNetworkReplyImplPrivate; 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, @@ -105,6 +109,13 @@ public: QNetworkReply *deleteResource(const QNetworkRequest &request); QNetworkReply *sendCustomRequest(const QNetworkRequest &request, const QByteArray &verb, QIODevice *data = 0); + void setConfiguration(const QNetworkConfiguration &config); + 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); @@ -115,6 +126,10 @@ Q_SIGNALS: void sslErrors(QNetworkReply *reply, const QList<QSslError> &errors); #endif + void networkSessionOnline(); + + void networkAccessChanged(bool enabled); + protected: virtual QNetworkReply *createRequest(Operation op, const QNetworkRequest &request, QIODevice *outgoingData = 0); @@ -124,6 +139,8 @@ private: Q_DECLARE_PRIVATE(QNetworkAccessManager) Q_PRIVATE_SLOT(d_func(), void _q_replyFinished()) Q_PRIVATE_SLOT(d_func(), void _q_replySslErrors(QList<QSslError>)) + Q_PRIVATE_SLOT(d_func(), void _q_networkSessionNewConfigurationActivated()) + Q_PRIVATE_SLOT(d_func(), void _q_networkSessionPreferredConfigurationChanged(QNetworkConfiguration,bool)) }; QT_END_NAMESPACE diff --git a/src/network/access/qnetworkaccessmanager_p.h b/src/network/access/qnetworkaccessmanager_p.h index 1749373..8d772f0 100644 --- a/src/network/access/qnetworkaccessmanager_p.h +++ b/src/network/access/qnetworkaccessmanager_p.h @@ -65,6 +65,7 @@ class QAuthenticator; class QAbstractNetworkCache; class QNetworkAuthenticationCredential; class QNetworkCookieJar; +class QNetworkSession; class QNetworkAccessManagerPrivate: public QObjectPrivate { @@ -74,6 +75,9 @@ public: #ifndef QT_NO_NETWORKPROXY proxyFactory(0), #endif + networkSession(0), + networkAccessEnabled(true), + initializeSession(true), cookieJarCreated(false) { } ~QNetworkAccessManagerPrivate(); @@ -99,6 +103,12 @@ public: QNetworkAccessBackend *findBackend(QNetworkAccessManager::Operation op, const QNetworkRequest &request); + void createSession(const QNetworkConfiguration &config); + + void _q_networkSessionNewConfigurationActivated(); + void _q_networkSessionPreferredConfigurationChanged(const QNetworkConfiguration &config, + bool isSeamless); + // this is the cache for storing downloaded files QAbstractNetworkCache *networkCache; @@ -110,8 +120,11 @@ public: QNetworkProxyFactory *proxyFactory; #endif - bool cookieJarCreated; + QNetworkSession *networkSession; + bool networkAccessEnabled; + bool initializeSession; + bool cookieJarCreated; // 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. diff --git a/src/network/access/qnetworkreply.cpp b/src/network/access/qnetworkreply.cpp index 15748fe..c8b8c1f 100644 --- a/src/network/access/qnetworkreply.cpp +++ b/src/network/access/qnetworkreply.cpp @@ -125,6 +125,11 @@ QNetworkReplyPrivate::QNetworkReplyPrivate() encrypted channel could not be established. The sslErrors() signal should have been emitted. + \value TemporaryNetworkFailureError the connection was broken due + to disconnection from the network, however the system has initiated + roaming to another access point. The request should be resubmitted + and will be processed as soon as the connection is re-established. + \value ProxyConnectionRefusedError the connection to the proxy server was refused (the proxy server is not accepting requests) diff --git a/src/network/access/qnetworkreply.h b/src/network/access/qnetworkreply.h index 5db4985..acb7379 100644 --- a/src/network/access/qnetworkreply.h +++ b/src/network/access/qnetworkreply.h @@ -77,6 +77,7 @@ public: TimeoutError, OperationCanceledError, SslHandshakeFailedError, + TemporaryNetworkFailureError, UnknownNetworkError = 99, // proxy errors (101-199): diff --git a/src/network/access/qnetworkreplyimpl.cpp b/src/network/access/qnetworkreplyimpl.cpp index 59c7d76..2175686 100644 --- a/src/network/access/qnetworkreplyimpl.cpp +++ b/src/network/access/qnetworkreplyimpl.cpp @@ -46,7 +46,9 @@ #include "QtCore/qcoreapplication.h" #include "QtCore/qdatetime.h" #include "QtNetwork/qsslconfiguration.h" +#include "QtNetwork/qnetworksession.h" #include "qnetworkaccesshttpbackend_p.h" +#include "qnetworkaccessmanager_p.h" #include <QtCore/QCoreApplication> @@ -57,7 +59,7 @@ inline QNetworkReplyImplPrivate::QNetworkReplyImplPrivate() copyDevice(0), cacheEnabled(false), cacheSaveDevice(0), notificationHandlingPaused(false), - bytesDownloaded(0), lastBytesDownloaded(-1), bytesUploaded(-1), + bytesDownloaded(0), lastBytesDownloaded(-1), bytesUploaded(-1), preMigrationDownloaded(-1), httpStatusCode(0), state(Idle) { @@ -82,7 +84,24 @@ void QNetworkReplyImplPrivate::_q_startOperation() return; } - backend->open(); + if (!backend->start()) { + // backend failed to start because the session state is not Connected. + // QNetworkAccessManager will call reply->backend->start() again for us when the session + // state changes. + state = WaitingForSession; + + QNetworkSession *session = manager->d_func()->networkSession; + + if (session) { + if (!session->isOpen()) + session->open(); + } else { + qWarning("Backend is waiting for QNetworkSession to connect, but there is none!"); + } + + return; + } + if (state != Finished) { if (operation == QNetworkAccessManager::GetOperation) pendingNotifications.append(NotifyDownstreamReadyWrite); @@ -134,6 +153,8 @@ void QNetworkReplyImplPrivate::_q_copyReadyRead() lastBytesDownloaded = bytesDownloaded; QVariant totalSize = cookedHeaders.value(QNetworkRequest::ContentLengthHeader); + if (preMigrationDownloaded != Q_INT64_C(-1)) + totalSize = totalSize.toLongLong() + preMigrationDownloaded; pauseNotificationHandling(); emit q->downloadProgress(bytesDownloaded, totalSize.isNull() ? Q_INT64_C(-1) : totalSize.toLongLong()); @@ -206,6 +227,26 @@ void QNetworkReplyImplPrivate::_q_bufferOutgoingData() } } +void QNetworkReplyImplPrivate::_q_networkSessionOnline() +{ + Q_Q(QNetworkReplyImpl); + + switch (state) { + case QNetworkReplyImplPrivate::Buffering: + case QNetworkReplyImplPrivate::Working: + case QNetworkReplyImplPrivate::Reconnecting: + // Migrate existing downloads to new network connection. + migrateBackend(); + break; + case QNetworkReplyImplPrivate::WaitingForSession: + // Start waiting requests. + QMetaObject::invokeMethod(q, "_q_startOperation", Qt::QueuedConnection); + break; + default: + ; + } +} + void QNetworkReplyImplPrivate::setup(QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *data) { @@ -457,6 +498,8 @@ void QNetworkReplyImplPrivate::appendDownstreamData(QByteDataBuffer &data) QPointer<QNetworkReplyImpl> qq = q; QVariant totalSize = cookedHeaders.value(QNetworkRequest::ContentLengthHeader); + if (preMigrationDownloaded != Q_INT64_C(-1)) + totalSize = totalSize.toLongLong() + preMigrationDownloaded; pauseNotificationHandling(); emit q->downloadProgress(bytesDownloaded, totalSize.isNull() ? Q_INT64_C(-1) : totalSize.toLongLong()); @@ -498,14 +541,42 @@ void QNetworkReplyImplPrivate::appendDownstreamData(QIODevice *data) void QNetworkReplyImplPrivate::finished() { Q_Q(QNetworkReplyImpl); - if (state == Finished || state == Aborted) + + if (state == Finished || state == Aborted || state == WaitingForSession) return; + pauseNotificationHandling(); + QVariant totalSize = cookedHeaders.value(QNetworkRequest::ContentLengthHeader); + if (preMigrationDownloaded != Q_INT64_C(-1)) + totalSize = totalSize.toLongLong() + preMigrationDownloaded; + + if (!manager.isNull()) { + QNetworkSession *session = manager->d_func()->networkSession; + if (session && session->state() == QNetworkSession::Roaming && + state == Working && errorCode != QNetworkReply::OperationCanceledError) { + // only content with a known size will fail with a temporary network failure error + if (!totalSize.isNull()) { + if (bytesDownloaded != totalSize) { + if (migrateBackend()) { + // either we are migrating or the request is finished/aborted + if (state == Reconnecting || state == WaitingForSession) { + resumeNotificationHandling(); + return; // exit early if we are migrating. + } + } else { + error(QNetworkReply::TemporaryNetworkFailureError, + q->tr("Temporary network failure.")); + } + } + } + } + } + resumeNotificationHandling(); + state = Finished; pendingNotifications.clear(); pauseNotificationHandling(); - QVariant totalSize = cookedHeaders.value(QNetworkRequest::ContentLengthHeader); if (totalSize.isNull() || totalSize == -1) { emit q->downloadProgress(bytesDownloaded, bytesDownloaded); } @@ -514,7 +585,9 @@ void QNetworkReplyImplPrivate::finished() emit q->uploadProgress(0, 0); resumeNotificationHandling(); - completeCacheSave(); + // if we don't know the total size of or we received everything save the cache + if (totalSize.isNull() || totalSize == -1 || bytesDownloaded == totalSize) + completeCacheSave(); // note: might not be a good idea, since users could decide to delete us // which would delete the backend too... @@ -722,6 +795,83 @@ bool QNetworkReplyImpl::event(QEvent *e) return QObject::event(e); } +/* + Migrates the backend of the QNetworkReply to a new network connection if required. Returns + true if the reply is migrated or it is not required; otherwise returns false. +*/ +bool QNetworkReplyImplPrivate::migrateBackend() +{ + Q_Q(QNetworkReplyImpl); + + // Network reply is already finished or aborted, don't need to migrate. + if (state == Finished || state == Aborted) + return true; + + // Backend does not support resuming download. + if (!backend->canResume()) + return false; + + // Request has outgoing data, not migrating. + if (outgoingData) + return false; + + // Request is serviced from the cache, don't need to migrate. + if (copyDevice) + return true; + + state = QNetworkReplyImplPrivate::Reconnecting; + + if (backend) { + delete backend; + backend = 0; + } + + cookedHeaders.clear(); + rawHeaders.clear(); + + preMigrationDownloaded = bytesDownloaded; + + backend = manager->d_func()->findBackend(operation, request); + + if (backend) { + backend->setParent(q); + backend->reply = this; + backend->setResumeOffset(bytesDownloaded); + } + + if (qobject_cast<QNetworkAccessHttpBackend *>(backend)) { + _q_startOperation(); + } else { + QMetaObject::invokeMethod(q, "_q_startOperation", Qt::QueuedConnection); + } + + return true; +} + +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() +{ +} + QT_END_NAMESPACE #include "moc_qnetworkreplyimpl_p.cpp" diff --git a/src/network/access/qnetworkreplyimpl_p.h b/src/network/access/qnetworkreplyimpl_p.h index 168e5cf..ec413cc 100644 --- a/src/network/access/qnetworkreplyimpl_p.h +++ b/src/network/access/qnetworkreplyimpl_p.h @@ -98,6 +98,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()) }; class QNetworkReplyImplPrivate: public QNetworkReplyPrivate @@ -110,11 +111,13 @@ public: }; enum State { - Idle, - Buffering, - Working, - Finished, - Aborted + Idle, // The reply is idle. + Buffering, // The reply is buffering outgoing data. + Working, // The reply is uploading/downloading data. + Finished, // The reply has finished. + Aborted, // The reply has been aborted. + WaitingForSession, // The reply is waiting for the session to open before connecting. + Reconnecting // The reply will reconnect to once roaming has completed. }; typedef QQueue<InternalNotifications> NotificationQueue; @@ -128,6 +131,7 @@ public: void _q_copyReadChannelFinished(); void _q_bufferOutgoingData(); void _q_bufferOutgoingDataFinished(); + void _q_networkSessionOnline(); void setup(QNetworkAccessManager::Operation op, const QNetworkRequest &request, QIODevice *outgoingData); @@ -161,6 +165,8 @@ public: QIODevice *copyDevice; QAbstractNetworkCache *networkCache() const; + bool migrateBackend(); + bool cacheEnabled; QIODevice *cacheSaveDevice; @@ -177,6 +183,7 @@ public: qint64 bytesDownloaded; qint64 lastBytesDownloaded; qint64 bytesUploaded; + qint64 preMigrationDownloaded; QString httpReasonPhrase; int httpStatusCode; @@ -186,6 +193,20 @@ public: Q_DECLARE_PUBLIC(QNetworkReplyImpl) }; +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 -1; } +}; + QT_END_NAMESPACE #endif diff --git a/src/network/bearer/bearer.pri b/src/network/bearer/bearer.pri new file mode 100644 index 0000000..44e97fd --- /dev/null +++ b/src/network/bearer/bearer.pri @@ -0,0 +1,18 @@ +# Qt network bearer management module + +HEADERS += bearer/qnetworkconfiguration.h \ + bearer/qnetworksession.h \ + bearer/qnetworkconfigmanager.h \ + bearer/qnetworkconfigmanager_p.h \ + bearer/qnetworkconfiguration_p.h \ + bearer/qnetworksession_p.h \ + bearer/qbearerengine_p.h \ + bearer/qbearerplugin_p.h + +SOURCES += bearer/qnetworksession.cpp \ + bearer/qnetworkconfigmanager.cpp \ + bearer/qnetworkconfiguration.cpp \ + bearer/qnetworkconfigmanager_p.cpp \ + bearer/qbearerengine.cpp \ + bearer/qbearerplugin.cpp + diff --git a/src/network/bearer/qbearerengine.cpp b/src/network/bearer/qbearerengine.cpp new file mode 100644 index 0000000..58d64f2 --- /dev/null +++ b/src/network/bearer/qbearerengine.cpp @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNetwork module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qbearerengine_p.h" + +QT_BEGIN_NAMESPACE + +QBearerEngine::QBearerEngine(QObject *parent) +: QObject(parent) +{ +} + +QBearerEngine::~QBearerEngine() +{ + QHash<QString, QNetworkConfigurationPrivatePointer>::Iterator it; + QHash<QString, QNetworkConfigurationPrivatePointer>::Iterator end; + for (it = snapConfigurations.begin(), end = snapConfigurations.end(); it != end; ++it) { + it.value()->isValid = false; + it.value()->id.clear(); + } + + for (it = accessPointConfigurations.begin(), end = accessPointConfigurations.end(); + it != end; ++it) { + it.value()->isValid = false; + it.value()->id.clear(); + } + + for (it = userChoiceConfigurations.begin(), end = userChoiceConfigurations.end(); + it != end; ++it) { + it.value()->isValid = false; + it.value()->id.clear(); + } +} + +#include "moc_qbearerengine_p.cpp" + +QT_END_NAMESPACE diff --git a/src/network/bearer/qbearerengine_p.h b/src/network/bearer/qbearerengine_p.h new file mode 100644 index 0000000..7e96877 --- /dev/null +++ b/src/network/bearer/qbearerengine_p.h @@ -0,0 +1,107 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNetwork module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QBEARERENGINE_P_H +#define QBEARERENGINE_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "qnetworkconfiguration_p.h" +#include "qnetworksession.h" +#include "qnetworkconfigmanager.h" + +#include <QtCore/qobject.h> +#include <QtCore/qglobal.h> +#include <QtCore/qlist.h> +#include <QtCore/qstring.h> +#include <QtCore/qhash.h> +#include <QtCore/qsharedpointer.h> + +QT_BEGIN_NAMESPACE + +class QNetworkConfiguration; + +class Q_NETWORK_EXPORT QBearerEngine : public QObject +{ + Q_OBJECT + +public: + QBearerEngine(QObject *parent = 0); + virtual ~QBearerEngine(); + + virtual bool hasIdentifier(const QString &id) = 0; + + virtual void requestUpdate() = 0; + + virtual QNetworkConfigurationManager::Capabilities capabilities() const = 0; + + virtual QNetworkSessionPrivate *createSessionBackend() = 0; + + virtual QNetworkConfigurationPrivatePointer defaultConfiguration() = 0; + +public: + //this table contains an up to date list of all configs at any time. + //it must be updated if configurations change, are added/removed or + //the members of ServiceNetworks change + QHash<QString, QNetworkConfigurationPrivatePointer> accessPointConfigurations; + QHash<QString, QNetworkConfigurationPrivatePointer> snapConfigurations; + QHash<QString, QNetworkConfigurationPrivatePointer> userChoiceConfigurations; + +Q_SIGNALS: + void configurationAdded(QNetworkConfigurationPrivatePointer config); + void configurationRemoved(QNetworkConfigurationPrivatePointer config); + void configurationChanged(QNetworkConfigurationPrivatePointer config); + + void updateCompleted(); +}; + +QT_END_NAMESPACE + +#endif diff --git a/src/network/bearer/qbearerplugin.cpp b/src/network/bearer/qbearerplugin.cpp new file mode 100644 index 0000000..4509fd0 --- /dev/null +++ b/src/network/bearer/qbearerplugin.cpp @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNetwork module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qbearerplugin_p.h" + +#include <QtCore/qdebug.h> + +QT_BEGIN_NAMESPACE + +QBearerEnginePlugin::QBearerEnginePlugin(QObject *parent) +: QObject(parent) +{ +} + +QBearerEnginePlugin::~QBearerEnginePlugin() +{ +} + +QT_END_NAMESPACE diff --git a/src/network/bearer/qbearerplugin_p.h b/src/network/bearer/qbearerplugin_p.h new file mode 100644 index 0000000..36709c2 --- /dev/null +++ b/src/network/bearer/qbearerplugin_p.h @@ -0,0 +1,93 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNetwork module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QBEARERPLUGIN_P_H +#define QBEARERPLUGIN_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "qbearerengine_p.h" + +#include <QtCore/qplugin.h> +#include <QtCore/qfactoryinterface.h> + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +QT_MODULE(Network) + +struct Q_NETWORK_EXPORT QBearerEngineFactoryInterface : public QFactoryInterface +{ + virtual QBearerEngine *create(const QString &key = QString()) const = 0; +}; + +#define QBearerEngineFactoryInterface_iid "com.trolltech.Qt.QBearerEngineFactoryInterface" +Q_DECLARE_INTERFACE(QBearerEngineFactoryInterface, QBearerEngineFactoryInterface_iid) + +class Q_NETWORK_EXPORT QBearerEnginePlugin : public QObject, public QBearerEngineFactoryInterface +{ + Q_OBJECT + Q_INTERFACES(QBearerEngineFactoryInterface:QFactoryInterface) + +public: + explicit QBearerEnginePlugin(QObject *parent = 0); + virtual ~QBearerEnginePlugin(); + + virtual QStringList keys() const = 0; + virtual QBearerEngine *create(const QString &key = QString()) const = 0; +}; + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif + diff --git a/src/network/bearer/qnetworkconfigmanager.cpp b/src/network/bearer/qnetworkconfigmanager.cpp new file mode 100644 index 0000000..f54a985 --- /dev/null +++ b/src/network/bearer/qnetworkconfigmanager.cpp @@ -0,0 +1,341 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNetwork module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qnetworkconfigmanager.h" + +#include "qnetworkconfigmanager_p.h" +#include "qbearerengine_p.h" + +#include <QtCore/qstringlist.h> + +QT_BEGIN_NAMESPACE + +Q_GLOBAL_STATIC(QNetworkConfigurationManagerPrivate, connManager); + +QNetworkConfigurationManagerPrivate *qNetworkConfigurationManagerPrivate() +{ + return connManager(); +} + +/*! + \class QNetworkConfigurationManager + + \brief The QNetworkConfigurationManager class manages the network configurations provided + by the system. + + \since 4.7 + + \inmodule QtNetwork + \ingroup bearer + + QNetworkConfigurationManager provides access to the network configurations known to the system and + enables applications to detect the system capabilities (with regards to network sessions) at runtime. + + A QNetworkConfiguration abstracts a set of configuration options describing how a + network interface has to be configured to connect to a particular target network. + QNetworkConfigurationManager maintains and updates the global list of + QNetworkConfigurations. Applications can access and filter this list via + allConfigurations(). If a new configuration is added or an existing one is removed or changed + the configurationAdded(), configurationRemoved() and configurationChanged() signals are emitted + respectively. + + The defaultConfiguration() can be used when intending to immediately create a new + network session without caring about the particular configuration. It returns + a \l QNetworkConfiguration::Discovered configuration. If there are not any + discovered ones an invalid configuration is returned. + + Some configuration updates may require some time to perform updates. A WLAN scan is + such an example. Unless the platform performs internal updates it may be required to + manually trigger configuration updates via QNetworkConfigurationManager::updateConfigurations(). + The completion of the update process is indicted by emitting the updateCompleted() + signal. The update process ensures that every existing QNetworkConfiguration instance + is updated. There is no need to ask for a renewed configuration list via allConfigurations(). + + \sa QNetworkConfiguration +*/ + +/*! + \fn void QNetworkConfigurationManager::configurationAdded(const QNetworkConfiguration& config) + + This signal is emitted whenever a new network configuration is added to the system. The new + configuration is specified by \a config. +*/ + +/*! + \fn void QNetworkConfigurationManager::configurationRemoved(const QNetworkConfiguration& configuration) + + This signal is emitted when a configuration is about to be removed from the system. The removed + \a configuration is invalid but retains name and identifier. +*/ + +/*! + \fn void QNetworkConfigurationManager::updateCompleted() + + This signal is emitted when the configuration update has been completed. Such an update can + be initiated via \l updateConfigurations(). +*/ + +/*! \fn void QNetworkConfigurationManager::configurationChanged(const QNetworkConfiguration& config) + + This signal is emitted when the \l {QNetworkConfiguration::state()}{state} of \a config changes. +*/ + +/*! + \fn void QNetworkConfigurationManager::onlineStateChanged(bool isOnline) + + This signal is emitted when the device changes from online to offline mode or vice versa. + \a isOnline represents the new state of the device. + + The state is considered to be online for as long as + \l{allConfigurations()}{allConfigurations}(QNetworkConfiguration::Active) returns a list with + at least one entry. +*/ + +/*! + \enum QNetworkConfigurationManager::Capability + + Specifies the system capabilities of the bearer API. The possible values are: + + \value CanStartAndStopInterfaces Network sessions and their underlying access points can be + started and stopped. If this flag is not set QNetworkSession + can only monitor but not influence the state of access points. + On some platforms this feature may require elevated user + permissions. This option is platform specific and may not + always be available. + \value DirectConnectionRouting Network sessions and their sockets can be bound to a + particular network interface. Any packet that passes through + the socket goes to the specified network interface and thus + disregards standard routing table entries. This may be useful + when two interfaces can reach overlapping IP ranges or an + application has specific needs in regards to target networks. + This option is platform specific and may not always be + available. + \value SystemSessionSupport If this flag is set the underlying platform ensures that a + network interface is not shut down until the last network + session has been \l{QNetworkSession::close()}{closed()}. This + works across multiple processes. If the platform session + support is missing this API can only ensure the above behavior + for network sessions within the same process. + In general mobile platforms (such as Symbian/S60) have such + support whereas most desktop platform lack this capability. + \value ApplicationLevelRoaming The system gives applications control over the systems roaming + behavior. Applications can initiate roaming (in case the + current link is not suitable) and are consulted if the system + has identified a more suitable access point. + \value ForcedRoaming The system disconnects an existing access point and reconnects + via a more suitable one. The application does not have any + control over this process and has to reconnect its active + sockets. + \value DataStatistics If this flag is set QNetworkSession can provide statistics + about transmitted and received data. +*/ + +/*! + Constructs a QNetworkConfigurationManager with the given \a parent. +*/ +QNetworkConfigurationManager::QNetworkConfigurationManager( QObject* parent ) + : QObject(parent) +{ + QNetworkConfigurationManagerPrivate* priv = connManager(); + connect(priv, SIGNAL(configurationAdded(QNetworkConfiguration)), + this, SIGNAL(configurationAdded(QNetworkConfiguration))); + connect(priv, SIGNAL(configurationRemoved(QNetworkConfiguration)), + this, SIGNAL(configurationRemoved(QNetworkConfiguration))); + connect(priv, SIGNAL(configurationUpdateComplete()), + this, SIGNAL(updateCompleted())); + connect(priv, SIGNAL(onlineStateChanged(bool)), + this, SIGNAL(onlineStateChanged(bool))); + connect(priv, SIGNAL(configurationChanged(QNetworkConfiguration)), + this, SIGNAL(configurationChanged(QNetworkConfiguration))); +} + +/*! + Frees the resources associated with the QNetworkConfigurationManager object. +*/ +QNetworkConfigurationManager::~QNetworkConfigurationManager() +{ +} + + +/*! + Returns the default configuration to be used. This function always returns a discovered + configuration; otherwise an invalid configuration. + + In some cases it may be required to call updateConfigurations() and wait for the + updateCompleted() signal before calling this function. + + \sa allConfigurations() +*/ +QNetworkConfiguration QNetworkConfigurationManager::defaultConfiguration() const +{ + return connManager()->defaultConfiguration(); +} + +/*! + Returns the list of configurations which comply with the given \a filter. + + By default this function returns all (defined and undefined) configurations. + + A wireless network with a particular SSID may only be accessible in a + certain area despite the fact that the system has a valid configuration + for it. Therefore the filter flag may be used to limit the list to + discovered and possibly connected configurations only. + + If \a filter is set to zero this function returns all possible configurations. + + Note that this function returns the states for all configurations as they are known at + the time of this function call. If for instance a configuration of type WLAN is defined + the system may have to perform a WLAN scan in order to determine whether it is + actually available. To obtain the most accurate state updateConfigurations() should + be used to update each configuration's state. Note that such an update may require + some time. It's completion is signalled by updateCompleted(). In the absence of a + configuration update this function returns the best estimate at the time of the call. +*/ +QList<QNetworkConfiguration> QNetworkConfigurationManager::allConfigurations(QNetworkConfiguration::StateFlags filter) const +{ + QList<QNetworkConfiguration> result; + QNetworkConfigurationManagerPrivate* conPriv = connManager(); + + foreach (QBearerEngine *engine, conPriv->sessionEngines) { + QHash<QString, QNetworkConfigurationPrivatePointer>::Iterator it; + QHash<QString, QNetworkConfigurationPrivatePointer>::Iterator end; + + //find all InternetAccessPoints + for (it = engine->accessPointConfigurations.begin(), + end = engine->accessPointConfigurations.end(); it != end; ++it) { + if ((it.value()->state & filter) == filter) { + QNetworkConfiguration pt; + pt.d = it.value(); + result << pt; + } + } + + //find all service networks + for (it = engine->snapConfigurations.begin(), + end = engine->snapConfigurations.end(); it != end; ++it) { + if ((it.value()->state & filter) == filter) { + QNetworkConfiguration pt; + pt.d = it.value(); + result << pt; + } + } + } + + return result; +} + +/*! + Returns the QNetworkConfiguration for \a identifier; otherwise returns an + invalid QNetworkConfiguration. + + \sa QNetworkConfiguration::identifier() +*/ +QNetworkConfiguration QNetworkConfigurationManager::configurationFromIdentifier(const QString& identifier) const +{ + QNetworkConfigurationManagerPrivate* conPriv = connManager(); + + QNetworkConfiguration item; + + foreach (QBearerEngine *engine, conPriv->sessionEngines) { + if (engine->accessPointConfigurations.contains(identifier)) + item.d = engine->accessPointConfigurations.value(identifier); + else if (engine->snapConfigurations.contains(identifier)) + item.d = engine->snapConfigurations.value(identifier); + else if (engine->userChoiceConfigurations.contains(identifier)) + item.d = engine->userChoiceConfigurations.value(identifier); + else + continue; + + return item; + } + + return item; +} + +/*! + Returns true if the system is considered to be connected to another device via an active + network interface; otherwise returns false. + + This is equivalent to the following code snippet: + + \snippet doc/src/snippets/code/src_network_bearer_qnetworkconfigmanager.cpp 0 + + \sa onlineStateChanged() +*/ +bool QNetworkConfigurationManager::isOnline() const +{ + QNetworkConfigurationManagerPrivate* conPriv = connManager(); + Q_UNUSED(conPriv); + QList<QNetworkConfiguration> activeConfigs = allConfigurations(QNetworkConfiguration::Active); + return activeConfigs.count() > 0; +} + +/*! + Returns the capabilities supported by the current platform. +*/ +QNetworkConfigurationManager::Capabilities QNetworkConfigurationManager::capabilities() const +{ + return connManager()->capFlags; +} + +/*! + Initiates an update of all configurations. This may be used to initiate WLAN scans or other + time consuming updates which may be required to obtain the correct state for configurations. + + This call is asynchronous. On completion of this update the updateCompleted() signal is + emitted. If new configurations are discovered or old ones were removed or changed the update + process may trigger the emission of one or multiple configurationAdded(), + configurationRemoved() and configurationChanged() signals. + + If a configuration state changes as a result of this update all existing QNetworkConfiguration + instances are updated automatically. + + \sa allConfigurations() +*/ +void QNetworkConfigurationManager::updateConfigurations() +{ + connManager()->performAsyncConfigurationUpdate(); +} + +#include "moc_qnetworkconfigmanager.cpp" + +QT_END_NAMESPACE + diff --git a/src/network/bearer/qnetworkconfigmanager.h b/src/network/bearer/qnetworkconfigmanager.h new file mode 100644 index 0000000..73041fe --- /dev/null +++ b/src/network/bearer/qnetworkconfigmanager.h @@ -0,0 +1,101 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNetwork module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QNETWORKCONFIGURATIONMANAGER_H +#define QNETWORKCONFIGURATIONMANAGER_H + +#include <QtCore/qobject.h> +#include <QtNetwork/qnetworkconfiguration.h> + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +QT_MODULE(Network) + +class QNetworkConfigurationManagerPrivate; +class Q_NETWORK_EXPORT QNetworkConfigurationManager : public QObject +{ + Q_OBJECT + +public: + + enum Capability { + CanStartAndStopInterfaces = 0x00000001, + DirectConnectionRouting = 0x00000002, + SystemSessionSupport = 0x00000004, + ApplicationLevelRoaming = 0x00000008, + ForcedRoaming = 0x00000010, + DataStatistics = 0x00000020 + }; + + Q_DECLARE_FLAGS(Capabilities, Capability) + + QNetworkConfigurationManager( QObject* parent = 0 ); + virtual ~QNetworkConfigurationManager(); + + + QNetworkConfigurationManager::Capabilities capabilities() const; + + QNetworkConfiguration defaultConfiguration() const; + QList<QNetworkConfiguration> allConfigurations(QNetworkConfiguration::StateFlags flags = 0) const; + QNetworkConfiguration configurationFromIdentifier(const QString& identifier) const; + void updateConfigurations(); + + bool isOnline() const; + +Q_SIGNALS: + void configurationAdded(const QNetworkConfiguration& config); + void configurationRemoved(const QNetworkConfiguration& config); + void configurationChanged(const QNetworkConfiguration& config); + void onlineStateChanged(bool isOnline); + void updateCompleted(); + +}; + +Q_DECLARE_OPERATORS_FOR_FLAGS(QNetworkConfigurationManager::Capabilities) + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif //QNETWORKCONFIGURATIONMANAGER_H + diff --git a/src/network/bearer/qnetworkconfigmanager_p.cpp b/src/network/bearer/qnetworkconfigmanager_p.cpp new file mode 100644 index 0000000..141d522 --- /dev/null +++ b/src/network/bearer/qnetworkconfigmanager_p.cpp @@ -0,0 +1,260 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNetwork module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qnetworkconfigmanager_p.h" +#include "qbearerplugin_p.h" + +#include <QtCore/private/qfactoryloader_p.h> + +#include <QtCore/qdebug.h> +#include <QtCore/qtimer.h> +#include <QtCore/qstringlist.h> + +QT_BEGIN_NAMESPACE + +Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader, + (QBearerEngineFactoryInterface_iid, QLatin1String("/bearer"))) + +QNetworkConfigurationManagerPrivate::~QNetworkConfigurationManagerPrivate() +{ + qDeleteAll(sessionEngines); +} + +void QNetworkConfigurationManagerPrivate::configurationAdded(QNetworkConfigurationPrivatePointer ptr) +{ + if (!firstUpdate) { + QNetworkConfiguration item; + item.d = ptr; + emit configurationAdded(item); + } + + if (ptr->state == QNetworkConfiguration::Active) { + onlineConfigurations.insert(ptr); + if (!firstUpdate && onlineConfigurations.count() == 1) + emit onlineStateChanged(true); + } +} + +void QNetworkConfigurationManagerPrivate::configurationRemoved(QNetworkConfigurationPrivatePointer ptr) +{ + ptr->isValid = false; + + if (!firstUpdate) { + QNetworkConfiguration item; + item.d = ptr; + emit configurationRemoved(item); + } + + onlineConfigurations.remove(ptr); + if (!firstUpdate && onlineConfigurations.isEmpty()) + emit onlineStateChanged(false); +} + +void QNetworkConfigurationManagerPrivate::configurationChanged(QNetworkConfigurationPrivatePointer ptr) +{ + if (!firstUpdate) { + QNetworkConfiguration item; + item.d = ptr; + emit configurationChanged(item); + } + + bool previous = !onlineConfigurations.isEmpty(); + + if (ptr->state == QNetworkConfiguration::Active) + onlineConfigurations.insert(ptr); + else + onlineConfigurations.remove(ptr); + + bool online = !onlineConfigurations.isEmpty(); + + if (!firstUpdate && online != previous) + emit onlineStateChanged(online); +} + +void QNetworkConfigurationManagerPrivate::updateConfigurations() +{ + if (firstUpdate) { + updating = false; + + QFactoryLoader *l = loader(); + + QBearerEngine *generic = 0; + + foreach (const QString &key, l->keys()) { + QBearerEnginePlugin *plugin = qobject_cast<QBearerEnginePlugin *>(l->instance(key)); + if (plugin) { + QBearerEngine *engine = plugin->create(key); + if (!engine) + continue; + + if (key == QLatin1String("generic")) + generic = engine; + else + sessionEngines.append(engine); + + connect(engine, SIGNAL(updateCompleted()), + this, SLOT(updateConfigurations())); + connect(engine, SIGNAL(configurationAdded(QNetworkConfigurationPrivatePointer)), + this, SLOT(configurationAdded(QNetworkConfigurationPrivatePointer))); + connect(engine, SIGNAL(configurationRemoved(QNetworkConfigurationPrivatePointer)), + this, SLOT(configurationRemoved(QNetworkConfigurationPrivatePointer))); + connect(engine, SIGNAL(configurationChanged(QNetworkConfigurationPrivatePointer)), + this, SLOT(configurationChanged(QNetworkConfigurationPrivatePointer))); + + capFlags |= engine->capabilities(); + } + } + + if (generic) + sessionEngines.append(generic); + } + + QBearerEngine *engine = qobject_cast<QBearerEngine *>(sender()); + if (!updatingEngines.isEmpty() && engine) { + int index = sessionEngines.indexOf(engine); + if (index >= 0) + updatingEngines.remove(index); + } + + if (updating && updatingEngines.isEmpty()) { + updating = false; + emit configurationUpdateComplete(); + } + + if (firstUpdate) + firstUpdate = false; +} + +/*! + Returns the default configuration of the first plugin, if one exists; otherwise returns an + invalid configuration. + + \internal +*/ +QNetworkConfiguration QNetworkConfigurationManagerPrivate::defaultConfiguration() +{ + foreach (QBearerEngine *engine, sessionEngines) { + QNetworkConfigurationPrivatePointer ptr = engine->defaultConfiguration(); + + if (ptr) { + QNetworkConfiguration config; + config.d = ptr; + return config; + } + } + + // Engines don't have a default configuration. + + // Return first active snap + QNetworkConfigurationPrivatePointer firstDiscovered; + + foreach (QBearerEngine *engine, sessionEngines) { + QHash<QString, QNetworkConfigurationPrivatePointer>::Iterator it; + QHash<QString, QNetworkConfigurationPrivatePointer>::Iterator end; + + for (it = engine->snapConfigurations.begin(), end = engine->snapConfigurations.end(); + it != end; ++it) { + if ((it.value()->state & QNetworkConfiguration::Active) == + QNetworkConfiguration::Active) { + QNetworkConfiguration config; + config.d = it.value(); + return config; + } else if ((it.value()->state & QNetworkConfiguration::Discovered) == + QNetworkConfiguration::Discovered) { + firstDiscovered = it.value(); + } + } + } + + // No Active SNAPs return first Discovered SNAP. + if (firstDiscovered) { + QNetworkConfiguration config; + config.d = firstDiscovered; + return config; + } + + // No Active or Discovered SNAPs, do same for InternetAccessPoints. + firstDiscovered.reset(); + + foreach (QBearerEngine *engine, sessionEngines) { + QHash<QString, QNetworkConfigurationPrivatePointer>::Iterator it; + QHash<QString, QNetworkConfigurationPrivatePointer>::Iterator end; + + for (it = engine->accessPointConfigurations.begin(), + end = engine->accessPointConfigurations.end(); it != end; ++it) { + if ((it.value()->state & QNetworkConfiguration::Active) == + QNetworkConfiguration::Active) { + QNetworkConfiguration config; + config.d = it.value(); + return config; + } else if ((it.value()->state & QNetworkConfiguration::Discovered) == + QNetworkConfiguration::Discovered) { + firstDiscovered = it.value(); + } + } + } + + // No Active InternetAccessPoint return first Discovered InternetAccessPoint. + if (firstDiscovered) { + QNetworkConfiguration config; + config.d = firstDiscovered; + return config; + } + + return QNetworkConfiguration(); +} + +void QNetworkConfigurationManagerPrivate::performAsyncConfigurationUpdate() +{ + if (sessionEngines.isEmpty()) { + emit configurationUpdateComplete(); + return; + } + + updating = true; + + for (int i = 0; i < sessionEngines.count(); ++i) { + updatingEngines.insert(i); + sessionEngines.at(i)->requestUpdate(); + } +} + +QT_END_NAMESPACE diff --git a/src/network/bearer/qnetworkconfigmanager_p.h b/src/network/bearer/qnetworkconfigmanager_p.h new file mode 100644 index 0000000..e178c2d --- /dev/null +++ b/src/network/bearer/qnetworkconfigmanager_p.h @@ -0,0 +1,114 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNetwork module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QNETWORKCONFIGURATIONMANAGERPRIVATE_H +#define QNETWORKCONFIGURATIONMANAGERPRIVATE_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "qnetworkconfigmanager.h" +#include "qnetworkconfiguration_p.h" + +QT_BEGIN_NAMESPACE + +class QBearerEngine; + +class QNetworkConfigurationManagerPrivate : public QObject +{ + Q_OBJECT +public: + QNetworkConfigurationManagerPrivate() + : QObject(0), capFlags(0), firstUpdate(true) + { + updateConfigurations(); + } + + virtual ~QNetworkConfigurationManagerPrivate(); + + QNetworkConfiguration defaultConfiguration(); + + QNetworkConfigurationManager::Capabilities capFlags; + + void performAsyncConfigurationUpdate(); + + bool firstUpdate; + +public slots: + void updateConfigurations(); + +Q_SIGNALS: + void configurationAdded(const QNetworkConfiguration& config); + void configurationRemoved(const QNetworkConfiguration& config); + void configurationUpdateComplete(); + void configurationChanged(const QNetworkConfiguration& config); + void onlineStateChanged(bool isOnline); + + void abort(); + +public: + QList<QBearerEngine *> sessionEngines; + +private: + QSet<QNetworkConfigurationPrivatePointer> onlineConfigurations; + + bool updating; + QSet<int> updatingEngines; + +private Q_SLOTS: + void configurationAdded(QNetworkConfigurationPrivatePointer ptr); + void configurationRemoved(QNetworkConfigurationPrivatePointer ptr); + void configurationChanged(QNetworkConfigurationPrivatePointer ptr); +}; + +Q_NETWORK_EXPORT QNetworkConfigurationManagerPrivate *qNetworkConfigurationManagerPrivate(); + +QT_END_NAMESPACE + +#endif //QNETWORKCONFIGURATIONMANAGERPRIVATE_H diff --git a/src/network/bearer/qnetworkconfiguration.cpp b/src/network/bearer/qnetworkconfiguration.cpp new file mode 100644 index 0000000..c551dc5 --- /dev/null +++ b/src/network/bearer/qnetworkconfiguration.cpp @@ -0,0 +1,396 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNetwork module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qnetworkconfiguration.h" + +#include "qnetworkconfiguration_p.h" + +QT_BEGIN_NAMESPACE + +/*! + \class QNetworkConfiguration + + \brief The QNetworkConfiguration class provides an abstraction of one or more access point configurations. + + \since 4.7 + + \inmodule QtNetwork + \ingroup bearer + + QNetworkConfiguration encapsulates a single access point or service network. + In most cases a single access point configuration can be mapped to one network + interface. However a single network interface may not always map to only one + access point configuration. Multiple configurations for the same + network device may enable multiple access points. An example + device that could exhibit such a configuration might be a + Smartphone which allows the user to manage multiple WLAN + configurations while the device itself has only one WLAN network device. + + The QNetworkConfiguration also supports the concept of service networks. + This concept allows the grouping of multiple access point configurations + into one entity. Such a group is called service network and can be + beneficial in cases whereby a network session to a + particular destination network is required (e.g. a company network). + When using a service network the user doesn't usually care which one of the + connectivity options is chosen (e.g. corporate WLAN or VPN via GPRS) + as long as he can reach the company's target server. Depending + on the current position and time some of the access points that make + up the service network may not even be available. Furthermore + automated access point roaming can be enabled which enables the device + to change the network interface configuration dynamically while maintaining + the applications connection to the target network. It allows adaption + to the changing environment and may enable optimization with regards to + cost, speed or other network parameters. + + Special configurations of type UserChoice provide a placeholder configuration which is + resolved to an actual network configuration by the platform when a + \l {QNetworkSession}{session} is \l {QNetworkSession::open()}{opened}. Not all platforms + support the concept of a user choice configuration. + + \section1 Configuration states + + The list of available configurations can be obtained via + QNetworkConfigurationManager::allConfigurations(). A configuration can have + multiple states. The \l Defined configuration state indicates that the configuration + is stored on the device. However the configuration is not yet ready to be activated + as e.g. a WLAN may not be available at the current time. + + The \l Discovered state implies that the configuration is \l Defined and + the outside conditions are such that the configuration can be used immediately + to open a new network session. An example of such an outside condition may be + that the Ethernet cable is actually connected to the device or that the WLAN + with the specified SSID is in range. + + The \l Active state implies that the configuration is \l Discovered. A configuration + in this state is currently being used by an application. The underlying network + interface has a valid IP configuration and can transfer IP packets between the + device and the target network. + + The \l Undefined state indicates that the system has knowledge of possible target + networks but cannot actually use that knowledge to connect to it. An example + for such a state could be an encrypted WLAN that has been discovered + but the user hasn't actually saved a configuration including the required password + which would allow the device to connect to it. + + Depending on the type of configuration some states are transient in nature. A GPRS/UMTS + connection may almost always be \l Discovered if the GSM/UMTS network is available. + However if the GSM/UMTS network looses the connection the associated configuration may change its state + from \l Discovered to \l Defined as well. A similar use case might be triggered by + WLAN availability. QNetworkConfigurationManager::updateConfigurations() can be used to + manually trigger updates of states. Note that some platforms do not require such updates + as they implicitly change the state once it has been discovered. If the state of a + configuration changes all related QNetworkConfiguration instances change their state automatically. + + \sa QNetworkSession, QNetworkConfigurationManager +*/ + +/*! + \enum QNetworkConfiguration::Type + + This enum describes the type of configuration. + + \value InternetAccessPoint The configuration specifies the details for a single access point. + Note that configurations of type InternetAccessPoint may be part + of other QNetworkConfigurations of type ServiceNetwork. + \value ServiceNetwork The configuration is based on a group of QNetworkConfigurations of + type InternetAccessPoint. All group members can reach the same + target network. This type of configuration is a mandatory + requirement for roaming enabled network sessions. On some + platforms this form of configuration may also be called Service + Network Access Point (SNAP). + \value UserChoice The configuration is a placeholder which will be resolved to an + actual configuration by the platform when a session is opened. Depending + on the platform the selection may generate a popup dialog asking the user + for his preferred choice. + \value Invalid The configuration is invalid. +*/ + +/*! + \enum QNetworkConfiguration::StateFlag + + Specifies the configuration states. + + \value Undefined This state is used for transient configurations such as newly discovered + WLANs for which the user has not actually created a configuration yet. + \value Defined Defined configurations are known to the system but are not immediately + usable (e.g. a configured WLAN is not within range or the Ethernet cable + is currently not plugged into the machine). + \value Discovered A discovered configuration can be immediately used to create a new + QNetworkSession. An example of a discovered configuration could be a WLAN + which is within in range. If the device moves out of range the discovered + flag is dropped. A second example is a GPRS configuration which generally + remains discovered for as long as the device has network coverage. A + configuration that has this state is also in state + QNetworkConfiguration::Defined. If the configuration is a service network + this flag is set if at least one of the underlying access points + configurations has the Discovered state. + \value Active The configuration is currently used by an open network session + (see \l QNetworkSession::isOpen()). However this does not mean that the + current process is the entity that created the open session. It merely + indicates that if a new QNetworkSession were to be constructed based on + this configuration \l QNetworkSession::state() would return + \l QNetworkSession::Connected. This state implies the + QNetworkConfiguration::Discovered state. +*/ + +/*! + \enum QNetworkConfiguration::Purpose + + Specifies the purpose of the configuration. + + \value UnknownPurpose The configuration doesn't specify any purpose. This is the default value. + \value PublicPurpose The configuration can be used for general purpose internet access. + \value PrivatePurpose The configuration is suitable to access a private network such as an office Intranet. + \value ServiceSpecificPurpose The configuration can be used for operator specific services (e.g. + receiving MMS messages or content streaming). +*/ + +/*! + Constructs an invalid configuration object. + + \sa isValid() +*/ +QNetworkConfiguration::QNetworkConfiguration() + : d(0) +{ +} + +/*! + Creates a copy of the QNetworkConfiguration object contained in \a other. +*/ +QNetworkConfiguration::QNetworkConfiguration(const QNetworkConfiguration& other) + : d(other.d) +{ +} + +/*! + Copies the content of the QNetworkConfiguration object contained in \a other into this one. +*/ +QNetworkConfiguration& QNetworkConfiguration::operator=(const QNetworkConfiguration& other) +{ + d = other.d; + return *this; +} + +/*! + Frees the resources associated with the QNetworkConfiguration object. +*/ +QNetworkConfiguration::~QNetworkConfiguration() +{ +} + +/*! + Returns true, if this configuration is the same as the \a other + configuration given; otherwise returns false. +*/ +bool QNetworkConfiguration::operator==(const QNetworkConfiguration& other) const +{ + if (!d) + return !other.d; + + if (!other.d) + return false; + + return (d == other.d); +} + +/*! + \fn bool QNetworkConfiguration::operator!=(const QNetworkConfiguration& other) const + + Returns true if this configuration is not the same as the \a other + configuration given; otherwise returns false. +*/ + +/*! + Returns the user visible name of this configuration. + + The name may either be the name of the underlying access point or the + name for service network that this configuration represents. +*/ +QString QNetworkConfiguration::name() const +{ + return d ? d->name : QString(); +} + +/*! + Returns the unique and platform specific identifier for this network configuration; + otherwise an empty string. +*/ +QString QNetworkConfiguration::identifier() const +{ + return d ? d->id : QString(); +} + +/*! + Returns the type of the configuration. + + A configuration can represent a single access point configuration or + a set of access point configurations. Such a set is called service network. + A configuration that is based on a service network can potentially support + roaming of network sessions. +*/ +QNetworkConfiguration::Type QNetworkConfiguration::type() const +{ + return d ? d->type : QNetworkConfiguration::Invalid; +} + +/*! + Returns true if this QNetworkConfiguration object is valid. + A configuration may become invalid if the user deletes the configuration or + the configuration was default-constructed. + + The addition and removal of configurations can be monitored via the + QNetworkConfigurationManager. + + \sa QNetworkConfigurationManager +*/ +bool QNetworkConfiguration::isValid() const +{ + return d ? d->isValid : false; +} + +/*! + Returns the current state of the configuration. +*/ +QNetworkConfiguration::StateFlags QNetworkConfiguration::state() const +{ + return d ? d->state : QNetworkConfiguration::Undefined; +} + +/*! + Returns the purpose of this configuration. + + The purpose field may be used to programmatically determine the + purpose of a configuration. Such information is usually part of the + access point or service network meta data. +*/ +QNetworkConfiguration::Purpose QNetworkConfiguration::purpose() const +{ + return d ? d->purpose : QNetworkConfiguration::UnknownPurpose; +} + +/*! + Returns true if this configuration supports roaming; otherwise false. +*/ +bool QNetworkConfiguration::isRoamingAvailable() const +{ + return d ? d->roamingSupported : false; +} + +/*! + Returns all sub configurations of this network configuration. + Only network configurations of type \l ServiceNetwork can have children. Otherwise + this function returns an empty list. +*/ +QList<QNetworkConfiguration> QNetworkConfiguration::children() const +{ + QList<QNetworkConfiguration> results; + if (type() != QNetworkConfiguration::ServiceNetwork || !isValid() ) + return results; + + QMutableListIterator<QNetworkConfigurationPrivatePointer> iter(d->serviceNetworkMembers); + while(iter.hasNext()) { + QNetworkConfigurationPrivatePointer p = iter.next(); + //if we have an invalid member get rid of it -> was deleted earlier on + if (!p->isValid) + iter.remove(); + + QNetworkConfiguration item; + item.d = p; + results << item; + } + + return results; +} + +/*! + Returns the type of bearer. The string is not translated and + therefore can not be shown to the user. The subsequent table presents the currently known + bearer types: + + \table + \header + \o Value + \o Description + \row + \o Unknown + \o The session is based on an unknown or unspecified bearer type. + \row + \o Ethernet + \o The session is based on Ethernet. + \row + \o WLAN + \o The session is based on Wireless LAN. + \row + \o 2G + \o The session uses CSD, GPRS, HSCSD, EDGE or cdmaOne. + \row + \o CDMA2000 + \o The session uses CDMA. + \row + \o WCDMA + \o The session uses W-CDMA/UMTS. + \row + \o HSPA + \o The session uses High Speed Packet Access. + \row + \o Bluetooth + \o The session uses Bluetooth. + \row + \o WiMAX + \o The session uses WiMAX. + \endtable + + This function returns an empty string if this is an invalid configuration, + a network configuration of type \l QNetworkConfiguration::ServiceNetwork or + \l QNetworkConfiguration::UserChoice. +*/ +QString QNetworkConfiguration::bearerName() const +{ + if (!isValid()) + return QString(); + + return d->bearerName(); +} + + +QT_END_NAMESPACE + diff --git a/src/network/bearer/qnetworkconfiguration.h b/src/network/bearer/qnetworkconfiguration.h new file mode 100644 index 0000000..dad6198 --- /dev/null +++ b/src/network/bearer/qnetworkconfiguration.h @@ -0,0 +1,115 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNetwork module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QNETWORKCONFIGURATION_H +#define QNETWORKCONFIGURATION_H + +#include <QtCore/qglobal.h> +#include <QtCore/qshareddata.h> +#include <QtCore/qstring.h> +#include <QtCore/qlist.h> + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +QT_MODULE(Network) + +class QNetworkConfigurationPrivate; +class Q_NETWORK_EXPORT QNetworkConfiguration +{ +public: + QNetworkConfiguration(); + QNetworkConfiguration(const QNetworkConfiguration& other); + QNetworkConfiguration &operator=(const QNetworkConfiguration& other); + ~QNetworkConfiguration(); + + bool operator==(const QNetworkConfiguration& cp) const; + inline bool operator!=(const QNetworkConfiguration& cp) const + { return !operator==(cp); } + + enum Type { + InternetAccessPoint = 0, + ServiceNetwork, + UserChoice, + Invalid + }; + + enum Purpose { + UnknownPurpose = 0, + PublicPurpose, + PrivatePurpose, + ServiceSpecificPurpose + }; + + enum StateFlag { + Undefined = 0x0000001, + Defined = 0x0000002, + Discovered = 0x0000006, + Active = 0x000000e + }; + + Q_DECLARE_FLAGS(StateFlags, StateFlag) + + StateFlags state() const; + Type type() const; + Purpose purpose() const; + QString bearerName() const; + QString identifier() const; + bool isRoamingAvailable() const; + QList<QNetworkConfiguration> children() const; + + QString name() const; + bool isValid() const; + +private: + friend class QNetworkConfigurationPrivate; + friend class QNetworkConfigurationManager; + friend class QNetworkConfigurationManagerPrivate; + friend class QNetworkSessionPrivate; + QExplicitlySharedDataPointer<QNetworkConfigurationPrivate> d; +}; + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif //QNETWORKCONFIGURATION_H diff --git a/src/network/bearer/qnetworkconfiguration_p.h b/src/network/bearer/qnetworkconfiguration_p.h new file mode 100644 index 0000000..6b40946 --- /dev/null +++ b/src/network/bearer/qnetworkconfiguration_p.h @@ -0,0 +1,107 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNetwork module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QNETWORKCONFIGURATIONPRIVATE_H +#define QNETWORKCONFIGURATIONPRIVATE_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "qnetworkconfiguration.h" +#include <QtCore/qshareddata.h> +#include <QtNetwork/QNetworkInterface> + +QT_BEGIN_NAMESPACE + +typedef QExplicitlySharedDataPointer<QNetworkConfigurationPrivate> QNetworkConfigurationPrivatePointer; +class QNetworkConfigurationPrivate : public QSharedData +{ +public: + QNetworkConfigurationPrivate () + : isValid(false), type(QNetworkConfiguration::Invalid), + roamingSupported(false), purpose(QNetworkConfiguration::UnknownPurpose), internet(false) + { + } + + ~QNetworkConfigurationPrivate() + { + //release pointers to member configurations + serviceNetworkMembers.clear(); + } + + virtual QString bearerName() const + { + return bearer; + } + + QString bearer; + + QString name; + + bool isValid; + QString id; + QNetworkConfiguration::StateFlags state; + QNetworkConfiguration::Type type; + bool roamingSupported; + QNetworkConfiguration::Purpose purpose; + + bool internet; + + QList<QNetworkConfigurationPrivatePointer> serviceNetworkMembers; + +private: + + // disallow detaching + QNetworkConfigurationPrivate &operator=(const QNetworkConfigurationPrivate &other); + QNetworkConfigurationPrivate(const QNetworkConfigurationPrivate &other); +}; + +QT_END_NAMESPACE + +#endif //QNETWORKCONFIGURATIONPRIVATE_H diff --git a/src/network/bearer/qnetworksession.cpp b/src/network/bearer/qnetworksession.cpp new file mode 100644 index 0000000..3e77354 --- /dev/null +++ b/src/network/bearer/qnetworksession.cpp @@ -0,0 +1,684 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNetwork module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QEventLoop> +#include <QTimer> + +#include "qnetworksession.h" +#include "qbearerengine_p.h" +#include "qnetworkconfigmanager_p.h" +#include "qnetworksession_p.h" + +QT_BEGIN_NAMESPACE + +/*! + \class QNetworkSession + + \brief The QNetworkSession class provides control over the system's access points + and enables session management for cases when multiple clients access the same access point. + + \since 4.7 + + \inmodule QtNetwork + \ingroup bearer + + A QNetworkSession enables control over the system's network interfaces. The session's configuration + parameter are determined via the QNetworkConfiguration object to which it is bound. Depending on the + type of the session (single access point or service network) a session may be linked to one or more + network interfaces. By means of \l{open()}{opening} and \l{close()}{closing} of network sessions + a developer can start and stop the systems network interfaces. If the configuration represents + multiple access points (see \l QNetworkConfiguration::ServiceNetwork) more advanced features such as roaming may be supported. + + QNetworkSession supports session management within the same process and depending on the platform's + capabilities may support out-of-process sessions. If the same + network configuration is used by multiple open sessions the underlying network interface is only terminated once + the last session has been closed. + + \section1 Roaming + + Applications may connect to the preferredConfigurationChanged() signal in order to + receive notifications when a more suitable access point becomes available. + In response to this signal the application must either initiate the roaming via migrate() + or ignore() the new access point. Once the session has roamed the + newConfigurationActivated() signal is emitted. The application may now test the + carrier and must either accept() or reject() it. The session will return to the previous + access point if the roaming was rejected. The subsequent state diagram depicts the required + state transitions. + + \image roaming-states.png + + Some platforms may distinguish forced roaming and application level roaming (ALR). + ALR implies that the application controls (via migrate(), ignore(), accept() and reject()) + whether a network session can roam from one access point to the next. Such control is useful + if the application maintains stateful socket connections and wants to control the transition from + one interface to the next. Forced roaming implies that the system automatically roams to the next network without + consulting the application. This has the advantage that the application can make use of roaming features + without actually being aware of it. It is expected that the application detects that the underlying + socket is broken and automatically reconnects via the new network link. + + If the platform supports both modes of roaming, an application indicates its preference + by connecting to the preferredConfigurationChanged() signal. Connecting to this signal means that + the application wants to take control over the roaming behavior and therefore implies application + level roaming. If the client does not connect to the preferredConfigurationChanged(), forced roaming + is used. If forced roaming is not supported the network session will not roam by default. + + Some applications may want to suppress any form of roaming altogether. Possible use cases may be + high priority downloads or remote services which cannot handle a roaming enabled client. Clients + can suppress roaming by connecting to the preferredConfigurationChanged() signal and answer each + signal emission with ignore(). + + \sa QNetworkConfiguration, QNetworkConfigurationManager +*/ + +/*! + \enum QNetworkSession::State + + This enum describes the connectivity state of the session. If the session is based on a + single access point configuration the state of the session is the same as the state of the + associated network interface. + + \value Invalid The session is invalid due to an invalid configuration. This may + happen due to a removed access point or a configuration that was + invalid to begin with. + \value NotAvailable The session is based on a defined but not yet discovered QNetworkConfiguration + (see \l QNetworkConfiguration::StateFlag). + \value Connecting The network session is being established. + \value Connected The network session is connected. If the current process wishes to use this session + it has to register its interest by calling open(). A network session + is considered to be ready for socket operations if it isOpen() and connected. + \value Closing The network session is in the process of being shut down. + \value Disconnected The network session is not connected. The associated QNetworkConfiguration + has the state QNetworkConfiguration::Discovered. + \value Roaming The network session is roaming from one access point to another + access point. +*/ + +/*! + \enum QNetworkSession::SessionError + + This enum describes the session errors that can occur. + + \value UnknownSessionError An unidentified error occurred. + \value SessionAbortedError The session was aborted by the user or system. + \value RoamingError The session cannot roam to a new configuration. + \value OperationNotSupportedError The operation is not supported for current configuration. + \value InvalidConfigurationError The operation cannot currently be performed for the + current configuration. +*/ + +/*! + \fn void QNetworkSession::stateChanged(QNetworkSession::State state) + + This signal is emitted whenever the state of the network session changes. + The \a state parameter is the new state. + + \sa state() +*/ + +/*! + \fn void QNetworkSession::error(QNetworkSession::SessionError error) + + This signal is emitted after an error occurred. The \a error parameter + describes the error that occurred. + + \sa error(), errorString() +*/ + +/*! + \fn void QNetworkSession::preferredConfigurationChanged(const QNetworkConfiguration& config, bool isSeamless) + + This signal is emitted when the preferred configuration/access point for the + session changes. Only sessions which are based on service network configurations + may emit this signal. \a config can be used to determine access point specific + details such as proxy settings and \a isSeamless indicates whether roaming will + break the sessions IP address. + + As a consequence to this signal the application must either start the roaming process + by calling migrate() or choose to ignore() the new access point. + + If the roaming process is non-seamless the IP address will change which means that + a socket becomes invalid. However seamless mobility can ensure that the local IP address + does not change. This is achieved by using a virtual IP address which is bound to the actual + link address. During the roaming process the virtual address is attached to the new link + address. + + Some platforms may support the concept of Forced Roaming and Application Level Roaming (ALR). + Forced roaming implies that the platform may simply roam to a new configuration without + consulting applications. It is up to the application to detect the link layer loss and reestablish + its sockets. In contrast ALR provides the opportunity to prevent the system from roaming. + If this session is based on a configuration that supports roaming the application can choose + whether it wants to be consulted (ALR use case) by connecting to this signal. For as long as this signal + connection remains the session remains registered as a roaming stakeholder; otherwise roaming will + be enforced by the platform. + + \sa migrate(), ignore(), QNetworkConfiguration::isRoamingAvailable() +*/ + +/*! + \fn void QNetworkSession::newConfigurationActivated() + + This signal is emitted once the session has roamed to the new access point. + The application may reopen its socket and test the suitability of the new network link. + Subsequently it must either accept() or reject() the new access point. + + \sa accept(), reject() +*/ + +/*! + \fn void QNetworkSession::opened() + + This signal is emitted when the network session has been opened. + + The underlying network interface will not be shut down as long as the session remains open. + Note that this feature is dependent on \l{QNetworkConfigurationManager::SystemSessionSupport}{system wide session support}. +*/ + +/*! + \fn void QNetworkSession::closed() + + This signal is emitted when the network session has been closed. +*/ + +/*! + Constructs a session based on \a connectionConfig with the given \a parent. + + \sa QNetworkConfiguration +*/ +QNetworkSession::QNetworkSession(const QNetworkConfiguration& connectionConfig, QObject* parent) +: QObject(parent), d(0) +{ + foreach (QBearerEngine *engine, qNetworkConfigurationManagerPrivate()->sessionEngines) { + if (engine->hasIdentifier(connectionConfig.identifier())) { + d = engine->createSessionBackend(); + d->q = this; + d->publicConfig = connectionConfig; + d->syncStateWithInterface(); + connect(d, SIGNAL(quitPendingWaitsForOpened()), this, SIGNAL(opened())); + connect(d, SIGNAL(error(QNetworkSession::SessionError)), + this, SIGNAL(error(QNetworkSession::SessionError))); + connect(d, SIGNAL(stateChanged(QNetworkSession::State)), + this, SIGNAL(stateChanged(QNetworkSession::State))); + connect(d, SIGNAL(closed()), this, SIGNAL(closed())); + connect(d, SIGNAL(preferredConfigurationChanged(QNetworkConfiguration,bool)), + this, SIGNAL(preferredConfigurationChanged(QNetworkConfiguration,bool))); + connect(d, SIGNAL(newConfigurationActivated()), + this, SIGNAL(newConfigurationActivated())); + break; + } + } +} + +/*! + Frees the resources associated with the QNetworkSession object. +*/ +QNetworkSession::~QNetworkSession() +{ + delete d; +} + +/*! + Creates an open session which increases the session counter on the underlying network interface. + The system will not terminate a network interface until the session reference counter reaches zero. + Therefore an open session allows an application to register its use of the interface. + + As a result of calling open() the interface will be started if it is not connected/up yet. + Some platforms may not provide support for out-of-process sessions. On such platforms the session + counter ignores any sessions held by another process. The platform capabilities can be + detected via QNetworkConfigurationManager::capabilities(). + + Note that this call is asynchronous. Depending on the outcome of this call the results can be enquired + by connecting to the stateChanged(), opened() or error() signals. + + It is not a requirement to open a session in order to monitor the underlying network interface. + + \sa close(), stop(), isOpen() +*/ +void QNetworkSession::open() +{ + if (d) + d->open(); +} + +/*! + Waits until the session has been opened, up to \a msecs milliseconds. If the session has been opened, this + function returns true; otherwise it returns false. In the case where it returns false, you can call error() + to determine the cause of the error. + + The following example waits up to one second for the session to be opened: + + \code + session->open(); + if (session->waitForOpened(1000)) + qDebug("Open!"); + \endcode + + If \a msecs is -1, this function will not time out. + + \sa open(), error() +*/ +bool QNetworkSession::waitForOpened(int msecs) +{ + if (!d) + return false; + + if (d->isOpen) + return true; + + if (d->state != Connecting) + return false; + + QEventLoop* loop = new QEventLoop(this); + QObject::connect(d, SIGNAL(quitPendingWaitsForOpened()), + loop, SLOT(quit())); + + //final call + if (msecs>=0) + QTimer::singleShot(msecs, loop, SLOT(quit())); + + loop->exec(); + loop->disconnect(); + loop->deleteLater(); + + return d->isOpen; +} + +/*! + Decreases the session counter on the associated network configuration. If the session counter reaches zero + the active network interface is shut down. This also means that state() will only change from \l Connected to + \l Disconnected if the current session was the last open session. + + If the platform does not support out-of-process sessions calling this function does not stop the + interface. In this case \l{stop()} has to be used to force a shut down. + The platform capabilities can be detected via QNetworkConfigurationManager::capabilities(). + + Note that this call is asynchronous. Depending on the outcome of this call the results can be enquired + by connecting to the stateChanged(), opened() or error() signals. + + \sa open(), stop(), isOpen() +*/ +void QNetworkSession::close() +{ + if (d) + d->close(); +} + +/*! + Invalidates all open sessions against the network interface and therefore stops the + underlying network interface. This function always changes the session's state() flag to + \l Disconnected. + + On Symbian platform, a 'NetworkControl' capability is required for + full interface-level stop (without the capability, only the current session is stopped). + + \sa open(), close() +*/ +void QNetworkSession::stop() +{ + if (d) + d->stop(); +} + +/*! + Returns the QNetworkConfiguration that this network session object is based on. + + \sa QNetworkConfiguration +*/ +QNetworkConfiguration QNetworkSession::configuration() const +{ + return d ? d->publicConfig : QNetworkConfiguration(); +} + +/*! + Returns the network interface that is used by this session. + + This function only returns a valid QNetworkInterface when this session is \l Connected. + + The returned interface may change as a result of a roaming process. + + Note: this function does not work in Symbian emulator due to the way the + connectivity is emulated on Windows. + + \sa state() +*/ +QNetworkInterface QNetworkSession::interface() const +{ + return d ? d->currentInterface() : QNetworkInterface(); +} + +/*! + Returns true if this session is open. If the number of all open sessions is greater than + zero the underlying network interface will remain connected/up. + + The session can be controlled via open() and close(). +*/ +bool QNetworkSession::isOpen() const +{ + return d ? d->isOpen : false; +} + +/*! + Returns the state of the session. + + If the session is based on a single access point configuration the state of the + session is the same as the state of the associated network interface. Therefore + a network session object can be used to monitor network interfaces. + + A \l QNetworkConfiguration::ServiceNetwork based session summarizes the state of all its children + and therefore returns the \l Connected state if at least one of the service network's + \l {QNetworkConfiguration::children()}{children()} configurations is active. + + Note that it is not required to hold an open session in order to obtain the network interface state. + A connected but closed session may be used to monitor network interfaces whereas an open and connected + session object may prevent the network interface from being shut down. + + \sa error(), stateChanged() +*/ +QNetworkSession::State QNetworkSession::state() const +{ + return d ? d->state : QNetworkSession::Invalid; +} + +/*! + Returns the type of error that last occurred. + + \sa state(), errorString() +*/ +QNetworkSession::SessionError QNetworkSession::error() const +{ + return d ? d->error() : InvalidConfigurationError; +} + +/*! + Returns a human-readable description of the last device error that + occurred. + + \sa error() +*/ +QString QNetworkSession::errorString() const +{ + return d ? d->errorString() : tr("Invalid configuration."); +} + +/*! + Returns the value for property \a key. + + A network session can have properties attached which may describe the session in more details. + This function can be used to gain access to those properties. + + The following property keys are guaranteed to be specified on all platforms: + + \table + \header + \o Key \o Description + \row + \o ActiveConfiguration + \o If the session \l isOpen() this property returns the identifier of the + QNetworkConfiguration that is used by this session; otherwise an empty string. + + The main purpose of this key is to determine which Internet access point is used + if the session is based on a \l{QNetworkConfiguration::ServiceNetwork}{ServiceNetwork}. + The following code snippet highlights the difference: + \code + QNetworkConfigurationManager mgr; + QNetworkConfiguration ap = mgr.defaultConfiguration(); + QNetworkSession* session = new QNetworkSession(ap); + ... //code activates session + + QString ident = session->sessionProperty("ActiveConfiguration").toString(); + if ( ap.type() == QNetworkConfiguration::ServiceNetwork ) { + Q_ASSERT( ap.identifier() != ident ); + Q_ASSERT( ap.children().contains( mgr.configurationFromIdentifier(ident) ) ); + } else if ( ap.type() == QNetworkConfiguration::InternetAccessPoint ) { + Q_ASSERT( ap.identifier() == ident ); + } + \endcode + \row + \o UserChoiceConfiguration + \o If the session \l isOpen() and is bound to a QNetworkConfiguration of type + UserChoice, this property returns the identifier of the QNetworkConfiguration that the + configuration resolved to when \l open() was called; otherwise an empty string. + + The purpose of this key is to determine the real QNetworkConfiguration that the + session is using. This key is different to \i ActiveConfiguration in that + this key may return an identifier for either a + \l {QNetworkConfiguration::ServiceNetwork}{service network} or a + \l {QNetworkConfiguration::InternetAccessPoint}{Internet access points} configurations + whereas \i ActiveConfiguration always returns identifiers to + \l {QNetworkConfiguration::InternetAccessPoint}{Internet access points} configurations. + \row + \o ConnectInBackground + \o Setting this property to \i true before calling \l open() implies that the connection attempt + is made but if no connection can be established, the user is not connsulted and asked to select + a suitable connection. This property is not set by default and support for it depends on the platform. + \endtable +*/ +QVariant QNetworkSession::sessionProperty(const QString& key) const +{ + if (!d) + return QVariant(); + + if (!d->publicConfig.isValid()) + return QVariant(); + + if (key == QLatin1String("ActiveConfiguration")) { + if (!d->isOpen) + return QString(); + else + return d->activeConfig.identifier(); + } + + if (key == QLatin1String("UserChoiceConfiguration")) { + if (!d->isOpen || d->publicConfig.type() != QNetworkConfiguration::UserChoice) + return QString(); + + if (d->serviceConfig.isValid()) + return d->serviceConfig.identifier(); + else + return d->activeConfig.identifier(); + } + + return d->sessionProperty(key); +} + +/*! + Sets the property \a value on the session. The property is identified using + \a key. Removing an already set property can be achieved by passing an + invalid QVariant. + + Note that the \i UserChoiceConfiguration and \i ActiveConfiguration + properties are read only and cannot be changed using this method. +*/ +void QNetworkSession::setSessionProperty(const QString& key, const QVariant& value) +{ + if (!d) + return; + + if (key == QLatin1String("ActiveConfiguration") || + key == QLatin1String("UserChoiceConfiguration")) { + return; + } + + d->setSessionProperty(key, value); +} + +/*! + Instructs the session to roam to the new access point. The old access point remains active + until the application calls accept(). + + The newConfigurationActivated() signal is emitted once roaming has been completed. + + \sa accept() +*/ +void QNetworkSession::migrate() +{ + if (d) + d->migrate(); +} + +/*! + This function indicates that the application does not wish to roam the session. + + \sa migrate() +*/ +void QNetworkSession::ignore() +{ + // Needed on at least Symbian platform: the roaming must be explicitly + // ignore()'d or migrate()'d + if (d) + d->ignore(); +} + +/*! + Instructs the session to permanently accept the new access point. Once this function + has been called the session may not return to the old access point. + + The old access point may be closed in the process if there are no other network sessions for it. + Therefore any open socket that still uses the old access point + may become unusable and should be closed before completing the migration. +*/ +void QNetworkSession::accept() +{ + if (d) + d->accept(); +} + +/*! + The new access point is not suitable for the application. By calling this function the + session returns to the previous access point/configuration. This action may invalidate + any socket that has been created via the not desired access point. + + \sa accept() +*/ +void QNetworkSession::reject() +{ + if (d) + d->reject(); +} + + +/*! + Returns the amount of data sent in bytes; otherwise 0. + + This field value includes the usage across all open network + sessions which use the same network interface. + + If the session is based on a service network configuration the number of + sent bytes across all active member configurations are returned. + + This function may not always be supported on all platforms and returns + 0. The platform capability can be detected via QNetworkConfigurationManager::DataStatistics. +*/ +quint64 QNetworkSession::bytesWritten() const +{ + return d ? d->bytesWritten() : Q_UINT64_C(0); +} + +/*! + Returns the amount of data received in bytes; otherwise 0. + + This field value includes the usage across all open network + sessions which use the same network interface. + + If the session is based on a service network configuration the number of + sent bytes across all active member configurations are returned. + + This function may not always be supported on all platforms and returns + 0. The platform capability can be detected via QNetworkConfigurationManager::DataStatistics. +*/ +quint64 QNetworkSession::bytesReceived() const +{ + return d ? d->bytesReceived() : Q_UINT64_C(0); +} + +/*! + Returns the number of seconds that the session has been active. +*/ +quint64 QNetworkSession::activeTime() const +{ + return d ? d->activeTime() : Q_UINT64_C(0); +} + +/*! + \internal + + This function is required to detect whether the client wants to control + the roaming process. If he connects to preferredConfigurationChanged() signal + he intends to influence it. Otherwise QNetworkSession always roams + without registering this session as a stakeholder in the roaming process. + + For more details check the Forced vs ALR roaming section in the QNetworkSession + class description. +*/ +void QNetworkSession::connectNotify(const char *signal) +{ + QObject::connectNotify(signal); + //check for preferredConfigurationChanged() signal connect notification + //This is not required on all platforms + if (!d) + return; + + if (qstrcmp(signal, SIGNAL(preferredConfigurationChanged(QNetworkConfiguration,bool))) == 0) + d->setALREnabled(true); +} + +/*! + \internal + + This function is called when the client disconnects from the preferredConfigurationChanged() + signal. + + \sa connectNotify() +*/ +void QNetworkSession::disconnectNotify(const char *signal) +{ + QObject::disconnectNotify(signal); + //check for preferredConfigurationChanged() signal disconnect notification + //This is not required on all platforms + if (!d) + return; + + if (qstrcmp(signal, SIGNAL(preferredConfigurationChanged(QNetworkConfiguration,bool))) == 0) + d->setALREnabled(false); +} + +#include "moc_qnetworksession.cpp" + +QT_END_NAMESPACE diff --git a/src/network/bearer/qnetworksession.h b/src/network/bearer/qnetworksession.h new file mode 100644 index 0000000..18437f6 --- /dev/null +++ b/src/network/bearer/qnetworksession.h @@ -0,0 +1,132 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNetwork module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QNETWORKSESSION_H +#define QNETWORKSESSION_H + +#include <QtCore/qobject.h> +#include <QtCore/qstring.h> +#include <QtNetwork/qnetworkinterface.h> +#include <QtCore/qvariant.h> +#include <QtNetwork/qnetworkconfiguration.h> + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +QT_MODULE(Network) + +class QNetworkSessionPrivate; +class Q_NETWORK_EXPORT QNetworkSession : public QObject +{ + Q_OBJECT +public: + enum State { + Invalid = 0, + NotAvailable, + Connecting, + Connected, + Closing, + Disconnected, + Roaming + }; + + enum SessionError { + UnknownSessionError = 0, + SessionAbortedError, + RoamingError, + OperationNotSupportedError, + InvalidConfigurationError + }; + + QNetworkSession(const QNetworkConfiguration& connConfig, QObject* parent =0); + virtual ~QNetworkSession(); + + bool isOpen() const; + QNetworkConfiguration configuration() const; + QNetworkInterface interface() const; + + State state() const; + SessionError error() const; + QString errorString() const; + QVariant sessionProperty(const QString& key) const; + void setSessionProperty(const QString& key, const QVariant& value); + + quint64 bytesWritten() const; + quint64 bytesReceived() const; + quint64 activeTime() const; + + bool waitForOpened(int msecs = 30000); + +public Q_SLOTS: + void open(); + void close(); + void stop(); + + //roaming related slots + void migrate(); + void ignore(); + void accept(); + void reject(); + + +Q_SIGNALS: + void stateChanged(QNetworkSession::State); + void opened(); + void closed(); + void error(QNetworkSession::SessionError); + void preferredConfigurationChanged(const QNetworkConfiguration& config, bool isSeamless); + void newConfigurationActivated(); + +protected: + virtual void connectNotify(const char *signal); + virtual void disconnectNotify(const char *signal); + +private: + QNetworkSessionPrivate* d; + friend class QNetworkSessionPrivate; + }; + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif //QNETWORKSESSION_H diff --git a/src/network/bearer/qnetworksession_p.h b/src/network/bearer/qnetworksession_p.h new file mode 100644 index 0000000..76691b3 --- /dev/null +++ b/src/network/bearer/qnetworksession_p.h @@ -0,0 +1,150 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtNetwork module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QNETWORKSESSIONPRIVATE_H +#define QNETWORKSESSIONPRIVATE_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "qnetworksession.h" +#include "qnetworkconfiguration_p.h" + +#include <QNetworkInterface> + +QT_BEGIN_NAMESPACE + +class Q_NETWORK_EXPORT QNetworkSessionPrivate : public QObject +{ + Q_OBJECT + + friend class QNetworkSession; + +public: + QNetworkSessionPrivate() + : state(QNetworkSession::Invalid), isOpen(false) + { + } + + virtual ~QNetworkSessionPrivate() + { + } + + //called by QNetworkSession constructor and ensures + //that the state is immediately updated (w/o actually opening + //a session). Also this function should take care of + //notification hooks to discover future state changes. + virtual void syncStateWithInterface() = 0; + + virtual QNetworkInterface currentInterface() const = 0; + virtual QVariant sessionProperty(const QString& key) const = 0; + virtual void setSessionProperty(const QString& key, const QVariant& value) = 0; + + virtual void open() = 0; + virtual void close() = 0; + virtual void stop() = 0; + + virtual void setALREnabled(bool /*enabled*/) { } + virtual void migrate() = 0; + virtual void accept() = 0; + virtual void ignore() = 0; + virtual void reject() = 0; + + virtual QString errorString() const = 0; //must return translated string + virtual QNetworkSession::SessionError error() const = 0; + + virtual quint64 bytesWritten() const = 0; + virtual quint64 bytesReceived() const = 0; + virtual quint64 activeTime() const = 0; + +protected: + inline QNetworkConfigurationPrivatePointer privateConfiguration(const QNetworkConfiguration &config) const + { + return config.d; + } + + inline void setPrivateConfiguration(QNetworkConfiguration &config, + QNetworkConfigurationPrivatePointer ptr) const + { + config.d = ptr; + } + +Q_SIGNALS: + //releases any pending waitForOpened() calls + void quitPendingWaitsForOpened(); + + void error(QNetworkSession::SessionError error); + void stateChanged(QNetworkSession::State state); + void closed(); + void newConfigurationActivated(); + void preferredConfigurationChanged(const QNetworkConfiguration &config, bool isSeamless); + +protected: + // The config set on QNetworkSession. + QNetworkConfiguration publicConfig; + + // If publicConfig is a ServiceNetwork this is a copy of publicConfig. + // If publicConfig is an UserChoice that is resolved to a ServiceNetwork this is the actual + // ServiceNetwork configuration. + QNetworkConfiguration serviceConfig; + + // This is the actual active configuration currently in use by the session. + // Either a copy of publicConfig or one of serviceConfig.children(). + QNetworkConfiguration activeConfig; + + QNetworkSession::State state; + bool isOpen; + + QNetworkSession *q; +}; + +QT_END_NAMESPACE + +#endif //QNETWORKSESSIONPRIVATE_H + diff --git a/src/network/network.pro b/src/network/network.pro index e890b94..8582d8a 100644 --- a/src/network/network.pro +++ b/src/network/network.pro @@ -17,6 +17,7 @@ unix:QMAKE_PKGCONFIG_REQUIRES = QtCore include(../qbase.pri) include(access/access.pri) +include(bearer/bearer.pri) include(kernel/kernel.pri) include(socket/socket.pri) include(ssl/ssl.pri) |