Qt 4.8
qnetworksession.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 QtNetwork module 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.h"
43 #include "qbearerengine_p.h"
44 
45 #include <QEventLoop>
46 #include <QTimer>
47 #include <QThread>
48 
50 #include "qnetworksession_p.h"
51 
52 #ifdef Q_OS_SYMBIAN
53 #include <es_sock.h>
54 #include <private/qcore_symbian_p.h>
55 #endif
56 
57 #ifndef QT_NO_BEARERMANAGEMENT
58 
60 
262  : QObject(parent), d(0)
263 {
264  // invalid configuration
265  if (!connectionConfig.identifier().isEmpty()) {
267  if (engine->hasIdentifier(connectionConfig.identifier())) {
268  d = engine->createSessionBackend();
269  d->q = this;
270  d->publicConfig = connectionConfig;
272  connect(d, SIGNAL(quitPendingWaitsForOpened()), this, SIGNAL(opened()));
277  connect(d, SIGNAL(closed()), this, SIGNAL(closed()));
282  break;
283  }
284  }
285  }
286 
287  qRegisterMetaType<QNetworkSession::State>();
288  qRegisterMetaType<QNetworkSession::SessionError>();
289 }
290 
295 {
296  delete d;
297 }
298 
317 {
318  if (d)
319  d->open();
320  else
322 }
323 
342 {
343  if (!d)
344  return false;
345 
346  if (d->isOpen)
347  return true;
348 
349  if (!(d->state == Connecting || d->state == Connected)) {
350  return false;
351  }
352 
353  QEventLoop loop;
354  QObject::connect(d, SIGNAL(quitPendingWaitsForOpened()), &loop, SLOT(quit()));
356 
357  //final call
358  if (msecs >= 0)
359  QTimer::singleShot(msecs, &loop, SLOT(quit()));
360 
361  // enter the event loop and wait for opened/error/timeout
363 
364  return d->isOpen;
365 }
366 
382 {
383  if (d)
384  d->close();
385 }
386 
398 {
399  if (d)
400  d->stop();
401 }
402 
409 {
410  return d ? d->publicConfig : QNetworkConfiguration();
411 }
412 
413 #ifndef QT_NO_NETWORKINTERFACE
414 
427 {
428  return d ? d->currentInterface() : QNetworkInterface();
429 }
430 #endif
431 
439 {
440  return d ? d->isOpen : false;
441 }
442 
461 {
462  return d ? d->state : QNetworkSession::Invalid;
463 }
464 
471 {
472  return d ? d->error() : InvalidConfigurationError;
473 }
474 
482 {
483  return d ? d->errorString() : tr("Invalid configuration.");
484 }
485 
554 {
555  if (!d || !d->publicConfig.isValid())
556  return QVariant();
557 
558  if (key == QLatin1String("ActiveConfiguration"))
559  return d->isOpen ? d->activeConfig.identifier() : QString();
560 
561  if (key == QLatin1String("UserChoiceConfiguration")) {
563  return QString();
564 
565  if (d->serviceConfig.isValid())
566  return d->serviceConfig.identifier();
567  else
568  return d->activeConfig.identifier();
569  }
570 
571  return d->sessionProperty(key);
572 }
573 
583 {
584  if (!d)
585  return;
586 
587  if (key == QLatin1String("ActiveConfiguration") ||
588  key == QLatin1String("UserChoiceConfiguration")) {
589  return;
590  }
591 
592  d->setSessionProperty(key, value);
593 }
594 
604 {
605  if (d)
606  d->migrate();
607 }
608 
615 {
616  // Needed on at least Symbian platform: the roaming must be explicitly
617  // ignore()'d or migrate()'d
618  if (d)
619  d->ignore();
620 }
621 
631 {
632  if (d)
633  d->accept();
634 }
635 
644 {
645  if (d)
646  d->reject();
647 }
648 
649 
665 {
666  return d ? d->bytesWritten() : Q_UINT64_C(0);
667 }
668 
684 {
685  return d ? d->bytesReceived() : Q_UINT64_C(0);
686 }
687 
692 {
693  return d ? d->activeTime() : Q_UINT64_C(0);
694 }
695 
710 void QNetworkSession::connectNotify(const char *signal)
711 {
712  QObject::connectNotify(signal);
713 
714  if (!d)
715  return;
716 
717  //check for preferredConfigurationChanged() signal connect notification
718  //This is not required on all platforms
720  d->setALREnabled(true);
721 }
722 
734 void QNetworkSession::disconnectNotify(const char *signal)
735 {
737 
738  if (!d)
739  return;
740 
741  //check for preferredConfigurationChanged() signal disconnect notification
742  //This is not required on all platforms
744  d->setALREnabled(false);
745 }
746 
747 #ifdef Q_OS_SYMBIAN
748 RConnection* QNetworkSessionPrivate::nativeSession(QNetworkSession &s)
749 {
750  if (!s.d)
751  return 0;
752  if (s.thread() != QThread::currentThread())
753  qWarning("QNetworkSessionPrivate::nativeSession called in wrong thread");
754  return s.d->nativeSession();
755 }
756 
757 TInt QNetworkSessionPrivate::nativeOpenSocket(QNetworkSession& s, RSocket& sock, TUint family, TUint type, TUint protocol)
758 {
759  if (!s.d)
760  return 0;
761  QMutexLocker lock(&(s.d->mutex));
762  RConnection *con = s.d->nativeSession();
763  if (!con || !con->SubSessionHandle())
764  return KErrNotReady;
765  return sock.Open(qt_symbianGetSocketServer(), family, type, protocol, *con);
766 }
767 
768 TInt QNetworkSessionPrivate::nativeOpenHostResolver(QNetworkSession& s, RHostResolver& resolver, TUint family, TUint protocol)
769 {
770  if (!s.d)
771  return 0;
772  QMutexLocker lock(&(s.d->mutex));
773  RConnection *con = s.d->nativeSession();
774  if (!con || !con->SubSessionHandle())
775  return KErrNotReady;
776  return resolver.Open(qt_symbianGetSocketServer(), family, protocol, *con);
777 }
778 
779 #endif
780 
781 #include "moc_qnetworksession.cpp"
782 
784 
785 #endif // QT_NO_BEARERMANAGEMENT
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QString errorString() const
Returns a human-readable description of the last device error that occurred.
double d
Definition: qnumeric_p.h:62
virtual void syncStateWithInterface()=0
QVariant sessionProperty(const QString &key) const
Returns the value for property key.
int type
Definition: qmetatype.cpp:239
virtual void connectNotify(const char *signal)
This function is required to detect whether the client wants to control the roaming process...
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
quint64 activeTime() const
Returns the number of seconds that the session has been active.
void migrate()
Instructs the session to roam to the new access point.
virtual QNetworkInterface currentInterface() const =0
virtual void stop()=0
void reject()
The new access point is not suitable for the application.
virtual ~QNetworkSession()
Frees the resources associated with the QNetworkSession object.
#define SLOT(a)
Definition: qobjectdefs.h:226
QNetworkSessionPrivate * d
void stateChanged(QNetworkSession::State)
This signal is emitted whenever the state of the network session changes.
State
This enum describes the connectivity state of the session.
QNetworkConfiguration serviceConfig
void opened()
This signal is emitted when the network session has been opened.
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
SessionError error() const
Returns the type of error that last occurred.
virtual void migrate()=0
The QString class provides a Unicode character string.
Definition: qstring.h:83
virtual QVariant sessionProperty(const QString &key) const =0
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
void ignore()
This function indicates that the application does not wish to roam the session.
virtual void connectNotify(const char *signal)
This virtual function is called when something has been connected to signal in this object...
Definition: qobject.cpp:3142
QNetworkConfiguration activeConfig
virtual void disconnectNotify(const char *signal)
This function is called when the client disconnects from the preferredConfigurationChanged() signal...
virtual void setALREnabled(bool)
virtual QNetworkSessionPrivate * createSessionBackend()=0
QNetworkConfiguration configuration() const
Returns the QNetworkConfiguration that this network session object is based on.
#define SIGNAL(a)
Definition: qobjectdefs.h:227
bool isValid() const
Returns true if this QNetworkConfiguration object is valid.
static QThread * currentThread()
Returns a pointer to a QThread which manages the currently executing thread.
Definition: qthread.cpp:419
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 preferredConfigurationChanged(const QNetworkConfiguration &config, bool isSeamless)
This signal is emitted when the preferred configuration/access point for the session changes...
The QEventLoop class provides a means of entering and leaving an event loop.
Definition: qeventloop.h:55
void accept()
Instructs the session to permanently accept the new access point.
unsigned __int64 quint64
Definition: qglobal.h:943
QNetworkInterface interface() const
Returns the network interface that is used by this session.
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
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
virtual void open()=0
quint64 bytesWritten() const
Returns the amount of data sent in bytes; otherwise 0.
virtual void reject()=0
#define emit
Definition: qobjectdefs.h:76
QNetworkConfigurationManagerPrivate * qNetworkConfigurationManagerPrivate()
QNetworkSession::State state
Q_CORE_EXPORT void qWarning(const char *,...)
State state() const
Returns the state of the session.
QNetworkConfiguration publicConfig
virtual quint64 activeTime() const =0
virtual quint64 bytesWritten() const =0
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
QString identifier() const
Returns the unique and platform specific identifier for this network configuration; otherwise an empt...
The QNetworkSession class provides control over the system&#39;s access points and enables session manage...
virtual void ignore()=0
virtual void disconnectNotify(const char *signal)
This virtual function is called when something has been disconnected from signal in this object...
Definition: qobject.cpp:3162
virtual void close()=0
bool isOpen() const
Returns true if this session is open.
quint64 bytesReceived() const
Returns the amount of data received in bytes; otherwise 0.
void open()
Creates an open session which increases the session counter on the underlying network interface...
int key
bool singleShot
This static function calls a slot after a given time interval.
Definition: qtimer.h:59
virtual bool hasIdentifier(const QString &id)=0
QList< QBearerEngine * > engines() const
virtual quint64 bytesReceived() const =0
static QReadWriteLock lock
Definition: proxyconf.cpp:399
virtual void setSessionProperty(const QString &key, const QVariant &value)=0
QNetworkSession(const QNetworkConfiguration &connConfig, QObject *parent=0)
Constructs a session based on connectionConfig with the given parent.
void stop()
Invalidates all open sessions against the network interface and therefore stops the underlying networ...
virtual QString errorString() const =0
void closed()
This signal is emitted when the network session has been closed.
int qstrcmp(const QByteArray &str1, const char *str2)
Definition: qbytearray.cpp:336
The QNetworkInterface class provides a listing of the host&#39;s IP addresses and network interfaces...
QThread * thread() const
Returns the thread in which the object lives.
Definition: qobject.cpp:1419
void setSessionProperty(const QString &key, const QVariant &value)
Sets the property value on the session.
void newConfigurationActivated()
This signal is emitted once the session has roamed to the new access point.
#define Q_UINT64_C(c)
Definition: qglobal.h:941
void close()
Decreases the session counter on the associated network configuration.
int exec(ProcessEventsFlags flags=AllEvents)
Enters the main event loop and waits until exit() is called.
Definition: qeventloop.cpp:181
Type type() const
Returns the type of the configuration.
virtual void accept()=0
bool waitForOpened(int msecs=30000)
Waits until the session has been opened, up to msecs milliseconds.