browsercore/core/network/WebNetworkConnectionManager.cpp
changeset 3 0954f5dd2cd0
parent 0 1450b09d0cfd
child 9 b39122337a00
equal deleted inserted replaced
1:b0dd75e285d2 3:0954f5dd2cd0
     1 /*
     1 /*
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     4 *
     5 * under the terms of "Eclipse Public License v1.0"
     5 * This program is free software: you can redistribute it and/or modify
     6 * which accompanies this distribution, and is available
     6 * it under the terms of the GNU Lesser General Public License as published by
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * the Free Software Foundation, version 2.1 of the License.
     8 *
     8 * 
     9 * Initial Contributors:
     9 * This program is distributed in the hope that it will be useful,
    10 * Nokia Corporation - initial contribution.
    10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11 *
    11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12 * Contributors:
    12 * GNU Lesser General Public License for more details.
    13 *
    13 *
    14 * Description: 
    14 * You should have received a copy of the GNU Lesser General Public License
    15 *
    15 * along with this program.  If not, 
    16 */
    16 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
    17 
    17 *
    18 
    18 * Description:
       
    19 *
       
    20 */
    19 #include "WebNetworkConnectionManager.h"
    21 #include "WebNetworkConnectionManager.h"
       
    22 #include "WebNetworkSession.h"
    20 
    23 
    21 namespace WRT {
    24 namespace WRT {
    22 
    25 
       
    26 /*!
       
    27     Constructs a WebNetworkConfigurationManager with the given \a parent.
       
    28 */
    23 WebNetworkConnectionManager::WebNetworkConnectionManager(QObject *parent)
    29 WebNetworkConnectionManager::WebNetworkConnectionManager(QObject *parent)
    24     : QObject(parent), m_WebNetworkSession(0)
    30     : QObject(parent), m_WebNetworkSession(0), m_offlined(false)
    25 { 
    31 { 
       
    32     // set up handlers for Network Configuration Manager signals
    26     connect(&m_NetworkConfigurationManager, SIGNAL(updateCompleted()), 
    33     connect(&m_NetworkConfigurationManager, SIGNAL(updateCompleted()), 
    27             this, SLOT(configurationUpdateCompleted()));
    34             this, SLOT(handleConfigurationUpdateCompleted()));
    28     connect(&m_NetworkConfigurationManager, SIGNAL(configurationAdded(const QNetworkConfiguration&)),
    35     connect(&m_NetworkConfigurationManager, SIGNAL(configurationAdded(const QNetworkConfiguration&)),
    29             this, SLOT(configurationAdded(const QNetworkConfiguration&)));
    36             this, SLOT(handleConfigurationAdded(const QNetworkConfiguration&)));
    30     connect(&m_NetworkConfigurationManager, SIGNAL(configurationRemoved(const QNetworkConfiguration&)),
    37     connect(&m_NetworkConfigurationManager, SIGNAL(configurationRemoved(const QNetworkConfiguration&)),
    31             this, SLOT(configurationRemoved(const QNetworkConfiguration&)));
    38             this, SLOT(handleConfigurationRemoved(const QNetworkConfiguration&)));
    32     connect(&m_NetworkConfigurationManager, SIGNAL(onlineStateChanged(bool)), 
    39     connect(&m_NetworkConfigurationManager, SIGNAL(onlineStateChanged(bool)), 
    33             this, SLOT(onlineStateChanged(bool)));
    40             this, SLOT(handleOnlineStateChanged(bool)));
    34     connect(&m_NetworkConfigurationManager, SIGNAL(configurationChanged(const QNetworkConfiguration&)),
    41     connect(&m_NetworkConfigurationManager, SIGNAL(configurationChanged(const QNetworkConfiguration&)),
    35             this, SLOT(configurationChanged(const QNetworkConfiguration&)));
    42             this, SLOT(handleConfigurationChanged(const QNetworkConfiguration&)));
    36     	      
    43 
    37     updateConfigurations();
    44 #ifdef QT_MOBILITY_SYSINFO 
    38 }
    45     // initialize the mapping between network mode string and SystemNetworkInfo::NetworkMode
    39 
    46     initializeMapString();
       
    47 #endif // QT_MOBILITY_SYSINFO
       
    48 }
       
    49 
       
    50 /*!
       
    51     Frees the resources associated with the WebNetworkConfigurationManager object.
       
    52 */
    40 WebNetworkConnectionManager::~WebNetworkConnectionManager()
    53 WebNetworkConnectionManager::~WebNetworkConnectionManager()
    41 {
    54 {
    42     if (m_WebNetworkSession)
    55     if (m_WebNetworkSession)
    43         deleteSession();
    56         deleteSession();
    44 }
    57 }
    45 
    58 
       
    59 /*!
       
    60     Initiates an update of all configurations. This may be used to initiate WLAN scans or other
       
    61     time consuming updates which may be required to obtain the correct state for configurations.
       
    62 
       
    63     This call is asynchronous. On completion of this update the updateCompleted() signal is
       
    64     emitted. If new configurations are discovered or old ones were removed or changed the update
       
    65     process may trigger the emission of one or multiple configurationAdded(),
       
    66     configurationRemoved() and configurationChanged() signals.
       
    67 
       
    68     If a configuration state changes as a result of this update all existing QNetworkConfiguration
       
    69     instances are updated automatically.
       
    70 */
    46 void WebNetworkConnectionManager::updateConfigurations()
    71 void WebNetworkConnectionManager::updateConfigurations()
    47 {
    72 {
    48     m_NetworkConfigurationManager.updateConfigurations();
    73     m_NetworkConfigurationManager.updateConfigurations();
    49 }
    74 }
    50 
    75 
       
    76 /*!
       
    77     Returns the default configuration to be used. This function always returns a discovered
       
    78     configuration; otherwise an invalid configuration. Only configuration type SNAP is handled.
       
    79 
       
    80     In some cases it may be required to call updateConfigurations() and wait for the
       
    81     updateCompleted() signal before calling this function.
       
    82 */
    51 QNetworkConfiguration WebNetworkConnectionManager::defaultConfiguration() const
    83 QNetworkConfiguration WebNetworkConnectionManager::defaultConfiguration() const
    52 {
    84 {
    53     const bool canStartIAP = (m_NetworkConfigurationManager.capabilities() 
    85     const bool canStartIAP = (m_NetworkConfigurationManager.capabilities() 
    54                               & QNetworkConfigurationManager::CanStartAndStopInterfaces);
    86                               & QNetworkConfigurationManager::CanStartAndStopInterfaces);
    55     QNetworkConfiguration config = m_NetworkConfigurationManager.defaultConfiguration();
    87     QNetworkConfiguration config = m_NetworkConfigurationManager.defaultConfiguration();
    72     }
   104     }
    73     
   105     
    74     return config;
   106     return config;
    75 }
   107 }
    76 
   108 
       
   109 /*! 
       
   110     Create a Web Network Session using a QNetworkConfiguration and connect to its signals.
       
   111     
       
   112 */
    77 void WebNetworkConnectionManager::createSession(QNetworkConfiguration config)
   113 void WebNetworkConnectionManager::createSession(QNetworkConfiguration config)
    78 {   
   114 {   
    79     m_WebNetworkSession = new WebNetworkSession(config);
   115     m_WebNetworkSession = new WebNetworkSession(config);
    80 }
   116 
    81 
   117     // set up handlers for the WebNetworkSession signals
       
   118     connect(m_WebNetworkSession, SIGNAL(sessionConfigurationChanged(const QNetworkConfiguration &)),
       
   119             this, SLOT(handleSessionConfigurationChanged(const QNetworkConfiguration &)));
       
   120 }
       
   121 
       
   122 /*! 
       
   123     Delete a Web Network Session.
       
   124     
       
   125 */
    82 void WebNetworkConnectionManager::deleteSession(void)
   126 void WebNetworkConnectionManager::deleteSession(void)
    83 {   
   127 {   
    84     delete m_WebNetworkSession;
   128     delete m_WebNetworkSession;
    85 }
   129     m_WebNetworkSession = 0;
    86 
   130 }
    87 void WebNetworkConnectionManager::configurationUpdateCompleted()
   131 
       
   132 /*!
       
   133     Handle the updateCompleted signal from Network Configuration Manager.
       
   134     
       
   135     It creates a Web Network Session using the default configuration.
       
   136 */
       
   137 void WebNetworkConnectionManager::handleConfigurationUpdateCompleted()
    88 {
   138 {
    89 	  qDebug() << "configurationUpdateCompleted: create new network connection session";
   139 	  qDebug() << "configurationUpdateCompleted: create new network connection session";
    90 	  if (m_WebNetworkSession)
   140 	  if (m_WebNetworkSession)
    91 	  {
   141 	  {
    92 	  	  qDebug() << "Delete old network connection session";
   142 	  	  qDebug() << "Delete old network connection session";
    94 	  }
   144 	  }
    95 	  
   145 	  
    96     createSession(defaultConfiguration());
   146     createSession(defaultConfiguration());
    97 }
   147 }
    98 
   148 
    99 void WebNetworkConnectionManager::configurationAdded(const QNetworkConfiguration& config)
   149 /*! 
       
   150     Handle the configurationAdded signal from Network Configuration Manager.
       
   151 */
       
   152 void WebNetworkConnectionManager::handleConfigurationAdded(const QNetworkConfiguration& config)
   100 {
   153 {
   101     qDebug() << "Configuration" << config.name() << "Added";    
   154     qDebug() << "Configuration" << config.name() << "Added";    
   102 }
   155 }
   103 
   156 
   104 void WebNetworkConnectionManager::configurationRemoved(const QNetworkConfiguration& config)
   157 /*! 
       
   158     Handle the configurationRemoved signal from Network Configuration Manager
       
   159 */
       
   160 void WebNetworkConnectionManager::handleConfigurationRemoved(const QNetworkConfiguration& config)
   105 {
   161 {
   106     qDebug() << "Configuration" << config.name() << "Removed";
   162     qDebug() << "Configuration" << config.name() << "Removed";
   107 }
   163 }
   108 
   164 
   109 void WebNetworkConnectionManager::onlineStateChanged(bool isOnline)
   165 /*! 
   110 {
   166     Handle the onlineStateChanged signal from Network Configuration Manager.
       
   167 */
       
   168 void WebNetworkConnectionManager::handleOnlineStateChanged(bool isOnline)
       
   169 {
       
   170     emit networkOnlineStateChanged(isOnline);
       
   171     
   111     if (!isOnline)
   172     if (!isOnline)
   112     {
   173     {
   113         qDebug() << "offline";
   174         qDebug() << "offline";
       
   175         m_offlined = true;
   114     }
   176     }
   115     else
   177     else
   116     {
   178     {
   117         qDebug() << "online";
   179         qDebug() << "online";
       
   180         m_offlined = false;
   118     }
   181     }
   119     // flash icon to indicate the online state change with "online" and "offline".
   182     // flash icon to indicate the online state change with "online" and "offline".
   120 }
   183 }
   121 
   184 
   122 void WebNetworkConnectionManager::configurationChanged(const QNetworkConfiguration &config)
   185 /*! 
   123 {
   186     Handle the configurationChanged signal from Network Configuration Manager.
   124     qDebug() << "Configuration" << config.name() << "Changed";  
   187 */
   125 }
   188 void WebNetworkConnectionManager::handleConfigurationChanged(const QNetworkConfiguration &config)
   126 }
   189 {
       
   190     qDebug() << "Configuration" << config.name() << "Changed";
       
   191     qDebug() << "bearername:" << config.bearerName() << "type:" << config.type() << "state:" << config.state() << "purpose:" << config.purpose();
       
   192     
       
   193     /* The QNetworkSession is closed becuase of previous offline condition. Re-open the session if 
       
   194        the configuration matches the configurations hold by the QNetworkSession */
       
   195 #ifdef NO_OFFLINED_BUG
       
   196     if (isOfflined())
       
   197     {
       
   198 #endif // NO_OFFLINED_BUG
       
   199         if (m_WebNetworkSession && !m_WebNetworkSession->isOpen())
       
   200         {
       
   201             QNetworkConfiguration sessionConfig = m_WebNetworkSession->configuration();
       
   202             QList<QNetworkConfiguration> children = sessionConfig.children();
       
   203         	  switch(sessionConfig.type())
       
   204             {
       
   205                 case QNetworkConfiguration::ServiceNetwork:        
       
   206                     /* Traverse all configuration to find the matching configuration */
       
   207                     foreach(QNetworkConfiguration tmpConfig, children)
       
   208                     {
       
   209         	              if (config == tmpConfig)
       
   210         	              {
       
   211         	              	  if ((config.state() == QNetworkConfiguration::Discovered) ||
       
   212         	              	  	   (config.state() == QNetworkConfiguration::Active))
       
   213         	                      m_WebNetworkSession->open();
       
   214             	              break;
       
   215             	          }
       
   216             	      }
       
   217                     break;  
       
   218                case QNetworkConfiguration::InternetAccessPoint:
       
   219         	          qDebug() << "InternetAccessPoint";
       
   220                     break;
       
   221                case QNetworkConfiguration::UserChoice:
       
   222         	          qDebug() << "UserChoice";
       
   223         	          break;
       
   224                default:
       
   225         	         break;
       
   226             } 	  
       
   227         }
       
   228 #ifdef NO_OFFLINED_BUG
       
   229     }
       
   230 #endif // NO_OFFLINED_BUG
       
   231 } 
       
   232 
       
   233 /*! 
       
   234     Handle the networkNameChanged signal from Network Configuration Manager and translate 
       
   235     sessionConfiguration signal to networknameChanged.
       
   236     
       
   237     It a\ emits networkNameChanged signal for no cellular network connetion.
       
   238 */
       
   239 void WebNetworkConnectionManager::handleSessionConfigurationChanged(const QNetworkConfiguration &config)
       
   240 {  
       
   241     qDebug() << "handleSessionConfigurationChanged" << "bearername:" << config.bearerName();
       
   242 
       
   243 #ifdef QT_MOBILITY_SYSINFO  
       
   244     QSystemNetworkInfo::NetworkMode mode;
       
   245     mode = m_mapStringNetworkMode[config.bearerName()];
       
   246 
       
   247     emit networkSessionNameChanged(mode, config.name());
       
   248 #endif // QT_MOBILITY_SYSINFO
       
   249 }
       
   250 
       
   251 #ifdef QT_MOBILITY_SYSINFO
       
   252 void WebNetworkConnectionManager::initializeMapString(void)
       
   253 {
       
   254 	  m_mapStringNetworkMode["Ethernet"] = QSystemNetworkInfo::EthernetMode;
       
   255 	  m_mapStringNetworkMode["WLAN"] = QSystemNetworkInfo::WlanMode;
       
   256 	  m_mapStringNetworkMode["2G"] = QSystemNetworkInfo::GsmMode;
       
   257 	  m_mapStringNetworkMode["CDMA2000"] = QSystemNetworkInfo::CdmaMode;
       
   258 	  m_mapStringNetworkMode["WCDMA"] = QSystemNetworkInfo::WcdmaMode;
       
   259 	  m_mapStringNetworkMode["HSPA"] = QSystemNetworkInfo::UnknownMode;
       
   260 	  m_mapStringNetworkMode["Bluetooth"] = QSystemNetworkInfo::BluetoothMode;
       
   261 	  m_mapStringNetworkMode["WiMAX"] = QSystemNetworkInfo::WimaxMode;
       
   262 	  m_mapStringNetworkMode[""] = QSystemNetworkInfo::UnknownMode;
       
   263 }
       
   264 #endif // QT_MOBILITY_SYSINFO
       
   265 
       
   266 } // WRT
       
   267