ginebra2/SystemNetworkImpl.cpp
changeset 3 0954f5dd2cd0
equal deleted inserted replaced
1:b0dd75e285d2 3:0954f5dd2cd0
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 *
       
     5 * This program is free software: you can redistribute it and/or modify
       
     6 * it under the terms of the GNU Lesser General Public License as published by
       
     7 * the Free Software Foundation, version 2.1 of the License.
       
     8 *
       
     9 * This program is distributed in the hope that it will be useful,
       
    10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12 * GNU Lesser General Public License for more details.
       
    13 *
       
    14 * You should have received a copy of the GNU Lesser General Public License
       
    15 * along with this program.  If not,
       
    16 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
       
    17 *
       
    18 * Description:
       
    19 *  This file implements the SystemNetworkImpl class.
       
    20 */
       
    21 #include <QList>
       
    22 #include <QString>
       
    23 #ifdef QT_MOBILITY_BEARER_MANAGEMENT
       
    24 #include "WebNetworkConnectionManagerSingleton.h"
       
    25 #endif // QT_MOBILITY_BEARER_MANAGEMENT
       
    26 #include "NetworkDelegate.h"
       
    27 #include "SystemNetworkImpl.h"
       
    28 #include "Utilities.h"
       
    29 
       
    30 namespace GVA {
       
    31 
       
    32 #define WCDMA2GSM_WORKAROUND // work around for QtMobility issue
       
    33 
       
    34 SystemNetworkImpl::SystemNetworkImpl()
       
    35     : m_currentMode(QSystemNetworkInfo::UnknownMode),
       
    36       m_sessionNetworkName("")
       
    37 {
       
    38     // create Qt Mobility API objects for network information
       
    39     m_networkInfo = new QSystemNetworkInfo(this);
       
    40 
       
    41     // set up handlers for system network info signals
       
    42     safe_connect(m_networkInfo, SIGNAL(networkModeChanged(
       
    43         QSystemNetworkInfo::NetworkMode)), this,
       
    44         SLOT(handleNetworkModeChanged(QSystemNetworkInfo::NetworkMode)));
       
    45     safe_connect(m_networkInfo, SIGNAL(networkNameChanged(
       
    46         QSystemNetworkInfo::NetworkMode, const QString&)), this,
       
    47         SLOT(handleNetworkNameChanged(QSystemNetworkInfo::NetworkMode, const QString&)));
       
    48     safe_connect(m_networkInfo, SIGNAL(networkSignalStrengthChanged(
       
    49         QSystemNetworkInfo::NetworkMode, int)), this,
       
    50         SLOT(handleNetworkSignalStrengthChanged(QSystemNetworkInfo::NetworkMode, int)));
       
    51     safe_connect(m_networkInfo, SIGNAL(networkStatusChanged(
       
    52         QSystemNetworkInfo::NetworkMode, QSystemNetworkInfo::NetworkStatus)), this,
       
    53         SLOT(handleNetworkStatusChanged(QSystemNetworkInfo::NetworkMode, QSystemNetworkInfo::NetworkStatus)));
       
    54 
       
    55 #ifdef QT_MOBILITY_BEARER_MANAGEMENT
       
    56     // Get the singleton instance of WebNetworkConnectionManager 
       
    57     WRT::WebNetworkConnectionManager &webNetworkConnectionManager 
       
    58     	  = WRT::WebNetworkConnectionManagerSingleton::Instance();
       
    59     
       
    60     safe_connect(&webNetworkConnectionManager, SIGNAL(networkOnlineStateChanged(bool)),
       
    61         this, SLOT(handleNetworkOnlineStateChanged(bool)));
       
    62     safe_connect(&webNetworkConnectionManager, SIGNAL(networkSessionNameChanged(
       
    63         QSystemNetworkInfo::NetworkMode, const QString&)), this,
       
    64         SLOT(handleNetworkSessionNameChanged(QSystemNetworkInfo::NetworkMode, const QString&)));
       
    65 
       
    66       // Update all configurations
       
    67     webNetworkConnectionManager.updateConfigurations();
       
    68 #endif // QT_MOBILITY_BEARER_MANAGEMENT
       
    69 }
       
    70 
       
    71 SystemNetworkImpl::~SystemNetworkImpl()
       
    72 {
       
    73     delete m_networkInfo;
       
    74 }
       
    75 
       
    76 //! Gets the network name for the current network mode.
       
    77 QString SystemNetworkImpl::getNetworkName() const
       
    78 {
       
    79     QString netName;
       
    80 
       
    81     switch(m_currentMode) {
       
    82         case QSystemNetworkInfo::WlanMode:
       
    83         case QSystemNetworkInfo::EthernetMode:
       
    84         case QSystemNetworkInfo::BluetoothMode:
       
    85         case QSystemNetworkInfo::WimaxMode:
       
    86             // for wireless cases use name from session.
       
    87 #ifdef QT_MOBILITY_BEARER_MANAGEMENT
       
    88             netName = m_sessionNetworkName;
       
    89 #else
       
    90             netName = m_networkInfo->networkName(m_currentMode);
       
    91             // if WLAN SSID name is unknown show "WiFi"
       
    92             if (netName == "")
       
    93                 netName = "WiFi";
       
    94 #endif // QT_MOBILITY_BEARER_MANAGEMENT
       
    95             break;
       
    96 
       
    97         default:
       
    98             netName = m_networkInfo->networkName(m_currentMode);
       
    99             break;
       
   100     }
       
   101 
       
   102     qDebug() << "SystemNetworkImpl::getNetworkName: network name " << netName;
       
   103     return (netName);
       
   104 }
       
   105 
       
   106 //! Gets the network signal strength for the current network mode.
       
   107 int SystemNetworkImpl::getNetworkSignalStrength() const
       
   108 {
       
   109     int strength = m_networkInfo->networkSignalStrength(m_currentMode);
       
   110     
       
   111     // Strength in WLAN mode is reported as -1 by QtMobility
       
   112     if ((strength == -1) && (m_currentMode == QSystemNetworkInfo::WlanMode)) {
       
   113         strength = 100;
       
   114     }
       
   115     
       
   116     return (strength);
       
   117 }
       
   118 
       
   119 //! Handles the networkNetworkModeChanged signal from system network info.
       
   120 /*!
       
   121   \param mode network mode of connection that changed
       
   122 */
       
   123 void SystemNetworkImpl::handleNetworkModeChanged(
       
   124     QSystemNetworkInfo::NetworkMode mode)
       
   125 {
       
   126     qDebug() << "SystemNetworkImpl::handleNetworkModeChanged" << "Mode:" << mode;
       
   127 }
       
   128 
       
   129 //! Handles the networkSignalStrengthChanged signal from system network info.
       
   130 /*!
       
   131   \param mode network mode of connection that changed
       
   132   \param strength new signal strength
       
   133 */
       
   134 void SystemNetworkImpl::handleNetworkSignalStrengthChanged(
       
   135     QSystemNetworkInfo::NetworkMode mode, int strength)
       
   136 {
       
   137     qDebug() << "SystemNetworkImpl::handleNetworkSignalStrengthChanged" << "Mode:" << mode << "strength:" << strength;
       
   138     
       
   139     // Only send signal strength changes for current mode.
       
   140     if (mode == m_currentMode) {
       
   141         // Unknown mode could mean network error so send negative strength to indicate offline.
       
   142         if (m_currentMode == QSystemNetworkInfo::UnknownMode)
       
   143             strength = -1;
       
   144 
       
   145         qDebug() << "SystemNetworkImpl::handleNetworkSignalStrengthChanged" << "emit strength:" << strength;
       
   146         emit networkSignalStrengthChanged(strength);
       
   147     }
       
   148 }
       
   149 
       
   150 //! Handles the networkNameChanged signal from system network info.
       
   151 /*!
       
   152   \param mode network mode of connection that changed
       
   153   \param name new network name
       
   154 */
       
   155 void SystemNetworkImpl::handleNetworkNameChanged(
       
   156         QSystemNetworkInfo::NetworkMode mode, const QString& name)
       
   157 {
       
   158     qDebug() << "SystemNetworkImpl::handleNetworkNameChanged" << "Mode:" << mode << "name:" << name;
       
   159     
       
   160     // Only send network name changes for current mode.
       
   161     if (mode == m_currentMode) {
       
   162         // Now update name.
       
   163         emit networkNameChanged(name);
       
   164     }
       
   165 }
       
   166 
       
   167 //! Handles the networkStatusChanged signal from system network info.
       
   168 /*!
       
   169   \param mode network mode of connection that changed
       
   170   \param status network status of connection that changed
       
   171 */
       
   172 void SystemNetworkImpl::handleNetworkStatusChanged(
       
   173         QSystemNetworkInfo::NetworkMode mode, QSystemNetworkInfo::NetworkStatus status)
       
   174 {
       
   175     qDebug() << "SystemNetworkImpl::handleNetworkStatusChanged" << "mode: " << mode << "status: " << status; 
       
   176 }
       
   177 
       
   178 #ifdef QT_MOBILITY_BEARER_MANAGEMENT
       
   179 //! Handles online state changed signal from the web network connection manager.
       
   180 /*!
       
   181   \param mode network mode of connection that changed
       
   182 */
       
   183 void SystemNetworkImpl::handleNetworkOnlineStateChanged(bool isOnline)
       
   184 {
       
   185     qDebug() << "SystemNetworkImpl::handleOnlineStateChanged" << "isOnline:" << isOnline;
       
   186     
       
   187     // Offline indicates no active network configurations.
       
   188     if (!isOnline) {
       
   189         qDebug() << "SystemNetworkImpl::handleOnlineStateChanged: change mode to unknown, emit -1 str";
       
   190         m_currentMode = QSystemNetworkInfo::UnknownMode;
       
   191         // negative strength indicates offline to UI
       
   192         emit networkSignalStrengthChanged(-1);
       
   193     }
       
   194     // Online indicates at least 1 active network config but not necessarily 
       
   195     // one being used by the browser.
       
   196 }
       
   197 
       
   198 void SystemNetworkImpl::handleNetworkSessionNameChanged(QSystemNetworkInfo::NetworkMode mode, const QString& name)
       
   199 {
       
   200     // UI must get non-negative strength to indicate online status
       
   201     int strength = m_networkInfo->networkSignalStrength(mode);
       
   202     
       
   203     qDebug() << "SystemNetworkImpl::handleNetworkSessionNameChanged" << "Mode:" << mode << "name:" << name << "strength:" << strength;
       
   204     
       
   205     switch (mode) {
       
   206         case QSystemNetworkInfo::WlanMode:
       
   207         case QSystemNetworkInfo::EthernetMode:
       
   208         case QSystemNetworkInfo::BluetoothMode:
       
   209         case QSystemNetworkInfo::WimaxMode:
       
   210             // for wireless cases use name from session.
       
   211             m_sessionNetworkName = name;
       
   212             break;
       
   213 
       
   214         default:
       
   215             // clear session name - not needed in this mode
       
   216             m_sessionNetworkName = "";
       
   217             break;
       
   218     }
       
   219 
       
   220 #ifdef WCDMA2GSM_WORKAROUND
       
   221     // Work around for QtMobility issue. Bearer management reports WCDMA bearer but
       
   222     // QSystemNetworkInfo sees connection as GSM mode. 
       
   223     if ((mode == QSystemNetworkInfo::WcdmaMode) && (strength < 0)) {
       
   224         strength = m_networkInfo->networkSignalStrength(QSystemNetworkInfo::GsmMode);
       
   225         if (strength >= 0)
       
   226             mode = QSystemNetworkInfo::GsmMode;
       
   227     }
       
   228 #endif
       
   229 
       
   230     qDebug() << "SystemNetworkImpl::handleNetworkSessionNameChanged: set mode to " << mode;
       
   231     m_currentMode = mode;
       
   232 
       
   233     // emit signal strength of new connection,
       
   234     // use wrapper access method for correct WLAN strength
       
   235     qDebug() << "SystemNetworkImpl::handleNetworkSessionNameChanged: emit str=" << getNetworkSignalStrength();
       
   236     emit networkSignalStrengthChanged(getNetworkSignalStrength());
       
   237 
       
   238     // Update network name on mode change.
       
   239     qDebug() << "SystemNetworkImpl::handleNetworkSessionNameChanged: emit network name= " << getNetworkName();
       
   240     emit networkNameChanged(getNetworkName());
       
   241 }
       
   242 #endif // QT_MOBILITY_BEARER_MANAGEMENT
       
   243 
       
   244 } // GVA