browsercore/core/network/WebNetworkSession.cpp
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Wed, 23 Jun 2010 17:59:43 +0300
changeset 5 0f2326c2a325
parent 0 1450b09d0cfd
child 6 1c3b8676e58c
permissions -rw-r--r--
Revision: 201024 Kit: 2010125

/*
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, version 2.1 of the License.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program.  If not, 
* see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
*
* Description:
*
*/

#include "WebNetworkSession.h"

/*!
    Constructs a Web Network Session based on \a QNetworkConfiguration with the given \a parent.

    \sa QNetworkConfiguration
*/
WebNetworkSession::WebNetworkSession(const QNetworkConfiguration &config, QObject *parent)
    : QObject(parent)
{   
    m_NetworkSession = new QNetworkSession(config);
    
    // set up the handlers for QNetworkSession signals
    connect(m_NetworkSession, SIGNAL(preferredConfigurationChanged(const QNetworkConfiguration&, bool)),
            this, SLOT(handlePreferredConfigurationChanged(const QNetworkConfiguration&, bool)));   
    connect(m_NetworkSession, SIGNAL(newConfigurationActivated()), this, SLOT(handleNewConfigurationActivated()));
    connect(m_NetworkSession, SIGNAL(stateChanged(QNetworkSession::State)),
            this, SLOT(handleStateChanged(QNetworkSession::State)));
    connect(m_NetworkSession, SIGNAL(opened()), this, SLOT(handleOpened()));
    connect(m_NetworkSession, SIGNAL(closed()), this, SLOT(handleClosed()));
    connect(m_NetworkSession, SIGNAL(error(QNetworkSession::SessionError)), 
            this, SLOT(handleError(QNetworkSession::SessionError)));
     
    m_NetworkSession->open();
}

/*!
    Frees the resources associated with the WebNetworkSession object.
*/
WebNetworkSession::~WebNetworkSession()
{
	  // Close the network connection session before delete.
	  m_NetworkSession->close();
    delete m_NetworkSession;
}

/*! 
    Handle the preferredConfigurationChanged signal from Network Session.
    
    It emits networkNameChnaged signal if it migrates to the new QNetworkConfiguration.
*/
void WebNetworkSession::handlePreferredConfigurationChanged(const QNetworkConfiguration &config, bool isSeamless)
{
    bool isSelected = TRUE;
    
    if (isSeamless)
    {
        m_NetworkSession->migrate();
        qDebug() << "Migrate to new Network Connection: " << config.name();
        emit sessionConfigurationChanged(config); 
    }
    else
    {
        // Dialog Box to select
        if (isSelected)
        {
            m_NetworkSession->migrate();
            qDebug() << "Migrate to new Network Connection: " << config.name();
            emit sessionConfigurationChanged(config); 
        }
        else
        {
            m_NetworkSession->ignore();
            qDebug() << "Ignore new Network Connection: " << config.name();
        }
    }
}

/*! 
    Handle the newConfigurationActivated from Network Session.
    
    It emits networknameChanged signal with current QNetworkConfiguration.
    
*/
void WebNetworkSession::handleNewConfigurationActivated()
{
    bool isConnected = TRUE;
    
    // isConnectionGood = testConnection();
    if (isConnected)
    {
        m_NetworkSession->accept();
        qDebug() << "Accept new Network Connection";
        // flash the new connection network
    }
    else
    {
        m_NetworkSession->reject();
        // flash the old connection network
        qDebug() << "Reject new Network Connection";
    }
    emit sessionConfigurationChanged(m_NetworkSession->configuration()); 
}

/*! 
    Handle the stateChanged signal from Network Session.
    
    It emits networkSignalStrengthChanged signal with current QNetworkConfiguraiton 
    and QNetworkSession::State.
*/
void WebNetworkSession::handleStateChanged(QNetworkSession::State state)
{
    switch (state) {
        case QNetworkSession::Invalid:
            qDebug() << "Invalid";
            break;
        case QNetworkSession::NotAvailable:
            qDebug() << "Not Available";
            break;
        case QNetworkSession::Connecting:
            qDebug() << "Connecting";
            break;
        case QNetworkSession::Connected:
            qDebug() << "Connected";
            break;
        case QNetworkSession::Closing:
            qDebug() << "Closing";
            break;
        case QNetworkSession::Disconnected:
            qDebug() << "Disconnected";
            break;
        case QNetworkSession::Roaming:
            qDebug() << "Roaming";
            break;
        default:
            qDebug() << "Unknown";
    }
    emit sessionStateChanged(m_NetworkSession->configuration(), state); 
}

/*! 
    Handle the opened signal from Network Session.
    
    It emits networkNameChanged signal with activeConfiguration.
*/
void WebNetworkSession::handleOpened()
{   
    qDebug() << "Session Opened";
   
    emit sessionConfigurationChanged(activeConfiguration());
}

/*! 
    Handle the closed signal from Network Session.
*/
void WebNetworkSession::handleClosed()
{
    qDebug() << "Session Closed";
}

/*! 
    Handle the error signal from Network Session.
*/
void WebNetworkSession::handleError(QNetworkSession::SessionError error)
{
    switch (error)
    {
        case QNetworkSession::UnknownSessionError:
            qDebug() << "UnknownSessionError";
            break;
        case QNetworkSession::SessionAbortedError:
            qDebug() << "SessionAbortedError";
            break;
        case QNetworkSession::RoamingError:
            qDebug() << "RoamingError";
            break;
        case QNetworkSession::OperationNotSupportedError:
            qDebug() << "OperationNotSupportedError";
            break;
        case QNetworkSession::InvalidConfigurationError:
            qDebug() << "InvalidConfigurationError";
            break;
        default:
        	  qDebug() << "Unknown Error";
    }
}

QNetworkConfiguration WebNetworkSession::activeConfiguration(void)
{
	  QString activeIdentifier = m_NetworkSession->sessionProperty("ActiveConfiguration").toString();
	  QNetworkConfiguration config = m_NetworkSession->configuration();
	  QNetworkConfiguration activeConfig;
	  QList<QNetworkConfiguration> children;
	  
	  qDebug() << activeIdentifier;
	  
	  switch(config.type())
    {
        case QNetworkConfiguration::ServiceNetwork:
        	  qDebug() << "ServiceNetwork";
            children = config.children();
            /* Traverse all configuration to find the active configuration */
            foreach(QNetworkConfiguration tmpConfig, children)
            {
        	      qDebug() << tmpConfig.identifier();
                if (activeIdentifier == tmpConfig.identifier())
                {
            	      activeConfig = tmpConfig;
            	      break;
            	  }
            }
            break;  
        case QNetworkConfiguration::InternetAccessPoint:
        	  qDebug() << "InternetAccessPoint";
    	      activeConfig = config;
            break;
        case QNetworkConfiguration::UserChoice:
        	  qDebug() << "UserChoice";
        	  break;
        default:
        	  break;
    }
    
    return activeConfig;
}