source: trunk/src/network/bearer/qnetworkconfiguration.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 18.9 KB
Line 
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 "qnetworkconfiguration.h"
43#include "qnetworkconfiguration_p.h"
44
45QT_BEGIN_NAMESPACE
46
47/*!
48 \class QNetworkConfiguration
49
50 \brief The QNetworkConfiguration class provides an abstraction of one or more access point configurations.
51
52 \since 4.7
53
54 \inmodule QtNetwork
55 \ingroup network
56
57 QNetworkConfiguration encapsulates a single access point or service network.
58 In most cases a single access point configuration can be mapped to one network
59 interface. However a single network interface may not always map to only one
60 access point configuration. Multiple configurations for the same
61 network device may enable multiple access points. An example
62 device that could exhibit such a configuration might be a
63 Smartphone which allows the user to manage multiple WLAN
64 configurations while the device itself has only one WLAN network device.
65
66 The QNetworkConfiguration also supports the concept of service networks.
67 This concept allows the grouping of multiple access point configurations
68 into one entity. Such a group is called service network and can be
69 beneficial in cases whereby a network session to a
70 particular destination network is required (e.g. a company network).
71 When using a service network the user doesn't usually care which one of the
72 connectivity options is chosen (e.g. corporate WLAN or VPN via GPRS)
73 as long as he can reach the company's target server. Depending
74 on the current position and time some of the access points that make
75 up the service network may not even be available. Furthermore
76 automated access point roaming can be enabled which enables the device
77 to change the network interface configuration dynamically while maintaining
78 the applications connection to the target network. It allows adaption
79 to the changing environment and may enable optimization with regards to
80 cost, speed or other network parameters.
81
82 Special configurations of type UserChoice provide a placeholder configuration which is
83 resolved to an actual network configuration by the platform when a
84 \l {QNetworkSession}{session} is \l {QNetworkSession::open()}{opened}. Not all platforms
85 support the concept of a user choice configuration.
86
87 \section1 Configuration states
88
89 The list of available configurations can be obtained via
90 QNetworkConfigurationManager::allConfigurations(). A configuration can have
91 multiple states. The \l Defined configuration state indicates that the configuration
92 is stored on the device. However the configuration is not yet ready to be activated
93 as e.g. a WLAN may not be available at the current time.
94
95 The \l Discovered state implies that the configuration is \l Defined and
96 the outside conditions are such that the configuration can be used immediately
97 to open a new network session. An example of such an outside condition may be
98 that the Ethernet cable is actually connected to the device or that the WLAN
99 with the specified SSID is in range.
100
101 The \l Active state implies that the configuration is \l Discovered. A configuration
102 in this state is currently being used by an application. The underlying network
103 interface has a valid IP configuration and can transfer IP packets between the
104 device and the target network.
105
106 The \l Undefined state indicates that the system has knowledge of possible target
107 networks but cannot actually use that knowledge to connect to it. An example
108 for such a state could be an encrypted WLAN that has been discovered
109 but the user hasn't actually saved a configuration including the required password
110 which would allow the device to connect to it.
111
112 Depending on the type of configuration some states are transient in nature. A GPRS/UMTS
113 connection may almost always be \l Discovered if the GSM/UMTS network is available.
114 However if the GSM/UMTS network looses the connection the associated configuration may change its state
115 from \l Discovered to \l Defined as well. A similar use case might be triggered by
116 WLAN availability. QNetworkConfigurationManager::updateConfigurations() can be used to
117 manually trigger updates of states. Note that some platforms do not require such updates
118 as they implicitly change the state once it has been discovered. If the state of a
119 configuration changes all related QNetworkConfiguration instances change their state automatically.
120
121 \sa QNetworkSession, QNetworkConfigurationManager
122*/
123
124/*!
125 \enum QNetworkConfiguration::Type
126
127 This enum describes the type of configuration.
128
129 \value InternetAccessPoint The configuration specifies the details for a single access point.
130 Note that configurations of type InternetAccessPoint may be part
131 of other QNetworkConfigurations of type ServiceNetwork.
132 \value ServiceNetwork The configuration is based on a group of QNetworkConfigurations of
133 type InternetAccessPoint. All group members can reach the same
134 target network. This type of configuration is a mandatory
135 requirement for roaming enabled network sessions. On some
136 platforms this form of configuration may also be called Service
137 Network Access Point (SNAP).
138 \value UserChoice The configuration is a placeholder which will be resolved to an
139 actual configuration by the platform when a session is opened. Depending
140 on the platform the selection may generate a popup dialog asking the user
141 for his preferred choice.
142 \value Invalid The configuration is invalid.
143*/
144
145/*!
146 \enum QNetworkConfiguration::StateFlag
147
148 Specifies the configuration states.
149
150 \value Undefined This state is used for transient configurations such as newly discovered
151 WLANs for which the user has not actually created a configuration yet.
152 \value Defined Defined configurations are known to the system but are not immediately
153 usable (e.g. a configured WLAN is not within range or the Ethernet cable
154 is currently not plugged into the machine).
155 \value Discovered A discovered configuration can be immediately used to create a new
156 QNetworkSession. An example of a discovered configuration could be a WLAN
157 which is within in range. If the device moves out of range the discovered
158 flag is dropped. A second example is a GPRS configuration which generally
159 remains discovered for as long as the device has network coverage. A
160 configuration that has this state is also in state
161 QNetworkConfiguration::Defined. If the configuration is a service network
162 this flag is set if at least one of the underlying access points
163 configurations has the Discovered state.
164 \value Active The configuration is currently used by an open network session
165 (see \l QNetworkSession::isOpen()). However this does not mean that the
166 current process is the entity that created the open session. It merely
167 indicates that if a new QNetworkSession were to be constructed based on
168 this configuration \l QNetworkSession::state() would return
169 \l QNetworkSession::Connected. This state implies the
170 QNetworkConfiguration::Discovered state.
171*/
172
173/*!
174 \enum QNetworkConfiguration::Purpose
175
176 Specifies the purpose of the configuration.
177
178 \value UnknownPurpose The configuration doesn't specify any purpose. This is the default value.
179 \value PublicPurpose The configuration can be used for general purpose internet access.
180 \value PrivatePurpose The configuration is suitable to access a private network such as an office Intranet.
181 \value ServiceSpecificPurpose The configuration can be used for operator specific services (e.g.
182 receiving MMS messages or content streaming).
183*/
184
185/*!
186 \enum QNetworkConfiguration::BearerType
187
188 Specifies the type of bearer used by a configuration.
189
190 \value BearerUnknown The type of bearer is unknown or unspecified. The bearerTypeName()
191 function may return additional information.
192 \value BearerEthernet The configuration is for an Ethernet interfaces.
193 \value BearerWLAN The configuration is for a Wireless LAN interface.
194 \value Bearer2G The configuration is for a CSD, GPRS, HSCSD, EDGE or cdmaOne interface.
195 \value BearerCDMA2000 The configuration is for CDMA interface.
196 \value BearerWCDMA The configuration is for W-CDMA/UMTS interface.
197 \value BearerHSPA The configuration is for High Speed Packet Access (HSPA) interface.
198 \value BearerBluetooth The configuration is for a Bluetooth interface.
199 \value BearerWiMAX The configuration is for a WiMAX interface.
200*/
201
202/*!
203 Constructs an invalid configuration object.
204
205 \sa isValid()
206*/
207QNetworkConfiguration::QNetworkConfiguration()
208 : d(0)
209{
210}
211
212/*!
213 Creates a copy of the QNetworkConfiguration object contained in \a other.
214*/
215QNetworkConfiguration::QNetworkConfiguration(const QNetworkConfiguration& other)
216 : d(other.d)
217{
218}
219
220/*!
221 Copies the content of the QNetworkConfiguration object contained in \a other into this one.
222*/
223QNetworkConfiguration& QNetworkConfiguration::operator=(const QNetworkConfiguration& other)
224{
225 d = other.d;
226 return *this;
227}
228
229/*!
230 Frees the resources associated with the QNetworkConfiguration object.
231*/
232QNetworkConfiguration::~QNetworkConfiguration()
233{
234}
235
236/*!
237 Returns true, if this configuration is the same as the \a other
238 configuration given; otherwise returns false.
239*/
240bool QNetworkConfiguration::operator==(const QNetworkConfiguration& other) const
241{
242 if (!d)
243 return !other.d;
244
245 if (!other.d)
246 return false;
247
248 return (d == other.d);
249}
250
251/*!
252 \fn bool QNetworkConfiguration::operator!=(const QNetworkConfiguration& other) const
253
254 Returns true if this configuration is not the same as the \a other
255 configuration given; otherwise returns false.
256*/
257
258/*!
259 Returns the user visible name of this configuration.
260
261 The name may either be the name of the underlying access point or the
262 name for service network that this configuration represents.
263*/
264QString QNetworkConfiguration::name() const
265{
266 if (!d)
267 return QString();
268
269 QMutexLocker locker(&d->mutex);
270 return d->name;
271}
272
273/*!
274 Returns the unique and platform specific identifier for this network configuration;
275 otherwise an empty string.
276*/
277QString QNetworkConfiguration::identifier() const
278{
279 if (!d)
280 return QString();
281
282 QMutexLocker locker(&d->mutex);
283 return d->id;
284}
285
286/*!
287 Returns the type of the configuration.
288
289 A configuration can represent a single access point configuration or
290 a set of access point configurations. Such a set is called service network.
291 A configuration that is based on a service network can potentially support
292 roaming of network sessions.
293*/
294QNetworkConfiguration::Type QNetworkConfiguration::type() const
295{
296 if (!d)
297 return QNetworkConfiguration::Invalid;
298
299 QMutexLocker locker(&d->mutex);
300 return d->type;
301}
302
303/*!
304 Returns true if this QNetworkConfiguration object is valid.
305 A configuration may become invalid if the user deletes the configuration or
306 the configuration was default-constructed.
307
308 The addition and removal of configurations can be monitored via the
309 QNetworkConfigurationManager.
310
311 \sa QNetworkConfigurationManager
312*/
313bool QNetworkConfiguration::isValid() const
314{
315 if (!d)
316 return false;
317
318 QMutexLocker locker(&d->mutex);
319 return d->isValid;
320}
321
322/*!
323 Returns the current state of the configuration.
324*/
325QNetworkConfiguration::StateFlags QNetworkConfiguration::state() const
326{
327 if (!d)
328 return QNetworkConfiguration::Undefined;
329
330 QMutexLocker locker(&d->mutex);
331 return d->state;
332}
333
334/*!
335 Returns the purpose of this configuration.
336
337 The purpose field may be used to programmatically determine the
338 purpose of a configuration. Such information is usually part of the
339 access point or service network meta data.
340*/
341QNetworkConfiguration::Purpose QNetworkConfiguration::purpose() const
342{
343 if (!d)
344 return QNetworkConfiguration::UnknownPurpose;
345
346 QMutexLocker locker(&d->mutex);
347 return d->purpose;
348}
349
350/*!
351 Returns true if this configuration supports roaming; otherwise false.
352*/
353bool QNetworkConfiguration::isRoamingAvailable() const
354{
355 if (!d)
356 return false;
357
358 QMutexLocker locker(&d->mutex);
359 return d->roamingSupported;
360}
361
362/*!
363 Returns all sub configurations of this network configuration in priority order. The first sub
364 configuration in the list has the highest priority.
365
366 Only network configurations of type \l ServiceNetwork can have children. Otherwise this
367 function returns an empty list.
368*/
369QList<QNetworkConfiguration> QNetworkConfiguration::children() const
370{
371 QList<QNetworkConfiguration> results;
372
373 if (type() != QNetworkConfiguration::ServiceNetwork || !isValid())
374 return results;
375
376 QMutexLocker locker(&d->mutex);
377
378 QMutableMapIterator<unsigned int, QNetworkConfigurationPrivatePointer> i(d->serviceNetworkMembers);
379 while (i.hasNext()) {
380 i.next();
381
382 QNetworkConfigurationPrivatePointer p = i.value();
383
384 //if we have an invalid member get rid of it -> was deleted earlier on
385 {
386 QMutexLocker childLocker(&p->mutex);
387
388 if (!p->isValid) {
389 i.remove();
390 continue;
391 }
392 }
393
394 QNetworkConfiguration item;
395 item.d = p;
396 results << item;
397 }
398
399 return results;
400}
401
402/*!
403 \fn QString QNetworkConfiguration::bearerName() const
404 \deprecated
405
406 This function is deprecated. It is equivalent to calling bearerTypeName(), however
407 bearerType() should be used in preference.
408*/
409
410/*!
411 Returns the type of bearer used by this network configuration.
412
413 If the bearer type is \l {QNetworkConfiguration::BearerUnknown}{unknown} the bearerTypeName()
414 function can be used to retrieve a textural type name for the bearer.
415
416 An invalid network configuration always returns the BearerUnknown value.
417*/
418QNetworkConfiguration::BearerType QNetworkConfiguration::bearerType() const
419{
420 if (!isValid())
421 return BearerUnknown;
422
423 QMutexLocker locker(&d->mutex);
424
425 return d->bearerType;
426}
427
428/*!
429 Returns the type of bearer used by this network configuration as a string.
430
431 The string is not translated and therefore can not be shown to the user. The subsequent table
432 shows the fixed mappings between BearerType and the bearer type name for known types. If the
433 BearerType is unknown this function may return additional information if it is available;
434 otherwise an empty string will be returned.
435
436 \table
437 \header
438 \o BearerType
439 \o Value
440 \row
441 \o BearerUnknown
442 \o
443 \o The session is based on an unknown or unspecified bearer type. The value of the
444 string returned describes the bearer type.
445 \row
446 \o BearerEthernet
447 \o Ethernet
448 \row
449 \o BearerWLAN
450 \o WLAN
451 \row
452 \o Bearer2G
453 \o 2G
454 \row
455 \o BearerCDMA2000
456 \o CDMA2000
457 \row
458 \o BearerWCDMA
459 \o WCDMA
460 \row
461 \o BearerHSPA
462 \o HSPA
463 \row
464 \o BearerBluetooth
465 \o Bluetooth
466 \row
467 \o BearerWiMAX
468 \o WiMAX
469 \endtable
470
471 This function returns an empty string if this is an invalid configuration, a network
472 configuration of type \l QNetworkConfiguration::ServiceNetwork or
473 \l QNetworkConfiguration::UserChoice.
474
475 \sa bearerType()
476*/
477QString QNetworkConfiguration::bearerTypeName() const
478{
479 if (!isValid())
480 return QString();
481
482 QMutexLocker locker(&d->mutex);
483
484 if (d->type == QNetworkConfiguration::ServiceNetwork ||
485 d->type == QNetworkConfiguration::UserChoice)
486 return QString();
487
488 switch (d->bearerType) {
489 case BearerUnknown:
490 return d->bearerTypeName();
491 case BearerEthernet:
492 return QLatin1String("Ethernet");
493 case BearerWLAN:
494 return QLatin1String("WLAN");
495 case Bearer2G:
496 return QLatin1String("2G");
497 case BearerCDMA2000:
498 return QLatin1String("CDMA2000");
499 case BearerWCDMA:
500 return QLatin1String("WCDMA");
501 case BearerHSPA:
502 return QLatin1String("HSPA");
503 case BearerBluetooth:
504 return QLatin1String("Bluetooth");
505 case BearerWiMAX:
506 return QLatin1String("WiMAX");
507 }
508
509 return QLatin1String("Unknown");
510}
511
512QT_END_NAMESPACE
513
Note: See TracBrowser for help on using the repository browser.