Qt 4.8
qnetworksession_impl.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qnetworksession_impl.h"
43 #include "qbearerengine_impl.h"
44 
45 #include <QtNetwork/qnetworksession.h>
46 #include <QtNetwork/private/qnetworkconfigmanager_p.h>
47 
48 #include <QtCore/qdatetime.h>
49 #include <QtCore/qdebug.h>
50 #include <QtCore/qmutex.h>
51 #include <QtCore/qstringlist.h>
52 
53 #ifndef QT_NO_BEARERMANAGEMENT
54 
56 
58 {
60 
61  foreach (QBearerEngine *engine, priv->engines()) {
62  QBearerEngineImpl *engineImpl = qobject_cast<QBearerEngineImpl *>(engine);
63  if (engineImpl && engineImpl->hasIdentifier(id))
64  return engineImpl;
65  }
66 
67  return 0;
68 }
69 
71 {
72  Q_OBJECT
73 
74 public:
77 
78  inline void forceSessionClose(const QNetworkConfiguration &config)
79  { emit forcedSessionClose(config); }
80 
81 Q_SIGNALS:
82  void forcedSessionClose(const QNetworkConfiguration &config);
83 };
84 
85 #include "qnetworksession_impl.moc"
86 
88 
90 {
93 
94  opened = false;
95  isOpen = false;
98 
99  qRegisterMetaType<QBearerEngineImpl::ConnectionError>("QBearerEngineImpl::ConnectionError");
100 
101  switch (publicConfig.type()) {
103  activeConfig = publicConfig;
104  engine = getEngineFromId(activeConfig.identifier());
105  if (engine) {
106  qRegisterMetaType<QNetworkConfigurationPrivatePointer>("QNetworkConfigurationPrivatePointer");
107  connect(engine, SIGNAL(configurationChanged(QNetworkConfigurationPrivatePointer)),
108  this, SLOT(configurationChanged(QNetworkConfigurationPrivatePointer)),
110  connect(engine, SIGNAL(connectionError(QString,QBearerEngineImpl::ConnectionError)),
111  this, SLOT(connectionError(QString,QBearerEngineImpl::ConnectionError)),
113  }
114  break;
116  serviceConfig = publicConfig;
117  // Defer setting engine and signals until open().
118  // fall through
120  // Defer setting serviceConfig and activeConfig until open().
121  // fall through
122  default:
123  engine = 0;
124  }
125 
126  networkConfigurationsChanged();
127 }
128 
130 {
131  if (serviceConfig.isValid()) {
134  } else if (!isOpen) {
137  state = QNetworkSession::Invalid;
138  emit stateChanged(state);
140  return;
141  }
142  opened = true;
143 
144  if ((activeConfig.state() & QNetworkConfiguration::Active) != QNetworkConfiguration::Active &&
147  emit stateChanged(state);
148 
149  engine->connectToId(activeConfig.identifier());
150  }
151 
152  isOpen = (activeConfig.state() & QNetworkConfiguration::Active) == QNetworkConfiguration::Active;
153  if (isOpen)
154  emit quitPendingWaitsForOpened();
155  }
156 }
157 
159 {
160  if (serviceConfig.isValid()) {
163  } else if (isOpen) {
164  opened = false;
165  isOpen = false;
166  emit closed();
167  }
168 }
169 
171 {
172  if (serviceConfig.isValid()) {
175  } else {
176  if ((activeConfig.state() & QNetworkConfiguration::Active) == QNetworkConfiguration::Active) {
177  state = QNetworkSession::Closing;
178  emit stateChanged(state);
179 
180  engine->disconnectFromId(activeConfig.identifier());
181 
182  sessionManager()->forceSessionClose(activeConfig);
183  }
184 
185  opened = false;
186  isOpen = false;
187  emit closed();
188  }
189 }
190 
192 {
193 }
194 
196 {
197 }
198 
200 {
201 }
202 
204 {
205 }
206 
207 #ifndef QT_NO_NETWORKINTERFACE
209 {
210  if (!engine || state != QNetworkSession::Connected || !publicConfig.isValid())
211  return QNetworkInterface();
212 
213  QString interface = engine->getInterfaceFromId(activeConfig.identifier());
214  if (interface.isEmpty())
215  return QNetworkInterface();
216  return QNetworkInterface::interfaceFromName(interface);
217 }
218 #endif
219 
221 {
222  if (key == QLatin1String("AutoCloseSessionTimeout")) {
223  if (engine && engine->requiresPolling() &&
224  !(engine->capabilities() & QNetworkConfigurationManager::CanStartAndStopInterfaces)) {
225  return sessionTimeout >= 0 ? sessionTimeout * 10000 : -1;
226  }
227  }
228 
229  return QVariant();
230 }
231 
233 {
234  if (key == QLatin1String("AutoCloseSessionTimeout")) {
235  if (engine && engine->requiresPolling() &&
236  !(engine->capabilities() & QNetworkConfigurationManager::CanStartAndStopInterfaces)) {
237  int timeout = value.toInt();
238  if (timeout >= 0) {
239  connect(engine, SIGNAL(updateCompleted()),
240  this, SLOT(decrementTimeout()), Qt::UniqueConnection);
241  sessionTimeout = timeout / 10000; // convert to poll intervals
242  } else {
243  disconnect(engine, SIGNAL(updateCompleted()), this, SLOT(decrementTimeout()));
244  sessionTimeout = -1;
245  }
246  }
247  }
248 }
249 
251 {
252  switch (lastError) {
254  return tr("Unknown session error.");
256  return tr("The session was aborted by the user or system.");
258  return tr("The requested operation is not supported by the system.");
260  return tr("The specified configuration cannot be used.");
262  return tr("Roaming was aborted or is not possible.");
263  default:
264  break;
265  }
266 
267  return QString();
268 }
269 
271 {
272  return lastError;
273 }
274 
276 {
277  if (engine && state == QNetworkSession::Connected)
278  return engine->bytesWritten(activeConfig.identifier());
279  return Q_UINT64_C(0);
280 }
281 
283 {
284  if (engine && state == QNetworkSession::Connected)
285  return engine->bytesReceived(activeConfig.identifier());
286  return Q_UINT64_C(0);
287 }
288 
290 {
291  if (state == QNetworkSession::Connected && startTime != Q_UINT64_C(0))
292  return QDateTime::currentDateTime().toTime_t() - startTime;
293  return Q_UINT64_C(0);
294 }
295 
297 {
298  QNetworkSession::State oldState = state;
299 
300  foreach (const QNetworkConfiguration &config, serviceConfig.children()) {
302  continue;
303 
304  if (activeConfig != config) {
305  if (engine) {
307  this, SLOT(connectionError(QString,QBearerEngineImpl::ConnectionError)));
308  }
309 
310  activeConfig = config;
311  engine = getEngineFromId(activeConfig.identifier());
312 
313  if (engine) {
314  connect(engine, SIGNAL(connectionError(QString,QBearerEngineImpl::ConnectionError)),
315  this, SLOT(connectionError(QString,QBearerEngineImpl::ConnectionError)),
317  }
318  emit newConfigurationActivated();
319  }
320 
322  if (state != oldState)
323  emit stateChanged(state);
324 
325  return;
326  }
327 
328  if (serviceConfig.children().isEmpty())
330  else
332 
333  if (state != oldState)
334  emit stateChanged(state);
335 }
336 
338 {
339  if (!engine)
340  return;
341 
342  QNetworkSession::State oldState = state;
343  state = engine->sessionStateForId(activeConfig.identifier());
344 
345  bool oldActive = isOpen;
346  isOpen = (state == QNetworkSession::Connected) ? opened : false;
347 
348  if (!oldActive && isOpen)
349  emit quitPendingWaitsForOpened();
350  if (oldActive && !isOpen)
351  emit closed();
352 
353  if (oldState != state)
354  emit stateChanged(state);
355 }
356 
358 {
359  if (serviceConfig.isValid())
360  updateStateFromServiceNetwork();
361  else
362  updateStateFromActiveConfig();
363 
364  startTime = engine->startTime(activeConfig.identifier());
365 }
366 
368 {
369  if (serviceConfig.isValid() &&
370  (config->id == serviceConfig.identifier() || config->id == activeConfig.identifier())) {
371  updateStateFromServiceNetwork();
372  } else if (config->id == activeConfig.identifier()) {
373  updateStateFromActiveConfig();
374  }
375 }
376 
378 {
379  if (activeConfig == config) {
380  opened = false;
381  isOpen = false;
382 
383  emit closed();
384 
387  }
388 }
389 
391 {
392  if (activeConfig.identifier() == id) {
393  networkConfigurationsChanged();
394  switch (error) {
397  opened = false;
398  break;
402  default:
404  }
405 
407  }
408 }
409 
411 {
412  if (--sessionTimeout <= 0) {
413  disconnect(engine, SIGNAL(updateCompleted()), this, SLOT(decrementTimeout()));
414  sessionTimeout = -1;
415  close();
416  }
417 }
418 
420 
421 #endif // QT_NO_BEARERMANAGEMENT
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
Q_GLOBAL_STATIC(QNetworkSessionManagerPrivate, sessionManager)
static QNetworkInterface interfaceFromName(const QString &name)
Returns a QNetworkInterface object for the interface named name.
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void forcedSessionClose(const QNetworkConfiguration &config)
#define error(msg)
#define SLOT(a)
Definition: qobjectdefs.h:226
uint toTime_t() const
Returns the datetime as the number of seconds that have passed since 1970-01-01T00:00:00, Coordinated Universal Time (Qt::UTC).
Definition: qdatetime.cpp:2505
State
This enum describes the connectivity state of the session.
QVariant sessionProperty(const QString &key) const
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
QList< QNetworkConfiguration > children() const
Returns all sub configurations of this network configuration in priority order.
The QString class provides a Unicode character string.
Definition: qstring.h:83
T * qobject_cast(QObject *object)
Definition: qobject.h:375
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_SIGNALS
Definition: qobjectdefs.h:72
int toInt(bool *ok=0) const
Returns the variant as an int if the variant has type() Int , Bool , ByteArray , Char ...
Definition: qvariant.cpp:2625
#define SIGNAL(a)
Definition: qobjectdefs.h:227
SessionError
This enum describes the session errors that can occur.
The QNetworkConfiguration class provides an abstraction of one or more access point configurations...
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
virtual QNetworkSession::SessionError error() const =0
void forceSessionClose(const QNetworkConfiguration &config)
unsigned __int64 quint64
Definition: qglobal.h:943
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
void forcedSessionClose(const QNetworkConfiguration &config)
#define emit
Definition: qobjectdefs.h:76
QNetworkConfigurationManagerPrivate * qNetworkConfigurationManagerPrivate()
#define Q_OBJECT
Definition: qobjectdefs.h:157
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
Disconnects signal in object sender from method in object receiver.
Definition: qobject.cpp:2895
QNetworkSessionManagerPrivate(QObject *parent=0)
void setSessionProperty(const QString &key, const QVariant &value)
static QBearerEngineImpl * getEngineFromId(const QString &id)
QNetworkSession::SessionError error() const
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
int key
StateFlags state() const
Returns the current state of the configuration.
virtual bool hasIdentifier(const QString &id)=0
static const QMetaObjectPrivate * priv(const uint *data)
QList< QBearerEngine * > engines() const
QNetworkInterface currentInterface() const
static QDateTime currentDateTime()
Returns the current datetime, as reported by the system clock, in the local time zone.
Definition: qdatetime.cpp:3138
The QNetworkInterface class provides a listing of the host&#39;s IP addresses and network interfaces...
#define Q_UINT64_C(c)
Definition: qglobal.h:941
void configurationChanged(QNetworkConfigurationPrivatePointer config)
void connectionError(const QString &id, QBearerEngineImpl::ConnectionError error)