convergedconnectionhandler/cchclientapi/src/cchuiconnectionhandler.cpp
changeset 0 a4daefaec16c
child 8 7117cbf1600a
equal deleted inserted replaced
-1:000000000000 0:a4daefaec16c
       
     1 /*
       
     2 * Copyright (c) 2008-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:  Provider WLAN search and SNAP handling functionality
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // Search WLAN
       
    20 #include <cmmanagerdef.h>
       
    21 #include <cmpluginwlandef.h> // bearer type
       
    22 #include <cmdestinationext.h>
       
    23 #include <cmconnectionmethoddef.h>
       
    24 #include <ConnectionUiUtilities.h>
       
    25 #include <cmconnectionmethodext.h>
       
    26 #include <WEPSecuritySettingsUI.h>
       
    27 #include <WPASecuritySettingsUI.h>
       
    28 
       
    29 #include "cchuilogger.h"
       
    30 #include "cchuicchhandler.h"
       
    31 #include "cchuispshandler.h"
       
    32 #include "cchuiconnectionhandler.h"
       
    33 
       
    34 const TInt KSsidLength = 256;
       
    35 const TInt KCmHighestPriority = 0;
       
    36 const TInt KDestinationArrayGranularity = 10;
       
    37 
       
    38 
       
    39 // ======== MEMBER FUNCTIONS ========
       
    40 
       
    41 CCchUiConnectionHandler::CCchUiConnectionHandler(
       
    42     CCchUiCchHandler& aCchHandler,
       
    43     CCchUiSpsHandler& aSpsHandler ):
       
    44     iCCHHandler( aCchHandler ),
       
    45     iSpsHandler( aSpsHandler )
       
    46     {
       
    47     }
       
    48 
       
    49 void CCchUiConnectionHandler::ConstructL()
       
    50     {
       
    51     iConnUiUtils = CConnectionUiUtilities::NewL();
       
    52     iCmManagerExt.OpenL();
       
    53     User::LeaveIfError( iConnMon.ConnectL() );
       
    54     }
       
    55 
       
    56 CCchUiConnectionHandler* CCchUiConnectionHandler::NewL(
       
    57     CCchUiCchHandler& aCchHandler,
       
    58     CCchUiSpsHandler& aSpsHandler )
       
    59     {
       
    60     CCchUiConnectionHandler* self = NewLC( 
       
    61         aCchHandler, aSpsHandler );
       
    62     CleanupStack::Pop(self);
       
    63     return self;
       
    64     }
       
    65 
       
    66 CCchUiConnectionHandler* CCchUiConnectionHandler::NewLC(
       
    67     CCchUiCchHandler& aCchHandler,
       
    68     CCchUiSpsHandler& aSpsHandler )
       
    69     {
       
    70     CCchUiConnectionHandler* self =
       
    71         new (ELeave) CCchUiConnectionHandler( 
       
    72             aCchHandler, aSpsHandler );
       
    73     CleanupStack::PushL(self);
       
    74     self->ConstructL();
       
    75     return self;
       
    76     }
       
    77 
       
    78 CCchUiConnectionHandler::~CCchUiConnectionHandler()
       
    79     {
       
    80     delete iConnUiUtils;
       
    81     iCmManagerExt.Close();
       
    82     iConnMon.Close();
       
    83     }
       
    84 
       
    85 // ---------------------------------------------------------------------------
       
    86 // Searches access poinsts and adds selected access point to snap
       
    87 // ---------------------------------------------------------------------------
       
    88 //
       
    89 void CCchUiConnectionHandler::SearchAccessPointsL( 
       
    90     TUint aServiceId, 
       
    91     TUint32 aSnapId, 
       
    92     const TDesC& aServiceName )
       
    93     {    
       
    94     CCHUIDEBUG( "CCchUiConnectionHandler::SearchAccessPointsL - IN" );
       
    95     
       
    96     // SSID of the WLAN network selected by user
       
    97     TWlanSsid ssid; 
       
    98     
       
    99     // Connection mode of the WLAN network
       
   100     TWlanConnectionMode connectionMode; 
       
   101     
       
   102     // Security mode of the WLAN network
       
   103     TWlanConnectionSecurityMode securityMode; 
       
   104     
       
   105     CCHUIDEBUG( 
       
   106         "CCchUiConnectionHandler::SearchAccessPointsL - begin search wlan" );
       
   107         
       
   108     TBool ret = iConnUiUtils->SearchWLANNetwork( 
       
   109         ssid, connectionMode, securityMode );
       
   110         
       
   111     if ( !ret )
       
   112         {        
       
   113         //ret is ETrue if user pressed OK softkey. Otherwise leave
       
   114         //with cancel
       
   115         User::Leave( KErrCancel );
       
   116         }
       
   117     
       
   118     CCHUIDEBUG( 
       
   119       "CCchUiConnectionHandler::SearchAccessPointsL - search wlan finished" ); 
       
   120     
       
   121     HBufC* ssid16 = HBufC::NewL( KSsidLength );
       
   122     CleanupStack::PushL( ssid16 );
       
   123     
       
   124     TPtr ssid16Ptr( ssid16->Des() );
       
   125     ssid16Ptr.Copy( ssid );
       
   126 
       
   127     //  Check if the snap returned by Cch really exists.
       
   128     if ( aSnapId )
       
   129         {
       
   130         RArray<TUint32> destIds = RArray<TUint32>( 1 );
       
   131         CleanupClosePushL( destIds );
       
   132         iCmManagerExt.AllDestinationsL( destIds );
       
   133         
       
   134         TBool found( EFalse );
       
   135         for ( TInt index = 0 ; index < destIds.Count() ; index++ )
       
   136             {
       
   137             if ( destIds[index] == aSnapId )
       
   138                 {
       
   139                 found = ETrue;
       
   140                 }
       
   141             }
       
   142         CleanupStack::PopAndDestroy( &destIds );
       
   143         
       
   144         // Reset snap id if the snap doesn't exist.
       
   145         if ( !found )
       
   146             {
       
   147             aSnapId = 0;
       
   148             }
       
   149         }
       
   150 
       
   151     if ( !aSnapId )
       
   152         {
       
   153         RCmDestinationExt newDestination = 
       
   154             CreateServiceSnapL( aServiceName );
       
   155         CleanupClosePushL( newDestination );
       
   156         
       
   157         TBool alreadyExists = EFalse;
       
   158 
       
   159         TInt iapId = AddNewConnectionMethodL( 
       
   160             newDestination, *ssid16, securityMode, alreadyExists, EFalse );
       
   161         
       
   162         if ( KErrCancel == iapId )
       
   163             {
       
   164             //Leave with cancel if user pressed Cancel softkey
       
   165             CleanupStack::PopAndDestroy( &newDestination );
       
   166             CleanupStack::PopAndDestroy( ssid16 );
       
   167             User::Leave( KErrCancel );
       
   168             }
       
   169 
       
   170         if ( !alreadyExists )
       
   171             {
       
   172             iSpsHandler.SetTemporaryIapIdL( aServiceId, iapId );
       
   173             }
       
   174         
       
   175         // Now the snap is created, set it to use with cch
       
   176         SetSnapToUseL( aServiceId, newDestination.Id() );
       
   177         
       
   178         // Self created, write id to service table in order to remove
       
   179         // in service uninstall
       
   180         TRAP_IGNORE( iSpsHandler.SetSnapIdL( 
       
   181             aServiceId, newDestination.Id() ) );
       
   182         
       
   183         CleanupStack::PopAndDestroy( &newDestination );
       
   184         CleanupStack::PopAndDestroy( ssid16 );
       
   185         }
       
   186     else
       
   187         {
       
   188         RCmDestinationExt destination = 
       
   189             iCmManagerExt.DestinationL( aSnapId );
       
   190         CleanupClosePushL( destination );
       
   191         
       
   192         TBool alreadyExists = EFalse;
       
   193         TInt iapId = AddNewConnectionMethodL( 
       
   194             destination, *ssid16, securityMode, alreadyExists, EFalse );
       
   195         
       
   196         CleanupStack::PopAndDestroy( &destination );
       
   197         CleanupStack::PopAndDestroy( ssid16 );
       
   198         
       
   199         if ( KErrCancel == iapId )
       
   200             {
       
   201             //Leave with cancel if user pressed Cancel softkey
       
   202             User::Leave( KErrCancel );
       
   203             }
       
   204 
       
   205         if ( !alreadyExists )
       
   206             {
       
   207             iSpsHandler.SetTemporaryIapIdL( 
       
   208                 aServiceId, iapId );
       
   209             }
       
   210         else
       
   211             {
       
   212             // Get current connection iap id
       
   213             TInt currentIapId( KErrNone );
       
   214             TInt error( KErrNone );
       
   215             iCCHHandler.GetCurrentConnectionIapIdL(
       
   216                 aServiceId, ECCHUnknown, currentIapId, error );
       
   217             
       
   218             CCHUIDEBUG2( "CCchUiConnectionHandler::SearchAccessPointsL - error:            %d", error );
       
   219             CCHUIDEBUG2( "CCchUiConnectionHandler::SearchAccessPointsL - current iap id:   %d", currentIapId );
       
   220             CCHUIDEBUG2( "CCchUiConnectionHandler::SearchAccessPointsL - selected iap id:  %d", iapId );
       
   221             
       
   222             // if selected iap is same as current iap no need to continue
       
   223             // because if we continue there will be unnecessary disable enable loop
       
   224             if ( currentIapId == iapId )
       
   225                 {
       
   226                 User::Leave( KErrCancel );
       
   227                 }
       
   228             }
       
   229         
       
   230         SetSnapToUseL( aServiceId, aSnapId ); 
       
   231         }
       
   232     
       
   233     CCHUIDEBUG( "CCchUiConnectionHandler::SearchAccessPointsL - OUT" );
       
   234     } 
       
   235 
       
   236 // ---------------------------------------------------------------------------
       
   237 // Check if connnection method already exists. If exists, connection method
       
   238 // id is returned, othewise KErrNotFound.
       
   239 // ---------------------------------------------------------------------------
       
   240 //
       
   241 TInt CCchUiConnectionHandler::ConnectionMethodAlreadyExistsL( 
       
   242     RCmDestinationExt& aDestination,
       
   243     const TDesC& aSsid,
       
   244     CMManager::TWlanSecMode aSecurityMode,
       
   245     TBool& aAlreadyExists,
       
   246     TBool aHidden )
       
   247     {
       
   248     CCHUIDEBUG( "CCchUiConnectionHandler::ConnectionMethodAlreadyExistsL" );
       
   249     
       
   250     TInt connectionMethodId = KErrNotFound;
       
   251     for ( TInt i=0 ; 
       
   252           i < aDestination.ConnectionMethodCount() && !aAlreadyExists ; 
       
   253           i++ )
       
   254         {
       
   255         HBufC* refSsid( NULL );
       
   256         
       
   257         RCmConnectionMethodExt cm = aDestination.ConnectionMethodL(i);
       
   258         CleanupClosePushL( cm );
       
   259         
       
   260         // If snap contains non wlan iaps, this will leave so trap
       
   261         TRAPD( err, refSsid = cm.GetStringAttributeL( CMManager::EWlanSSID ));
       
   262         if ( refSsid && !err )
       
   263             {
       
   264             CleanupStack::PushL( refSsid );
       
   265             
       
   266             if ( refSsid->Compare( aSsid ) == 0 )
       
   267                 {
       
   268                 CCHUIDEBUG( "Matching wlan SSID Found" );
       
   269                 
       
   270                 // check security and hidden
       
   271                 TBool refHidden = cm.GetBoolAttributeL( 
       
   272                    CMManager::EWlanScanSSID );
       
   273                 TUint refSecurity = cm.GetIntAttributeL( 
       
   274                     CMManager::EWlanSecurityMode );
       
   275                 
       
   276                 // ref AP security mode is stored as EWlanSecModeWpa in case of EWlanSecModeWpa2
       
   277                 TBool exception( EFalse ); 
       
   278                 
       
   279                 if ( aSecurityMode == CMManager::EWlanSecModeWpa2 && 
       
   280                      refSecurity == CMManager::EWlanSecModeWpa )
       
   281                     {
       
   282                     exception = ETrue;
       
   283                     }
       
   284                 
       
   285                 if ( refHidden == aHidden &&
       
   286                     ( refSecurity == aSecurityMode || exception ) )
       
   287                     {              
       
   288                     connectionMethodId = 
       
   289                         cm.GetIntAttributeL( CMManager::ECmIapId );
       
   290                     aAlreadyExists = ETrue;
       
   291                     }
       
   292                 }
       
   293             CleanupStack::PopAndDestroy( refSsid );
       
   294             }
       
   295         else if ( KErrNoMemory == err )
       
   296             {
       
   297             User::Leave( err );
       
   298             }
       
   299         else
       
   300             {
       
   301             // for note from code analysis tool
       
   302             }
       
   303         CleanupStack::PopAndDestroy( &cm );
       
   304         }
       
   305     
       
   306     CCHUIDEBUG2( "ConnectionMethodAlreadyExistsL connection method id=%d", 
       
   307          connectionMethodId );
       
   308     
       
   309     return connectionMethodId;
       
   310     }
       
   311 
       
   312 // ---------------------------------------------------------------------------
       
   313 // Add new connection method.
       
   314 // ---------------------------------------------------------------------------
       
   315 //
       
   316 TInt CCchUiConnectionHandler::AddNewConnectionMethodL( 
       
   317     RCmDestinationExt& aDestination, 
       
   318     const TDesC& aSsid,
       
   319     TWlanConnectionSecurityMode aSecurityMode,
       
   320     TBool& aAlreadyExists,
       
   321     TBool aSetHidden )
       
   322     {
       
   323     CCHUIDEBUG( "CCchUiConnectionHandler::AddNewConnectionMethodL" );
       
   324     
       
   325     CMManager::TWlanSecMode securityModeToSet = CMManager::EWlanSecModeOpen;
       
   326     switch ( aSecurityMode )
       
   327         {
       
   328         case EWlanConnectionSecurityOpen:
       
   329             {
       
   330             securityModeToSet = CMManager::EWlanSecModeOpen;
       
   331             }
       
   332             break;
       
   333         
       
   334         case EWlanConnectionSecurityWep:
       
   335             {
       
   336             securityModeToSet = CMManager::EWlanSecModeWep;
       
   337             }
       
   338             break;
       
   339         
       
   340         case EWlanConnectionSecurity802d1x:
       
   341             {
       
   342             securityModeToSet = CMManager::EWlanSecMode802_1x;
       
   343             }
       
   344             break;
       
   345         
       
   346         case EWlanConnectionSecurityWpa:
       
   347             {
       
   348             securityModeToSet = CMManager::EWlanSecModeWpa;
       
   349             }
       
   350             break;
       
   351         
       
   352         case EWlanConnectionSecurityWpaPsk:
       
   353             {
       
   354             securityModeToSet = CMManager::EWlanSecModeWpa2;
       
   355             }
       
   356             break;
       
   357             
       
   358         default:
       
   359             break;
       
   360         }    
       
   361     
       
   362     // Check is iap with same ssid already exists
       
   363     RArray<TUint32> destArray;
       
   364     CleanupClosePushL( destArray );
       
   365     
       
   366     iCmManagerExt.AllDestinationsL( destArray );
       
   367     
       
   368     for ( TInt i( 0 ); i < destArray.Count(); i++ )
       
   369         {
       
   370         RCmDestinationExt destination =
       
   371             iCmManagerExt.DestinationL( destArray[ i ] );
       
   372         CleanupClosePushL( destination );
       
   373         
       
   374         TInt connectionId = ConnectionMethodAlreadyExistsL( 
       
   375             destination, aSsid, securityModeToSet, aAlreadyExists, aSetHidden );
       
   376         
       
   377         if ( aAlreadyExists && KErrNotFound != connectionId )
       
   378            {
       
   379            RCmConnectionMethodExt connectionMethod =
       
   380                destination.ConnectionMethodByIDL( connectionId );
       
   381            CleanupClosePushL( connectionMethod );
       
   382            if ( destination.Id() != aDestination.Id() )
       
   383                {
       
   384                CCHUIDEBUG( "Copy existing connection method to destination" );
       
   385                iCmManagerExt.CopyConnectionMethodL(
       
   386                    aDestination, connectionMethod );
       
   387                }
       
   388            TRAP_IGNORE( aDestination.ModifyPriorityL( 
       
   389                connectionMethod, KCmHighestPriority ) );
       
   390            aDestination.UpdateL();
       
   391            
       
   392            CleanupStack::PopAndDestroy( &connectionMethod );
       
   393            CleanupStack::PopAndDestroy( &destination );
       
   394            CleanupStack::PopAndDestroy( &destArray );
       
   395            return connectionId;
       
   396            }
       
   397         CleanupStack::PopAndDestroy( &destination );
       
   398         }
       
   399     CleanupStack::PopAndDestroy( &destArray );
       
   400     
       
   401     TInt ret = KErrNone;
       
   402     
       
   403     RCmConnectionMethodExt newConnMethod = 
       
   404         aDestination.CreateConnectionMethodL( KUidWlanBearerType );
       
   405     CleanupClosePushL( newConnMethod );
       
   406     
       
   407     // Set attributes
       
   408     newConnMethod.SetStringAttributeL( CMManager::ECmName, aSsid );
       
   409     newConnMethod.SetStringAttributeL( CMManager::EWlanSSID, aSsid );
       
   410     
       
   411     if ( aSetHidden )
       
   412         {
       
   413         newConnMethod.SetBoolAttributeL( 
       
   414             CMManager::EWlanScanSSID, ETrue );
       
   415         }
       
   416     
       
   417     newConnMethod.SetIntAttributeL( 
       
   418         CMManager::EWlanSecurityMode, securityModeToSet );
       
   419 
       
   420     TRAP_IGNORE( aDestination.ModifyPriorityL( 
       
   421         newConnMethod, KCmHighestPriority ));
       
   422     
       
   423     aDestination.UpdateL();
       
   424     ret = newConnMethod.GetIntAttributeL( CMManager::ECmIapId );
       
   425 
       
   426     CConnectionUiUtilities* connUiUtils = CConnectionUiUtilities::NewL();
       
   427     CleanupStack::PushL( connUiUtils );
       
   428     
       
   429     TInt createdUid( 0 );
       
   430     createdUid = newConnMethod.GetIntAttributeL( CMManager::EWlanServiceId );
       
   431 
       
   432     HBufC* key = HBufC::NewL( KWlanWpaPskMaxLength );
       
   433     CleanupStack::PushL( key );
       
   434     TPtr keyPtr( key->Des() );
       
   435     
       
   436     TBool hex = EFalse;  
       
   437     
       
   438     if ( securityModeToSet == CMManager::EWlanSecModeWep )    
       
   439         {
       
   440         if ( connUiUtils->EasyWepDlg( &keyPtr, hex ) )
       
   441             {
       
   442             SaveSecuritySettingsL( aSecurityMode, key, hex, createdUid );
       
   443             }
       
   444         else
       
   445             {
       
   446             aDestination.DeleteConnectionMethodL( newConnMethod );
       
   447             aDestination.UpdateL();
       
   448             
       
   449             ret = KErrCancel;
       
   450             }
       
   451         }
       
   452     else if ( securityModeToSet == CMManager::EWlanSecModeWpa || 
       
   453               securityModeToSet == CMManager::EWlanSecModeWpa2 )
       
   454         {
       
   455         if ( connUiUtils->EasyWpaDlg( &keyPtr ) )
       
   456             {
       
   457             SaveSecuritySettingsL( aSecurityMode, key, hex, createdUid );
       
   458             }
       
   459         else
       
   460             {
       
   461             aDestination.DeleteConnectionMethodL( newConnMethod );
       
   462             aDestination.UpdateL();
       
   463             
       
   464             ret = KErrCancel;
       
   465             }
       
   466         }
       
   467     else
       
   468         {
       
   469         CCHUIDEBUG( "AddNewConnectionMethodL - WlanConnectionSecurityOpen" );
       
   470         }
       
   471     CleanupStack::PopAndDestroy( key );          
       
   472     CleanupStack::PopAndDestroy( connUiUtils );   
       
   473     CleanupStack::PopAndDestroy( &newConnMethod );
       
   474     
       
   475     CCHUIDEBUG2( "AddNewConnectionMethodL - return: %d", ret );
       
   476     return ret;
       
   477     }
       
   478 
       
   479 // ---------------------------------------------------------------------------
       
   480 // Save security settings.
       
   481 // ---------------------------------------------------------------------------
       
   482 //
       
   483 void CCchUiConnectionHandler::SaveSecuritySettingsL(
       
   484     const TWlanConnectionSecurityMode aSecMode, 
       
   485     const HBufC* aKey, 
       
   486     const TBool aHex, 
       
   487     const TUint32 aIapId )
       
   488     {
       
   489     CCHUIDEBUG( "CCchUiConnectionHandler::SaveSecuritySettingsL - IN" );
       
   490     
       
   491     CMDBSession* db = CMDBSession::NewL( CMDBSession::LatestVersion() );
       
   492     CleanupStack::PushL( db );
       
   493     switch ( aSecMode )
       
   494         {
       
   495         case EWlanConnectionSecurityWep:
       
   496             {
       
   497             CWEPSecuritySettings* wepSecSettings = 
       
   498                 CWEPSecuritySettings::NewL();
       
   499             CleanupStack::PushL( wepSecSettings );            
       
   500             wepSecSettings->LoadL( aIapId, *db );            
       
   501             User::LeaveIfError( 
       
   502                 wepSecSettings->SetKeyDataL( 0, *aKey, aHex ) );
       
   503             wepSecSettings->SaveL( aIapId, *db );             
       
   504             CleanupStack::PopAndDestroy( wepSecSettings ); 
       
   505             break;
       
   506             }
       
   507         case EWlanConnectionSecurityWpaPsk:
       
   508             {   
       
   509             CWPASecuritySettings* wpaSecSettings = 
       
   510                 CWPASecuritySettings::NewL( ESecurityModeWpa );
       
   511             CleanupStack::PushL( wpaSecSettings );           
       
   512             wpaSecSettings->LoadL( aIapId, *db );            
       
   513             User::LeaveIfError( wpaSecSettings->SetWPAPreSharedKey( *aKey ) );
       
   514             wpaSecSettings->SaveL( aIapId, *db, ESavingBrandNewAP, 0 ) ;            
       
   515             CleanupStack::PopAndDestroy( wpaSecSettings );         
       
   516             break;                     
       
   517             }
       
   518         case EWlanConnectionSecurityWpa:            
       
   519         case EWlanConnectionSecurity802d1x: 
       
   520             {
       
   521             CWPASecuritySettings* wpaSecSettings = 
       
   522                     CWPASecuritySettings::NewL( 
       
   523                         aSecMode == EWlanConnectionSecurityWpa ? 
       
   524                         ESecurityModeWpa : ESecurityMode8021x );
       
   525             CleanupStack::PushL( wpaSecSettings );            
       
   526             wpaSecSettings->LoadL( aIapId, *db );            
       
   527             wpaSecSettings->SaveL( aIapId, *db, ESavingBrandNewAP, 0 );             
       
   528             CleanupStack::PopAndDestroy( wpaSecSettings );   
       
   529             break;
       
   530             }
       
   531         default:
       
   532             // Fore example EWlanConnectionSecurityOpen -> no need to save
       
   533             // any security settings.
       
   534             break;
       
   535         }
       
   536     CleanupStack::PopAndDestroy( db ); // db
       
   537     
       
   538     CCHUIDEBUG( "CCchUiConnectionHandler::SaveSecuritySettingsL - OUT" );
       
   539     }
       
   540 
       
   541 // ---------------------------------------------------------------------------
       
   542 // Sets SNAP into use via CCH. 
       
   543 // ---------------------------------------------------------------------------
       
   544 //
       
   545 void CCchUiConnectionHandler::SetSnapToUseL( 
       
   546     TUint aServiceId, TUint32 aSNAPId )
       
   547     {
       
   548     CCHUIDEBUG( "CCchUiConnectionHandler::SetSnapToUseL - IN" );
       
   549     
       
   550     CCHUIDEBUG2( "SetSnapToUseL - aServiceId: %d", aServiceId );
       
   551     CCHUIDEBUG2( "SetSnapToUseL - aSNAPId: %d", aSNAPId );
       
   552     
       
   553     TInt err( KErrNone );
       
   554     iCCHHandler.SetConnectionSnapIdL( aServiceId, aSNAPId, err );
       
   555     
       
   556     if ( err )
       
   557         {
       
   558         User::Leave( err );
       
   559         }
       
   560     
       
   561     CCHUIDEBUG( "CCchUiConnectionHandler::SetSnapToUseL - OUT" );
       
   562     } 
       
   563     
       
   564 // ---------------------------------------------------------------------------
       
   565 // Copies specific iap from specific snap to target snap
       
   566 // ---------------------------------------------------------------------------
       
   567 //
       
   568 void CCchUiConnectionHandler::CopyIapToServiceSnapL( 
       
   569     TUint aServiceId, 
       
   570     const TDesC& aServiceName, 
       
   571     TUint32 aSourceIap, 
       
   572     TUint32 aTargetSnap )
       
   573     {
       
   574     CCHUIDEBUG( "CCchUiConnectionHandler::CopyIapToServiceSnapL - IN" );
       
   575     
       
   576     CCHUIDEBUG3( "CopyIapToServiceSnapL - aSourceIap: %d, aTargetSnap: %d", 
       
   577         aSourceIap, aTargetSnap );
       
   578     
       
   579     RCmDestinationExt targetSnap;
       
   580     TRAPD( err, ( targetSnap = iCmManagerExt.DestinationL( aTargetSnap ) ) );
       
   581     CCHUIDEBUG2( " -> get target snap err: %d", err );
       
   582     if ( err )
       
   583         {
       
   584         CCHUIDEBUG( "CopyIapToServiceSnapL - Create snap for service");
       
   585         
       
   586         targetSnap = CreateServiceSnapL( aServiceName );
       
   587         CleanupClosePushL( targetSnap );
       
   588         // Now the snap is created, set it to use with cch
       
   589         SetSnapToUseL( aServiceId, targetSnap.Id() );
       
   590         }
       
   591     else
       
   592         {
       
   593         CleanupClosePushL( targetSnap );
       
   594         }
       
   595     CCHUIDEBUG( "CopyIapToServiceSnapL - Get source connection");    
       
   596     
       
   597     RCmConnectionMethodExt sourceConn = 
       
   598         iCmManagerExt.ConnectionMethodL( aSourceIap );       
       
   599     CleanupClosePushL( sourceConn );
       
   600     
       
   601     TInt conMethodCount = targetSnap.ConnectionMethodCount();
       
   602     TUint32 sourceIapId = sourceConn.GetIntAttributeL( CMManager::ECmIapId );
       
   603     TBool matchFound = false;
       
   604     
       
   605     for ( TInt ndx = 0 ; ndx < conMethodCount && matchFound == 0; ndx ++ )
       
   606         {
       
   607         RCmConnectionMethodExt cm = targetSnap.ConnectionMethodL( ndx );
       
   608         CleanupClosePushL( cm );
       
   609         
       
   610         TUint32 targetIapId = cm.GetIntAttributeL( CMManager::ECmIapId );
       
   611         
       
   612         if( targetIapId == sourceIapId )
       
   613             {
       
   614             matchFound =true;
       
   615             }
       
   616         CleanupStack::PopAndDestroy( &cm );
       
   617         }
       
   618     if( !matchFound )
       
   619         {
       
   620         CCHUIDEBUG( 
       
   621           "CopyIapToServiceSnapL - Get source connection ok -> copy connection");    
       
   622         
       
   623         iCmManagerExt.CopyConnectionMethodL( targetSnap, sourceConn );
       
   624         }
       
   625     CleanupStack::PopAndDestroy( &sourceConn );
       
   626     CleanupStack::PopAndDestroy( &targetSnap );      
       
   627     
       
   628     CCHUIDEBUG( "CCchUiConnectionHandler::CopyIapToServiceSnapL - OUT" );
       
   629     }     
       
   630     
       
   631 // ---------------------------------------------------------------------------
       
   632 // Removes connection method from SNAP.
       
   633 // ---------------------------------------------------------------------------
       
   634 //
       
   635 void CCchUiConnectionHandler::RemoveConnectionL( 
       
   636     const TDesC& aServiceName, TInt aIapId )
       
   637     {
       
   638     CCHUIDEBUG( "CCchUiConnectionHandler::RemoveConnectionL - IN" );
       
   639     CCHUIDEBUG2( "RemoveConnectionL - aServiceName=%S", &aServiceName );
       
   640     CCHUIDEBUG2( "RemoveConnectionL - aIapId=%d", aIapId );
       
   641     
       
   642     RArray<TUint32> destIds = RArray<TUint32>( 1 );
       
   643     CleanupClosePushL( destIds );
       
   644     iCmManagerExt.AllDestinationsL( destIds );
       
   645     
       
   646     for ( TInt i( 0 ) ; i < destIds.Count() ; i++ )
       
   647         {
       
   648         RCmDestinationExt dest = iCmManagerExt.DestinationL( destIds[ i ] );
       
   649         CleanupClosePushL( dest );
       
   650         
       
   651         HBufC* destName = dest.NameLC();        
       
   652         if ( destName->Des().Compare( aServiceName ) == 0 )
       
   653             {
       
   654             CCHUIDEBUG( "RemoveConnectionL - get connection method");
       
   655             
       
   656             RCmConnectionMethodExt connMethod = 
       
   657                    iCmManagerExt.ConnectionMethodL( aIapId );        
       
   658             
       
   659             CCHUIDEBUG( "RemoveConnectionL - remove connection method");
       
   660             
       
   661             dest.RemoveConnectionMethodL( connMethod );
       
   662             dest.UpdateL();
       
   663             
       
   664             CCHUIDEBUG( "RemoveConnectionL - connection method removed");
       
   665             }
       
   666         
       
   667         CleanupStack::PopAndDestroy( destName );
       
   668         CleanupStack::PopAndDestroy( &dest );
       
   669         }
       
   670     
       
   671     CleanupStack::PopAndDestroy( &destIds );
       
   672     
       
   673     CCHUIDEBUG( "CCchUiConnectionHandler::RemoveConnectionL - OUT" );
       
   674     }
       
   675 
       
   676 // ---------------------------------------------------------------------------
       
   677 // Creates service snap
       
   678 // ---------------------------------------------------------------------------
       
   679 //
       
   680 RCmDestinationExt CCchUiConnectionHandler::CreateServiceSnapL(
       
   681     const TDesC& aServiceName )
       
   682     {
       
   683     CCHUIDEBUG( "CCchUiConnectionHandler::CreateServiceSnapL" );
       
   684 
       
   685     // Create snap again, user might have deleted it
       
   686     RCmDestinationExt newDestination;
       
   687     
       
   688     // Check if snap with service name already exists.
       
   689     TBool snapAlreadyExists = EFalse;
       
   690     RArray<TUint32> destIdArray = 
       
   691         RArray<TUint32>( KDestinationArrayGranularity );
       
   692     CleanupClosePushL( destIdArray );
       
   693     
       
   694     CCHUIDEBUG( "CreateServiceSnapL - check if snap exists");
       
   695     
       
   696     iCmManagerExt.AllDestinationsL( destIdArray );
       
   697     
       
   698     CCHUIDEBUG2( "CreateServiceSnapL - destination count: %d", 
       
   699         destIdArray.Count() );
       
   700     
       
   701     for ( TInt i = 0; 
       
   702           i < destIdArray.Count() && !snapAlreadyExists; 
       
   703           i++ )
       
   704         {
       
   705         RCmDestinationExt dest = 
       
   706             iCmManagerExt.DestinationL( destIdArray[i] );
       
   707         CleanupClosePushL( dest );
       
   708         HBufC* destName = dest.NameLC();
       
   709         if ( *destName == aServiceName )
       
   710             {
       
   711             CCHUIDEBUG( "CreateServiceSnapL - snap already exists" );
       
   712             snapAlreadyExists = ETrue;
       
   713             newDestination = iCmManagerExt.DestinationL( destIdArray[i] );
       
   714             }
       
   715         CleanupStack::PopAndDestroy( destName );
       
   716         CleanupStack::PopAndDestroy( &dest );
       
   717         }
       
   718     CleanupStack::PopAndDestroy( &destIdArray );                
       
   719     
       
   720     if ( !snapAlreadyExists )
       
   721         {
       
   722         newDestination = 
       
   723             iCmManagerExt.CreateDestinationL( aServiceName );
       
   724         CleanupClosePushL( newDestination );
       
   725         newDestination.SetMetadataL( 
       
   726             CMManager::ESnapMetadataHiddenAgent, ETrue );
       
   727         newDestination.UpdateL();    
       
   728         CleanupStack::Pop( &newDestination );
       
   729         }
       
   730     
       
   731     return newDestination;
       
   732     }
       
   733