voipplugins/sipconnectionprovider/src/scpprofilehandler.cpp
changeset 0 a4daefaec16c
child 9 bddb6d4447db
equal deleted inserted replaced
-1:000000000000 0:a4daefaec16c
       
     1 /*
       
     2 * Copyright (c) 2007-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 
       
    19 #include <sip.h>
       
    20 #include <sipprofileregistry.h>
       
    21 #include <sipprofilealrcontroller.h>
       
    22 #include <sipprofile.h>
       
    23 #include <sipmanagedprofile.h>
       
    24 #include <sipmanagedprofileregistry.h>
       
    25 #include <ctsydomainpskeys.h>
       
    26 #include <bamdesca.h>
       
    27 #include <sipprofileregistryobserver.h> 
       
    28 #include "ipvmbxinterface.h"
       
    29 #include "scpprofilehandler.h"
       
    30 #include "scplogger.h"
       
    31 #include "scpsipconnection.h"
       
    32 #include "cipappphoneutils.h"
       
    33 #include "cipapputilsaddressresolver.h"
       
    34 
       
    35 #include "scputility.h"
       
    36 
       
    37 const TInt KTempStringlength = 200;
       
    38 const TUint KSCPUserAgentStringLength = 12;
       
    39 const TUint KSCPWlanMacAddressLength = 50;
       
    40 const TUint KSCPMaxTerminalTypeLength = 64;
       
    41 _LIT( KScpUserAgentString, "User-Agent: " );
       
    42 _LIT8 ( KSCPWlanMacAddressFrmt, "-" );
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // CScpProfileHandler::CScpProfileHandler
       
    46 // -----------------------------------------------------------------------------
       
    47 //
       
    48 CScpProfileHandler::CScpProfileHandler()
       
    49     {
       
    50     }
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // CScpProfileHandler::ConstructL
       
    54 // -----------------------------------------------------------------------------
       
    55 //
       
    56 void CScpProfileHandler::ConstructL()
       
    57     {
       
    58     SCPLOGSTRING( "CScpProfileHandler::ConstructL" );
       
    59 
       
    60     iSip = CSIP::NewL( TUid::Null(), *this );
       
    61     iProfileRegistry = CSIPProfileRegistry::NewL( *iSip, *this );
       
    62     iManagedProfileRegistry = CSIPManagedProfileRegistry::NewL( *this );
       
    63     iAlrController = CSipProfileAlrController::NewL( *iProfileRegistry,
       
    64                                                      *this );
       
    65     }
       
    66 
       
    67 // -----------------------------------------------------------------------------
       
    68 // CScpProfileHandler::NewL
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 CScpProfileHandler* CScpProfileHandler::NewL()
       
    72     {
       
    73     CScpProfileHandler* self =  new ( ELeave ) CScpProfileHandler();
       
    74     CleanupStack::PushL( self );
       
    75     self->ConstructL();
       
    76     CleanupStack::Pop( self );
       
    77     return self;
       
    78     }
       
    79  
       
    80 // -----------------------------------------------------------------------------
       
    81 // CScpProfileHandler::~CScpProfileHandler
       
    82 // -----------------------------------------------------------------------------
       
    83 //
       
    84 CScpProfileHandler::~CScpProfileHandler()
       
    85     {  
       
    86     SCPLOGSTRING( "CScpProfileHandler::~CScpProfileHandler" );
       
    87 
       
    88     iObservers.Close();
       
    89     iSipConnections.ResetAndDestroy();
       
    90     iSipConnections.Close();
       
    91 
       
    92     delete iVmbxInterface;
       
    93     delete iAlrController;
       
    94     delete iManagedProfileRegistry;
       
    95     delete iProfileRegistry;    
       
    96     delete iSip;
       
    97     }
       
    98 
       
    99 // -----------------------------------------------------------------------------
       
   100 // CScpProfileHandler::AddObserverL
       
   101 // -----------------------------------------------------------------------------
       
   102 //
       
   103 void CScpProfileHandler::AddObserverL( MScpSipConnectionObserver& aObserver )
       
   104     {
       
   105     SCPLOGSTRING2( "CScpProfileHandler::AddObserverL: 0x%x", &aObserver );
       
   106 
       
   107     TInt result = iObservers.Find( &aObserver );
       
   108 
       
   109     if ( result == KErrNotFound )
       
   110         {
       
   111         iObservers.AppendL( &aObserver );
       
   112         }
       
   113     else
       
   114         {
       
   115         User::Leave( KErrAlreadyExists );
       
   116         }
       
   117     }
       
   118     
       
   119 // -----------------------------------------------------------------------------
       
   120 // CScpProfileHandler::RemoveObserver
       
   121 // -----------------------------------------------------------------------------
       
   122 //
       
   123 TInt CScpProfileHandler::RemoveObserver( MScpSipConnectionObserver& aObserver )
       
   124     {
       
   125     SCPLOGSTRING2( "CScpProfileHandler::RemoveObserver: 0x%x", &aObserver );
       
   126 
       
   127     TInt result = iObservers.Find( &aObserver );
       
   128     
       
   129     if ( result != KErrNotFound )
       
   130         {
       
   131         iObservers.Remove( result );
       
   132         return KErrNone;
       
   133         }
       
   134 
       
   135     return KErrNotFound;
       
   136     }
       
   137 
       
   138 // -----------------------------------------------------------------------------
       
   139 // CScpProfileHandler::GetCurrentState
       
   140 // -----------------------------------------------------------------------------
       
   141 //
       
   142 TInt CScpProfileHandler::GetCurrentState( TUint32 aProfileId, 
       
   143     CScpSipConnection::TConnectionState& aState,
       
   144     TInt& aError ) const
       
   145     {
       
   146     SCPLOGSTRING( "CScpProfileHandler::GetCurrentState" );
       
   147 
       
   148     TInt result( KErrNone );
       
   149 
       
   150     // Go through own sip profiles and check state
       
   151     // If not found, create SipConnection and check state
       
   152     CScpSipConnection* sipConnection = GetSipConnection( aProfileId );
       
   153 
       
   154     TBool sipConnectionCreated = EFalse;
       
   155 
       
   156     if( !sipConnection )
       
   157         {
       
   158         sipConnectionCreated = ETrue;
       
   159         TRAP( result, sipConnection = CreateSipConnectionL( aProfileId ) );
       
   160         }
       
   161 
       
   162     if( result == KErrNone )
       
   163         {
       
   164         sipConnection->GetState( aState, aError );
       
   165         }
       
   166 
       
   167     if( sipConnectionCreated )
       
   168         {
       
   169         delete sipConnection;
       
   170         }
       
   171        
       
   172     return result;
       
   173     }
       
   174 
       
   175 // -----------------------------------------------------------------------------
       
   176 // CScpProfileHandler::RegisterProfileL
       
   177 // -----------------------------------------------------------------------------
       
   178 //
       
   179 void CScpProfileHandler::RegisterProfileL( TUint32 aProfileId )
       
   180     {
       
   181     SCPLOGSTRING2("CScpProfileHandler::RegisterProfileL profile id: %d", aProfileId );
       
   182 
       
   183     // Check if sip connection already exists
       
   184     TBool found( EFalse );
       
   185     CScpSipConnection* sipConnection( NULL );
       
   186     
       
   187     for( TInt i=0; i<iSipConnections.Count(); i++ )
       
   188         {
       
   189         sipConnection = iSipConnections[ i ];
       
   190 
       
   191         if( sipConnection->ProfileId() == aProfileId )
       
   192             {
       
   193             found = ETrue;
       
   194             sipConnection->EnableL();
       
   195             break;
       
   196             }    
       
   197         }
       
   198 
       
   199     // Otherwise create it
       
   200     if( !found )
       
   201         {
       
   202         sipConnection = CreateSipConnectionL( aProfileId );
       
   203         CleanupStack::PushL( sipConnection );        
       
   204         // try enabling
       
   205         sipConnection->AddObserver( *this );
       
   206         sipConnection->EnableL();
       
   207         // add to array
       
   208         iSipConnections.AppendL( sipConnection );
       
   209         CleanupStack::Pop( sipConnection );
       
   210         }
       
   211     }
       
   212 
       
   213 // -----------------------------------------------------------------------------
       
   214 // CScpProfileHandler::SipConnectionExists
       
   215 // -----------------------------------------------------------------------------
       
   216 //
       
   217 TBool CScpProfileHandler::SipConnectionExists( TUint32 aProfileId ) const
       
   218     {
       
   219     SCPLOGSTRING2("CScpProfileHandler::SipConnectionExists profile id: %d", aProfileId );
       
   220 
       
   221     return ( GetSipConnection( aProfileId ) != NULL );
       
   222     }
       
   223 
       
   224 // -----------------------------------------------------------------------------
       
   225 // CScpProfileHandler::GetSipConnection
       
   226 // -----------------------------------------------------------------------------
       
   227 //
       
   228 CScpSipConnection* CScpProfileHandler::GetSipConnection( TUint32 aProfileId ) const
       
   229     {
       
   230     SCPLOGSTRING2("CScpProfileHandler::GetSipConnection profile id: %d", aProfileId );
       
   231 
       
   232     CScpSipConnection* sipConnection( NULL );
       
   233     
       
   234     for( TInt i=0; i<iSipConnections.Count(); i++ )
       
   235         {
       
   236         sipConnection = iSipConnections[ i ];
       
   237 
       
   238         if( sipConnection->ProfileId() == aProfileId )
       
   239             {
       
   240             return sipConnection;
       
   241             }    
       
   242         }
       
   243 
       
   244     return NULL;
       
   245     }
       
   246 
       
   247 // -----------------------------------------------------------------------------
       
   248 // CScpProfileHandler::CreateSipConnectionL
       
   249 // -----------------------------------------------------------------------------
       
   250 //
       
   251 CScpSipConnection* CScpProfileHandler::CreateSipConnectionL( TUint32 aProfileId ) const
       
   252     {
       
   253     SCPLOGSTRING2("CScpProfileHandler::CreateSipConnectionL profile: %d", aProfileId);
       
   254 
       
   255     return CScpSipConnection::NewL( aProfileId,
       
   256                                     *iProfileRegistry,
       
   257                                     *iManagedProfileRegistry,
       
   258                                     *iSip );
       
   259     }
       
   260 
       
   261 // -----------------------------------------------------------------------------
       
   262 // CScpProfileHandler::UnRegisterProfile
       
   263 // -----------------------------------------------------------------------------
       
   264 //
       
   265 TInt CScpProfileHandler::UnregisterProfile( TUint32 aProfileId )
       
   266     {
       
   267     SCPLOGSTRING2("CScpProfileHandler::UnregisterProfile id: %d", aProfileId );
       
   268 
       
   269     CScpSipConnection* sipConnection( NULL );    
       
   270 
       
   271     for( TInt i=0; i<iSipConnections.Count(); i++ )
       
   272         {
       
   273         sipConnection = iSipConnections[ i ];
       
   274 
       
   275         if( sipConnection->ProfileId() == aProfileId )
       
   276             {
       
   277             return sipConnection->Disable();
       
   278             }
       
   279         }
       
   280 
       
   281     return KErrNotFound;
       
   282     }
       
   283 
       
   284 // -----------------------------------------------------------------------------
       
   285 // CScpProfileHandler::CancelRegistration
       
   286 // -----------------------------------------------------------------------------
       
   287 //
       
   288 TInt CScpProfileHandler::CancelRegistration( TUint32 aProfileId )
       
   289     {
       
   290     SCPLOGSTRING2("CScpProfileHandler::UnregisterProfile id: %d", aProfileId );
       
   291 
       
   292     TInt result = UnregisterProfile( aProfileId );
       
   293 
       
   294     if( result == KErrNone ||
       
   295         result == KErrNotFound || 
       
   296         result == KErrArgument )
       
   297         {
       
   298         result = RemoveSipConnection( aProfileId );
       
   299         }
       
   300 
       
   301     return result;
       
   302     }
       
   303 
       
   304 // -----------------------------------------------------------------------------
       
   305 // CScpProfileHandler::IncomingRequest
       
   306 // -----------------------------------------------------------------------------
       
   307 //
       
   308 void CScpProfileHandler::IncomingRequest( TUint32 /*aIapId*/,
       
   309                                           CSIPServerTransaction* /*aTransaction*/ )
       
   310     {
       
   311     SCPLOGSTRING( "CScpProfileHandler::IncomingRequest" );
       
   312     }
       
   313 
       
   314 // -----------------------------------------------------------------------------
       
   315 // CScpProfileHandler::TimedOut
       
   316 // -----------------------------------------------------------------------------
       
   317 //
       
   318 void CScpProfileHandler::TimedOut(
       
   319     CSIPServerTransaction& /*aTransaction*/ )
       
   320     {
       
   321     SCPLOGSTRING( "CScpProfileHandler::TimedOut" );
       
   322     }
       
   323 
       
   324 // -----------------------------------------------------------------------------
       
   325 // CScpProfileHandler::RemoveSipConnection
       
   326 // -----------------------------------------------------------------------------
       
   327 //
       
   328 TInt CScpProfileHandler::RemoveSipConnection( TUint32 aProfileId )
       
   329     {
       
   330     SCPLOGSTRING2( "CScpProfileHandler::RemoveSipConnection id: %i", 
       
   331                    aProfileId );
       
   332 
       
   333     TInt ret = ( KErrNotFound );
       
   334     CScpSipConnection* sipConnection( NULL );
       
   335     
       
   336     sipConnection =  GetSipConnection( aProfileId );
       
   337     
       
   338     if ( sipConnection )
       
   339         {
       
   340         TInt index = iSipConnections.Find( sipConnection );
       
   341         iSipConnections.Remove( index );
       
   342         delete sipConnection;
       
   343         ret = KErrNone;
       
   344         }
       
   345     return ret;
       
   346     }
       
   347 
       
   348 
       
   349 // -----------------------------------------------------------------------------
       
   350 // CScpProfileHandler::ProfileRegistryEventOccurred
       
   351 // -----------------------------------------------------------------------------
       
   352 //
       
   353 void CScpProfileHandler::ProfileRegistryEventOccurred(
       
   354     TUint32 aProfileId,
       
   355     MSIPProfileRegistryObserver::TEvent aEvent )
       
   356     {
       
   357     SCPLOGSTRING3( "CScpProfileHandler::ProfileRegistryEventOccurred id: %i event: %d", 
       
   358                    aProfileId, aEvent );
       
   359 
       
   360     CScpSipConnection* sipConnection = GetSipConnection( aProfileId );
       
   361     
       
   362     if( sipConnection )
       
   363         {
       
   364         sipConnection->ProfileRegistryEventOccurred( aEvent );
       
   365         
       
   366         // Deal with sip profile removals
       
   367         switch ( aEvent )
       
   368             {
       
   369             case EProfileDeregistered:
       
   370                 {
       
   371                 // Remove sip connection only if user has requested deregistration
       
   372                 CScpSipConnection::TRegistrationRequestState state =
       
   373                     sipConnection->RegistrationRequestState();
       
   374 
       
   375                 if( state == CScpSipConnection::EDeregistrationRequested )
       
   376                     {
       
   377                     RemoveSipConnection( aProfileId );
       
   378                     }
       
   379                 }
       
   380                 break;
       
   381 
       
   382             case EProfileDestroyed:
       
   383                 {
       
   384                 RemoveSipConnection( aProfileId );
       
   385                 }
       
   386                 break;
       
   387                
       
   388             default:
       
   389                 break;
       
   390             }
       
   391         }      
       
   392     }
       
   393 
       
   394 // -----------------------------------------------------------------------------
       
   395 // CScpProfileHandler::ProfileRegistryErrorOccurred
       
   396 // -----------------------------------------------------------------------------
       
   397 //
       
   398 void CScpProfileHandler::ProfileRegistryErrorOccurred( TUint32 aProfileId,
       
   399                                                        TInt aError )
       
   400     {
       
   401     SCPLOGSTRING3( "CScpProfileHandler::ProfileRegistryErrorOccurred profile:%i error: %d", 
       
   402                    aProfileId, aError );
       
   403 
       
   404     CScpSipConnection* sipConnection = GetSipConnection( aProfileId );
       
   405     
       
   406     if( sipConnection )
       
   407         {
       
   408         sipConnection->ProfileRegistryErrorOccurred( aError );
       
   409         }
       
   410     }
       
   411       
       
   412 // -----------------------------------------------------------------------------
       
   413 // CScpProfileHandler::ProfileExist
       
   414 // -----------------------------------------------------------------------------
       
   415 //
       
   416 TBool CScpProfileHandler::ProfileExists( TUint32 aProfileId ) const
       
   417     {    
       
   418     SCPLOGSTRING2( "CScpProfileHandler::ProfileExists id: %d", aProfileId );
       
   419 
       
   420     CSIPProfile* profile( NULL );
       
   421     TRAPD( result, profile = iProfileRegistry->ProfileL( aProfileId ) );
       
   422 
       
   423     if( result == KErrNone && profile )
       
   424         {
       
   425         delete profile;
       
   426         return ETrue;
       
   427         }
       
   428 
       
   429     return EFalse;             
       
   430     }
       
   431 
       
   432 // -----------------------------------------------------------------------------
       
   433 // CScpProfileHandler::SetSipProfileReserved
       
   434 // -----------------------------------------------------------------------------
       
   435 //
       
   436 void CScpProfileHandler::SetSipProfileReserved( TUint32 aProfileId, TBool aReserved )
       
   437     {
       
   438     SCPLOGSTRING3( "CScpProfileHandler::SetSipProfileReserved id: %d reserved: %d", 
       
   439                    aProfileId, aReserved );
       
   440 
       
   441     CScpSipConnection* sipConnection = GetSipConnection( aProfileId );
       
   442     
       
   443     if( sipConnection )
       
   444         {
       
   445         sipConnection->SetReserved( aReserved );
       
   446 
       
   447         // If profile was freed and iap availability alr event had come
       
   448         // before, let ALR controller to know that the profile is up
       
   449         // for ALR
       
   450         if( aReserved == EFalse && 
       
   451             sipConnection->IapAvailableOffered() )
       
   452             {
       
   453             TRAP_IGNORE( iAlrController->RefreshIapAvailabilityL( aProfileId ) );
       
   454     
       
   455             // Reset the iap availability offered flag
       
   456             sipConnection->SetIapAvailableOffered( EFalse );
       
   457             }
       
   458         }
       
   459     }
       
   460 
       
   461 // -----------------------------------------------------------------------------
       
   462 // CScpProfileHandler::AlrEvent
       
   463 // -----------------------------------------------------------------------------
       
   464 //
       
   465 void CScpProfileHandler::AlrEvent( MSipProfileAlrObserver::TEvent aEvent,
       
   466                                    TUint32 aProfileId,
       
   467 #ifdef _DEBUG
       
   468                                    TUint32 aSnapId,
       
   469 #else
       
   470                                    TUint32 /*aSnapId*/,
       
   471 #endif
       
   472                                    TUint32 aIapId )
       
   473     {
       
   474     SCPLOGSTRING2("CScpProfileHandler::AlrEvent profile:%i", aProfileId);
       
   475     SCPLOGSTRING2("CScpProfileHandler::AlrEvent aEvent:%i", (TInt)aEvent);
       
   476     SCPLOGSTRING2("CScpProfileHandler::AlrEvent aSnapId:%i", aSnapId);
       
   477     SCPLOGSTRING2("CScpProfileHandler::AlrEvent aIapId:%i", aIapId);
       
   478 
       
   479     CScpSipConnection* sipConnection( NULL );
       
   480     
       
   481     switch( aEvent )
       
   482         {
       
   483         case MSipProfileAlrObserver::EIapAvailable:
       
   484             {
       
   485             TBool reserved( EFalse );
       
   486             sipConnection = GetSipConnection( aProfileId );
       
   487     
       
   488             if( sipConnection )
       
   489                 {
       
   490                 reserved = sipConnection->Reserved();
       
   491 
       
   492                 if( reserved )
       
   493                     {
       
   494                     TRAPD( result, 
       
   495                         iAlrController->DisallowMigrationL( aProfileId, aIapId ) );
       
   496 
       
   497                     if( result == KErrNone )
       
   498                         {
       
   499                         // Raise the iap availability offered flag
       
   500                         sipConnection->SetIapAvailableOffered( ETrue );
       
   501                         }
       
   502                     }
       
   503                 else
       
   504                     {
       
   505                     sipConnection->SetProfileCurrentlyRoaming();
       
   506 
       
   507                     TRAP_IGNORE(
       
   508                         iAlrController->AllowMigrationL( aProfileId, aIapId ) );
       
   509                     }
       
   510                 }
       
   511 
       
   512             break;
       
   513             }
       
   514 
       
   515         // For possible beyond use (E.g. Maybe we should change CHH state
       
   516         // during iap migration -> VoIP call not possible if iap migration
       
   517         // ongoing.)
       
   518         case MSipProfileAlrObserver::EMigrationStarted:
       
   519             {
       
   520             sipConnection = GetSipConnection( aProfileId );
       
   521             
       
   522             if( sipConnection )
       
   523                 {
       
   524                 sipConnection->HandleMigrationStarted();
       
   525                 }
       
   526             
       
   527             break;
       
   528             }
       
   529         
       
   530         case MSipProfileAlrObserver::EMigrationCompleted:
       
   531         default:
       
   532             {
       
   533             break;
       
   534             }            
       
   535         }
       
   536     }
       
   537     
       
   538 // -----------------------------------------------------------------------------
       
   539 // CScpProfileHandler::AlrError
       
   540 // -----------------------------------------------------------------------------
       
   541 //
       
   542 void CScpProfileHandler::AlrError( 
       
   543     TInt aError,
       
   544     TUint32 aProfileId,
       
   545     TUint32 aSnapId,
       
   546     TUint32 aIapId )
       
   547     {
       
   548     SCPLOGSTRING("CScpProfileHandler::AlrError");
       
   549     SCPLOGSTRING2("     profile id: %d", aProfileId);
       
   550     SCPLOGSTRING2("     error:      %d", aError);
       
   551     
       
   552     CScpSipConnection* sipConnection = GetSipConnection( aProfileId );
       
   553     if( sipConnection )
       
   554         {
       
   555         sipConnection->HandleMigrationError( 
       
   556             aError, aProfileId, aSnapId, aIapId );
       
   557         }
       
   558     }
       
   559 
       
   560 // -----------------------------------------------------------------------------
       
   561 // CScpProfileHandler::HandleSipConnectionEvent
       
   562 // -----------------------------------------------------------------------------
       
   563 //
       
   564 void CScpProfileHandler::HandleSipConnectionEvent( TUint32 aProfileId,
       
   565                                                    TScpConnectionEvent aSipEvent )
       
   566     {
       
   567     SCPLOGSTRING3( 
       
   568         "CScpProfileHandler::HandleSipConnectionEvent profile id: %d sip event: %d",
       
   569         aProfileId, aSipEvent );
       
   570 
       
   571     // Make a copy of the observers array, since observers
       
   572     // may be removed during the event handling
       
   573     RPointerArray< MScpSipConnectionObserver > observers;
       
   574     
       
   575     for ( TInt i=0; i < iObservers.Count(); i++ )
       
   576         {      
       
   577         observers.Append( iObservers[ i ] );
       
   578         }
       
   579 
       
   580     // Send notify to observers
       
   581     for( TInt i=0; i < observers.Count(); i++ )
       
   582         {
       
   583         //check that the observer is still valid before triggering notify
       
   584         if ( KErrNotFound != iObservers.Find( observers[ i ] ) )
       
   585             {
       
   586             observers[ i ]->HandleSipConnectionEvent( aProfileId, aSipEvent );
       
   587             }
       
   588         }
       
   589 
       
   590     observers.Close();
       
   591         
       
   592     // if registration failed, time to delete connection
       
   593     if ( aSipEvent == EScpRegistrationFailed )
       
   594         {
       
   595         RemoveSipConnection( aProfileId );        
       
   596         }
       
   597     }
       
   598 
       
   599 // -----------------------------------------------------------------------------
       
   600 // CScpProfileHandler::VmbxInterfaceL
       
   601 // -----------------------------------------------------------------------------
       
   602 //
       
   603 CIpVmbxInterface& CScpProfileHandler::VmbxInterfaceL( MIpVmbxObserver& aObserver )
       
   604     {
       
   605     SCPLOGSTRING( "CScpProfileHandler::VmbxInterfaceL" );
       
   606 
       
   607     // Todo: save observer and set MIpVmbxObserver to this class
       
   608 	// problem when several vmbx services, current observer logic isn't work
       
   609     
       
   610     if( !iVmbxInterface )
       
   611         {
       
   612         iVmbxInterface = CIpVmbxInterface::NewL( aObserver );
       
   613         }
       
   614 
       
   615     return *iVmbxInterface;
       
   616     }
       
   617 
       
   618 // -----------------------------------------------------------------------------
       
   619 // CScpProfileHandler::DeleteVmbxInterface
       
   620 // -----------------------------------------------------------------------------
       
   621 //
       
   622 void CScpProfileHandler::DeleteVmbxInterface()
       
   623     {
       
   624     SCPLOGSTRING( "CScpProfileHandler::DeleteVmbxInterface" );
       
   625 
       
   626     if( iVmbxInterface )
       
   627         {
       
   628         delete iVmbxInterface;
       
   629         iVmbxInterface = NULL;
       
   630         }
       
   631     }
       
   632 
       
   633 // -----------------------------------------------------------------------------
       
   634 // CScpProfileHandler::UpdateSipProfile
       
   635 // -----------------------------------------------------------------------------
       
   636 //
       
   637 void CScpProfileHandler::UpdateSipProfileL( 
       
   638     TUint32 aProfileId,
       
   639     TBool aTerminalType,
       
   640     TBool aWlanMac,
       
   641     TInt aStringLength )
       
   642     {
       
   643     SCPLOGSTRING( "CScpProfileHandler::UpdateSipProfileL IN" );
       
   644     SCPLOGSTRING2( " profile id:    %d", aProfileId );
       
   645     SCPLOGSTRING2( " terminal type: %d", aTerminalType );
       
   646     SCPLOGSTRING2( " wlan mac:      %d", aWlanMac );
       
   647     SCPLOGSTRING2( " string length: %d", aStringLength );
       
   648     
       
   649     // Do not update, if terminal type is not defined in user agent header
       
   650     if ( !aTerminalType )
       
   651         {
       
   652         return;
       
   653         }
       
   654     
       
   655     const MDesC8Array* array;
       
   656     CSIPProfile* sipProfile = iProfileRegistry->ProfileL( aProfileId );
       
   657     CleanupStack::PushL( sipProfile );
       
   658     User::LeaveIfError( sipProfile->GetParameter( KSIPHeaders, array ) );
       
   659 
       
   660     TBuf<KSCPMaxTerminalTypeLength> terminalType( KNullDesC );
       
   661     TBuf<KTempStringlength> tempHeader( KNullDesC );
       
   662     
       
   663     // 1. Get the terminal type and seek is user agent header's 
       
   664     // terminal type same, if so no need to continue
       
   665     CIpAppPhoneUtils* libIpAppPhoneUtils = CIpAppPhoneUtils::NewLC();
       
   666     libIpAppPhoneUtils->GetTerminalTypeL( terminalType );   
       
   667     CleanupStack::PopAndDestroy( libIpAppPhoneUtils );
       
   668     
       
   669     for( TInt t( 0 ); t < array->MdcaCount(); t++ )
       
   670         {
       
   671         tempHeader.Copy( array->MdcaPoint( t ) );
       
   672         if( tempHeader.Find( terminalType ) != KErrNotFound )
       
   673             {
       
   674             SCPLOGSTRING(" terminal type found");
       
   675             CleanupStack::PopAndDestroy( sipProfile );
       
   676             return;
       
   677             }
       
   678         }
       
   679     
       
   680     // 2. If appropriate terminal type not found, we need to update it
       
   681     if ( array->MdcaCount() )
       
   682         {
       
   683         TBuf8<KTempStringlength> userAgentHeader;
       
   684         CDesC8ArrayFlat* uahArray = new ( ELeave ) CDesC8ArrayFlat( 1 );
       
   685         CleanupStack::PushL( uahArray );
       
   686         //Find the User-Agent string
       
   687         for( TInt t( 0 ); t < array->MdcaCount(); t++ )
       
   688             {
       
   689             tempHeader.Copy( array->MdcaPoint( t ) );
       
   690             // user-Agent found, now change it
       
   691             if( tempHeader.Find( KScpUserAgentString ) != KErrNotFound )
       
   692                 {
       
   693                 // 3. now we have the right user agent header which is not correct
       
   694                 // replace the old terminal type 
       
   695                 TInt len( KErrNone );
       
   696                 // Get mac if defined
       
   697                 if ( aWlanMac )
       
   698                     {
       
   699                     TBuf8<KSCPWlanMacAddressLength> wlanMacAddress( KNullDesC8 );
       
   700                     TBuf<KSCPWlanMacAddressLength>  tempAddr( KNullDesC );
       
   701                     CIPAppUtilsAddressResolver* addressResolver = 
       
   702                         CIPAppUtilsAddressResolver::NewLC();    
       
   703                     User::LeaveIfError( addressResolver->GetWlanMACAddress( 
       
   704                         wlanMacAddress, KSCPWlanMacAddressFrmt ) );
       
   705                     CleanupStack::PopAndDestroy( addressResolver );
       
   706                     tempAddr.Copy( wlanMacAddress );
       
   707                     
       
   708                     // leave the space before MAC ADDR (-1)
       
   709                     len = tempHeader.Find( tempAddr ) - KSCPUserAgentStringLength - 1;
       
   710                     }
       
   711                 else
       
   712                     {
       
   713                     // leave the space before free string (-1)
       
   714                     len = tempHeader.Length() - KSCPUserAgentStringLength - aStringLength - 1;
       
   715                     }
       
   716                 // make sure that the len is correct value(not below zero)
       
   717                 if ( 0 >= len )
       
   718                     {
       
   719                     User::Leave( KErrGeneral );
       
   720                     }
       
   721                 tempHeader.Replace( KSCPUserAgentStringLength, len, terminalType );
       
   722                 SCPLOGSTRING2(" header:        %S", &tempHeader);
       
   723                 userAgentHeader.Append( tempHeader );
       
   724                 uahArray->AppendL( userAgentHeader );
       
   725                 }
       
   726             else
       
   727                 {
       
   728                 //if this entry is not user agent, leave it untouched
       
   729                 uahArray->AppendL( array->MdcaPoint( t ) );    
       
   730                 }
       
   731             }
       
   732 
       
   733         CSIPManagedProfile* managedProfile = static_cast<CSIPManagedProfile*>( 
       
   734             iProfileRegistry->ProfileL( aProfileId ) );
       
   735         CleanupStack::PushL( managedProfile );
       
   736         //Update the new user-agent to sipprofile
       
   737         User::LeaveIfError( 
       
   738             managedProfile->SetParameter( KSIPHeaders, *uahArray ) );
       
   739         iManagedProfileRegistry->SaveL( *managedProfile );
       
   740         CleanupStack::PopAndDestroy( managedProfile );
       
   741         CleanupStack::PopAndDestroy( uahArray );
       
   742         }        
       
   743     CleanupStack::PopAndDestroy( sipProfile );    
       
   744     SCPLOGSTRING( "CScpProfileHandler::UpdateSipProfileL OUT" );
       
   745     }
       
   746 
       
   747 // -----------------------------------------------------------------------------
       
   748 // CScpProfileHandler::SetUsernameAndPasswordL
       
   749 // -----------------------------------------------------------------------------
       
   750 //
       
   751 void CScpProfileHandler::SetUsernameAndPasswordL( TUint32 aProfileId,
       
   752                                                   const TDesC8& aUsername,
       
   753                                                   TBool aSetUsername,
       
   754                                                   const TDesC8& aPassword,
       
   755                                                   TBool aSetPassword )
       
   756     {
       
   757     SCPLOGSTRING( "CScpProfileHandler::SetUsernameAndPasswordL" );
       
   758 
       
   759     CSIPManagedProfile* profile = static_cast<CSIPManagedProfile*>
       
   760                          (iProfileRegistry->ProfileL( aProfileId ));
       
   761     CleanupStack::PushL( profile );
       
   762 
       
   763     SCPLOGSTRING( "CScpProfileHandler::SetUsernameAndPasswordL sip profile availble" );
       
   764     
       
   765     if( aSetUsername )
       
   766         {
       
   767         RBuf8 formattedUsername;
       
   768         CleanupClosePushL( formattedUsername );
       
   769         
       
   770         if( !TScpUtility::CheckSipUsername( aUsername ) )
       
   771             {
       
   772             // Since username is returned with prefix and domain, it needs
       
   773             // to be possible to also set it with prefix and domain. 
       
   774             // Strip prefix and domain from user aor if found.
       
   775             // Prefix might be sip or sips so search by colon.
       
   776             // If username is empty, leave with KErrArgument
       
   777             if ( aUsername == KNullDesC8 )
       
   778                 {
       
   779                 User::Leave( KErrArgument );
       
   780                 }
       
   781             else
       
   782                 {
       
   783                 User::LeaveIfError( TScpUtility::RemovePrefixAndDomain( 
       
   784                     aUsername, formattedUsername ) );
       
   785                 }
       
   786             }
       
   787         else
       
   788             {
       
   789             formattedUsername.CreateL( aUsername.Length() );
       
   790             formattedUsername.Copy( aUsername );
       
   791             }
       
   792         
       
   793         TBool validAorExists( EFalse );       
       
   794         const TDesC8* aorPtr( NULL );
       
   795         
       
   796         TInt err = profile->GetParameter( KSIPUserAor, aorPtr );
       
   797         
       
   798         TInt loc( KErrNotFound );
       
   799         if ( !err && aorPtr->Length() )
       
   800             {
       
   801             loc = aorPtr->Find( KAt() );
       
   802             
       
   803             if ( KErrNotFound != loc )
       
   804                 {         
       
   805                 TPtrC8 ptr = aorPtr->Right( 1 );
       
   806                 
       
   807                 if ( ptr.Compare( KAt ) != 0 )
       
   808                     {                    
       
   809                     // @ is found and there is something in domain part
       
   810                     validAorExists = ETrue;
       
   811                     }              
       
   812                 }
       
   813             }
       
   814        
       
   815        HBufC8* newAor = NULL;
       
   816        TBuf8<KSipSchemeMaxLength> prefix;
       
   817        TScpUtility::GetValidPrefix( aUsername, prefix );
       
   818        
       
   819        if ( validAorExists )
       
   820            {           
       
   821            SCPLOGSTRING( "CScpProfileHandler::SetUsernameAndPasswordL valid aor" );
       
   822            TPtrC8 domainPart = aorPtr->Mid( loc );
       
   823            
       
   824            newAor = HBufC8::NewLC( prefix.Length() + 
       
   825                                    domainPart.Length() + 
       
   826                                    formattedUsername.Length() );
       
   827            newAor->Des().Append( prefix );
       
   828            newAor->Des().Append( formattedUsername );
       
   829            newAor->Des().Append( domainPart );
       
   830            }
       
   831        else
       
   832            {         
       
   833            SCPLOGSTRING( "CScpProfileHandler::SetUsernameAndPasswordL no valid aor" );
       
   834            // Check if given username contains valid aor
       
   835            TInt loc = aUsername.Find( KAt() );
       
   836            
       
   837            if ( KErrNotFound != loc )
       
   838                {               
       
   839                TPtrC8 ptrRight = aorPtr->Right( 1 );
       
   840                TPtrC8 ptrLeft = aorPtr->Left( 1 );
       
   841                
       
   842                if ( ( ptrRight.Compare( KAt ) != 0 ) && 
       
   843                    ( ptrLeft.Compare( KAt ) != 0 ) )
       
   844                    {                   
       
   845                    // both sides of @ have data --> aor is valid
       
   846                    newAor = HBufC8::NewLC( aUsername.Length() );
       
   847                    newAor->Des().Copy( aUsername );
       
   848                    }
       
   849                else
       
   850                    {
       
   851                    User::Leave( KErrArgument );
       
   852                    }
       
   853                }
       
   854            else
       
   855                {               
       
   856                User::Leave( KErrArgument );
       
   857                }    
       
   858            }
       
   859 
       
   860         SCPLOGSTRING( "CScpProfileHandler::SetUsernameAndPasswordL set new aor value" );
       
   861         
       
   862         User::LeaveIfError( profile->SetParameter( KSIPUserAor, newAor->Des() ) );
       
   863         CleanupStack::PopAndDestroy( newAor );
       
   864         
       
   865         // Add digest username & password
       
   866         SCPLOGSTRING( "CScpProfileHandler:SetUsernameAndPasswordL outbound & digest username" );
       
   867         User::LeaveIfError( profile->SetParameter( KSIPOutboundProxy,
       
   868                                                    KSIPDigestUserName,
       
   869                                                    formattedUsername ) );
       
   870 
       
   871 
       
   872         SCPLOGSTRING( "CScpProfileHandler:SetUsernameAndPasswordL registrar & digest username" );
       
   873         User::LeaveIfError( profile->SetParameter( KSIPRegistrar,
       
   874                                                    KSIPDigestUserName,
       
   875                                                    formattedUsername ) );
       
   876         CleanupStack::PopAndDestroy( &formattedUsername );                                                   
       
   877         }
       
   878 
       
   879     if( aSetPassword )
       
   880         {
       
   881         if( !TScpUtility::CheckSipPassword( aPassword ) )
       
   882             {
       
   883             User::Leave( KErrArgument );
       
   884             }
       
   885         SCPLOGSTRING( "CScpProfileHandler:SetUsernameAndPasswordL outbound & digest pwd" );
       
   886         User::LeaveIfError( profile->SetParameter( KSIPOutboundProxy,
       
   887                                                    KSIPDigestPassword,
       
   888                                                    aPassword ) );    
       
   889         SCPLOGSTRING( "CScpProfileHandler:SetUsernameAndPasswordL registrar & digest pwd" );
       
   890         User::LeaveIfError( profile->SetParameter( KSIPRegistrar,
       
   891                                                    KSIPDigestPassword,
       
   892                                                    aPassword ) );
       
   893         }
       
   894     
       
   895     SCPLOGSTRING( "CScpProfileHandler:SetUsernameAndPasswordL save sip changes" );
       
   896 
       
   897     // Save changes
       
   898     TRAPD( err, iManagedProfileRegistry->SaveL( *profile ) );
       
   899 
       
   900     CleanupStack::PopAndDestroy( profile );    
       
   901     
       
   902     SCPLOGSTRING2( "CScpProfileHandler:SetUsernameAndPasswordL save sip err: %d", err );
       
   903     User::LeaveIfError( err );
       
   904     }
       
   905 
       
   906 
       
   907 
       
   908 // -----------------------------------------------------------------------------
       
   909 // CScpProfileHandler::GetDebugInfo
       
   910 // -----------------------------------------------------------------------------
       
   911 //
       
   912 #ifdef _DEBUG
       
   913 void CScpProfileHandler::GetDebugInfo( TDes& aInfo ) const
       
   914     {
       
   915     TInt connections = iSipConnections.Count();
       
   916     TInt registeredConnections( 0 );
       
   917     TInt registeringConnections( 0 );
       
   918     TInt unregisteringConnections( 0 );
       
   919     TInt unregisteredConnections( 0 );
       
   920     
       
   921     for( TInt i=0; i<iSipConnections.Count(); i++ )
       
   922         {
       
   923         CScpSipConnection* sipConnection = iSipConnections[ i ];
       
   924         CScpSipConnection::TConnectionState state;
       
   925         TInt error;
       
   926 
       
   927         sipConnection->GetState( state, error );
       
   928 
       
   929         switch( state )
       
   930             {
       
   931             case CScpSipConnection::ERegistered:
       
   932                 registeredConnections++;
       
   933                 break;
       
   934 
       
   935             case CScpSipConnection::ERegistering:
       
   936                 registeringConnections++;
       
   937                 break;
       
   938 
       
   939             case CScpSipConnection::EDeregistering:
       
   940                 unregisteringConnections++;
       
   941                 break;
       
   942 
       
   943             case CScpSipConnection::EDeregistered:
       
   944                 unregisteredConnections++;
       
   945                 break;
       
   946 
       
   947             default:
       
   948                 break;
       
   949             }
       
   950         }
       
   951 
       
   952     TBuf< 255 > buffer;
       
   953     buffer.Format( _L( "Connections: %d\n Registered: %d\n Registering: %d\n Unregistering: %d\n Unregistered: %d\n" ),
       
   954                         connections, registeredConnections, registeringConnections,
       
   955                         unregisteringConnections, unregisteredConnections );
       
   956 
       
   957     aInfo.Append( buffer );
       
   958     }
       
   959 #endif
       
   960             
       
   961 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   962 
       
   963 //  End of File