connectionmonitoring/indicatorobserver/src/indicatorobserver.cpp
changeset 18 fcbbe021d614
child 27 489cf6208544
equal deleted inserted replaced
4:77415202bfc8 18:fcbbe021d614
       
     1 /*
       
     2  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     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  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description:
       
    15  * 
       
    16  */
       
    17 
       
    18 #include <QtCore>
       
    19 #include <QCoreApplication>
       
    20 
       
    21 #include <hbindicator.h>
       
    22 #include <hbindicatorplugininterface.h>
       
    23 #include <hbindicatorinterface.h>
       
    24 #include <qnetworkconfigmanager.h>
       
    25 #include <qnetworkconfiguration.h>
       
    26 #include <xqsettingsmanager.h>
       
    27 #include <wlandevicesettingsinternalcrkeys.h>
       
    28 
       
    29 #include "indicatorobserver.h"
       
    30 
       
    31 #include "OstTraceDefinitions.h"
       
    32 #ifdef OST_TRACE_COMPILER_IN_USE
       
    33 #include "indicatorobserverTraces.h"
       
    34 #endif
       
    35 
       
    36 QTM_USE_NAMESPACE
       
    37 
       
    38 /*!
       
    39     IndicatorObserver::IndicatorObserver
       
    40 */
       
    41 IndicatorObserver::IndicatorObserver(int argc, char* argv[]) :
       
    42     QCoreApplication(argc, argv),
       
    43     mNetConfigurationManager(new QNetworkConfigurationManager(this)),
       
    44     mSettingsManager(new XQSettingsManager(this)),
       
    45     mActiveCellularConfigurations(new QList<QNetworkConfiguration>),
       
    46     mActiveWlanConfigurations(new QList<QNetworkConfiguration>),    
       
    47     mWlanEnabled(0),
       
    48     mWlanIndicatorIsActivated(false),
       
    49     mCellularIndicatorIsActivated(false)
       
    50     
       
    51 {
       
    52     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_CONSTRUCTOR_ENTRY, "-->");
       
    53 
       
    54     bool connectStatus = false;
       
    55 
       
    56     connectStatus = connect(
       
    57         mNetConfigurationManager,
       
    58         SIGNAL(configurationChanged(const QNetworkConfiguration&)),
       
    59         this,
       
    60         SLOT(handleConfigurationChanged(const QNetworkConfiguration&)));
       
    61     
       
    62     if (!connectStatus){
       
    63         OstTrace0(TRACE_FLOW, INDICATOROBSERVER_CONNECT_FAILED, "Connecting handleConfigurationChanged SLOT failed");     
       
    64         }
       
    65     
       
    66     connectStatus = connect(
       
    67         mSettingsManager,
       
    68         SIGNAL(valueChanged(const XQSettingsKey, const QVariant)),
       
    69         this,
       
    70         SLOT(updateWlanRadioStatus(const XQSettingsKey, const QVariant)));
       
    71     
       
    72     if (!connectStatus){
       
    73         OstTrace0(TRACE_FLOW, INDICATOROBSERVER_CONNECT_FAILED_DUP1, "Connecting updateWlanRadioStatus SLOT failed");     
       
    74         }
       
    75     
       
    76     // Subscribe for WLAN ON/OFF change indications
       
    77     XQSettingsKey wlanKey(
       
    78         XQSettingsKey::TargetCentralRepository,
       
    79         KCRUidWlanDeviceSettingsRegistryId.iUid,
       
    80         KWlanOnOff);
       
    81     
       
    82     //Read current status of WLAN radio
       
    83     //mWlanEnabled = mSettingsManager->readItemValue(wlanKey).toInt() ? true : false;
       
    84     mWlanEnabled = true; 
       
    85     //TODO: Replace above code with commented code when WlanOnOff implementation is in release.
       
    86     //TODO: Remeber to add check for read failure(call error() method from settings manager).
       
    87     
       
    88     mSettingsManager->startMonitoring(wlanKey);
       
    89 
       
    90     initializeIndicators();
       
    91     
       
    92     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_CONSTRUCTOR_EXIT, "<--");
       
    93 }
       
    94 
       
    95 /*!
       
    96     IndicatorObserver::~IndicatorObserver
       
    97 */
       
    98 IndicatorObserver::~IndicatorObserver()
       
    99 {
       
   100     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_DESTRUCTOR_ENTRY, "-->");
       
   101      
       
   102     if(mCellularIndicatorIsActivated) {
       
   103         deactivateCellularIndicatorPlugin();
       
   104     }
       
   105     
       
   106     if (mActiveCellularConfigurations) {
       
   107         mActiveCellularConfigurations->clear();
       
   108         delete mActiveCellularConfigurations;
       
   109         }
       
   110     
       
   111 
       
   112     if (mWlanIndicatorIsActivated) {
       
   113         deactivateWlanIndicatorPlugin();
       
   114     }
       
   115     
       
   116     if (mActiveWlanConfigurations) {
       
   117         mActiveWlanConfigurations->clear();
       
   118         delete mActiveWlanConfigurations;
       
   119         }
       
   120     
       
   121     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_DESTRUCTOR_EXIT, "<--");
       
   122 }
       
   123 
       
   124 /*!
       
   125     IndicatorObserver::initializeIndicators
       
   126 */
       
   127 void IndicatorObserver::initializeIndicators()
       
   128 {
       
   129 
       
   130     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_INITIALIZEINDICATORS_ENTRY, "-->");
       
   131     
       
   132     findActiveConfigurations();
       
   133     updateWlanIndicator();
       
   134     updateCellularIndicator();
       
   135     
       
   136     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_INITIALIZEINDICATORS_EXIT, "<--");
       
   137 }
       
   138 
       
   139 /*!
       
   140     IndicatorObserver::updateWlanRadioStatus
       
   141 */
       
   142 void IndicatorObserver::updateWlanRadioStatus(const XQSettingsKey &key, const QVariant &value)
       
   143 {
       
   144     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_UPDATEWLANRADIOSTATUS_ENTRY, "-->");
       
   145     
       
   146     // The key parameter is not used, since only WLAN ON/OFF setting is connected to this slot
       
   147     Q_UNUSED(key);
       
   148     
       
   149     // Inform about WLAN ON/OFF status change
       
   150     Q_UNUSED(value); //TODO: to be removed with final implementation. To keep compiler satisfied.
       
   151     //    mWlanEnabled = value.toInt() ? true : false;
       
   152     
       
   153     mWlanEnabled = true; //TODO: Replace with above code when WlanOnOff implementation is in release
       
   154     
       
   155     if (mWlanEnabled == false) {    
       
   156         deactivateWlanIndicatorPlugin();
       
   157     } else {
       
   158         updateWlanIndicator();
       
   159     }
       
   160     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_UPDATEWLANRADIOSTATUS_EXIT, "<--");
       
   161 }
       
   162 
       
   163 /*!
       
   164     IndicatorObserver::findActiveConfigurations
       
   165 */
       
   166 void IndicatorObserver::findActiveConfigurations()
       
   167 {
       
   168     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_FINDACTIVECONFIGURATIONS_ENTRY, "-->");
       
   169     
       
   170     mActiveWlanConfigurations->clear();
       
   171     mActiveCellularConfigurations->clear();
       
   172             
       
   173     //Let's find active connections if any    
       
   174     QList<QNetworkConfiguration> allConfigurations = mNetConfigurationManager->allConfigurations(QNetworkConfiguration::Active);
       
   175     
       
   176     for (int i=0; i<allConfigurations.count(); i++) {
       
   177         if (allConfigurations[i].type() == QNetworkConfiguration::InternetAccessPoint) {
       
   178 
       
   179             //Let's check the bearer type
       
   180             QString bearerName = allConfigurations[i].bearerName();
       
   181         
       
   182             if (bearerName == bearerWLAN) {
       
   183                 mActiveWlanConfigurations->append(allConfigurations[i]);
       
   184             } else if (bearerName == bearer2G ||
       
   185                        bearerName == bearerWCDMA ||
       
   186                        bearerName == bearerHSPA || 
       
   187                        bearerName == bearerCDMA2000) {
       
   188                 mActiveCellularConfigurations->append(allConfigurations[i]);
       
   189             }  
       
   190         }
       
   191     }
       
   192     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_FINDACTIVECONFIGURATIONS_EXIT, "<--");
       
   193 }
       
   194 
       
   195 /*!
       
   196     IndicatorObserver::updateWlanIndicator
       
   197 */
       
   198 void IndicatorObserver::updateWlanIndicator()
       
   199 {
       
   200     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_UPDATEWLANINDICATOR_ENTRY, "-->");
       
   201     QList<QVariant> list;
       
   202     
       
   203     int count = mActiveWlanConfigurations->count();
       
   204 
       
   205     //We do not deactivate WlanIndicator plugin here as it is done in updateWlanRadioStatus method
       
   206     //as WLAN radio status determines whether to show indicator or not
       
   207     if ( mWlanEnabled ) {
       
   208         if(count == 0) {
       
   209             list.insert(0, wlanNotConnected);
       
   210             activateWlanIndicatorPlugin(list);
       
   211         } else {
       
   212             list.insert(0, wlanConnected);
       
   213             list.insert(1, mActiveWlanConfigurations->at(0).name());          
       
   214             activateWlanIndicatorPlugin(list);
       
   215         }
       
   216     }
       
   217     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_UPDATEWLANINDICATOR_EXIT, "<--");
       
   218 }
       
   219 
       
   220 /*!
       
   221     IndicatorObserver::updateCellularIndicator
       
   222 */
       
   223 void IndicatorObserver::updateCellularIndicator()
       
   224 {
       
   225     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_UPDATECELLULARINDICATOR_ENTRY, "-->");
       
   226     
       
   227     QList<QVariant> list;
       
   228 
       
   229     int count = mActiveCellularConfigurations->count();
       
   230  
       
   231     if ( count == 0 ) {
       
   232         if ( mCellularIndicatorIsActivated ){
       
   233             deactivateCellularIndicatorPlugin();
       
   234         }
       
   235     } else {
       
   236         if (count == 1) {
       
   237             list.insert(0, count);
       
   238             list.insert(1, mActiveCellularConfigurations->at(0).name());
       
   239             list.insert(2, mActiveCellularConfigurations->at(0).identifier().toInt());
       
   240         } else {
       
   241             list.insert(0, count);
       
   242         
       
   243         } 
       
   244         activateCellularIndicatorPlugin(list);        
       
   245     }
       
   246     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_UPDATECELLULARINDICATOR_EXIT, "<--");
       
   247 }
       
   248 
       
   249 /*!
       
   250     IndicatorObserver::handleConfigurationChanged
       
   251 */
       
   252 void IndicatorObserver::handleConfigurationChanged(const QNetworkConfiguration& config)
       
   253 {
       
   254     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_HANDLECONFIGURATIONCHANGED_ENTRY, "-->");
       
   255     
       
   256     switch (config.state())
       
   257     {   
       
   258         case QNetworkConfiguration::Defined: //To handle situation where we have active connection and it is lost due to bad radio conditions 
       
   259         case QNetworkConfiguration::Discovered:            
       
   260         case QNetworkConfiguration::Active:
       
   261             {
       
   262                 findActiveConfigurations();
       
   263 
       
   264                 QString bearerName = config.bearerName();
       
   265                 
       
   266                 if (bearerName == bearerWLAN) {
       
   267                     updateWlanIndicator();
       
   268                 } else if (bearerName == bearer2G ||
       
   269                            bearerName == bearerWCDMA ||
       
   270                            bearerName == bearerHSPA  ||
       
   271                            bearerName == bearerCDMA2000) {                   
       
   272                     updateCellularIndicator();
       
   273                 }
       
   274             }
       
   275             break;
       
   276             
       
   277         default:
       
   278             break;
       
   279     }
       
   280     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_HANDLECONFIGURATIONCHANGED_EXIT, "<--");
       
   281 }
       
   282 
       
   283 /*!
       
   284     IndicatorObserver::activateCellularIndicatorPlugin
       
   285 */
       
   286 void IndicatorObserver::activateCellularIndicatorPlugin(QList<QVariant> list)
       
   287 {
       
   288     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_ACTIVATECELLULARINDICATORPLUGIN_ENTRY, "-->");
       
   289     
       
   290     HbIndicator indicator;    
       
   291     bool success = indicator.activate("com.nokia.hb.indicator.connectivity.cellularindicatorplugin/1.0", list);
       
   292     
       
   293     if (!success) {
       
   294         mCellularIndicatorIsActivated = false;
       
   295         OstTrace0(TRACE_FLOW, INDICATOROBSERVER_CELLULAR_INDICATOR_ACTIVATION_FAILED, "Cellular indicator activation failed"); 
       
   296     } else {
       
   297         mCellularIndicatorIsActivated = true;
       
   298     }
       
   299     
       
   300     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_ACTIVATECELLULARINDICATORPLUGIN_EXIT, "<--");
       
   301 }
       
   302 
       
   303 /*!
       
   304     IndicatorObserver::deactivateCellularIndicatorPlugin
       
   305 */
       
   306 void IndicatorObserver::deactivateCellularIndicatorPlugin()
       
   307 {
       
   308     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_DEACTIVATECELLULARINDICATORPLUGIN_ENTRY, "-->");
       
   309     
       
   310     HbIndicator indicator;
       
   311     indicator.deactivate("com.nokia.hb.indicator.connectivity.cellularindicatorplugin/1.0");
       
   312     mCellularIndicatorIsActivated = false;
       
   313     
       
   314     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_DEACTIVATECELLULARINDICATORPLUGIN_EXIT, "<--");
       
   315 }
       
   316 
       
   317 /*!
       
   318     IndicatorObserver::activateWlanIndicatorPlugin
       
   319 */
       
   320 void IndicatorObserver::activateWlanIndicatorPlugin(QList<QVariant> list)
       
   321 {
       
   322     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_ACTIVATEWLANINDICATORPLUGIN_ENTRY, "-->");
       
   323 
       
   324     HbIndicator indicator;
       
   325     bool success = indicator.activate("com.nokia.hb.indicator.connectivity.wlanindicatorplugin/1.0", list);
       
   326     
       
   327     if (!success) {
       
   328         mWlanIndicatorIsActivated = false;
       
   329         OstTrace0(TRACE_FLOW, INDICATOROBSERVER_WLAN_INDICATOR_ACTIVATION_FAILED, "WLAN indicator activation failed"); 
       
   330     } else {
       
   331         mWlanIndicatorIsActivated = true;
       
   332     }
       
   333     
       
   334     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_ACTIVATEWLANINDICATORPLUGIN_EXIT, "<--");    
       
   335 }
       
   336 
       
   337 /*!
       
   338     IndicatorObserver::deactivateWlanIndicatorPlugin
       
   339 */
       
   340 void IndicatorObserver::deactivateWlanIndicatorPlugin()
       
   341 {
       
   342     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_DEACTIVATEWLANINDICATORPLUGIN_ENTRY, "-->");
       
   343 
       
   344     HbIndicator indicator;
       
   345     indicator.deactivate("com.nokia.hb.indicator.connectivity.wlanindicatorplugin/1.0");
       
   346     mWlanIndicatorIsActivated = false;
       
   347     
       
   348     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_DEACTIVATEWLANINDICATORPLUGIN_EXIT, "<--");   
       
   349 }