examples/network/bearermonitor/sessionwidget.cpp
changeset 30 5dc02b23752f
child 37 758a864f9613
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:BSD$
       
    10 ** You may use this file under the terms of the BSD license as follows:
       
    11 **
       
    12 ** "Redistribution and use in source and binary forms, with or without
       
    13 ** modification, are permitted provided that the following conditions are
       
    14 ** met:
       
    15 **   * Redistributions of source code must retain the above copyright
       
    16 **     notice, this list of conditions and the following disclaimer.
       
    17 **   * Redistributions in binary form must reproduce the above copyright
       
    18 **     notice, this list of conditions and the following disclaimer in
       
    19 **     the documentation and/or other materials provided with the
       
    20 **     distribution.
       
    21 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
       
    22 **     the names of its contributors may be used to endorse or promote
       
    23 **     products derived from this software without specific prior written
       
    24 **     permission.
       
    25 **
       
    26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       
    27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       
    28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       
    29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       
    30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       
    32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       
    33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       
    34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
       
    37 ** $QT_END_LICENSE$
       
    38 **
       
    39 ****************************************************************************/
       
    40 
       
    41 #include "sessionwidget.h"
       
    42 #include "qnetworkconfigmanager.h"
       
    43 
       
    44 SessionWidget::SessionWidget(const QNetworkConfiguration &config, QWidget *parent)
       
    45 :   QWidget(parent), statsTimer(-1)
       
    46 {
       
    47     setupUi(this);
       
    48 
       
    49 #ifdef QT_NO_NETWORKINTERFACE
       
    50     interfaceName->setVisible(false);
       
    51     interfaceNameLabel->setVisible(false);
       
    52     interfaceGuid->setVisible(false);
       
    53     interfaceGuidLabel->setVisible(false);
       
    54 #endif
       
    55 
       
    56     session = new QNetworkSession(config, this);
       
    57 
       
    58     connect(session, SIGNAL(stateChanged(QNetworkSession::State)),
       
    59             this, SLOT(updateSession()));
       
    60     connect(session, SIGNAL(error(QNetworkSession::SessionError)),
       
    61             this, SLOT(updateSessionError(QNetworkSession::SessionError)));
       
    62 
       
    63     updateSession();
       
    64 
       
    65     sessionId->setText(QString("0x%1").arg(qulonglong(session), 8, 16, QChar('0')));
       
    66 
       
    67     configuration->setText(session->configuration().name());
       
    68 
       
    69     connect(openSessionButton, SIGNAL(clicked()),
       
    70             this, SLOT(openSession()));
       
    71     connect(openSyncSessionButton, SIGNAL(clicked()),
       
    72             this, SLOT(openSyncSession()));
       
    73     connect(closeSessionButton, SIGNAL(clicked()),
       
    74             this, SLOT(closeSession()));
       
    75     connect(stopSessionButton, SIGNAL(clicked()),
       
    76             this, SLOT(stopSession()));
       
    77 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
       
    78     connect(deleteSessionButton, SIGNAL(clicked()),
       
    79             this, SLOT(deleteSession()));
       
    80 #endif
       
    81 }
       
    82 
       
    83 SessionWidget::~SessionWidget()
       
    84 {
       
    85     delete session;
       
    86 }
       
    87 
       
    88 void SessionWidget::timerEvent(QTimerEvent *e)
       
    89 {
       
    90     if (e->timerId() == statsTimer) {
       
    91         rxData->setText(QString::number(session->bytesReceived()));
       
    92         txData->setText(QString::number(session->bytesWritten()));
       
    93         activeTime->setText(QString::number(session->activeTime()));
       
    94     }
       
    95 }
       
    96 
       
    97 #if defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
       
    98 void SessionWidget::deleteSession()
       
    99 {
       
   100     delete this;
       
   101 }
       
   102 #endif
       
   103 
       
   104 void SessionWidget::updateSession()
       
   105 {
       
   106     updateSessionState(session->state());
       
   107 
       
   108     if (session->state() == QNetworkSession::Connected)
       
   109         statsTimer = startTimer(1000);
       
   110     else
       
   111         killTimer(statsTimer);
       
   112 
       
   113     if (session->configuration().type() == QNetworkConfiguration::InternetAccessPoint)
       
   114         bearer->setText(session->configuration().bearerName());
       
   115     else {
       
   116         QNetworkConfigurationManager mgr;
       
   117         QNetworkConfiguration c = mgr.configurationFromIdentifier(session->sessionProperty("ActiveConfiguration").toString());
       
   118         bearer->setText(c.bearerName());
       
   119     }
       
   120 
       
   121 #ifndef QT_NO_NETWORKINTERFACE
       
   122     interfaceName->setText(session->interface().humanReadableName());
       
   123     interfaceGuid->setText(session->interface().name());
       
   124 #endif
       
   125 }
       
   126 
       
   127 void SessionWidget::openSession()
       
   128 {
       
   129     clearError();
       
   130     session->open();
       
   131     updateSession();
       
   132 }
       
   133 
       
   134 void SessionWidget::openSyncSession()
       
   135 {
       
   136     clearError();
       
   137     session->open();
       
   138     session->waitForOpened();
       
   139     updateSession();
       
   140 }
       
   141 
       
   142 void SessionWidget::closeSession()
       
   143 {
       
   144     clearError();
       
   145     session->close();
       
   146     updateSession();
       
   147 }
       
   148 
       
   149 void SessionWidget::stopSession()
       
   150 {
       
   151     clearError();
       
   152     session->stop();
       
   153     updateSession();
       
   154 }
       
   155 
       
   156 void SessionWidget::updateSessionState(QNetworkSession::State state)
       
   157 {
       
   158     QString s = tr("%1 (%2)");
       
   159 
       
   160     switch (state) {
       
   161     case QNetworkSession::Invalid:
       
   162         s = s.arg(tr("Invalid"));
       
   163         break;
       
   164     case QNetworkSession::NotAvailable:
       
   165         s = s.arg(tr("Not Available"));
       
   166         break;
       
   167     case QNetworkSession::Connecting:
       
   168         s = s.arg(tr("Connecting"));
       
   169         break;
       
   170     case QNetworkSession::Connected:
       
   171         s = s.arg(tr("Connected"));
       
   172         break;
       
   173     case QNetworkSession::Closing:
       
   174         s = s.arg(tr("Closing"));
       
   175         break;
       
   176     case QNetworkSession::Disconnected:
       
   177         s = s.arg(tr("Disconnected"));
       
   178         break;
       
   179     case QNetworkSession::Roaming:
       
   180         s = s.arg(tr("Roaming"));
       
   181         break;
       
   182     default:
       
   183         s = s.arg(tr("Unknown"));
       
   184     }
       
   185 
       
   186     if (session->isOpen())
       
   187         s = s.arg(tr("Open"));
       
   188     else
       
   189         s = s.arg(tr("Closed"));
       
   190 
       
   191     sessionState->setText(s);
       
   192 }
       
   193 
       
   194 void SessionWidget::updateSessionError(QNetworkSession::SessionError error)
       
   195 {
       
   196     lastError->setText(QString::number(error));
       
   197     errorString->setText(session->errorString());
       
   198 }
       
   199 
       
   200 void SessionWidget::clearError()
       
   201 {
       
   202     lastError->clear();
       
   203     errorString->clear();
       
   204 }