| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the QtNetwork module of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 10 | ** Commercial Usage
|
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 14 | ** a written agreement between you and Nokia.
|
|---|
| 15 | **
|
|---|
| 16 | ** GNU Lesser General Public License Usage
|
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 20 | ** packaging of this file. Please review the following information to
|
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 23 | **
|
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
|---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
|---|
| 37 | ** Nokia at qt-info@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include "qnetworkconfigmanager.h"
|
|---|
| 43 |
|
|---|
| 44 | #include "qnetworkconfigmanager_p.h"
|
|---|
| 45 | #include "qbearerengine_p.h"
|
|---|
| 46 |
|
|---|
| 47 | #include <QtCore/qstringlist.h>
|
|---|
| 48 | #include <QtCore/qcoreapplication.h>
|
|---|
| 49 |
|
|---|
| 50 | #ifndef QT_NO_BEARERMANAGEMENT
|
|---|
| 51 |
|
|---|
| 52 | QT_BEGIN_NAMESPACE
|
|---|
| 53 |
|
|---|
| 54 | #define Q_GLOBAL_STATIC_QAPP_DESTRUCTION(TYPE, NAME) \
|
|---|
| 55 | Q_GLOBAL_STATIC_INIT(TYPE, NAME); \
|
|---|
| 56 | static void NAME##_cleanup() \
|
|---|
| 57 | { \
|
|---|
| 58 | delete this_##NAME.pointer; \
|
|---|
| 59 | this_##NAME.pointer = 0; \
|
|---|
| 60 | this_##NAME.destroyed = true; \
|
|---|
| 61 | } \
|
|---|
| 62 | static TYPE *NAME() \
|
|---|
| 63 | { \
|
|---|
| 64 | if (!this_##NAME.pointer && !this_##NAME.destroyed) { \
|
|---|
| 65 | TYPE *x = new TYPE; \
|
|---|
| 66 | if (!this_##NAME.pointer.testAndSetOrdered(0, x)) \
|
|---|
| 67 | delete x; \
|
|---|
| 68 | else \
|
|---|
| 69 | qAddPostRoutine(NAME##_cleanup); \
|
|---|
| 70 | } \
|
|---|
| 71 | return this_##NAME.pointer; \
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | Q_GLOBAL_STATIC_QAPP_DESTRUCTION(QNetworkConfigurationManagerPrivate, connManager);
|
|---|
| 75 |
|
|---|
| 76 | QNetworkConfigurationManagerPrivate *qNetworkConfigurationManagerPrivate()
|
|---|
| 77 | {
|
|---|
| 78 | static bool initialized = false;
|
|---|
| 79 |
|
|---|
| 80 | QNetworkConfigurationManagerPrivate *m = connManager();
|
|---|
| 81 | if (!initialized) {
|
|---|
| 82 | initialized = true;
|
|---|
| 83 | m->updateConfigurations();
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | return m;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | /*!
|
|---|
| 90 | \class QNetworkConfigurationManager
|
|---|
| 91 |
|
|---|
| 92 | \brief The QNetworkConfigurationManager class manages the network configurations provided
|
|---|
| 93 | by the system.
|
|---|
| 94 |
|
|---|
| 95 | \since 4.7
|
|---|
| 96 |
|
|---|
| 97 | \inmodule QtNetwork
|
|---|
| 98 | \ingroup network
|
|---|
| 99 |
|
|---|
| 100 | QNetworkConfigurationManager provides access to the network configurations known to the system and
|
|---|
| 101 | enables applications to detect the system capabilities (with regards to network sessions) at runtime.
|
|---|
| 102 |
|
|---|
| 103 | A QNetworkConfiguration abstracts a set of configuration options describing how a
|
|---|
| 104 | network interface has to be configured to connect to a particular target network.
|
|---|
| 105 | QNetworkConfigurationManager maintains and updates the global list of
|
|---|
| 106 | QNetworkConfigurations. Applications can access and filter this list via
|
|---|
| 107 | allConfigurations(). If a new configuration is added or an existing one is removed or changed
|
|---|
| 108 | the configurationAdded(), configurationRemoved() and configurationChanged() signals are emitted
|
|---|
| 109 | respectively.
|
|---|
| 110 |
|
|---|
| 111 | The defaultConfiguration() can be used when intending to immediately create a new
|
|---|
| 112 | network session without caring about the particular configuration. It returns
|
|---|
| 113 | a \l QNetworkConfiguration::Discovered configuration. If there are not any
|
|---|
| 114 | discovered ones an invalid configuration is returned.
|
|---|
| 115 |
|
|---|
| 116 | Some configuration updates may require some time to perform updates. A WLAN scan is
|
|---|
| 117 | such an example. Unless the platform performs internal updates it may be required to
|
|---|
| 118 | manually trigger configuration updates via QNetworkConfigurationManager::updateConfigurations().
|
|---|
| 119 | The completion of the update process is indicted by emitting the updateCompleted()
|
|---|
| 120 | signal. The update process ensures that every existing QNetworkConfiguration instance
|
|---|
| 121 | is updated. There is no need to ask for a renewed configuration list via allConfigurations().
|
|---|
| 122 |
|
|---|
| 123 | \sa QNetworkConfiguration
|
|---|
| 124 | */
|
|---|
| 125 |
|
|---|
| 126 | /*!
|
|---|
| 127 | \fn void QNetworkConfigurationManager::configurationAdded(const QNetworkConfiguration& config)
|
|---|
| 128 |
|
|---|
| 129 | This signal is emitted whenever a new network configuration is added to the system. The new
|
|---|
| 130 | configuration is specified by \a config.
|
|---|
| 131 | */
|
|---|
| 132 |
|
|---|
| 133 | /*!
|
|---|
| 134 | \fn void QNetworkConfigurationManager::configurationRemoved(const QNetworkConfiguration& configuration)
|
|---|
| 135 |
|
|---|
| 136 | This signal is emitted when a configuration is about to be removed from the system. The removed
|
|---|
| 137 | \a configuration is invalid but retains name and identifier.
|
|---|
| 138 | */
|
|---|
| 139 |
|
|---|
| 140 | /*!
|
|---|
| 141 | \fn void QNetworkConfigurationManager::updateCompleted()
|
|---|
| 142 |
|
|---|
| 143 | This signal is emitted when the configuration update has been completed. Such an update can
|
|---|
| 144 | be initiated via \l updateConfigurations().
|
|---|
| 145 | */
|
|---|
| 146 |
|
|---|
| 147 | /*! \fn void QNetworkConfigurationManager::configurationChanged(const QNetworkConfiguration& config)
|
|---|
| 148 |
|
|---|
| 149 | This signal is emitted when the \l {QNetworkConfiguration::state()}{state} of \a config changes.
|
|---|
| 150 | */
|
|---|
| 151 |
|
|---|
| 152 | /*!
|
|---|
| 153 | \fn void QNetworkConfigurationManager::onlineStateChanged(bool isOnline)
|
|---|
| 154 |
|
|---|
| 155 | This signal is emitted when the device changes from online to offline mode or vice versa.
|
|---|
| 156 | \a isOnline represents the new state of the device.
|
|---|
| 157 |
|
|---|
| 158 | The state is considered to be online for as long as
|
|---|
| 159 | \l{allConfigurations()}{allConfigurations}(QNetworkConfiguration::Active) returns a list with
|
|---|
| 160 | at least one entry.
|
|---|
| 161 | */
|
|---|
| 162 |
|
|---|
| 163 | /*!
|
|---|
| 164 | \enum QNetworkConfigurationManager::Capability
|
|---|
| 165 |
|
|---|
| 166 | Specifies the system capabilities of the bearer API. The possible values are:
|
|---|
| 167 |
|
|---|
| 168 | \value CanStartAndStopInterfaces Network sessions and their underlying access points can be
|
|---|
| 169 | started and stopped. If this flag is not set QNetworkSession
|
|---|
| 170 | can only monitor but not influence the state of access points.
|
|---|
| 171 | On some platforms this feature may require elevated user
|
|---|
| 172 | permissions. This option is platform specific and may not
|
|---|
| 173 | always be available.
|
|---|
| 174 | \value DirectConnectionRouting Network sessions and their sockets can be bound to a
|
|---|
| 175 | particular network interface. Any packet that passes through
|
|---|
| 176 | the socket goes to the specified network interface and thus
|
|---|
| 177 | disregards standard routing table entries. This may be useful
|
|---|
| 178 | when two interfaces can reach overlapping IP ranges or an
|
|---|
| 179 | application has specific needs in regards to target networks.
|
|---|
| 180 | This option is platform specific and may not always be
|
|---|
| 181 | available.
|
|---|
| 182 | \value SystemSessionSupport If this flag is set the underlying platform ensures that a
|
|---|
| 183 | network interface is not shut down until the last network
|
|---|
| 184 | session has been \l{QNetworkSession::close()}{closed()}. This
|
|---|
| 185 | works across multiple processes. If the platform session
|
|---|
| 186 | support is missing this API can only ensure the above behavior
|
|---|
| 187 | for network sessions within the same process.
|
|---|
| 188 | In general mobile platforms (such as Symbian/S60) have such
|
|---|
| 189 | support whereas most desktop platform lack this capability.
|
|---|
| 190 | \value ApplicationLevelRoaming The system gives applications control over the systems roaming
|
|---|
| 191 | behavior. Applications can initiate roaming (in case the
|
|---|
| 192 | current link is not suitable) and are consulted if the system
|
|---|
| 193 | has identified a more suitable access point.
|
|---|
| 194 | \value ForcedRoaming The system disconnects an existing access point and reconnects
|
|---|
| 195 | via a more suitable one. The application does not have any
|
|---|
| 196 | control over this process and has to reconnect its active
|
|---|
| 197 | sockets.
|
|---|
| 198 | \value DataStatistics If this flag is set QNetworkSession can provide statistics
|
|---|
| 199 | about transmitted and received data.
|
|---|
| 200 | \value NetworkSessionRequired If this flag is set the platform requires that a network
|
|---|
| 201 | session is created before network operations can be performed.
|
|---|
| 202 | */
|
|---|
| 203 |
|
|---|
| 204 | /*!
|
|---|
| 205 | Constructs a QNetworkConfigurationManager with the given \a parent.
|
|---|
| 206 | */
|
|---|
| 207 | QNetworkConfigurationManager::QNetworkConfigurationManager( QObject* parent )
|
|---|
| 208 | : QObject(parent)
|
|---|
| 209 | {
|
|---|
| 210 | QNetworkConfigurationManagerPrivate *priv = qNetworkConfigurationManagerPrivate();
|
|---|
| 211 |
|
|---|
| 212 | connect(priv, SIGNAL(configurationAdded(QNetworkConfiguration)),
|
|---|
| 213 | this, SIGNAL(configurationAdded(QNetworkConfiguration)));
|
|---|
| 214 | connect(priv, SIGNAL(configurationRemoved(QNetworkConfiguration)),
|
|---|
| 215 | this, SIGNAL(configurationRemoved(QNetworkConfiguration)));
|
|---|
| 216 | connect(priv, SIGNAL(configurationUpdateComplete()),
|
|---|
| 217 | this, SIGNAL(updateCompleted()));
|
|---|
| 218 | connect(priv, SIGNAL(onlineStateChanged(bool)),
|
|---|
| 219 | this, SIGNAL(onlineStateChanged(bool)));
|
|---|
| 220 | connect(priv, SIGNAL(configurationChanged(QNetworkConfiguration)),
|
|---|
| 221 | this, SIGNAL(configurationChanged(QNetworkConfiguration)));
|
|---|
| 222 |
|
|---|
| 223 | priv->enablePolling();
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | /*!
|
|---|
| 227 | Frees the resources associated with the QNetworkConfigurationManager object.
|
|---|
| 228 | */
|
|---|
| 229 | QNetworkConfigurationManager::~QNetworkConfigurationManager()
|
|---|
| 230 | {
|
|---|
| 231 | QNetworkConfigurationManagerPrivate *priv = connManager();
|
|---|
| 232 | if (priv)
|
|---|
| 233 | priv->disablePolling();
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 | /*!
|
|---|
| 238 | Returns the default configuration to be used. This function always returns a discovered
|
|---|
| 239 | configuration; otherwise an invalid configuration.
|
|---|
| 240 |
|
|---|
| 241 | In some cases it may be required to call updateConfigurations() and wait for the
|
|---|
| 242 | updateCompleted() signal before calling this function.
|
|---|
| 243 |
|
|---|
| 244 | \sa allConfigurations()
|
|---|
| 245 | */
|
|---|
| 246 | QNetworkConfiguration QNetworkConfigurationManager::defaultConfiguration() const
|
|---|
| 247 | {
|
|---|
| 248 | QNetworkConfigurationManagerPrivate *priv = connManager();
|
|---|
| 249 | if (priv)
|
|---|
| 250 | return priv->defaultConfiguration();
|
|---|
| 251 |
|
|---|
| 252 | return QNetworkConfiguration();
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | /*!
|
|---|
| 256 | Returns the list of configurations which comply with the given \a filter.
|
|---|
| 257 |
|
|---|
| 258 | By default this function returns all (defined and undefined) configurations.
|
|---|
| 259 |
|
|---|
| 260 | A wireless network with a particular SSID may only be accessible in a
|
|---|
| 261 | certain area despite the fact that the system has a valid configuration
|
|---|
| 262 | for it. Therefore the filter flag may be used to limit the list to
|
|---|
| 263 | discovered and possibly connected configurations only.
|
|---|
| 264 |
|
|---|
| 265 | If \a filter is set to zero this function returns all possible configurations.
|
|---|
| 266 |
|
|---|
| 267 | Note that this function returns the states for all configurations as they are known at
|
|---|
| 268 | the time of this function call. If for instance a configuration of type WLAN is defined
|
|---|
| 269 | the system may have to perform a WLAN scan in order to determine whether it is
|
|---|
| 270 | actually available. To obtain the most accurate state updateConfigurations() should
|
|---|
| 271 | be used to update each configuration's state. Note that such an update may require
|
|---|
| 272 | some time. It's completion is signalled by updateCompleted(). In the absence of a
|
|---|
| 273 | configuration update this function returns the best estimate at the time of the call.
|
|---|
| 274 | Therefore, if WLAN configurations are of interest, it is recommended that
|
|---|
| 275 | updateConfigurations() is called once after QNetworkConfigurationManager
|
|---|
| 276 | instantiation (WLAN scans are too time consuming to perform in constructor).
|
|---|
| 277 | After this the data is kept automatically up-to-date as the system reports
|
|---|
| 278 | any changes.
|
|---|
| 279 | */
|
|---|
| 280 | QList<QNetworkConfiguration> QNetworkConfigurationManager::allConfigurations(QNetworkConfiguration::StateFlags filter) const
|
|---|
| 281 | {
|
|---|
| 282 | QNetworkConfigurationManagerPrivate *priv = connManager();
|
|---|
| 283 | if (priv)
|
|---|
| 284 | return priv->allConfigurations(filter);
|
|---|
| 285 |
|
|---|
| 286 | return QList<QNetworkConfiguration>();
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | /*!
|
|---|
| 290 | Returns the QNetworkConfiguration for \a identifier; otherwise returns an
|
|---|
| 291 | invalid QNetworkConfiguration.
|
|---|
| 292 |
|
|---|
| 293 | \sa QNetworkConfiguration::identifier()
|
|---|
| 294 | */
|
|---|
| 295 | QNetworkConfiguration QNetworkConfigurationManager::configurationFromIdentifier(const QString &identifier) const
|
|---|
| 296 | {
|
|---|
| 297 | QNetworkConfigurationManagerPrivate *priv = connManager();
|
|---|
| 298 | if (priv)
|
|---|
| 299 | return priv->configurationFromIdentifier(identifier);
|
|---|
| 300 |
|
|---|
| 301 | return QNetworkConfiguration();
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | /*!
|
|---|
| 305 | Returns true if the system is considered to be connected to another device via an active
|
|---|
| 306 | network interface; otherwise returns false.
|
|---|
| 307 |
|
|---|
| 308 | This is equivalent to the following code snippet:
|
|---|
| 309 |
|
|---|
| 310 | \snippet doc/src/snippets/code/src_network_bearer_qnetworkconfigmanager.cpp 0
|
|---|
| 311 |
|
|---|
| 312 | \sa onlineStateChanged()
|
|---|
| 313 | */
|
|---|
| 314 | bool QNetworkConfigurationManager::isOnline() const
|
|---|
| 315 | {
|
|---|
| 316 | QNetworkConfigurationManagerPrivate *priv = connManager();
|
|---|
| 317 | if (priv)
|
|---|
| 318 | return priv->isOnline();
|
|---|
| 319 |
|
|---|
| 320 | return false;
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | /*!
|
|---|
| 324 | Returns the capabilities supported by the current platform.
|
|---|
| 325 | */
|
|---|
| 326 | QNetworkConfigurationManager::Capabilities QNetworkConfigurationManager::capabilities() const
|
|---|
| 327 | {
|
|---|
| 328 | QNetworkConfigurationManagerPrivate *priv = connManager();
|
|---|
| 329 | if (priv)
|
|---|
| 330 | return priv->capabilities();
|
|---|
| 331 |
|
|---|
| 332 | return QNetworkConfigurationManager::Capabilities(0);
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | /*!
|
|---|
| 336 | Initiates an update of all configurations. This may be used to initiate WLAN scans or other
|
|---|
| 337 | time consuming updates which may be required to obtain the correct state for configurations.
|
|---|
| 338 |
|
|---|
| 339 | This call is asynchronous. On completion of this update the updateCompleted() signal is
|
|---|
| 340 | emitted. If new configurations are discovered or old ones were removed or changed the update
|
|---|
| 341 | process may trigger the emission of one or multiple configurationAdded(),
|
|---|
| 342 | configurationRemoved() and configurationChanged() signals.
|
|---|
| 343 |
|
|---|
| 344 | If a configuration state changes as a result of this update all existing QNetworkConfiguration
|
|---|
| 345 | instances are updated automatically.
|
|---|
| 346 |
|
|---|
| 347 | \sa allConfigurations()
|
|---|
| 348 | */
|
|---|
| 349 | void QNetworkConfigurationManager::updateConfigurations()
|
|---|
| 350 | {
|
|---|
| 351 | QNetworkConfigurationManagerPrivate *priv = connManager();
|
|---|
| 352 | if (priv)
|
|---|
| 353 | priv->performAsyncConfigurationUpdate();
|
|---|
| 354 | }
|
|---|
| 355 |
|
|---|
| 356 | #include "moc_qnetworkconfigmanager.cpp"
|
|---|
| 357 |
|
|---|
| 358 | QT_END_NAMESPACE
|
|---|
| 359 |
|
|---|
| 360 | #endif // QT_NO_BEARERMANAGEMENT
|
|---|