wlanutilities/wlanindicatorplugin/src/wlanindicatorpluginimplementation.cpp
changeset 0 56b72877c1cb
child 12 981afc7d3841
equal deleted inserted replaced
-1:000000000000 0:56b72877c1cb
       
     1 /*
       
     2 * Copyright (c) 2008 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:  wlanindicatorplugin implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <eikenv.h>
       
    22 #include <avkon.rsg>
       
    23 #include <avkon.hrh>
       
    24 #include <apgcli.h>
       
    25 #include <apgtask.h>
       
    26 #include <coemain.h>
       
    27 #include <bautils.h>
       
    28 #include <data_caging_path_literals.hrh>
       
    29 #include <StringLoader.h>
       
    30 #include <rconnmon.h>
       
    31 #include <cmmanager.h>
       
    32 #include <cmconnectionmethod.h>
       
    33 #include <wlanmgmtclient.h>
       
    34 #include <wlanscaninfo.h>
       
    35 #include <cdblen.h>
       
    36 #include <commdb.h>
       
    37 #include <WlanCdbCols.h>
       
    38 #include <utf.h>
       
    39 #include <wlanindicator.rsg>
       
    40 #include "wlanindicatorpluginimplementation.h"
       
    41 
       
    42 
       
    43 // CONSTANTS
       
    44 const TUid KSnifferAppUid = { 0x10281CAA };
       
    45 _LIT( KDriveZ, "z:" );
       
    46 _LIT( KWlanIndicatorResourceFile, "wlanindicator.rsc" );
       
    47 
       
    48 using namespace CMManager;
       
    49 
       
    50 // ======== MEMBER FUNCTIONS ========
       
    51 
       
    52 // ---------------------------------------------------------------------------
       
    53 // CWlanIndicatorPluginImplementation::CWlanIndicatorPluginImplementation
       
    54 // C++ default constructor can NOT contain any code, that
       
    55 // might leave.
       
    56 // ---------------------------------------------------------------------------
       
    57 //
       
    58 CWlanIndicatorPluginImplementation::CWlanIndicatorPluginImplementation()
       
    59     : iCoeEnv( CCoeEnv::Static() )
       
    60     {
       
    61     }
       
    62 
       
    63 // ---------------------------------------------------------------------------
       
    64 // CWlanIndicatorPluginImplementation::ConstructL
       
    65 // Symbian 2nd phase constructor can leave.
       
    66 // ---------------------------------------------------------------------------
       
    67 //
       
    68 void CWlanIndicatorPluginImplementation::ConstructL()
       
    69     {    
       
    70     TFileName fileName;
       
    71 
       
    72     fileName.Append( KDriveZ );
       
    73     fileName.Append( KDC_RESOURCE_FILES_DIR );   
       
    74     fileName.Append( KWlanIndicatorResourceFile );
       
    75 
       
    76     BaflUtils::NearestLanguageFile( iCoeEnv->FsSession(), fileName );
       
    77     iResource = iCoeEnv->AddResourceFileL( fileName );
       
    78     
       
    79     iConnMonitor.ConnectL();
       
    80     
       
    81     iWlanMgmtClient = CWlanMgmtClient::NewL();
       
    82     
       
    83     }
       
    84 
       
    85 // -----------------------------------------------------------------------------
       
    86 // CWlanIndicatorPluginImplementation::NewL
       
    87 // Two-phased constructor.
       
    88 // -----------------------------------------------------------------------------
       
    89 //
       
    90 CWlanIndicatorPluginImplementation* CWlanIndicatorPluginImplementation::NewL()
       
    91     {
       
    92     CWlanIndicatorPluginImplementation* self = 
       
    93         new( ELeave ) CWlanIndicatorPluginImplementation;
       
    94     CleanupStack::PushL( self );
       
    95     self->ConstructL();
       
    96     CleanupStack::Pop( self );
       
    97     return self;
       
    98     }
       
    99 
       
   100 // -----------------------------------------------------------------------------
       
   101 // CWlanIndicatorPluginImplementation::~CWlanIndicatorPluginImplementation
       
   102 // Destructor.
       
   103 // -----------------------------------------------------------------------------
       
   104 //
       
   105 CWlanIndicatorPluginImplementation::~CWlanIndicatorPluginImplementation()
       
   106     {
       
   107     iCoeEnv->DeleteResourceFile( iResource );
       
   108  
       
   109     iConnMonitor.Close();
       
   110                
       
   111     delete iWlanMgmtClient;
       
   112     
       
   113     }
       
   114 
       
   115 
       
   116 // ---------------------------------------------------------------------------
       
   117 // CWlanIndicatorPluginImplementation::HandleIndicatorTapL
       
   118 // ---------------------------------------------------------------------------
       
   119 //
       
   120 void CWlanIndicatorPluginImplementation::HandleIndicatorTapL( const TInt aUid )
       
   121     {
       
   122     
       
   123     switch ( aUid )
       
   124         {
       
   125         case EAknIndicatorWlanAvailable:
       
   126         case EAknIndicatorWlanActive:
       
   127         case EAknIndicatorWlanActiveSecure:
       
   128             {
       
   129 
       
   130             LaunchWlanSnifferL();
       
   131             
       
   132             break;
       
   133             }   
       
   134 
       
   135         default:
       
   136             
       
   137             break;
       
   138 
       
   139         }
       
   140 
       
   141 
       
   142     }
       
   143 
       
   144 
       
   145 
       
   146 // ---------------------------------------------------------------------------
       
   147 // CWlanIndicatorPluginImplementation::TextL( const TInt aUid, TInt& aTextType )
       
   148 // ---------------------------------------------------------------------------
       
   149 //    
       
   150 HBufC* CWlanIndicatorPluginImplementation::TextL( const TInt aUid,
       
   151                                                  TInt& aTextType )
       
   152     {   
       
   153     HBufC* textBuf = NULL;
       
   154     
       
   155     switch ( aUid )
       
   156         {
       
   157         case EAknIndicatorWlanAvailable:
       
   158             {
       
   159             textBuf = CreateWlanNetworksFoundTextL();           
       
   160             aTextType = EAknIndicatorPluginLinkText;
       
   161                         
       
   162             break;
       
   163             }
       
   164             
       
   165         case EAknIndicatorWlanActive:
       
   166         case EAknIndicatorWlanActiveSecure:
       
   167             {                        
       
   168             textBuf = CreateWlanConnectedTextL();
       
   169             aTextType = EAknIndicatorPluginLinkText;
       
   170             
       
   171             break;
       
   172             }
       
   173 
       
   174         default:
       
   175             
       
   176             break;
       
   177 
       
   178         }
       
   179     
       
   180     
       
   181     return textBuf;
       
   182     
       
   183     }
       
   184     
       
   185 // ---------------------------------------------------------------------------
       
   186 // CWlanIndicatorPluginImplementation::CreateWlanNetworksFoundTextL()
       
   187 // ---------------------------------------------------------------------------
       
   188 //
       
   189 HBufC* CWlanIndicatorPluginImplementation::CreateWlanNetworksFoundTextL()
       
   190     {
       
   191     
       
   192     HBufC* dynText = NULL;
       
   193     HBufC* knownNetworkName = NULL;   
       
   194     TInt availableCount( 0 );
       
   195 
       
   196     knownNetworkName = AvailableNetworksInfoL( availableCount );
       
   197     
       
   198     CleanupStack::PushL( knownNetworkName ); 
       
   199     
       
   200     if ( !knownNetworkName )
       
   201         {
       
   202         if ( availableCount == 1 )
       
   203             {
       
   204             // dynText: "qtn_uni_ind_wlan_avail_unknown        WLAN network found"
       
   205             dynText = StringLoader::LoadL (
       
   206                     R_QTN_UNI_IND_WLAN_AVAIL_UNKNOWN, iCoeEnv );
       
   207             }
       
   208         else
       
   209             {
       
   210             // dynText: "qtn_uni_ind_wlan_avail_unknown_many    WLAN networks found"
       
   211             dynText = StringLoader::LoadL (
       
   212                     R_QTN_UNI_IND_WLAN_AVAIL_UNKNOWN_MANY, iCoeEnv );            
       
   213             }
       
   214         }
       
   215     else
       
   216         {
       
   217         // dynText: "qtn_uni_ind_wlan_avail_known        '%U' found"
       
   218         dynText = StringLoader::LoadL ( 
       
   219                 R_QTN_UNI_IND_WLAN_AVAIL_KNOWN, *knownNetworkName, iCoeEnv );
       
   220                        
       
   221         }
       
   222     
       
   223     CleanupStack::PopAndDestroy( knownNetworkName );
       
   224 
       
   225     return dynText;
       
   226     }
       
   227 
       
   228 
       
   229 // ---------------------------------------------------------------------------
       
   230 // CWlanIndicatorPluginImplementation::CreateWlanConnectedTextL()
       
   231 // ---------------------------------------------------------------------------
       
   232 //
       
   233 HBufC* CWlanIndicatorPluginImplementation::CreateWlanConnectedTextL()
       
   234     {
       
   235     
       
   236     HBufC* dynText = NULL;
       
   237     HBufC* connectedNetworkName = NULL;
       
   238                   
       
   239     connectedNetworkName = ConnectionNameL( );
       
   240     
       
   241     CleanupStack::PushL( connectedNetworkName );
       
   242     
       
   243     if ( connectedNetworkName )
       
   244         {
       
   245         // "qtn_uni_ind_wlan_conn            '%U' connected"
       
   246         dynText = StringLoader::LoadL ( 
       
   247                 R_QTN_UNI_IND_WLAN_CONN, *connectedNetworkName, iCoeEnv );
       
   248         }
       
   249     
       
   250     CleanupStack::PopAndDestroy( connectedNetworkName );
       
   251             
       
   252     return dynText;
       
   253     }       
       
   254 
       
   255 
       
   256 // ---------------------------------------------------------------------------
       
   257 // CWlanIndicatorPluginImplementation::AvailableNetworksInfoL()
       
   258 // ---------------------------------------------------------------------------
       
   259 //
       
   260 HBufC* CWlanIndicatorPluginImplementation::AvailableNetworksInfoL(
       
   261     TInt& aAvailableCount )
       
   262     {
       
   263     
       
   264     HBufC* knownNetworkName = NULL; 
       
   265  
       
   266     // get available iaps
       
   267     RArray<TUint> availableIaps;
       
   268     TInt iapError( 0 );
       
   269     iapError = iWlanMgmtClient->GetAvailableIaps( availableIaps );
       
   270         
       
   271     if ( iapError == KErrNone )
       
   272         {
       
   273         // if there are known networks, get the name of the first one
       
   274         // in an alphabetical order
       
   275         if ( availableIaps.Count() )
       
   276             {
       
   277             // get all network names and put them into a sorted array
       
   278         
       
   279             // use one TWliWlanInfo struct to point to each IAP in a loop
       
   280             TWliWlanInfo* availableInfo = new ( ELeave ) TWliWlanInfo();
       
   281             CleanupStack::PushL( availableInfo );
       
   282         
       
   283             // descriptor array with granularity 2
       
   284             CDesCArray* networkNames = new (ELeave)CDesCArrayFlat( 2 );
       
   285             CleanupStack::PushL( networkNames );
       
   286       
       
   287             for ( TInt i = 0; i < availableIaps.Count() ; i++ )
       
   288                 {
       
   289                 availableInfo->iIapId = availableIaps[i];
       
   290                 GetWlanInfoFromIapL( *availableInfo );
       
   291             
       
   292                 // insert the name into the proper place
       
   293                 // in alphabetical order
       
   294                 networkNames->InsertIsqL(
       
   295                     ( availableInfo->iNetworkName ), ECmpFolded );
       
   296                 }
       
   297         
       
   298             knownNetworkName = ( networkNames->MdcaPoint(0) ).AllocL();
       
   299         
       
   300             CleanupStack::PopAndDestroy( networkNames );
       
   301             CleanupStack::PopAndDestroy( availableInfo );
       
   302                
       
   303             }
       
   304             
       
   305         else // unknown networks only        
       
   306             {
       
   307             CWlanScanInfo* scanInfo = CWlanScanInfo::NewL();                    
       
   308 
       
   309             TInt scanError( 0 );
       
   310             scanError = iWlanMgmtClient->GetScanResults( *scanInfo );
       
   311             if ( scanError == KErrNone )
       
   312                 {
       
   313                 // Find if we have 1 or more networks available,
       
   314                 // we don't have to know the exact amount
       
   315                 scanInfo->Next() ? aAvailableCount = 2 : aAvailableCount = 1;
       
   316                 }
       
   317             
       
   318             delete scanInfo;
       
   319         
       
   320             }
       
   321         }    
       
   322     
       
   323     availableIaps.Close();
       
   324     
       
   325     return knownNetworkName;
       
   326 
       
   327     }
       
   328 
       
   329 // ---------------------------------------------------------------------------
       
   330 // CWlanIndicatorPluginImplementation::GetWlanInfoFromIapL()
       
   331 // ---------------------------------------------------------------------------
       
   332 //
       
   333 void CWlanIndicatorPluginImplementation::GetWlanInfoFromIapL( TWliWlanInfo& aWlanInfo )
       
   334     {
       
   335     CCommsDatabase* commsDb = CCommsDatabase::NewL();    
       
   336     CleanupStack::PushL( commsDb );
       
   337     
       
   338     CCommsDbTableView* commsDbIapTableView = commsDb->OpenViewMatchingUintLC(
       
   339         TPtrC( IAP ), TPtrC( COMMDB_ID ), aWlanInfo.iIapId );
       
   340 
       
   341     User::LeaveIfError( commsDbIapTableView->GotoFirstRecord() );
       
   342 
       
   343     commsDbIapTableView->ReadTextL( TPtrC( COMMDB_NAME ), aWlanInfo.iNetworkName );
       
   344     
       
   345     CleanupStack::PopAndDestroy( commsDbIapTableView );
       
   346     CleanupStack::PopAndDestroy( commsDb );
       
   347     }
       
   348 
       
   349 // ---------------------------------------------------------------------------
       
   350 // CWlanIndicatorPluginImplementation::ConnectionNameL()
       
   351 // ---------------------------------------------------------------------------
       
   352 //
       
   353 HBufC* CWlanIndicatorPluginImplementation::ConnectionNameL( )
       
   354     {
       
   355     HBufC* connName = NULL;
       
   356     
       
   357     // get the connection ID
       
   358     TUint connectionId = FindWlanBearerConnectedL();
       
   359     
       
   360     TUint connectedIapId(0);
       
   361     TRequestStatus status;
       
   362  
       
   363     // get the IAP ID
       
   364     iConnMonitor.GetUintAttribute( connectionId, 0, KIAPId,
       
   365         connectedIapId, status );
       
   366     User::WaitForRequest( status );
       
   367  
       
   368     // get the connection method from cmmanager
       
   369     RCmManager cmManager;
       
   370     cmManager.OpenLC();
       
   371     
       
   372     RCmConnectionMethod connMethod = cmManager.ConnectionMethodL( connectedIapId );  
       
   373     CleanupClosePushL( connMethod );
       
   374     
       
   375     connName = connMethod.GetStringAttributeL( ECmName );
       
   376         
       
   377     CleanupStack::PopAndDestroy( 2, &cmManager ); //cmManager and connMethod
       
   378     
       
   379     return connName;
       
   380     }
       
   381 
       
   382 // ---------------------------------------------------------------------------
       
   383 // CWlanIndicatorPluginImplementation::FindWlanBearerConnectedL()
       
   384 // ---------------------------------------------------------------------------
       
   385 //
       
   386 TUint CWlanIndicatorPluginImplementation::FindWlanBearerConnectedL( )
       
   387     {
       
   388     TUint connectionId( 0 );
       
   389     
       
   390     TUint connCount( 0 );
       
   391     TUint subConnectionCount( 0 );
       
   392     TRequestStatus status;
       
   393     
       
   394     iConnMonitor.GetConnectionCount( connCount, status );
       
   395     User::WaitForRequest( status );
       
   396     
       
   397     if ( status.Int() == KErrNone )
       
   398         {    
       
   399         for ( TUint i = 1; i <= connCount; ++i )
       
   400             {
       
   401             User::LeaveIfError( iConnMonitor.GetConnectionInfo( i, connectionId, 
       
   402                                                         subConnectionCount ) );
       
   403             if ( CheckIfWlan( connectionId ) )
       
   404                 {
       
   405                 break;
       
   406                 }
       
   407             }
       
   408         }
       
   409     
       
   410     return connectionId;
       
   411     }
       
   412 
       
   413 // ---------------------------------------------------------------------------
       
   414 // CWlanIndicatorPluginImplementation::CheckIfWlan()
       
   415 // ---------------------------------------------------------------------------
       
   416 //
       
   417 TBool CWlanIndicatorPluginImplementation::CheckIfWlan( TUint aConnectionId )
       
   418     {
       
   419     TConnMonBearerType bearerType( EBearerUnknown );
       
   420 
       
   421     TBool foundWlanBearer( EFalse );
       
   422     
       
   423     TRequestStatus status;
       
   424 
       
   425     // Add only connections with valid id
       
   426     if ( aConnectionId > 0 )
       
   427         {
       
   428         iConnMonitor.GetIntAttribute( aConnectionId, 0, KBearer, (TInt&)bearerType, 
       
   429             status );
       
   430         User::WaitForRequest( status );        
       
   431 
       
   432         if ( status.Int() == KErrNone )
       
   433             {
       
   434             if ( bearerType == EBearerWLAN )
       
   435                 {
       
   436                 foundWlanBearer = ETrue;
       
   437                 }
       
   438             }
       
   439 
       
   440         }
       
   441 
       
   442     return foundWlanBearer;
       
   443     
       
   444     }
       
   445 
       
   446 // ---------------------------------------------------------------------------
       
   447 // CWlanIndicatorPluginImplementation::LaunchWlanSnifferL()
       
   448 // ---------------------------------------------------------------------------
       
   449 //
       
   450 void CWlanIndicatorPluginImplementation::LaunchWlanSnifferL()
       
   451     {
       
   452 
       
   453     RApaLsSession appArcSession;
       
   454 
       
   455     User::LeaveIfError( appArcSession.Connect() ); // connect to AppArc server
       
   456     CleanupClosePushL( appArcSession );
       
   457 
       
   458     // check if the app is already running
       
   459     TApaTaskList taskList( CEikonEnv::Static()->WsSession() );
       
   460     TApaTask task = taskList.FindApp( KSnifferAppUid );
       
   461     
       
   462     if ( task.Exists() )
       
   463         {
       
   464         task.BringToForeground();
       
   465         }
       
   466     else
       
   467         {
       
   468         TThreadId threadId;
       
   469         User::LeaveIfError( appArcSession.StartDocument( KNullDesC, 
       
   470                                 KSnifferAppUid, threadId ) );
       
   471                                 
       
   472         }
       
   473         
       
   474     CleanupStack::PopAndDestroy(); // appArcSession
       
   475     
       
   476     }
       
   477 
       
   478 //  End of File