ginebra2/DeviceDelegate.cpp
changeset 3 0954f5dd2cd0
parent 0 1450b09d0cfd
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
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
     4 *
     9 * Initial Contributors:
     5 * This program is free software: you can redistribute it and/or modify
    10 * Nokia Corporation - initial contribution.
     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.
    11 *
     8 *
    12 * Contributors:
     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 *
    13 *
    14 * Description: 
    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/".
    15 *
    17 *
       
    18 * Description:
       
    19 *   This file implements the DeviceDelegate class.
    16 */
    20 */
    17 
       
    18 #include <QList>
    21 #include <QList>
    19 #include <QString>
    22 #include <QString>
    20 #include "DeviceDelegate.h"
    23 #include "DeviceDelegate.h"
    21 #include "Utilities.h"
    24 #include "Utilities.h"
    22 
    25 
    23 namespace GVA {
    26 namespace GVA {
    24 //! DeviceDelegate default constructor.
    27 
    25 DeviceDelegate::DeviceDelegate()
    28 DeviceDelegate::DeviceDelegate(DeviceImpl *deviceImpl = NULL)
    26     : m_batteryCharging(false), m_updating(false)
       
    27 {
    29 {
    28     // this will be the name javascript uses to access properties and signals 
    30       m_deviceImpl = deviceImpl;
       
    31       if (m_deviceImpl)
       
    32       {
       
    33           // When the m_deviceInfo signals battery level changed,
       
    34         // DeviceDelegate emits batteryLevelChanged
       
    35         safe_connect(m_deviceImpl, SIGNAL(batteryLevelChanged(int)),
       
    36             this, SIGNAL(batteryLevelChanged(int)));
       
    37       }
       
    38 
       
    39     // this will be the name javascript uses to access properties and signals
    29     // from this class
    40     // from this class
    30     setObjectName("deviceDelegate");
    41     setObjectName("deviceDelegate");
    31     
       
    32 #ifdef QT_MOBILITY_BEARER_SYSINFO
       
    33     // create Qt Mobility API objects for device info
       
    34     m_deviceInfo = new QSystemDeviceInfo(this);
       
    35     m_networkInfo = new QSystemNetworkInfo(this);
       
    36     m_networkConfigManager = new QNetworkConfigurationManager(this);
       
    37     
       
    38     // Guess the network mode currently used for browser - 
       
    39     // a guess is really the best we can do right now.
       
    40     m_currentMode = getInternetConfigurationMode();
       
    41     
       
    42     // need the configuration manager to update its configuration list, when it
       
    43     // is done it will send us a signal
       
    44     safe_connect(m_networkConfigManager, SIGNAL(updateCompleted()), 
       
    45         this, SLOT(handleUpdateComplete()));
       
    46 
       
    47     // tell configuration manager to update its configuration list
       
    48     m_updating = true;
       
    49     m_networkConfigManager->updateConfigurations();
       
    50     
       
    51     // set up handlers for other network configuration manager signals
       
    52     safe_connect(m_networkConfigManager, SIGNAL(configurationAdded(const QNetworkConfiguration&)),
       
    53             this, SLOT(configurationAdded(const QNetworkConfiguration&)));
       
    54     safe_connect(m_networkConfigManager, SIGNAL(configurationRemoved(const QNetworkConfiguration&)),
       
    55             this, SLOT(configurationRemoved(const QNetworkConfiguration&)));
       
    56     safe_connect(m_networkConfigManager, SIGNAL(configurationChanged(const QNetworkConfiguration&)),
       
    57             this, SLOT(configurationChanged(const QNetworkConfiguration)));
       
    58     
       
    59     // When the m_deviceInfo signals battery level changed, 
       
    60     // DeviceDelegate emits batteryLevelChanged
       
    61     safe_connect(m_deviceInfo, SIGNAL(batteryLevelChanged(int)), 
       
    62         this, SIGNAL(batteryLevelChanged(int)));
       
    63     
       
    64     // set current charging state then keep it up to date with signal handler
       
    65     m_batteryCharging = (m_deviceInfo->currentPowerState() == 
       
    66         QSystemDeviceInfo::WallPowerChargingBattery) ? true : false;
       
    67     //qDebug() << "DeviceDelegate: charging state = " << m_deviceInfo->currentPowerState();
       
    68     //qDebug() << "DeviceDelegate: charging = " << m_batteryCharging;
       
    69     safe_connect(m_deviceInfo, SIGNAL(powerStateChanged(QSystemDeviceInfo::PowerState)), 
       
    70         this, SLOT(handlePowerStateChanged(QSystemDeviceInfo::PowerState)));
       
    71     
       
    72     // set up handlers for system network info signals
       
    73     safe_connect(m_networkInfo, SIGNAL(networkSignalStrengthChanged(
       
    74         QSystemNetworkInfo::NetworkMode, int)), this, 
       
    75         SLOT(handleNetworkSignalStrengthChanged(QSystemNetworkInfo::NetworkMode, int)));
       
    76     
       
    77     safe_connect(m_networkInfo, SIGNAL(networkNameChanged(
       
    78         QSystemNetworkInfo::NetworkMode, const QString&)), this, 
       
    79         SLOT(handleNetworkNameChanged(QSystemNetworkInfo::NetworkMode, const QString&)));
       
    80 #endif // QT_MOBILITY_BEARER_SYSINFO
       
    81 }
    42 }
    82 
    43 
    83 //! DeviceDelegate destructor.
       
    84 DeviceDelegate::~DeviceDelegate()
    44 DeviceDelegate::~DeviceDelegate()
    85 {
    45 {
    86     // clean up
    46       // cleanup
    87 #ifdef QT_MOBILITY_BEARER_SYSINFO
    47       if (m_deviceImpl)
    88     delete m_deviceInfo;
    48         delete m_deviceImpl;
    89     delete m_networkInfo;
       
    90     delete m_networkConfigManager;
       
    91 #endif
       
    92 }
    49 }
    93 
       
    94 #ifdef QT_MOBILITY_BEARER_SYSINFO
       
    95 //! Converts bearer name string to network mode enumerator.
       
    96 //! The network configuration uses strings to represent the same info that
       
    97 //! the system network info uses an enumeration to represent
       
    98 /*!
       
    99   \param name bearer name to convert
       
   100 */
       
   101 QSystemNetworkInfo::NetworkMode DeviceDelegate::bearerNameToMode(QString name) const
       
   102 {
       
   103     QSystemNetworkInfo::NetworkMode mode = QSystemNetworkInfo::UnknownMode;
       
   104     
       
   105     if (name == "Unknown")
       
   106         mode = QSystemNetworkInfo::UnknownMode;
       
   107     else if (name == "Ethernet")
       
   108         mode = QSystemNetworkInfo::EthernetMode;
       
   109     else if (name == "WLAN")
       
   110         mode = QSystemNetworkInfo::WlanMode;
       
   111     else if (name == "2G") {
       
   112         // there currently isn't a 2G in the enumeration but by trial and 
       
   113         // error I found that it corresponds to either GSM or WCDMA mode
       
   114         // You can tell which mode to choose be getting the signal strength;  
       
   115         // if you have the wrong mode it returns -1.
       
   116         if (m_networkInfo->networkSignalStrength(QSystemNetworkInfo::GsmMode) >= 0)
       
   117             mode = QSystemNetworkInfo::GsmMode; // T-Mobile uses this mode
       
   118         else
       
   119             mode = QSystemNetworkInfo::WcdmaMode; // AT&T uses this mode
       
   120         //qDebug() << "DeviceDelegate: 2G";
       
   121     } else if (name == "CDMA2000")
       
   122         mode = QSystemNetworkInfo::CdmaMode;
       
   123     else if (name == "WCDMA")
       
   124         mode = QSystemNetworkInfo::WcdmaMode;
       
   125     else if (name == "HSPA")
       
   126         // HSPA isn't currently in the enumeration
       
   127         mode = QSystemNetworkInfo::UnknownMode;
       
   128     else if (name == "Bluetooth")
       
   129         mode = QSystemNetworkInfo::BluetoothMode;
       
   130     else if (name == "WiMAX")
       
   131         mode = QSystemNetworkInfo::WimaxMode;
       
   132     
       
   133     //qDebug() << "DeviceDelegate: Mode " << mode;
       
   134     
       
   135     return (mode);
       
   136 }
       
   137 #endif // QT_MOBILITY_BEARER_SYSINFO
       
   138 
    50 
   139 //! Gets the current battery level.
    51 //! Gets the current battery level.
   140 int DeviceDelegate::getBatteryLevel() const
    52 int DeviceDelegate::getBatteryLevel() const
   141 {
    53 {
   142 #ifdef QT_MOBILITY_BEARER_SYSINFO
    54     return m_deviceImpl->getBatteryLevel();
   143     return (m_deviceInfo->batteryLevel());
       
   144 #else
       
   145     return (100); // can't get real level, return full
       
   146 #endif
       
   147 }
    55 }
   148 
    56 
   149 //! Returns true if the battery is charging.
    57 //! Returns true if the battery is charging.
   150 bool DeviceDelegate::isBatteryCharging() const
    58 bool DeviceDelegate::isBatteryCharging() const
   151 {
    59 {
   152     return (m_batteryCharging);
    60     return m_deviceImpl->isBatteryCharging();
   153 }
    61 }
   154 
       
   155 //! Gets the network signal strength for the current network mode.
       
   156 int DeviceDelegate::getNetworkSignalStrength() const
       
   157 {
       
   158 #ifdef QT_MOBILITY_BEARER_SYSINFO
       
   159     int strength = m_networkInfo->networkSignalStrength(m_currentMode);
       
   160     
       
   161     // Strength in WLAN mode is reported as -1 by QtMobility
       
   162     if ((strength == -1) && (m_currentMode == QSystemNetworkInfo::WlanMode)) {
       
   163         strength = 100;
       
   164     }
       
   165     
       
   166     return (strength);
       
   167 #else
       
   168     return (100); // can't get real level, return full
       
   169 #endif
       
   170 }
       
   171 
       
   172 //! Gets the network name for the current network mode.
       
   173 QString DeviceDelegate::getNetworkName() const
       
   174 {
       
   175 #ifdef QT_MOBILITY_BEARER_SYSINFO
       
   176     QString netName = m_networkInfo->networkName(m_currentMode);
       
   177     
       
   178     // if WLAN SSID name is unknown show "WiFi"
       
   179     if ((m_currentMode == QSystemNetworkInfo::WlanMode) &&
       
   180         (netName == "")) {
       
   181         netName = "WiFi";
       
   182     }
       
   183     
       
   184     //qDebug() << "DeviceDelegate: network name " << netName;
       
   185     return (netName);
       
   186 #else
       
   187     return (""); // can't get real name
       
   188 #endif
       
   189 }
       
   190 
       
   191 #ifdef QT_MOBILITY_BEARER_SYSINFO
       
   192 //! Emits a signal for the specified signal strength.
       
   193 /*!
       
   194   \param strength new signal strength
       
   195 */
       
   196 void DeviceDelegate::updateSignalStrength(int strength)
       
   197 {
       
   198     //qDebug() << "DeviceDelegate: Signal Strength " << strength;
       
   199     // currently getting a -1 signal strength for WLAN, change to 100.
       
   200     if (strength == -1) {
       
   201         if (m_currentMode == QSystemNetworkInfo::WlanMode) {
       
   202             //qDebug() << "DeviceDelegate: In WLAN mode so use strength 100.";
       
   203             strength = 100;
       
   204         } //else if (m_currentMode != QSystemNetworkInfo::UnknownMode) {
       
   205         //    qDebug() << "DeviceDelegate: Neg str in mode " << m_currentMode;
       
   206         //}
       
   207     }
       
   208     
       
   209     emit networkSignalStrengthChanged(strength);
       
   210 }
       
   211 
       
   212 
       
   213 //! Search for an active internet access point and return the network mode.
       
   214 //! Returns the network mode used by the browser (best guess anyway)
       
   215 //! My best guess is that the first active internet access point is the one 
       
   216 //! used by the browser.
       
   217 QSystemNetworkInfo::NetworkMode DeviceDelegate::getInternetConfigurationMode()
       
   218 {
       
   219     // function return value
       
   220     QSystemNetworkInfo::NetworkMode mode = QSystemNetworkInfo::UnknownMode;
       
   221     QString bearerName; // network configuration bearer name
       
   222     bool found = false; // flag set when configuration found
       
   223     // active network configurations
       
   224     QList<QNetworkConfiguration> activeConfigurations = 
       
   225         m_networkConfigManager->allConfigurations(QNetworkConfiguration::Active);
       
   226     
       
   227     // check each active configuration until we find one that fits the bill
       
   228     while (!found && !activeConfigurations.isEmpty()) {
       
   229         QNetworkConfiguration config = activeConfigurations.takeFirst();
       
   230         
       
   231         //qDebug() << "DeviceDelegate: Have Active Config - type " << config.type();
       
   232         // service networks contain children configurations which need to be explored
       
   233         if (config.type() == QNetworkConfiguration::ServiceNetwork) {
       
   234             //qDebug() << "DeviceDelegate: Found a ServiceNetwork!";
       
   235             foreach (const QNetworkConfiguration &child, config.children()) {
       
   236                 if ((child.type() == QNetworkConfiguration::InternetAccessPoint) &&
       
   237                     (child.state() == QNetworkConfiguration::Active)) {
       
   238                     found = true;
       
   239                     m_currentConfigIdentifier = child.identifier();
       
   240                     bearerName = child.bearerName();
       
   241                     //qDebug() << "   Found InternetAccessPoint - " << bearerName;
       
   242                     //qDebug() << "   identifier: " << child.identifier();
       
   243                     break; // exit foreach loop
       
   244                 }
       
   245             }
       
   246         } else if (config.type() == QNetworkConfiguration::InternetAccessPoint) {
       
   247             found = true;
       
   248             m_currentConfigIdentifier = config.identifier();
       
   249             bearerName = config.bearerName();
       
   250             //qDebug() << "DeviceDelegate: Found an InternetAccessPoint - " << bearerName;
       
   251             //qDebug() << "   identifier: " << config.identifier();
       
   252         }
       
   253     }
       
   254     
       
   255     // get the mode of the found network configuration
       
   256     if (found) {
       
   257         //qDebug() << "DeviceDelegate::getInternetConfigurationMode: use bearer " << bearerName;
       
   258         mode = bearerNameToMode(bearerName);
       
   259     } else {
       
   260         //qDebug() << "DeviceDelegate: Failed to find an active internet access point.";
       
   261         m_currentConfigIdentifier = "";
       
   262     }
       
   263     
       
   264     return (mode);
       
   265 }
       
   266 
       
   267 //! Handles the updateCompleted signal from the configuration manager.
       
   268 void DeviceDelegate::handleUpdateComplete()
       
   269 {
       
   270     // search for appropriate network configuration mode again
       
   271     QSystemNetworkInfo::NetworkMode mode = getInternetConfigurationMode();
       
   272     
       
   273     // if the mode has changed we need to update the signal strength and network name
       
   274     if (mode != m_currentMode) {
       
   275         m_currentMode = mode;
       
   276         updateSignalStrength(m_networkInfo->networkSignalStrength(m_currentMode));
       
   277         emit networkNameChanged(getNetworkName());
       
   278     }
       
   279     
       
   280     m_updating = false;
       
   281 }
       
   282 
       
   283 //! Handles the configurationAdded signal from the configuration manager.
       
   284 /*!
       
   285   \param config added network configuration
       
   286 */
       
   287 void DeviceDelegate::configurationAdded(const QNetworkConfiguration &config)
       
   288 {
       
   289     //qDebug() << "DeviceDelegate: Configuration Added.";
       
   290     if (!m_updating && (m_currentMode == QSystemNetworkInfo::UnknownMode) &&
       
   291         (config.state() == QNetworkConfiguration::Active) && 
       
   292         (config.type() == QNetworkConfiguration::InternetAccessPoint)) {
       
   293         // use this new configuration's mode, update the signal strength and network name
       
   294         //qDebug() << "DeviceDelegate: Use new configuration with bearer " << config.bearerName();
       
   295         m_currentConfigIdentifier = config.identifier();
       
   296         m_currentMode = bearerNameToMode(config.bearerName());
       
   297         updateSignalStrength(m_networkInfo->networkSignalStrength(m_currentMode));
       
   298         emit networkNameChanged(getNetworkName());
       
   299     }
       
   300 }
       
   301 
       
   302 //! Handles the configurationRemoved signal from the configuration manager.
       
   303 /*!
       
   304   \param config removed network configuration
       
   305 */
       
   306 void DeviceDelegate::configurationRemoved(const QNetworkConfiguration &config)
       
   307 {
       
   308     //qDebug() << "DeviceDelegate: Configuration Removed.";
       
   309     if ((!m_updating) && (config.identifier() == m_currentConfigIdentifier)) {
       
   310         //qDebug() << "DeviceDelegate: Find new Configuration.";
       
   311         // search for appropriate network configuration mode again
       
   312         m_currentMode = getInternetConfigurationMode();
       
   313         updateSignalStrength(m_networkInfo->networkSignalStrength(m_currentMode));
       
   314         emit networkNameChanged(getNetworkName());
       
   315     }
       
   316 }
       
   317 
       
   318 //! Handles the configurationChanged signal from the configuration manager.
       
   319 /*!
       
   320   \param config changed network configuration
       
   321 */
       
   322 void DeviceDelegate::configurationChanged(const QNetworkConfiguration &config)
       
   323 {
       
   324     //qDebug() << "DeviceDelegate: Configuration Changed.";
       
   325     if (!m_updating) {
       
   326         // if network mode currently unknown maybe this change will allow us to
       
   327         // identify the netork mode
       
   328         if (m_currentMode == QSystemNetworkInfo::UnknownMode) {
       
   329             // can we now identify the network mode?
       
   330             if ((config.state() == QNetworkConfiguration::Active) && 
       
   331                 (config.type() == QNetworkConfiguration::InternetAccessPoint)) {
       
   332                 //qDebug() << "DeviceDelegate: Use new configuration with bearer " << config.bearerName();
       
   333                 m_currentConfigIdentifier = config.identifier();
       
   334                 m_currentMode = bearerNameToMode(config.bearerName());
       
   335                 updateSignalStrength(m_networkInfo->networkSignalStrength(m_currentMode));
       
   336                 emit networkNameChanged(getNetworkName());
       
   337             }
       
   338         // Did the configuration currently being used change in a notable way?
       
   339         } else if ((config.identifier() == m_currentConfigIdentifier) && 
       
   340             ((config.state() != QNetworkConfiguration::Active) || 
       
   341              (config.type() != QNetworkConfiguration::InternetAccessPoint))) {
       
   342             //qDebug() << "DeviceDelegate: Change configuration.";
       
   343             // search for appropriate network configuration mode again
       
   344             m_currentMode = getInternetConfigurationMode();
       
   345             updateSignalStrength(m_networkInfo->networkSignalStrength(m_currentMode));
       
   346             emit networkNameChanged(getNetworkName());
       
   347         }
       
   348     }
       
   349 }
       
   350 
       
   351 //! Handles the networkSignalStrengthChanged signal from system network info.
       
   352 /*!
       
   353   \param mode network mode of connection that changed
       
   354   \param strength new signal strength
       
   355 */
       
   356 void DeviceDelegate::handleNetworkSignalStrengthChanged(
       
   357     QSystemNetworkInfo::NetworkMode mode, int strength)
       
   358 {
       
   359     // Only send signal strength changes for current mode.
       
   360     if (mode == m_currentMode)
       
   361         updateSignalStrength(strength);
       
   362 }
       
   363 
       
   364 //! Handles the networkNameChanged signal from system network info.
       
   365 /*!
       
   366   \param mode network mode of connection that changed
       
   367   \param name new network name
       
   368 */
       
   369 void DeviceDelegate::handleNetworkNameChanged(
       
   370         QSystemNetworkInfo::NetworkMode mode, const QString& name)
       
   371 {
       
   372     // Only send network name changes for current mode.
       
   373     if (mode == m_currentMode)
       
   374         emit networkNameChanged(name);
       
   375 }
       
   376 
       
   377 //! Handles the powerStateChanged signal from system device info.
       
   378 /*!
       
   379   \param state new power state
       
   380 */
       
   381 void DeviceDelegate::handlePowerStateChanged(QSystemDeviceInfo::PowerState state)
       
   382 {
       
   383     bool batteryCharging = 
       
   384         (state == QSystemDeviceInfo::WallPowerChargingBattery) ? true : false;
       
   385     
       
   386     //qDebug() << "DeviceDelegate: new charging state = " << state;
       
   387     if (batteryCharging != m_batteryCharging) {
       
   388         m_batteryCharging = batteryCharging;
       
   389         //qDebug() << "DeviceDelegate: new charging = " << m_batteryCharging;
       
   390         // emit battery level - subscriber will get charging state if desired
       
   391         emit batteryLevelChanged(m_deviceInfo->batteryLevel());
       
   392     }
       
   393 }
       
   394 
       
   395 #endif // QT_MOBILITY_BEARER_SYSINFO
       
   396 
    62 
   397 } // GVA
    63 } // GVA
   398 
    64