connectionmonitoring/indicatorobserver/src/indicatorobserver.cpp
branchRCL_3
changeset 58 83ca720e2b9a
parent 57 05bc53fe583b
child 62 bb1f80fb7db2
equal deleted inserted replaced
57:05bc53fe583b 58:83ca720e2b9a
     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     mWlanForceDisabled(0),
       
    49     mWlanIndicatorIsActivated(false),
       
    50     mCellularIndicatorIsActivated(false),
       
    51     mWlanIndicator(NULL),
       
    52     mCellularIndicator(NULL)
       
    53     
       
    54 {
       
    55     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_CONSTRUCTOR_ENTRY, "-->");
       
    56 
       
    57     bool connectStatus = false;
       
    58 
       
    59     connectStatus = connect(
       
    60         mNetConfigurationManager,
       
    61         SIGNAL(configurationChanged(const QNetworkConfiguration&)),
       
    62         this,
       
    63         SLOT(handleConfigurationChanged(const QNetworkConfiguration&)));
       
    64     
       
    65     if (!connectStatus){
       
    66         OstTrace0(TRACE_FLOW, INDICATOROBSERVER_CONNECT_FAILED, "Connecting handleConfigurationChanged SLOT failed");     
       
    67         }
       
    68     
       
    69     connectStatus = connect(
       
    70         mSettingsManager,
       
    71         SIGNAL(valueChanged(const XQSettingsKey, const QVariant)),
       
    72         this,
       
    73         SLOT(updateWlanRadioStatus(const XQSettingsKey, const QVariant)));
       
    74     
       
    75     if (!connectStatus){
       
    76         OstTrace0(TRACE_FLOW, INDICATOROBSERVER_CONNECT_FAILED_DUP1, "Connecting updateWlanRadioStatus SLOT failed");     
       
    77         }
       
    78     
       
    79     // Subscribe for WLAN ON/OFF change indications
       
    80     XQSettingsKey wlanKey(
       
    81         XQSettingsKey::TargetCentralRepository,
       
    82         KCRUidWlanDeviceSettingsRegistryId.iUid,
       
    83         KWlanOnOff);
       
    84     
       
    85     // Also subscribe for KForceDisableWlan change indications
       
    86     XQSettingsKey wlanForceKey(
       
    87         XQSettingsKey::TargetCentralRepository,
       
    88         KCRUidWlanDeviceSettingsRegistryId.iUid, 
       
    89         KWlanForceDisable);
       
    90     
       
    91     //Read current status of WLAN radio
       
    92     mWlanEnabled = mSettingsManager->readItemValue(wlanKey).toInt() ? true : false;
       
    93     mWlanForceDisabled = mSettingsManager->readItemValue(wlanForceKey).toInt() ? true : false;
       
    94 
       
    95     mSettingsManager->startMonitoring(wlanKey);
       
    96     mSettingsManager->startMonitoring(wlanForceKey);
       
    97 
       
    98     initializeIndicators();
       
    99     
       
   100     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_CONSTRUCTOR_EXIT, "<--");
       
   101 }
       
   102 
       
   103 
       
   104 /*!
       
   105     IndicatorObserver::~IndicatorObserver
       
   106 */
       
   107 IndicatorObserver::~IndicatorObserver()
       
   108 {
       
   109     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_DESTRUCTOR_ENTRY, "-->");
       
   110      
       
   111     if(mCellularIndicatorIsActivated) {
       
   112         deactivateCellularIndicatorPlugin();
       
   113     }
       
   114     
       
   115     if (mActiveCellularConfigurations) {
       
   116         mActiveCellularConfigurations->clear();
       
   117         delete mActiveCellularConfigurations;
       
   118         }
       
   119     
       
   120 
       
   121     if (mWlanIndicatorIsActivated) {
       
   122         deactivateWlanIndicatorPlugin();
       
   123     }
       
   124     
       
   125     if (mActiveWlanConfigurations) {
       
   126         mActiveWlanConfigurations->clear();
       
   127         delete mActiveWlanConfigurations;
       
   128         }
       
   129     
       
   130     delete mCellularIndicator;
       
   131     delete mWlanIndicator;
       
   132     
       
   133     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_DESTRUCTOR_EXIT, "<--");
       
   134 }
       
   135 
       
   136 /*!
       
   137     IndicatorObserver::initializeIndicators
       
   138 */
       
   139 void IndicatorObserver::initializeIndicators()
       
   140 {
       
   141 
       
   142     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_INITIALIZEINDICATORS_ENTRY, "-->");
       
   143     
       
   144     // create the indicators and make connections
       
   145     mCellularIndicator = new HbIndicator();  
       
   146     // connect the user activated slot here, so that the indicator know to start the connview in case
       
   147     // the user taps the indicator
       
   148     bool conn = QObject::connect(
       
   149                     mCellularIndicator, 
       
   150                     SIGNAL(userActivated(const QString&, const QVariantMap&)), 
       
   151                     this, 
       
   152                     SLOT(userActivateCellularIndicator(const QString&, const QVariantMap&)));
       
   153     
       
   154     mWlanIndicator = new HbIndicator();  
       
   155     // connect the user activated slot here, so that the indicator know to start the wlan sniffer in case
       
   156     // the user taps the indicator
       
   157     conn = QObject::connect(
       
   158                     mWlanIndicator, 
       
   159                     SIGNAL(userActivated(const QString&, const QVariantMap&)), 
       
   160                     this, 
       
   161                     SLOT(userActivateWlanIndicator(const QString&, const QVariantMap&)));
       
   162     
       
   163     findActiveConfigurations();
       
   164     updateWlanIndicator();
       
   165     updateCellularIndicator();
       
   166     
       
   167     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_INITIALIZEINDICATORS_EXIT, "<--");
       
   168 }
       
   169 
       
   170 /*!
       
   171     IndicatorObserver::updateWlanRadioStatus
       
   172 */
       
   173 void IndicatorObserver::updateWlanRadioStatus(const XQSettingsKey &key, const QVariant &value)
       
   174 {
       
   175     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_UPDATEWLANRADIOSTATUS_ENTRY, "-->");
       
   176     
       
   177     // The change notification is received either for the WlanOnoff key, or the
       
   178     // ForceDisableWlan key
       
   179     if (KWlanOnOff == key.key()) {
       
   180         mWlanEnabled = value.toInt() ? true : false;
       
   181     } else {
       
   182         mWlanForceDisabled = value.toInt() ? true : false;
       
   183     }
       
   184     
       
   185     if (mWlanEnabled == false || mWlanForceDisabled == true) {    
       
   186         deactivateWlanIndicatorPlugin();
       
   187     } else {
       
   188         updateWlanIndicator();
       
   189     }
       
   190     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_UPDATEWLANRADIOSTATUS_EXIT, "<--");
       
   191 }
       
   192 
       
   193 /*!
       
   194     IndicatorObserver::findActiveConfigurations
       
   195 */
       
   196 void IndicatorObserver::findActiveConfigurations()
       
   197 {
       
   198     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_FINDACTIVECONFIGURATIONS_ENTRY, "-->");
       
   199     
       
   200     mActiveWlanConfigurations->clear();
       
   201     mActiveCellularConfigurations->clear();
       
   202             
       
   203     //Let's find active connections if any    
       
   204     QList<QNetworkConfiguration> allConfigurations = mNetConfigurationManager->allConfigurations(QNetworkConfiguration::Active);
       
   205     
       
   206     for (int i=0; i<allConfigurations.count(); i++) {
       
   207         if (allConfigurations[i].type() == QNetworkConfiguration::InternetAccessPoint) {
       
   208 
       
   209             //Let's check the bearer type
       
   210             QString bearerName = allConfigurations[i].bearerName();
       
   211         
       
   212             if (bearerName == bearerWLAN) {
       
   213                 mActiveWlanConfigurations->append(allConfigurations[i]);
       
   214             } else if (bearerName == bearer2G ||
       
   215                        bearerName == bearerWCDMA ||
       
   216                        bearerName == bearerHSPA || 
       
   217                        bearerName == bearerCDMA2000) {
       
   218                 mActiveCellularConfigurations->append(allConfigurations[i]);
       
   219             }  
       
   220         }
       
   221     }
       
   222     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_FINDACTIVECONFIGURATIONS_EXIT, "<--");
       
   223 }
       
   224 
       
   225 /*!
       
   226     IndicatorObserver::updateWlanIndicator
       
   227 */
       
   228 void IndicatorObserver::updateWlanIndicator()
       
   229 {
       
   230     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_UPDATEWLANINDICATOR_ENTRY, "-->");
       
   231     QList<QVariant> list;
       
   232     
       
   233     int count = mActiveWlanConfigurations->count();
       
   234 
       
   235     //We do not deactivate WlanIndicator plugin here as it is done in updateWlanRadioStatus method
       
   236     //as WLAN radio status determines whether to show indicator or not
       
   237     if ( mWlanEnabled && !mWlanForceDisabled) {
       
   238         if(count == 0) {
       
   239             list.insert(0, wlanNotConnected);
       
   240             activateWlanIndicatorPlugin(list);
       
   241         } else {
       
   242             list.insert(0, wlanConnected);
       
   243             list.insert(1, mActiveWlanConfigurations->at(0).name());          
       
   244             activateWlanIndicatorPlugin(list);
       
   245         }
       
   246     }
       
   247     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_UPDATEWLANINDICATOR_EXIT, "<--");
       
   248 }
       
   249 
       
   250 /*!
       
   251     IndicatorObserver::updateCellularIndicator
       
   252 */
       
   253 void IndicatorObserver::updateCellularIndicator()
       
   254 {
       
   255     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_UPDATECELLULARINDICATOR_ENTRY, "-->");
       
   256     
       
   257     QList<QVariant> list;
       
   258 
       
   259     int count = mActiveCellularConfigurations->count();
       
   260  
       
   261     if ( count == 0 ) {
       
   262         if ( mCellularIndicatorIsActivated ){
       
   263             deactivateCellularIndicatorPlugin();
       
   264         }
       
   265     } else {
       
   266         if (count == 1) {
       
   267             list.insert(0, count);
       
   268             list.insert(1, mActiveCellularConfigurations->at(0).name());
       
   269             list.insert(2, mActiveCellularConfigurations->at(0).identifier().toInt());
       
   270         } else {
       
   271             list.insert(0, count);
       
   272         
       
   273         } 
       
   274         activateCellularIndicatorPlugin(list);        
       
   275     }
       
   276     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_UPDATECELLULARINDICATOR_EXIT, "<--");
       
   277 }
       
   278 
       
   279 /*!
       
   280     IndicatorObserver::handleConfigurationChanged
       
   281 */
       
   282 void IndicatorObserver::handleConfigurationChanged(const QNetworkConfiguration& config)
       
   283 {
       
   284     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_HANDLECONFIGURATIONCHANGED_ENTRY, "-->");
       
   285     
       
   286     switch (config.state())
       
   287     {   
       
   288         case QNetworkConfiguration::Defined: //To handle situation where we have active connection and it is lost due to bad radio conditions 
       
   289         case QNetworkConfiguration::Discovered:            
       
   290         case QNetworkConfiguration::Active:
       
   291             {
       
   292                 findActiveConfigurations();
       
   293 
       
   294                 QString bearerName = config.bearerName();
       
   295                 
       
   296                 if (bearerName == bearerWLAN) {
       
   297                     updateWlanIndicator();
       
   298                 } else if (bearerName == bearer2G ||
       
   299                            bearerName == bearerWCDMA ||
       
   300                            bearerName == bearerHSPA  ||
       
   301                            bearerName == bearerCDMA2000) {                   
       
   302                     updateCellularIndicator();
       
   303                 }
       
   304             }
       
   305             break;
       
   306             
       
   307         default:
       
   308             break;
       
   309     }
       
   310     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_HANDLECONFIGURATIONCHANGED_EXIT, "<--");
       
   311 }
       
   312 
       
   313 void IndicatorObserver::userActivateCellularIndicator(const QString &type, const QVariantMap &data)
       
   314 {
       
   315     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_USERACTIVATECELLULARINDICATOR_ENTRY, "-->");
       
   316     
       
   317     Q_UNUSED(data);
       
   318     Q_UNUSED(type);
       
   319     // Show connection view
       
   320     QProcess::startDetached("connview");
       
   321     
       
   322     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_USERACTIVATECELLULARINDICATOR_EXIT, "<--");
       
   323 }
       
   324 
       
   325 void IndicatorObserver::userActivateWlanIndicator(const QString &type, const QVariantMap &data)
       
   326 {
       
   327     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_USERACTIVATEWLANINDICATOR_ENTRY, "-->");
       
   328     
       
   329     Q_UNUSED(data);
       
   330     Q_UNUSED(type);
       
   331     // Show wlan list view
       
   332     QProcess::startDetached("WlanSniffer");
       
   333 
       
   334     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_USERACTIVATEWLANINDICATOR_EXIT, "<--");
       
   335 }
       
   336 
       
   337 /*!
       
   338     IndicatorObserver::activateCellularIndicatorPlugin
       
   339 */
       
   340 void IndicatorObserver::activateCellularIndicatorPlugin(QList<QVariant> list)
       
   341 {
       
   342     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_ACTIVATECELLULARINDICATORPLUGIN_ENTRY, "-->");
       
   343     
       
   344     bool success = mCellularIndicator->activate("com.nokia.hb.indicator.connectivity.cellularindicatorplugin/1.0", list);
       
   345      
       
   346     if (!success) {
       
   347         mCellularIndicatorIsActivated = false;
       
   348         OstTrace0(TRACE_FLOW, INDICATOROBSERVER_CELLULAR_INDICATOR_ACTIVATION_FAILED, "Cellular indicator activation failed"); 
       
   349     } else {
       
   350         mCellularIndicatorIsActivated = true;
       
   351     }
       
   352     
       
   353     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_ACTIVATECELLULARINDICATORPLUGIN_EXIT, "<--");
       
   354 }
       
   355 
       
   356 /*!
       
   357     IndicatorObserver::deactivateCellularIndicatorPlugin
       
   358 */
       
   359 void IndicatorObserver::deactivateCellularIndicatorPlugin()
       
   360 {
       
   361     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_DEACTIVATECELLULARINDICATORPLUGIN_ENTRY, "-->");
       
   362     
       
   363     mCellularIndicator->deactivate("com.nokia.hb.indicator.connectivity.cellularindicatorplugin/1.0");
       
   364     mCellularIndicatorIsActivated = false;
       
   365     
       
   366     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_DEACTIVATECELLULARINDICATORPLUGIN_EXIT, "<--");
       
   367 }
       
   368 
       
   369 /*!
       
   370     IndicatorObserver::activateWlanIndicatorPlugin
       
   371 */
       
   372 void IndicatorObserver::activateWlanIndicatorPlugin(QList<QVariant> list)
       
   373 {
       
   374     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_ACTIVATEWLANINDICATORPLUGIN_ENTRY, "-->");
       
   375     
       
   376     bool success = mWlanIndicator->activate("com.nokia.hb.indicator.connectivity.wlanindicatorplugin/1.0", list);
       
   377    
       
   378     if (!success) {
       
   379         mWlanIndicatorIsActivated = false;
       
   380         OstTrace0(TRACE_FLOW, INDICATOROBSERVER_WLAN_INDICATOR_ACTIVATION_FAILED, "WLAN indicator activation failed"); 
       
   381     } else {
       
   382         mWlanIndicatorIsActivated = true;
       
   383     }
       
   384     
       
   385     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_ACTIVATEWLANINDICATORPLUGIN_EXIT, "<--");    
       
   386 }
       
   387 
       
   388 /*!
       
   389     IndicatorObserver::deactivateWlanIndicatorPlugin
       
   390 */
       
   391 void IndicatorObserver::deactivateWlanIndicatorPlugin()
       
   392 {
       
   393     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_DEACTIVATEWLANINDICATORPLUGIN_ENTRY, "-->");
       
   394 
       
   395     mWlanIndicator->deactivate("com.nokia.hb.indicator.connectivity.wlanindicatorplugin/1.0");
       
   396     mWlanIndicatorIsActivated = false;
       
   397     
       
   398     OstTrace0(TRACE_FLOW, INDICATOROBSERVER_DEACTIVATEWLANINDICATORPLUGIN_EXIT, "<--");   
       
   399 }